Created
January 26, 2016 14:43
-
-
Save mveinot/2b801c97e14eac892e94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ESP8266 with 20x4 i2c LCD | |
// Compatible with the Arduino IDE 1.6.4 | |
// Library https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library | |
// Bavensky :3 | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <Wire.h> | |
#include <LiquidCrystal_I2C.h> | |
LiquidCrystal_I2C lcd(0x27, 20, 4); | |
WiFiClient w_client; | |
PubSubClient mqtt(w_client); | |
void debug(String, bool); | |
void callback(char* topic, byte* payload, unsigned int length); | |
void reconnect(); | |
enum Area { kitchen, pool, laundry, crawlspace, washer, dryer }; | |
uint8_t celcius[8] = {B00100, | |
B01010, | |
B00100, | |
B00000, | |
B00000, | |
B00000, | |
B00000, | |
}; | |
String temp[6] = { "0.0", "0.0", "0.0", "0.0", "", "" }; | |
String area_name[4] = {"Kitchen", "Pool", "Laundry", "Storage" }; | |
unsigned long last_update = 0; | |
bool show_laundry = false; | |
void setup() { | |
lcd.init(); | |
lcd.backlight(); | |
lcd.createChar(1, celcius); | |
Serial.begin(115200); | |
lcd.home(); | |
lcd.print("HomeStatus loading"); | |
lcd.setCursor(0, 1); | |
lcd.print("Connecting to "); | |
lcd.print("vicnet"); | |
lcd.setCursor(0, 2); | |
WiFi.hostname("Notifier"); | |
WiFi.begin("******", "******"); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
debug(".", 0); | |
} | |
lcd.print("WiFi connected"); | |
lcd.setCursor(0, 3); | |
lcd.print("IP: "); | |
lcd.print(WiFi.localIP().toString()); | |
mqtt.setServer("mqttserver.com", 1883); | |
mqtt.setCallback(callback); | |
delay(3000); | |
} | |
void loop() { | |
if (!mqtt.connected()) { | |
reconnect(); | |
} | |
mqtt.loop(); | |
if (last_update + 10000 < millis()) | |
{ | |
if (show_laundry) | |
{ | |
lcd.backlight(); | |
lcd.clear(); | |
lcd.print("Laundry Status"); | |
show_laundry = false; | |
} else | |
{ | |
lcd.noBacklight(); | |
lcd.clear(); | |
for (int i = 0; i < 4; i++) | |
{ | |
lcd.setCursor(0, i); | |
lcd.print(area_name[i]); | |
lcd.print(": "); | |
lcd.setCursor(9, i); | |
lcd.print(temp[i]); | |
lcd.write(1); | |
lcd.print("C"); | |
} | |
} | |
last_update = millis(); | |
} | |
yield(); | |
} | |
void debug(String in_str, bool newline) | |
{ | |
Serial.print(in_str); | |
if (newline) | |
{ | |
Serial.println(""); | |
} | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!mqtt.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (mqtt.connect("NotifierClient", "mqtt_user", "mqtt_pass")) { | |
Serial.println("connected"); | |
// Once connected, publish an announcement... | |
mqtt.publish("temperature/Information", "Notifier connected"); | |
// ... and resubscribe | |
mqtt.subscribe("temperature/#"); | |
mqtt.subscribe("laundry/#"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(mqtt.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void callback(char* in_topic, byte* in_payload, unsigned int in_length) | |
{ | |
lcd.setCursor(19, 0); | |
lcd.print("*"); | |
String topic(in_topic); | |
char c_message[in_length + 1]; | |
for (int i = 0; i < in_length; i++) | |
{ | |
c_message[i] = (char)in_payload[i]; | |
} | |
c_message[in_length] = '\0'; | |
String str_temp(c_message); | |
if (topic.indexOf("Kitchen") > -1) | |
{ | |
Serial.println("k"); | |
temp[kitchen] = str_temp; | |
} | |
if (topic.indexOf("Pool") > -1) | |
{ | |
Serial.println("p"); | |
temp[pool] = str_temp; | |
} | |
if (topic.indexOf("Laundry") > -1) | |
{ | |
Serial.println("l"); | |
temp[laundry] = str_temp; | |
} | |
if (topic.indexOf("Crawlspace") > -1) | |
{ | |
Serial.println("c"); | |
temp[crawlspace] = str_temp; | |
} | |
if (topic.indexOf("Washer") > -1) | |
{ | |
Serial.println("w"); | |
temp[washer] = str_temp; | |
show_laundry = true; | |
} | |
if (topic.indexOf("Dryer") > -1) | |
{ | |
Serial.println("d"); | |
temp[dryer] = str_temp; | |
show_laundry = true; | |
} | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
Serial.print(str_temp); | |
Serial.println(); | |
lcd.setCursor(19, 0); | |
lcd.print(" "); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment