First backup. Part 1 works - it connects to wifi, the button cycles the button colors, MQTT is connected. No payload is sent yet.

This commit is contained in:
Normanras
2023-07-25 09:05:52 -04:00
commit ae2efe28a4
2 changed files with 157 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,157 @@
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#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();
}
}
}