diff --git a/examples/LED/LED.ino b/examples/LED/LED.ino index 1a8bf80..92e84cf 100644 --- a/examples/LED/LED.ino +++ b/examples/LED/LED.ino @@ -1,51 +1,53 @@ /** - * Simple server compliant with Mozilla's proposed WoT API - * Originally based on the HelloServer example - * Tested on ESP8266, ESP32, Arduino boards with WINC1500 modules (shields or - * MKR1000) - * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#define LARGE_JSON_BUFFERS 1 + #include #include "Thing.h" #include "WebThingAdapter.h" // TODO: Hardcode your wifi credentials here (and keep it private) -const char *ssid = "public"; +const char *ssid = ""; const char *password = ""; #if defined(LED_BUILTIN) const int ledPin = LED_BUILTIN; #else -const int ledPin = 13; // manually configure LED pin +const int ledPin = 19; // manually configure LED pin #endif +const int externalPin = 19; + +ThingActionObject *action_generator(DynamicJsonDocument *); + WebThingAdapter *adapter; -const char *ledTypes[] = {"OnOffSwitch", "Light", nullptr}; -ThingDevice led("led", "Built-in LED", ledTypes); -ThingProperty ledOn("on", "", BOOLEAN, "OnOffProperty"); +const char *lampTypes[] = {"OnOffSwitch", "Light", nullptr}; +ThingDevice lamp("lamp123", "My Lamp", lampTypes); -bool lastOn = false; +ThingProperty lampOn("state", "Whether the lamp is turned on", BOOLEAN, + "OnOffProperty"); + +StaticJsonDocument<256> toggleInput; +JsonObject toggleInputObj = toggleInput.to(); +ThingAction toggle("toggle", "Toggle", "toggle the lamp on/off", + "ToggleAction", &toggleInputObj, action_generator); void setup(void) { pinMode(ledPin, OUTPUT); + pinMode(externalPin, OUTPUT); digitalWrite(ledPin, HIGH); Serial.begin(115200); Serial.println(""); Serial.print("Connecting to \""); Serial.print(ssid); Serial.println("\""); -#if defined(ESP8266) || defined(ESP32) - WiFi.mode(WIFI_STA); -#endif WiFi.begin(ssid, password); - Serial.println(""); - - // Wait for connection + bool blink = true; while (WiFi.status() != WL_CONNECTED) { delay(500); @@ -60,26 +62,66 @@ void setup(void) { Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); - adapter = new WebThingAdapter("w25", WiFi.localIP()); + adapter = new WebThingAdapter("led-lamp", WiFi.localIP()); + + lamp.description = "A web connected led"; + + lampOn.title = "On/Off"; + lamp.addProperty(&lampOn); - led.addProperty(&ledOn); - adapter->addDevice(&led); + toggleInputObj["type"] = "object"; + JsonObject stateInputProperties = + toggleInputObj.createNestedObject("properties"); + JsonObject stateInput = + stateInputProperties.createNestedObject("state"); + stateInput["type"] = "boolean"; + lamp.addAction(&toggle); + + adapter->addDevice(&lamp); adapter->begin(); + Serial.println("HTTP server started"); Serial.print("http://"); Serial.print(WiFi.localIP()); Serial.print("/things/"); - Serial.println(led.id); -} + Serial.println(lamp.id); + // set initial values + ThingPropertyValue initialOn = {.boolean = false}; + lampOn.setValue(initialOn); + digitalWrite(externalPin, LOW); + (void)lampOn.changedValueOrNull(); + +} +bool lastOn = false; void loop(void) { adapter->update(); - bool on = ledOn.getValue().boolean; - digitalWrite(ledPin, on ? LOW : HIGH); // active low led - if (on != lastOn) { - Serial.print(led.id); - Serial.print(": "); - Serial.println(on); + bool on = lampOn.getValue().boolean; + + if (on) { + digitalWrite(externalPin, HIGH); + } else { + digitalWrite(externalPin, LOW); + } + + if (lastOn != on) { + lastOn = on; } - lastOn = on; +} + +void do_toggle(const JsonVariant &input) { + Serial.println("toggle call"); + + JsonObject inputObj = input.as(); + bool state = inputObj["state"]; + + Serial.print("state: "); + Serial.println(state); + + ThingDataValue value = {.boolean = state}; + lampOn.setValue(value); +} + +ThingActionObject *action_generator(DynamicJsonDocument *input) { + return new ThingActionObject("toggle", input, do_toggle, nullptr); }