Skip to content

Simple led example #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 71 additions & 29 deletions examples/LED/LED.ino
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>
#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<JsonObject>();
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);
Expand All @@ -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<JsonObject>();
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);
}