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 <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
- #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- // Change the credentials below, so your ESP32 connects to your router
- #define WIFI_SSID "MYSSID"
- #define WIFI_PASSWORD "MYPWD"
- // 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, NNN)
- #define MQTT_PORT 1883
- // Create objects to handle MQTT client
- AsyncMqttClient mqttClient;
- TimerHandle_t mqttReconnectTimer;
- TimerHandle_t wifiReconnectTimer;
- // 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) {
- // display.clearDisplay();
- // display.setTextSize(1); // Normal 1:1 pixel scale
- // display.setTextColor(SSD1306_WHITE); // Draw white text
- // display.setCursor(0,0); // Start at top-left corner
- // display.println(F("Temperature:"));
- // display.setTextColor(SSD1306_WHITE); // Draw white text
- // display.print(messageTemp);
- // display.display();
- testdrawstyles();
- }
- 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);
- // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
- if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
- Serial.println(F("SSD1306 allocation failed"));
- for(;;); // Don't proceed, loop forever
- }
- // Define buttonPin as an INPUT
- pinMode(buttonPin, INPUT);
- 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;
- }
- void testdrawstyles(void) {
- display.clearDisplay();
- display.setTextSize(1); // Normal 1:1 pixel scale
- display.setTextColor(SSD1306_WHITE); // Draw white text
- display.setCursor(0,0); // Start at top-left corner
- display.println(F("Hello, world!"));
- display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
- display.println(3.141592);
- display.setTextSize(2); // Draw 2X-scale text
- display.setTextColor(SSD1306_WHITE);
- display.print("0x"); display.println(0xDEADBEEF, HEX);
- display.display();
- delay(2000);
- }
Add Comment
Please, Sign In to add comment