Skip to content

Commit 5c05ffb

Browse files
committed
Update README's example
1 parent 3bebb7e commit 5c05ffb

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

README.md

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,65 @@ libraries to your project.
5353
## Example
5454

5555
```c++
56+
#include <Arduino.h>
5657
#include <Thing.h>
5758
#include <WebThingAdapter.h>
5859

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+
}
6692

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());
6898

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");
73104

74-
adapter.addDevice(&toaster);
105+
analogWriteRange(255);
75106
}
76107

77-
void loop() {
108+
void loop(void){
78109
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);
93115
}
94116
}
95117
```

0 commit comments

Comments
 (0)