@@ -53,43 +53,65 @@ libraries to your project.
53
53
## Example
54
54
55
55
``` c++
56
+ #include < Arduino.h>
56
57
#include < Thing.h>
57
58
#include < WebThingAdapter.h>
58
59
59
- WebThingAdapter adapter ("localtoast");
60
-
61
- ThingDevice toaster("toaster-1", "My Toaster", "toaster");
62
-
63
- ThingProperty toasterOn("on", "Toasting", BOOLEAN);
64
- ThingProperty toasterTimer("timer", "Time left in toast", NUMBER);
65
- ThingProperty toasterToast("toast", "Toast requested", BOOLEAN);
60
+ const char * ssid = " ......" ;
61
+ const char * password = " .........." ;
62
+
63
+ #if defined(LED_BUILTIN)
64
+ const int lampPin = LED_BUILTIN;
65
+ #else
66
+ const int lampPin = 13; // manully configure LED pin
67
+ #endif
68
+
69
+ WebThingAdapter adapter ("led-lamp");
70
+
71
+ const char* lampTypes[ ] = {"OnOffSwitch", "Light", nullptr};
72
+ ThingDevice lamp("lamp", "My Lamp", lampTypes);
73
+
74
+ ThingProperty lampOn("on", "Whether the lamp is turned on", BOOLEAN, "OnOffProperty");
75
+ ThingProperty lampLevel("level", "The level of light from 0-100", NUMBER, "BrightnessProperty");
76
+
77
+ void setup(void){
78
+ pinMode(lampPin, OUTPUT);
79
+ digitalWrite(lampPin, HIGH);
80
+ Serial.begin(115200);
81
+ #if defined(ESP8266) || defined(ESP32)
82
+ WiFi.mode(WIFI_STA);
83
+ #endif
84
+ WiFi.begin(ssid, password);
85
+ Serial.println("");
86
+
87
+ // Wait for connection
88
+ while (WiFi.status() != WL_CONNECTED) {
89
+ delay(500);
90
+ Serial.print(".");
91
+ }
66
92
67
- ToasterInterface toasterInterface;
93
+ Serial.println("");
94
+ Serial.print("Connected to ");
95
+ Serial.println(ssid);
96
+ Serial.print("IP address: ");
97
+ Serial.println(WiFi.localIP());
68
98
69
- void setup() {
70
- toaster.addProperty(&toasterOn);
71
- toaster.addProperty(&toasterTimer);
72
- toaster.addProperty(&toasterToast);
99
+ lamp.addProperty(&lampOn);
100
+ lamp.addProperty(&lampLevel);
101
+ adapter.addDevice(&lamp);
102
+ adapter.begin();
103
+ Serial.println("HTTP server started");
73
104
74
- adapter.addDevice(&toaster );
105
+ analogWriteRange(255 );
75
106
}
76
107
77
- void loop() {
108
+ void loop(void) {
78
109
adapter.update();
79
-
80
- ThingPropertyValue onValue;
81
- onValue.boolean = toasterInterface.isToasting();
82
- toasterOn.setValue(onValue);
83
-
84
- ThingPropertyValue timerValue;
85
- timerValue.number = toasterInterface.getSecondsLeft();
86
- toasterTimer.setValue(timerValue);
87
-
88
- if (toasterToast.getValue().boolean) {
89
- ThingPropertyValue toastValue;
90
- toastValue.boolean = false;
91
- toasterToast.setValue(toastValue);
92
- toasterInterface.startToasting();
110
+ if (lampOn.getValue().boolean) {
111
+ int level = map(lampLevel.getValue().number, 0, 100, 255, 0);
112
+ analogWrite(lampPin, level);
113
+ } else {
114
+ analogWrite(lampPin, 255);
93
115
}
94
116
}
95
117
```
0 commit comments