From d02532787d310006f2a8327bc3435712fa7d631f Mon Sep 17 00:00:00 2001 From: Alexandre B Date: Sun, 17 Dec 2023 12:24:36 -0500 Subject: [PATCH] . Overall configuration --- ESP32_Temperture-Node/platformio.ini | 6 ++ ESP32_Temperture-Node/src/config.h | 2 +- ESP32_Temperture-Node/src/main.cpp | 138 ++++++++++++++++++++++++++- 3 files changed, 144 insertions(+), 2 deletions(-) diff --git a/ESP32_Temperture-Node/platformio.ini b/ESP32_Temperture-Node/platformio.ini index c4b493e61..7543edcfd 100644 --- a/ESP32_Temperture-Node/platformio.ini +++ b/ESP32_Temperture-Node/platformio.ini @@ -12,3 +12,9 @@ platform = espressif32 board = esp32doit-devkit-v1 framework = arduino +monitor_speed = 115200 +lib_deps = + knolleary/PubSubClient@^2.8 + +[platformio] +description = ESP32 Sensors node diff --git a/ESP32_Temperture-Node/src/config.h b/ESP32_Temperture-Node/src/config.h index a195a9f69..cf74dfb02 100644 --- a/ESP32_Temperture-Node/src/config.h +++ b/ESP32_Temperture-Node/src/config.h @@ -1,7 +1,7 @@ // Uncomment modules as required //#define RTC //#define MICRO_SD -#define BMP280 // Adafruit BMP280; temp & pressure +//#define BMP280 // Adafruit BMP280; temp & pressure //#define BME280 // Generic BME280; temp, pressure & humidity //#define AWSIoT diff --git a/ESP32_Temperture-Node/src/main.cpp b/ESP32_Temperture-Node/src/main.cpp index 01a46d66b..aabbc44be 100644 --- a/ESP32_Temperture-Node/src/main.cpp +++ b/ESP32_Temperture-Node/src/main.cpp @@ -23,6 +23,7 @@ struct { float temperature = 0.0; } sensors_values; +// Sensors modules specified in config.h file. // BME280 #ifdef BME280 // WaveShare BME280 @@ -35,7 +36,7 @@ Adafruit_BME280 bme; #define BMP_MISO (19) #define BMP_MOSI (23) #define BMP_CS (5) -Adafruit_BMP280 bmp(BMP_CS); +//Adafruit_BMP280 bmp(BMP_CS); #endif // Mosquitto @@ -192,7 +193,142 @@ void mosquitto_connect () } void setup() { + Serial.begin(115200); + Serial.println(); + sensors_values.humidity = 0.0; + sensors_values.pressure = 0.0; + sensors_values.temperature = 0.0; + + // Initialize GPIO + #ifdef devkit_36pin_001 + #endif + #ifdef devkit_30pin_001 + pinMode(LED_PIN, OUTPUT); + pinMode(PING_PIN, OUTPUT); + pinMode(SWITCH_1, OUTPUT); + pinMode(SWITCH_2, OUTPUT); + + pinMode(FAN_RPM, INPUT); + digitalWrite(FAN_RPM, HIGH); + //attachInterrupt(digitalPinToInterrupt(FAN_RPM), rpm_fan, FALLING); + //pinMode(DAC_CH1, OUTPUT); + // Active level is LOW + digitalWrite(SWITCH_1, LOW); + digitalWrite(SWITCH_2, LOW); + //digitalWrite(DAC_CH1, LOW); + dacWrite(DAC1, 0); + #endif + + Serial.println("setup"); + Serial.println("setup done"); + + // WaveShare BME280 + #ifdef BME280 + unsigned status = bme.begin(); + if (!status) { + Serial.println("Could not find a valid BME/BMP280 sensor, check wiring!"); + Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID());//,16); + Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n"); + Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n"); + Serial.print(" ID of 0x60 represents a BME 280.\n"); + Serial.print(" ID of 0x61 represents a BME 680.\n"); + while (1); + } + else { + humidity = bme.readHumidity(); + pressure = bme.readPressure() / 100.0F; + } + #endif + + // BMP280 + #ifdef BMP280 + unsigned status_bmp280; + status_bmp280 = bmp.begin(); + if (!status_bmp280) { + Serial.println("Could not find BMP280"); + Serial.println(bmp.sensorID(),16); + while (1); + } + else { + Serial.println(bmp.sensorID(),16); + } + #endif + + // Initialize RTC module, if defined + #ifdef RTC + rtc.begin(); + if (! rtc.begin()) + { + Serial.println("Couldn't find RTC"); + while (1); + } + temp = rtc.getTemperature(); + #endif + // Uncomment when compiling for the first time + //rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); + /*if (rtc.lostPower()) { + Serial.println("RTC lost power, lets set the time!"); + // following line sets the RTC to the date & time this sketch was compiled + rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); + // This line sets the RTC with an explicit date & time, for example to set + // January 21, 2014 at 3am you would call: + // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); + }*/ + + String hostname = "ESP32LF"; + WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE); + WiFi.setHostname(hostname.c_str()); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); + WiFi.mode(WIFI_STA); + Serial.println("Connecting to Wi-Fi"); + + // Connect to wifi. + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("#"); + } + Serial.print("\nCONNECTED\nIP: "); + Serial.println(WiFi.localIP()); + + Serial.print("Connecting to Mosquitto at IP: "); + Serial.print(mqtt_server); + #ifdef MQTT // MOSQUITTO MQTT port 1883 + Serial.println(":1883"); + connection.setServer(mqtt_server, 1883); + if(connection.connect("ESP32")) { + Serial.println("Mosquitto Connected!"); + connection.setCallback(mosquito_callback); + digitalWrite(LED_PIN, HIGH); + } + else { + Serial.print("Mosquitto state: "); + digitalWrite(LED_PIN, LOW); + } + Serial.println(connection.state()); + #endif + #ifdef MQTT_SSL // MOSQUITTO MQTT port 8883 + Serial.println(":8883"); + connection.setServer(mqtt_server, 8883); + espClientSSL.setCACert(NODE_CERT_CA); + espClientSSL.setCertificate(NODE_CERT_CRT); + espClientSSL.setPrivateKey(NODE_CERT_PRIVATE); + //connection.setCallback(mosquito_callback); + if(connection.connect("esp32")) { + Serial.println("Mosquitto Connected!"); + connection.subscribe("esp32/sw1"); + connection.subscribe("esp32/sw2"); + connection.subscribe(MQTT_IOT_CHANNEL_OUTPUT_SWITCH_2); + connection.subscribe(MQTT_IOT_CHANNEL_OUTPUT_PWM_1); + connection.setCallback(mosquito_callback); + digitalWrite(LED_PIN, HIGH); + } + else { + Serial.print("Mosquitto state: "); + digitalWrite(LED_PIN, LOW); + } + Serial.println(connection.state()); + #endif } void loop() {