Skip to content

Commit

Permalink
add the wemos button
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
  • Loading branch information
SvenDowideit committed Dec 12, 2019
1 parent f65b573 commit 8397f6a
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 7 deletions.
92 changes: 92 additions & 0 deletions lib/switch/switch.cpp
@@ -0,0 +1,92 @@


#include "switch.h"
#include <ArduinoJson.h>

//#include <Wire.h>


char *SwitchDevice::deviceType = "switch";

// esp8266-4c11aedd362/multipass/button {"state":0,"device":"multipass","time":"2019-12-12T08:14:44"}


// use D3
#define SwitchPin1 D3

SwitchDevice::SwitchDevice(Mqtt *m) {
mqtt = m;
}

void SwitchDevice::setup() {
//Serial.begin(115200);
Serial.println("Switch Shield Testing...");
pinMode(SwitchPin1, INPUT);
}

const char *SwitchDevice::IsMessageForMe(const char * topic) {
size_t length = strlen(SwitchDevice::deviceType);
if (strncmp(SwitchDevice::deviceType, topic, length) == 0) {
return topic+length+1;
}
return NULL;
}

void SwitchDevice::loop() {
if (!initialised) {
delay(1);
setup();
subscribe();
}
// the buttonPin is read multiple times and the value must remain the same for debounceDelay millis to toggle the LED

// read button state, HIGH when pressed, LOW when not
thisButtonState = digitalRead(SwitchPin1);

// if the current state does not match the previous state
// the button was just pressed/released, or is transition noise
if (thisButtonState != lastButtonState) {
// reset the timer
lastDebounceTime = millis();
}

// once delay millis have elapsed, if the state remains the same, register the press
if ((millis() - lastDebounceTime) > debounceDelay) {

// if the button state has changed
if (thisButtonState != buttonState) {
buttonState = thisButtonState;
StaticJsonDocument<300> root;
root["state"] = buttonState;
mqtt->publish(mqtt->getObject(), "button", root);
}
}

// persist for next loop iteration
lastButtonState = thisButtonState;
}

// TODO: add a status topic to request what the current state is
void SwitchDevice::subscribe() {
initialised = true;
}

void SwitchDevice::mqtt_callback_fn(const char* topic, const char* payload, unsigned int length) {
// 10 elements in the json payload?
// DynamicJsonBuffer jb(JSON_OBJECT_SIZE(10));
// JsonObject& obj = jb.parseObject(payload);
StaticJsonDocument<300> obj;
DeserializationError error = deserializeJson(obj, payload);
if (error) {
Serial.printf("Failed to parse JSON: %s\n", error.c_str());
return;
}

String cmd = obj["Switch"] | "off";
Serial.printf("Switch set: %s\r\n", cmd.c_str());
if (cmd == "on") {
digitalWrite(SwitchPin1, !LOW);
} else {
digitalWrite(SwitchPin1, LOW);
}
}
34 changes: 34 additions & 0 deletions lib/switch/switch.h
@@ -0,0 +1,34 @@
#ifndef FollyEngine_Switch_h
#define FollyEngine_Switch_h

#include <device.h>
#include <mqtt.h>

class SwitchDevice: public Device {
public:
SwitchDevice(Mqtt *mqtt);

virtual void setup();
virtual void loop();
virtual void subscribe();
virtual const char* IsMessageForMe(const char *topic);
virtual void mqtt_callback_fn(const char* topic, const char* payload, unsigned int length);

private:
static char *deviceType;
Mqtt *mqtt;
boolean initialised = false;

// the current state of the button
int buttonState = LOW;

// the current and previous readings from the input pin
int thisButtonState = LOW;
int lastButtonState = LOW;

// time is measured in milliseconds and will quickly exceed limitations of an integer, so we use a long for these two
unsigned long lastDebounceTime = 0; // the time the button state last switched
unsigned long debounceDelay = 50; // the state must remain the same for this many millis to register the button press
};

#endif
1 change: 1 addition & 0 deletions lib/web/web.cpp
Expand Up @@ -63,6 +63,7 @@ String getContentType(String filename) { // convert the file extension to the MI

void web_setup(const char *update_password) {
// SPIFFS.begin(); // Start the SPI Flash Files System
//server.authenticate("folly", update_password);

server.on("/metrics", handle_metrics);

Expand Down
18 changes: 11 additions & 7 deletions src/neopixel_string.ino
Expand Up @@ -2,17 +2,19 @@

#ifdef ESP8266
#include <servo.h>

#include <neopixel.h>
#include <motor.h>
#include <relay.h>
#include <switch.h>

#else
//esp32 only
#include <touch.h>
#endif

#include <web.h>
#include <mqtt.h>
#include <neopixel.h>
#include <motor.h>
#include <relay.h>


//
//breif:
Expand All @@ -27,7 +29,7 @@
// GO READ https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/

//BUILD with "LOLIN(WEMOS) D1 R2 & mini"
Mqtt mqtt = Mqtt(SECRET_SSID, SECRET_PASSWORD, "mqtt", 1883, "multipass");
Mqtt mqtt = Mqtt(SECRET_SSID, SECRET_PASSWORD, "192.168.43.84", 1883, "multipass");


Device *devices[5];
Expand Down Expand Up @@ -75,7 +77,7 @@ void setup() {
// https://github.com/wemos/D1_mini_Examples/blob/master/examples/04.Shields/RGB_LED_Shield/simple/simple.ino
// #define LEDPIN D4
// #define LED_NUM 7
// devices[deviceCount++] = new NeopixelString(D4, 7, NEO_GRB + NEO_KHZ800, &mqtt); // GRB
devices[deviceCount++] = new NeopixelString(D4, 7, NEO_GRB + NEO_KHZ800, &mqtt); // GRB

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
Expand All @@ -92,8 +94,10 @@ void setup() {

// use D3 and D6
//devices[deviceCount++] = new ServoDevice(&mqtt);
// use D3
devices[deviceCount++] = new SwitchDevice(&mqtt);
// use D1
devices[deviceCount++] = new RelayDevice(&mqtt);
//devices[deviceCount++] = new RelayDevice(&mqtt);
#else
devices[deviceCount++] = new TouchDevice(&mqtt); //<<<<<<<<<------ for the esp32
#endif
Expand Down

0 comments on commit 8397f6a

Please sign in to comment.