Skip to content

Commit 1cf8cb8

Browse files
lgLindstrommrstegeman
authored andcommitted
Async Property, Tested in example (#55)
1 parent 962fa43 commit 1cf8cb8

File tree

3 files changed

+181
-2
lines changed

3 files changed

+181
-2
lines changed

Thing.h

+16-2
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,16 @@ class ThingItem {
322322
};
323323

324324
class ThingProperty : public ThingItem {
325+
private:
326+
void (*callback)(ThingPropertyValue);
327+
325328
public:
326329
const char **propertyEnum = nullptr;
327330

328331
ThingProperty(const char *id_, const char *description_, ThingDataType type_,
329-
const char *atType_)
330-
: ThingItem(id_, description_, type_, atType_) {}
332+
const char *atType_,
333+
void (*callback_)(ThingPropertyValue) = nullptr)
334+
: ThingItem(id_, description_, type_, atType_), callback(callback_) {}
331335

332336
void serialize(JsonObject obj, String deviceId, String resourceType) {
333337
ThingItem::serialize(obj, deviceId, resourceType);
@@ -344,6 +348,12 @@ class ThingProperty : public ThingItem {
344348
}
345349
}
346350
}
351+
352+
void changed(ThingPropertyValue newValue) {
353+
if (callback != nullptr) {
354+
callback(newValue);
355+
}
356+
}
347357
};
348358

349359
#ifndef WITHOUT_WS
@@ -575,22 +585,26 @@ class ThingDevice {
575585
ThingDataValue value;
576586
value.boolean = newValue.as<bool>();
577587
property->setValue(value);
588+
property->changed(value);
578589
break;
579590
}
580591
case NUMBER: {
581592
ThingDataValue value;
582593
value.number = newValue.as<double>();
583594
property->setValue(value);
595+
property->changed(value);
584596
break;
585597
}
586598
case INTEGER: {
587599
ThingDataValue value;
588600
value.integer = newValue.as<signed long long>();
589601
property->setValue(value);
602+
property->changed(value);
590603
break;
591604
}
592605
case STRING:
593606
*(property->getValue().string) = newValue.as<String>();
607+
property->changed(property->getValue());
594608
break;
595609
}
596610
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; http://docs.platformio.org/page/projectconf.html
10+
11+
[platformio]
12+
; uncomment below to build a one env
13+
; env_default= d1
14+
; env_default= nodemcuv2
15+
; env_default= esp32dev
16+
; env_default= samd21g18a
17+
18+
[global]
19+
lib_deps =
20+
https://github.com/mozilla-iot/webthing-arduino.git
21+
ArduinoJson
22+
monitor_speed = 115200
23+
24+
[env:d1]
25+
platform = espressif8266
26+
board = d1
27+
framework = arduino
28+
lib_deps =
29+
${global.lib_deps}
30+
ESP Async WebServer
31+
lib_ignore = WiFi101
32+
lib_ldf_mode = deep+
33+
monitor_speed = ${global.monitor_speed}
34+
35+
[env:nodemcuv2]
36+
platform = espressif8266
37+
board = nodemcuv2
38+
framework = arduino
39+
lib_deps =
40+
${global.lib_deps}
41+
ESP Async WebServer
42+
lib_ignore =
43+
ArduinoMDNS
44+
WiFi101
45+
lib_ldf_mode = deep+
46+
monitor_speed = ${global.monitor_speed}
47+
48+
[env:esp32dev]
49+
platform = espressif32
50+
board = esp32dev
51+
framework = arduino
52+
lib_deps =
53+
${global.lib_deps}
54+
ESP Async WebServer
55+
lib_ignore = WiFi101
56+
lib_ldf_mode = deep+
57+
monitor_speed = ${global.monitor_speed}
58+
59+
[env:samd21g18a]
60+
platform = atmelsam
61+
board = samd21g18a
62+
framework = arduino
63+
lib_deps =
64+
${global.lib_deps}
65+
WiFi101
66+
ArduinoMDNS
67+
lib_ldf_mode = deep+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)