|
| 1 | +/** |
| 2 | + * Test of webthing-arduino with async behavior. Works with |
| 3 | + * thing-url-adapter. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <Arduino.h> |
| 7 | +#include <Thing.h> |
| 8 | +#include <WebThingAdapter.h> |
| 9 | + |
| 10 | +const char *ssid = "XXXXXXX"; |
| 11 | +const char *password = "XXXXXXX"; |
| 12 | + |
| 13 | +int loginCounter; |
| 14 | +WebThingAdapter *adapter; |
| 15 | +// Forward declaration |
| 16 | +void onOffChanged(ThingPropertyValue newValue); |
| 17 | +void textChanged(ThingPropertyValue newValue); |
| 18 | +void numberChanged(ThingPropertyValue newValue); |
| 19 | + |
| 20 | +const char *asyncProperties[] = {"asyncProperty", nullptr}; |
| 21 | +ThingDevice textDisplay("asyncProperty", "AsyncProperty Test", |
| 22 | + asyncProperties); |
| 23 | +ThingProperty text("text", "", STRING, nullptr, textChanged); |
| 24 | +ThingProperty onOff("bool", "", BOOLEAN, "OnOffProperty", onOffChanged); |
| 25 | +ThingProperty number("number", "", NUMBER, nullptr, numberChanged); |
| 26 | + |
| 27 | +String message = "message"; |
| 28 | +String lastMessage = message; |
| 29 | +boolean lastState = false; |
| 30 | +int lastNumber = 0; |
| 31 | + |
| 32 | +// Callback functions |
| 33 | +// |
| 34 | +void onOffChanged(ThingPropertyValue newValue) { |
| 35 | + Serial.print("On/Off changed to : "); |
| 36 | + Serial.println(newValue.boolean); |
| 37 | +} |
| 38 | + |
| 39 | +void textChanged(ThingPropertyValue newValue) { |
| 40 | + Serial.print("New message : "); |
| 41 | + Serial.println(*newValue.string); |
| 42 | +} |
| 43 | + |
| 44 | +void numberChanged(ThingPropertyValue newValue) { |
| 45 | + Serial.print("New number : "); |
| 46 | + Serial.println(newValue.number); |
| 47 | +} |
| 48 | + |
| 49 | +void setup() { |
| 50 | + Serial.begin(115200); |
| 51 | + |
| 52 | + // counter used for login time out, function needed with ESP32 |
| 53 | + loginCounter = 0; |
| 54 | + |
| 55 | +#if defined(ESP8266) || defined(ESP32) |
| 56 | + WiFi.mode(WIFI_STA); |
| 57 | +#endif |
| 58 | + |
| 59 | + Serial.print("Connecting : "); |
| 60 | + WiFi.begin(ssid, password); |
| 61 | + |
| 62 | + // Wait for connection |
| 63 | + while (WiFi.status() != WL_CONNECTED) { |
| 64 | + |
| 65 | + // more than 5 sec wait, try to login again. |
| 66 | + if (loginCounter == 10) { |
| 67 | + loginCounter = 0; |
| 68 | + WiFi.begin(ssid, password); |
| 69 | + } else { |
| 70 | + loginCounter++; |
| 71 | + } |
| 72 | + delay(500); |
| 73 | + Serial.print("."); |
| 74 | + } |
| 75 | + |
| 76 | + Serial.println(""); |
| 77 | + Serial.print("Connected to "); |
| 78 | + Serial.println(ssid); |
| 79 | + Serial.print("IP address: "); |
| 80 | + Serial.println(WiFi.localIP()); |
| 81 | + |
| 82 | + adapter = new WebThingAdapter("asyncProperty", WiFi.localIP()); |
| 83 | + |
| 84 | + ThingPropertyValue value; |
| 85 | + value.string = &message; |
| 86 | + text.setValue(value); |
| 87 | + |
| 88 | + textDisplay.addProperty(&text); |
| 89 | + textDisplay.addProperty(&onOff); |
| 90 | + textDisplay.addProperty(&number); |
| 91 | + adapter->addDevice(&textDisplay); |
| 92 | + adapter->begin(); |
| 93 | +} |
| 94 | + |
| 95 | +void loop() { |
| 96 | + delay(500); |
| 97 | + adapter->update(); |
| 98 | +} |
0 commit comments