Actions
Task #7373
closedTask #7371: mozilla iot
make a mozilla WebThings Framework
Added by David Hasselhoff about 6 years ago. Updated about 6 years ago.
Start date:
11/29/2019
Due date:
% Done:
40%
Estimated time:
PM Check date:
Updated by David Hasselhoff about 6 years ago
- Status changed from New to In Progress
Does not work with ipv6 out of the boy
Updated by David Hasselhoff about 6 years ago
/**
* 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/.
*/
#include <Arduino.h>
#include "Thing.h"
#include "WebThingAdapter.h"
class Button
{
private:
int lastValue;
unsigned long lastDebounceTime;
void (*pCallback)(Button*);
public:
String label;
int pin;
int state;
bool pressed;
void read();
Button(int PIN, void (*pCallbackFunction)(Button*));
};
Button::Button(int PIN, void (*pCallbackFunction)(Button*))
{
pin = PIN;
pCallback = pCallbackFunction;
pinMode(pin, INPUT_PULLUP);
state = digitalRead(pin);
pressed = state == LOW;
lastValue = state;
lastDebounceTime = 0;
}
void Button::read()
{
int value = digitalRead(pin);
if (value != lastValue){
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > 10){
if (value != state){
state = value;
pressed = state == LOW;
pCallback(this);
}
}
lastValue = value;
}
const char* ssid = "Digxxxil";
const char* password = "unxx";
const int ledPin = 5; // pin is also relais
const int releaisPin = ledPin; // pin is also led
WebThingAdapter* adapter;
const char* ledTypes[] = {"OnOffSwitch", "Light", nullptr};
ThingDevice led("led", "Built-in LED", ledTypes);
ThingProperty ledOn("on", "", BOOLEAN, "OnOffProperty");
const char* buttonTypes[] = {"PushButton", "Button", nullptr};
ThingDevice button("Button", "Button is pressed o not", buttonTypes);
ThingProperty buttonOn("on", "button pin", BOOLEAN, "PressedNotPressed");
const char* buttonCountTypes[] = {"count", "count", nullptr};
ThingDevice count("Count", "button count count count", buttonCountTypes);
ThingProperty buttonCount("count", "button pin", NUMBER, "count count");
bool lastOn = false;
bool buttonValue = false;
int buttonPressedCount = 0;
void myButtonCallback(Button* button)
{
ThingPropertyValue buttonValue;
ThingPropertyValue bCount;
if(button->pressed){
buttonPressedCount++;
buttonValue.boolean = true;
bCount.number = buttonPressedCount;
buttonCount.setValue(bCount);
//buttonCount.setValue(buttonCount);
buttonOn.setValue(buttonValue);
Serial.println("button on");
}
else{
buttonValue.boolean = false;
buttonOn.setValue(buttonValue);
Serial.println("button off");
}
}
const int buttonPin = 0;
Button mButton = Button(buttonPin, &myButtonCallback);
const int buttonAmount = 1;
Button* buttons[buttonAmount] = {
&mButton
};
void setup(void)
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
pinMode(buttonPin, INPUT);
buttonValue = digitalRead(buttonPin);
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
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 ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
adapter = new WebThingAdapter("w25", WiFi.localIP());
led.id = WiFi.macAddress();
led.title = WiFi.macAddress();
led.addProperty(&ledOn);
button.addProperty(&buttonOn);
count.addProperty(&buttonCount);
adapter->addDevice(&led);
adapter->addDevice(&button);
adapter->addDevice(&count);
adapter->begin();
Serial.println("HTTP server started");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print("/things/");
Serial.println(led.id);
}
void loop(void)
{
for (int i = 0; i < buttonAmount; i++){
buttons[i]->read();
}
adapter->update();
bool on = !ledOn.getValue().boolean;
digitalWrite(ledPin, on ? LOW : HIGH); // active low led
buttonValue = digitalRead(buttonPin);
if (on != lastOn) {
Serial.print(led.id);
Serial.print(": ");
Serial.println(on);
}
lastOn = on;
}
Updated by David Hasselhoff about 6 years ago
- % Done changed from 0 to 40
Try it with 2X10 devices, good luck
Updated by David Hasselhoff about 6 years ago
- Status changed from In Progress to Closed
Actions