From ae2efe28a44468fa58ffaf77ec3547e5179d1b0b Mon Sep 17 00:00:00 2001 From: Normanras <44226464+Normanras@users.noreply.github.com> Date: Tue, 25 Jul 2023 09:05:52 -0400 Subject: [PATCH] First backup. Part 1 works - it connects to wifi, the button cycles the button colors, MQTT is connected. No payload is sent yet. --- .DS_Store | Bin 0 -> 6148 bytes .../ESP_Medicine_Indicator.ino | 157 ++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 .DS_Store create mode 100644 ESP_Medicine_Indicator/ESP_Medicine_Indicator.ino diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..cb0cb34d9d3fb699d822180bc93f981168116595 GIT binary patch literal 6148 zcmeHK!AiqG5Pe&Fh_R<1%dFbWQNy&$X~Ly(tDHX_bh!l}%Wq@` z$I?qkOf)WyTV>uMf`*bcG+2ufG@SF%)|JMsvW6pA^AYUH!8(+XpU(YbUXD;IyLSa# zfn5cT^l~KifBJL(zZ>LNu7E3WPzr=$HLE5}$!cpaIjOagc0&`$x>k7`!a**@Tq~vc bjF!RqC +#include +#include +#include +#include + +#define NEO_PIN 14 // Pin for all the Neopixels +#define SIG_PIX 2 // Number of temperature pixels +#define BUTTON_FWD_PIN 4 +#define mqtt_topic "home/medicine" + +const char* ssid = "Saints&Strangers_"; +const char* password = "xYhnac-5mixdu"; +const char* mqtt_server = "192.168.200.55"; +const char* mqtt_user = "hass"; +const char* mqtt_password = "Over9+look*"; + +boolean oldState = HIGH; +int mode = 0; // Active mode on startup + +WiFiClient espClient; +PubSubClient client(espClient); +long lastMsg = 0; +char msg[50]; +float value = 0; + +Adafruit_NeoPixel NeoJewel = Adafruit_NeoPixel(SIG_PIX, NEO_PIN, NEO_GRB + NEO_KHZ800); +unsigned long delayTime; + +// Taken Medicine Color +uint32_t pineGreen = NeoJewel.Color(14, 170, 26); +// Not Yet Taken Color +uint32_t pureRed = NeoJewel.Color(255, 0, 0); +// Warning Blink Color +uint32_t yellow = NeoJewel.Color(255, 229, 0); + +void setup() { + delayTime = 1000; + Serial.begin(9600); + delay(500); + + while (!Serial) { + // wait for serial port to connect. Needed for native USB port only + } + + Serial.println("Attempting to connect to SSID: "); + Serial.print(ssid); + WiFi.begin(ssid, password); + + // Attempt to connect to WiFi network: + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); //You can get IP address assigned to ESP + Serial.println("Mac Address: "); + Serial.println(WiFi.macAddress()); + delay(100); + client.setServer("192.168.200.55", 1883); + WiFi.setAutoReconnect(true); + WiFi.persistent(true); + + // Necessary code for NeoPixel setup + pinMode(BUTTON_FWD_PIN, INPUT_PULLUP); + + //Initialization of the Jewel + NeoJewel.setBrightness(100); + NeoJewel.begin(); + NeoJewel.clear(); + NeoJewel.show(); +} + +void loop() { + boolean newState = digitalRead(BUTTON_FWD_PIN); + if((newState == LOW) && (oldState == HIGH)) { + delay(30); + newState = digitalRead(BUTTON_FWD_PIN); + if(newState == LOW) { + if(++mode > 4) mode = 0; + switch(mode) { + case 0: + NeoJewel.clear(); + NeoJewel.fill(yellow, 0, 2); + NeoJewel.show(); + break; + case 1: + // Nothing Taken + NeoJewel.clear(); + NeoJewel.fill(pureRed, 0, 2); + NeoJewel.show(); + break; + case 2: + // Morning Taken + NeoJewel.clear(); + NeoJewel.fill(pineGreen, 0, 1); + NeoJewel.fill(pureRed, 1, 2); + NeoJewel.show(); + break; + case 3: + // Afternoon Taken + NeoJewel.clear(); + NeoJewel.fill(pineGreen, 1, 2); + NeoJewel.fill(pureRed, 0, 1); + NeoJewel.show(); + break; + case 4: + // Both Taken + NeoJewel.clear(); + NeoJewel.fill(pineGreen, 0, 2); + NeoJewel.show(); + break; + } + } + } + // Set the last-read button state to the old state (reset) + oldState = newState; + + if (WiFi.status() != WL_CONNECTED) { + delay(1000); + WiFi.disconnect(); + ESP.restart(); + } + + delay(1000); + if (!client.connected()) { + reconnect(); + } + client.loop(); + +/*long now = millis(); +if (now - lastMsg > 5000) { + lastMsg = now; +delay(3000); + }*/ +} + +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.println("Connecting to MQTT..."); + // Create a random client ID + String clientId = "ESP8266Client-"; + clientId += String(random(0xffff), HEX); + if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) { + Serial.println("connected"); + } else { + Serial.println("failed with state "); + Serial.print(client.state()); + delay(2000); + ESP.restart(); + } + } +} \ No newline at end of file