Props to prop no plural
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
/* BlinkOnWifiProp.ino
|
||||
MIT License (c) Marie Faure <dev at faure dot systems>
|
||||
|
||||
Adapt the Blink example (https://www.arduino.cc/en/tutorial/blink) as a
|
||||
simple MQTT prop. Avoid delay() calls (except short ones) in loop() to
|
||||
ensure CPU for MQTT protocol. Use PropAction checks instead.
|
||||
|
||||
Copy and change it to build your first Arduino connected prop, you will
|
||||
only be limited by your imagination.
|
||||
|
||||
Requirements:
|
||||
- install ArduinoProps.zip library and dependencies (https://github.com/fauresystems/ArduinoProps)
|
||||
*/
|
||||
#include "ArduinoProps.h"
|
||||
|
||||
// Setup your WiFi network
|
||||
const char* ssid = "";
|
||||
const char *passphrase = "";
|
||||
|
||||
// If you're running xcape.io Room software you have to respect prop inbox/outbox
|
||||
// topicw syntax: Room/[escape room name]/Props/[propsname]/inbox|outbox
|
||||
// https://xcape.io/go/room
|
||||
|
||||
WifiProp prop(u8"Arduino Blink", // as MQTT client id, should be unique per client for given broker
|
||||
u8"Room/My room/Props/Arduino Blink/inbox",
|
||||
u8"Room/My room/Props/Arduino Blink/outbox",
|
||||
"192.168.1.53", // your MQTT server IP address
|
||||
1883); // your MQTT server port;
|
||||
|
||||
// Builtin led is not available with the shield
|
||||
#undef LED_BUILTIN
|
||||
#define LED_BUILTIN 8
|
||||
|
||||
PropDataLogical clignoter(u8"clignote", u8"oui", u8"non", true);
|
||||
PropDataLogical led(u8"led");
|
||||
PropDataText rssi(u8"rssi");
|
||||
|
||||
void clignote(); // forward
|
||||
PropAction clignoteAction = PropAction(1000, clignote);
|
||||
|
||||
bool wifiBegun(false);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
|
||||
prop.addData(&clignoter);
|
||||
prop.addData(&led);
|
||||
prop.addData(&rssi);
|
||||
|
||||
prop.begin(InboxMessage::run);
|
||||
|
||||
pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output
|
||||
|
||||
// At this point, the broker is not connected yet
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (!wifiBegun) {
|
||||
WiFi.begin(ssid, passphrase);
|
||||
Serial.println(WiFi.firmwareVersion());
|
||||
delay(250); // acceptable freeze for this prop (otherwise use PropAction for async-like behavior)
|
||||
// do static IP configuration disabling the dhcp client, must be called after every WiFi.begin()
|
||||
String fv = WiFi.firmwareVersion();
|
||||
if (fv.startsWith("1.0")) {
|
||||
Serial.println("Please upgrade the firmware for static IP");
|
||||
// see https://github.com/fauresystems/ArduinoProps/blob/master/WifiNinaFirmware.md
|
||||
}
|
||||
else {
|
||||
//WiFi.config(IPAddress(192, 168, 1, 21), // local_ip
|
||||
// IPAddress(192, 168, 1, 1), // dns_server
|
||||
// IPAddress(192, 168, 1, 1), // gateway
|
||||
// IPAddress(255, 255, 255, 0)); // subnet
|
||||
}
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
wifiBegun = true;
|
||||
Serial.println(WiFi.localIP());
|
||||
Serial.println(WiFi.subnetMask());
|
||||
Serial.println(WiFi.gatewayIP());
|
||||
} else {
|
||||
WiFi.end();
|
||||
}
|
||||
} else if (wifiBegun && WiFi.status() != WL_CONNECTED) {
|
||||
WiFi.end();
|
||||
wifiBegun = false;
|
||||
}
|
||||
|
||||
prop.loop();
|
||||
|
||||
rssi.setValue(WiFi.RSSI() + String(" dBm")); // https://www.metageek.com/training/resources/understanding-rssi.html
|
||||
|
||||
led.setValue(digitalRead(LED_BUILTIN)); // read I/O
|
||||
|
||||
clignoteAction.check(); // do your stuff, don't freeze the loop with delay() calls
|
||||
}
|
||||
|
||||
void clignote()
|
||||
{
|
||||
if (clignoter.value()) {
|
||||
led.setValue(!led.value());
|
||||
digitalWrite(LED_BUILTIN, led.value() ? HIGH : LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void InboxMessage::run(String a) {
|
||||
|
||||
if (a == u8"app:startup")
|
||||
{
|
||||
prop.sendAllData();
|
||||
prop.sendDone(a);
|
||||
}
|
||||
else if (a == u8"reset-mcu")
|
||||
{
|
||||
prop.resetMcu();
|
||||
}
|
||||
else if (a == "clignoter:1")
|
||||
{
|
||||
clignoter.setValue(true);
|
||||
|
||||
prop.sendAllData(); // all data change, we don't have to be selctive then
|
||||
prop.sendDone(a); // acknowledge prop command action
|
||||
}
|
||||
else if (a == "clignoter:0")
|
||||
{
|
||||
clignoter.setValue(false);
|
||||
|
||||
prop.sendAllData(); // all data change, we don't have to be selctive then
|
||||
prop.sendDone(a); // acknowledge prop command action
|
||||
}
|
||||
else
|
||||
{
|
||||
// acknowledge omition of the prop command
|
||||
prop.sendOmit(a);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user