Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********
- Rui Santos
- Complete project details at https://randomnerdtutorials.com
- *********/
- #include <WiFi.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 "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, 4)
- #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 = 16;
- const int lcdRows = 2;
- // 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;
- //DAK added events below to satisfy compiler for enumerated event handler in 'switch' function.
- case SYSTEM_EVENT_WIFI_READY:
- Serial.println("WIFI Ready!");
- break;
- case SYSTEM_EVENT_SCAN_DONE:
- break;
- case SYSTEM_EVENT_STA_START:
- break;
- case SYSTEM_EVENT_STA_STOP:
- break;
- case SYSTEM_EVENT_STA_CONNECTED:
- break;
- case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
- Serial.println("Authmode has CHANGED!");
- break;
- case SYSTEM_EVENT_STA_LOST_IP:
- Serial.println("Lost the IP");
- break;
- case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
- break;
- case SYSTEM_EVENT_STA_WPS_ER_FAILED:
- break;
- case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
- break;
- case SYSTEM_EVENT_STA_WPS_ER_PIN:
- break;
- case SYSTEM_EVENT_AP_START:
- break;
- case SYSTEM_EVENT_AP_STOP:
- break;
- case SYSTEM_EVENT_AP_STAIPASSIGNED:
- break;
- case SYSTEM_EVENT_AP_STACONNECTED:
- break;
- case SYSTEM_EVENT_AP_STADISCONNECTED:
- break;
- case SYSTEM_EVENT_AP_PROBEREQRECVED:
- break;
- case SYSTEM_EVENT_GOT_IP6:
- break;
- case SYSTEM_EVENT_ETH_START:
- break;
- case SYSTEM_EVENT_ETH_STOP:
- break;
- case SYSTEM_EVENT_ETH_CONNECTED:
- break;
- case SYSTEM_EVENT_ETH_DISCONNECTED:
- break;
- case SYSTEM_EVENT_ETH_GOT_IP:
- break;
- case SYSTEM_EVENT_MAX:
- 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) {
- lcd.clear();
- lcd.setCursor(1, 0);
- lcd.write(0);
- lcd.print(" Temperature");
- lcd.setCursor(0, 1);
- 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);
- // 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement