Skip to content

Instantly share code, notes, and snippets.

@dsantiago
Last active Jun 29, 2021
Embed
What would you like to do?
Correcting LCD malfunction
#include <WiFi.h>
#include <Wire.h>
extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#include <AsyncMqttClient.h>
#include <LiquidCrystal_I2C.h>
// Change the credentials below, so your ESP32 connects to your router
#define WIFI_SSID "wifi_name"
#define WIFI_PASSWORD "wifi_pass"
// 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, XXX)
#define MQTT_PORT 1883
// Create objects to handle MQTT client
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
// Set the LCD number of columns and rows
const int lcdColumns = 20;
const int lcdRows = 4;
// 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 = 32;
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 connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
// Add more topics that want your ESP32 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("esp32/temperature", 0);
Serial.print("Subscribing at QoS 0, packetId: ");
Serial.println(packetIdSub);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}
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, "esp32/temperature") == 0) {
Serial.println("=== Updating LCD... ===");
Wire.setClock(10000);
lcd.setCursor(0, 2);
lcd.print(std::string(" ", lcdColumns).c_str()); // Clears row, better than the slow lcd.clear()
lcd.setCursor(0, 2);
lcd.print(messageTemp);
}
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() {
// Initialize LCD
lcd.init();
// Turn on LCD backlight
lcd.backlight();
// Create thermometer icon
lcd.createChar(0, thermometerIcon);
lcd.setCursor(2, 0);
lcd.write(0);
// Print header
lcd.print(" Temperature");
// Define buttonPin as an INPUT
pinMode(buttonPin, INPUT);
Serial.begin(115200);
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
WiFi.onEvent(WiFiEvent);
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() {
// 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 esp32/led to toggle the LED (turn the LED on or off)P
if (buttonState == HIGH) {
mqttClient.publish("esp32/led", 0, true, "toggle");
Serial.println("Publishing on topic esp32/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