From ad538bd55490973b758b4d4652e726e39d89aa9a Mon Sep 17 00:00:00 2001 From: Citrullin Date: Thu, 19 Aug 2021 02:47:36 +0200 Subject: [PATCH 1/2] Add simple internal LED on/off example --- examples/LED/LED.ino | 108 ++++++++++++++++++++++++++++++------------- 1 file changed, 77 insertions(+), 31 deletions(-) diff --git a/examples/LED/LED.ino b/examples/LED/LED.ino index 1a8bf80..2c596dc 100644 --- a/examples/LED/LED.ino +++ b/examples/LED/LED.ino @@ -1,39 +1,45 @@ /** - * 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" +#include +#include // 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; +const int lampPin = LED_BUILTIN; #else -const int ledPin = 13; // manually configure LED pin +const int lampPin = 13; // manually configure LED pin #endif +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); + +ThingProperty lampOn("state", "Whether the lamp is turned on", BOOLEAN, + "OnOffProperty"); -bool lastOn = false; +StaticJsonDocument<256> toggleInput; +JsonObject toggleInputObj = toggleInput.to(); +ThingAction toggle("toggle", "Toggle", "toggle the lamp on/off", + "ToggleAction", &toggleInputObj, action_generator); + +bool lastOn = true; void setup(void) { - pinMode(ledPin, OUTPUT); - digitalWrite(ledPin, HIGH); + pinMode(lampPin, OUTPUT); + digitalWrite(lampPin, HIGH); Serial.begin(115200); Serial.println(""); Serial.print("Connecting to \""); @@ -46,40 +52,80 @@ void setup(void) { Serial.println(""); // Wait for connection - bool blink = true; while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); - digitalWrite(ledPin, blink ? LOW : HIGH); // active low led - blink = !blink; } - digitalWrite(ledPin, HIGH); // active low led Serial.println(""); Serial.print("Connected to "); 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 lamp"; - led.addProperty(&ledOn); - adapter->addDevice(&led); + lampOn.title = "On/Off"; + lamp.addProperty(&lampOn); + + 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 = true}; + lampOn.setValue(initialOn); + (void)lampOn.changedValueOrNull(); + } 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(lampPin, HIGH); + } else { + digitalWrite(lampPin, LOW); } - lastOn = on; + + if (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); + + if (state) { + digitalWrite(lampPin, HIGH); + } else { + digitalWrite(lampPin, LOW); + } + + ThingDataValue value = {.boolean = state}; + lampOn.setValue(value); +} + +ThingActionObject *action_generator(DynamicJsonDocument *input) { + return new ThingActionObject("toggle", input, do_toggle, nullptr); } From aaed5297ff2c4ed91d9a339cb458a1a37cf1007a Mon Sep 17 00:00:00 2001 From: Citrullin Date: Thu, 26 Aug 2021 01:35:18 +0200 Subject: [PATCH 2/2] Use external LED --- examples/LED/LED.ino | 46 ++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/examples/LED/LED.ino b/examples/LED/LED.ino index 2c596dc..92e84cf 100644 --- a/examples/LED/LED.ino +++ b/examples/LED/LED.ino @@ -7,19 +7,21 @@ #define LARGE_JSON_BUFFERS 1 #include -#include -#include +#include "Thing.h" +#include "WebThingAdapter.h" // TODO: Hardcode your wifi credentials here (and keep it private) const char *ssid = ""; const char *password = ""; #if defined(LED_BUILTIN) -const int lampPin = LED_BUILTIN; +const int ledPin = LED_BUILTIN; #else -const int lampPin = 13; // manually configure LED pin +const int ledPin = 19; // manually configure LED pin #endif +const int externalPin = 19; + ThingActionObject *action_generator(DynamicJsonDocument *); WebThingAdapter *adapter; @@ -35,27 +37,25 @@ JsonObject toggleInputObj = toggleInput.to(); ThingAction toggle("toggle", "Toggle", "toggle the lamp on/off", "ToggleAction", &toggleInputObj, action_generator); -bool lastOn = true; - void setup(void) { - pinMode(lampPin, OUTPUT); - digitalWrite(lampPin, HIGH); + 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); Serial.print("."); + digitalWrite(ledPin, blink ? LOW : HIGH); // active low led + blink = !blink; } + digitalWrite(ledPin, HIGH); // active low led Serial.println(""); Serial.print("Connected to "); @@ -64,7 +64,7 @@ void setup(void) { Serial.println(WiFi.localIP()); adapter = new WebThingAdapter("led-lamp", WiFi.localIP()); - lamp.description = "A web connected lamp"; + lamp.description = "A web connected led"; lampOn.title = "On/Off"; lamp.addProperty(&lampOn); @@ -87,19 +87,21 @@ void setup(void) { Serial.println(lamp.id); // set initial values - ThingPropertyValue initialOn = {.boolean = true}; + ThingPropertyValue initialOn = {.boolean = false}; lampOn.setValue(initialOn); + digitalWrite(externalPin, LOW); (void)lampOn.changedValueOrNull(); } - +bool lastOn = false; void loop(void) { adapter->update(); bool on = lampOn.getValue().boolean; + if (on) { - digitalWrite(lampPin, HIGH); + digitalWrite(externalPin, HIGH); } else { - digitalWrite(lampPin, LOW); + digitalWrite(externalPin, LOW); } if (lastOn != on) { @@ -116,12 +118,6 @@ void do_toggle(const JsonVariant &input) { Serial.print("state: "); Serial.println(state); - if (state) { - digitalWrite(lampPin, HIGH); - } else { - digitalWrite(lampPin, LOW); - } - ThingDataValue value = {.boolean = state}; lampOn.setValue(value); }