Arduino
Μυρίσκος Ιωάννης
ESP32 MQTT BME280 Sensor Readings
ESP32 MQTT BME280 Sensor Readings
/* Rui Santos Complete project details at https://RandomNerdTutorials.com/esp32-mqtt-publish-bme280-arduino/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.*/
#include <Wire.h>#include <Adafruit_Sensor.h>#include <Adafruit_BME280.h>#include <WiFi.h>extern "C" { #include "freertos/FreeRTOS.h" #include "freertos/timers.h"}#include <AsyncMqttClient.h>
#define WIFI_SSID "REPLACE_WITH_YOUR_SSID"#define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD"
// Raspberry Pi Mosquitto MQTT Broker#define MQTT_HOST IPAddress(192, 168, 1, XXX)// For a cloud MQTT broker, type the domain name//#define MQTT_HOST "example.com"#define MQTT_PORT 1883
// Temperature MQTT Topics#define MQTT_PUB_TEMP "esp32/bme280/temperature"#define MQTT_PUB_HUM "esp32/bme280/humidity"#define MQTT_PUB_PRES "esp32/bme280/pressure"
// BME280 I2CAdafruit_BME280 bme;// Variables to hold sensor readingsfloat temp;float hum;float pres;
AsyncMqttClient mqttClient;TimerHandle_t mqttReconnectTimer;TimerHandle_t wifiReconnectTimer;
unsigned long previousMillis = 0; // Stores last time temperature was publishedconst long interval = 10000; // Interval at which to publish sensor readings
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; }}
void onMqttConnect(bool sessionPresent) { Serial.println("Connected to MQTT."); Serial.print("Session present: "); Serial.println(sessionPresent);}
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.print("Publish acknowledged."); Serial.print(" packetId: "); Serial.println(packetId);}
void setup() { Serial.begin(115200); Serial.println(); // Initialize BME280 sensor if (!bme.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); while (1); } 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.onPublish(onMqttPublish); mqttClient.setServer(MQTT_HOST, MQTT_PORT); // If your broker requires authentication (username and password), set them below //mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD"); connectToWifi();}
void loop() { unsigned long currentMillis = millis(); // Every X number of seconds (interval = 10 seconds) // it publishes a new MQTT message if (currentMillis - previousMillis >= interval) { // Save the last time a new reading was published previousMillis = currentMillis; // New BME280 sensor readings temp = bme.readTemperature(); //temp = 1.8*bme.readTemperature() + 32; hum = bme.readHumidity(); pres = bme.readPressure()/100.0F; // Publish an MQTT message on topic esp32/BME2800/temperature uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str()); Serial.printf("Publishing on topic %s at QoS 1, packetId: %i", MQTT_PUB_TEMP, packetIdPub1); Serial.printf("Message: %.2f \n", temp);
// Publish an MQTT message on topic esp32/BME2800/humidity uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_HUM, 1, true, String(hum).c_str()); Serial.printf("Publishing on topic %s at QoS 1, packetId %i: ", MQTT_PUB_HUM, packetIdPub2); Serial.printf("Message: %.2f \n", hum);
// Publish an MQTT message on topic esp32/BME2800/pressure uint16_t packetIdPub3 = mqttClient.publish(MQTT_PUB_PRES, 1, true, String(pres).c_str()); Serial.printf("Publishing on topic %s at QoS 1, packetId: %i", MQTT_PUB_PRES, packetIdPub3); Serial.printf("Message: %.3f \n", pres); }}
#include <Wire.h>#include <Adafruit_Sensor.h>#include <Adafruit_BME280.h>#include <WiFi.h>extern "C" { #include "freertos/FreeRTOS.h" #include "freertos/timers.h"}#include <AsyncMqttClient.h>
#define WIFI_SSID "REPLACE_WITH_YOUR_SSID"#define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD"
// Raspberry Pi Mosquitto MQTT Broker#define MQTT_HOST IPAddress(192, 168, 1, XXX)// For a cloud MQTT broker, type the domain name//#define MQTT_HOST "example.com"#define MQTT_PORT 1883
// Temperature MQTT Topics#define MQTT_PUB_TEMP "esp32/bme280/temperature"#define MQTT_PUB_HUM "esp32/bme280/humidity"#define MQTT_PUB_PRES "esp32/bme280/pressure"
// BME280 I2CAdafruit_BME280 bme;// Variables to hold sensor readingsfloat temp;float hum;float pres;
AsyncMqttClient mqttClient;TimerHandle_t mqttReconnectTimer;TimerHandle_t wifiReconnectTimer;
unsigned long previousMillis = 0; // Stores last time temperature was publishedconst long interval = 10000; // Interval at which to publish sensor readings
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; }}
void onMqttConnect(bool sessionPresent) { Serial.println("Connected to MQTT."); Serial.print("Session present: "); Serial.println(sessionPresent);}
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.print("Publish acknowledged."); Serial.print(" packetId: "); Serial.println(packetId);}
void setup() { Serial.begin(115200); Serial.println(); // Initialize BME280 sensor if (!bme.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); while (1); } 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.onPublish(onMqttPublish); mqttClient.setServer(MQTT_HOST, MQTT_PORT); // If your broker requires authentication (username and password), set them below //mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD"); connectToWifi();}
void loop() { unsigned long currentMillis = millis(); // Every X number of seconds (interval = 10 seconds) // it publishes a new MQTT message if (currentMillis - previousMillis >= interval) { // Save the last time a new reading was published previousMillis = currentMillis; // New BME280 sensor readings temp = bme.readTemperature(); //temp = 1.8*bme.readTemperature() + 32; hum = bme.readHumidity(); pres = bme.readPressure()/100.0F; // Publish an MQTT message on topic esp32/BME2800/temperature uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str()); Serial.printf("Publishing on topic %s at QoS 1, packetId: %i", MQTT_PUB_TEMP, packetIdPub1); Serial.printf("Message: %.2f \n", temp);
// Publish an MQTT message on topic esp32/BME2800/humidity uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_HUM, 1, true, String(hum).c_str()); Serial.printf("Publishing on topic %s at QoS 1, packetId %i: ", MQTT_PUB_HUM, packetIdPub2); Serial.printf("Message: %.2f \n", hum);
// Publish an MQTT message on topic esp32/BME2800/pressure uint16_t packetIdPub3 = mqttClient.publish(MQTT_PUB_PRES, 1, true, String(pres).c_str()); Serial.printf("Publishing on topic %s at QoS 1, packetId: %i", MQTT_PUB_PRES, packetIdPub3); Serial.printf("Message: %.3f \n", pres); }}
EEPROM in ESP8266NodeMCU
EEPROM in ESP8266NodeMCU
#include <EEPROM.h>
if (RemoteXY.button_1!=0){ EEPROM.begin(512); EEPROM.put( 11,RemoteXY.edit_1); EEPROM.put( 21,RemoteXY.edit_2); EEPROM.commit(); delay(500); RemoteXY.led_1_r = 0 ; RemoteXY.led_1_g = 200 ; EEPROM.begin(512); EEPROM.get( 11,RemoteXY.edit_1); EEPROM.get( 21,RemoteXY.edit_2); }
Serial Communication between NodeMCU and Arduino
Serial Communication between NodeMCU and Arduino
https://engineeringprojectshub.com/serial-communication-between-nodemcu-and-arduino/?fbclid=IwAR2b-v5HaelrDtcUVT3_zFM5thHU0lu10Oho3snyVCCKWlwN-_e2vYcEc80
https://www.youtube.com/watch?v=ArzJIAbwiDw&feature=emb_logo
https://www.youtube.com/watch?v=ArzJIAbwiDw&feature=emb_logo
ARDUINO FULL CODE
//Arduino code#include <SoftwareSerial.h>SoftwareSerial s(5,6); void setup() {s.begin(9600);} void loop() {int data=50;if(s.available()>0){ s.write(data);}}NODEMCU FULL CODE
#include <SoftwareSerial.h>SoftwareSerial s(D6,D5);int data;void setup() {s.begin(9600);Serial.begin(9600);} void loop() { s.write("s"); if (s.available()>0) { data=s.read(); Serial.println(data); } }