-
-
Save sarasantos/230e702a6058c3b06a1c738e97d69f16 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
/********* | |
Rui Santos | |
Complete project details at https://RandomNerdTutorials.com/home-automation-using-esp8266/ | |
*********/ | |
#include <ESP8266WiFi.h> | |
#include <Ticker.h> | |
#include <AsyncMqttClient.h> | |
#include <LiquidCrystal_I2C.h> | |
// Change the credentials below, so your ESP8266 connects to your router | |
#define WIFI_SSID "REPLACE_WITH_YOUR_SSID" | |
#define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD" | |
// Change the MQTT_HOST variable to your Raspberry Pi IP address, | |
// so it connects to your Mosquitto MQTT broker | |
#define MQTT_HOST IPAddress(192, 168, 1, 74) | |
#define MQTT_PORT 1883 | |
// Create objects to handle MQTT client | |
AsyncMqttClient mqttClient; | |
Ticker mqttReconnectTimer; | |
Ticker wifiReconnectTimer; | |
WiFiEventHandler wifiConnectHandler; | |
WiFiEventHandler wifiDisconnectHandler; | |
// Set the LCD number of columns and rows | |
const int lcdColumns = 16; | |
const int lcdRows = 2; | |
bool updateDisplay = false; | |
String messageReceived; | |
// Set LCD address, number of columns and rows | |
// if you don't know your display address, run an I2C scanner sketch | |
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); | |
// Thermometer icon | |
byte thermometerIcon[8] = { | |
B00100, | |
B01010, | |
B01010, | |
B01010, | |
B01010, | |
B10001, | |
B11111, | |
B01110 | |
}; | |
// Define GPIO where the pushbutton is connected to | |
const int buttonPin = 14; | |
int buttonState; // current reading from the input pin (pushbutton) | |
int lastButtonState = LOW; // previous reading from the input pin (pushbutton) | |
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled | |
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers | |
void connectToWifi() { | |
Serial.println("Connecting to Wi-Fi..."); | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
} | |
void onWifiConnect(const WiFiEventStationModeGotIP& event) { | |
Serial.println("Connected to Wi-Fi."); | |
connectToMqtt(); | |
} | |
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { | |
Serial.println("Disconnected from Wi-Fi."); | |
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi | |
wifiReconnectTimer.once(2, connectToWifi); | |
} | |
void connectToMqtt() { | |
Serial.println("Connecting to MQTT..."); | |
mqttClient.connect(); | |
} | |
// Add more topics that want your ESP8266 to be subscribed to | |
void onMqttConnect(bool sessionPresent) { | |
Serial.println("Connected to MQTT."); | |
Serial.print("Session present: "); | |
Serial.println(sessionPresent); | |
uint16_t packetIdSub = mqttClient.subscribe("esp8266/temperature", 0); | |
Serial.print("Subscribing at QoS 0, packetId: "); | |
Serial.println(packetIdSub); | |
} | |
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { | |
Serial.println("Disconnected from MQTT."); | |
if (WiFi.isConnected()) { | |
mqttReconnectTimer.once(2, connectToMqtt); | |
} | |
} | |
void onMqttSubscribe(uint16_t packetId, uint8_t qos) { | |
Serial.println("Subscribe acknowledged."); | |
Serial.print(" packetId: "); | |
Serial.println(packetId); | |
Serial.print(" qos: "); | |
Serial.println(qos); | |
} | |
void onMqttUnsubscribe(uint16_t packetId) { | |
Serial.println("Unsubscribe acknowledged."); | |
Serial.print(" packetId: "); | |
Serial.println(packetId); | |
} | |
void onMqttPublish(uint16_t packetId) { | |
Serial.println("Publish acknowledged."); | |
Serial.print(" packetId: "); | |
Serial.println(packetId); | |
} | |
// You can modify this function to handle what happens when you receive a certain message in a specific topic | |
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { | |
String messageTemp; | |
for (int i = 0; i < len; i++) { | |
//Serial.print((char)payload[i]); | |
messageTemp += (char)payload[i]; | |
} | |
if (strcmp(topic, "esp8266/temperature") == 0) { | |
messageReceived = messageTemp; | |
updateDisplay = true; | |
} | |
Serial.println("Publish received."); | |
Serial.print(" message: "); | |
Serial.println(messageTemp); | |
Serial.print(" topic: "); | |
Serial.println(topic); | |
Serial.print(" qos: "); | |
Serial.println(properties.qos); | |
Serial.print(" dup: "); | |
Serial.println(properties.dup); | |
Serial.print(" retain: "); | |
Serial.println(properties.retain); | |
Serial.print(" len: "); | |
Serial.println(len); | |
Serial.print(" index: "); | |
Serial.println(index); | |
Serial.print(" total: "); | |
Serial.println(total); | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.print(" 1"); | |
// Initialize LCD | |
lcd.init(); | |
// Turn on LCD backlight | |
lcd.backlight(); | |
// Create thermometer icon | |
lcd.createChar(0, thermometerIcon); | |
// Define buttonPin as an INPUT | |
pinMode(buttonPin, INPUT); | |
Serial.print("2"); | |
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect); | |
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect); | |
mqttClient.onConnect(onMqttConnect); | |
mqttClient.onDisconnect(onMqttDisconnect); | |
mqttClient.onSubscribe(onMqttSubscribe); | |
mqttClient.onUnsubscribe(onMqttUnsubscribe); | |
mqttClient.onMessage(onMqttMessage); | |
mqttClient.onPublish(onMqttPublish); | |
mqttClient.setServer(MQTT_HOST, MQTT_PORT); | |
connectToWifi(); | |
} | |
void loop() { | |
if (updateDisplay){ | |
lcd.clear(); | |
lcd.setCursor(1, 0); | |
lcd.write(0); | |
lcd.print(" Temperature"); | |
lcd.setCursor(0, 1); | |
lcd.print(messageReceived); | |
updateDisplay = false; | |
} | |
// Read the state of the pushbutton and save it in a local variable | |
int reading = digitalRead(buttonPin); | |
// If the pushbutton state changed (due to noise or pressing it), reset the timer | |
if (reading != lastButtonState) { | |
// Reset the debouncing timer | |
lastDebounceTime = millis(); | |
} | |
// If the button state has changed, after the debounce time | |
if ((millis() - lastDebounceTime) > debounceDelay) { | |
// And if the current reading is different than the current buttonState | |
if (reading != buttonState) { | |
buttonState = reading; | |
// Publish an MQTT message on topic esp8266/led to toggle the LED (turn the LED on or off)P | |
if (buttonState == HIGH) { | |
mqttClient.publish("esp8266/led", 0, true, "toggle"); | |
Serial.println("Publishing on topic esp8266/led topic at QoS 0"); | |
} | |
} | |
} | |
// Save the reading. Next time through the loop, it'll be the lastButtonState | |
lastButtonState = reading; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment