diff --git a/lib/switch/switch.cpp b/lib/switch/switch.cpp new file mode 100644 index 0000000..500cca8 --- /dev/null +++ b/lib/switch/switch.cpp @@ -0,0 +1,92 @@ + + +#include "switch.h" +#include + +//#include + + +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); + } +} diff --git a/lib/switch/switch.h b/lib/switch/switch.h new file mode 100644 index 0000000..0e57d71 --- /dev/null +++ b/lib/switch/switch.h @@ -0,0 +1,34 @@ +#ifndef FollyEngine_Switch_h +#define FollyEngine_Switch_h + +#include +#include + +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 \ No newline at end of file diff --git a/lib/web/web.cpp b/lib/web/web.cpp index a25bc94..5ec7b79 100644 --- a/lib/web/web.cpp +++ b/lib/web/web.cpp @@ -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); diff --git a/src/neopixel_string.ino b/src/neopixel_string.ino index e687df6..7c35984 100644 --- a/src/neopixel_string.ino +++ b/src/neopixel_string.ino @@ -2,6 +2,12 @@ #ifdef ESP8266 #include + +#include +#include +#include +#include + #else //esp32 only #include @@ -9,10 +15,6 @@ #include #include -#include -#include -#include - // //breif: @@ -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]; @@ -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 @@ -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