Skip to content

Oled display example #13

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
Show file tree
Hide file tree
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
108 changes: 77 additions & 31 deletions examples/LED/LED.ino
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>
#include "Thing.h"
#include "WebThingAdapter.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;
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<JsonObject>();
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 \"");
Expand All @@ -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<JsonObject>();
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);
}
Loading