/********* Rui Santos Complete project details at https://randomnerdtutorials.com *********/ #include #include #include #include #include #include "Adafruit_LC709203F.h" Adafruit_LC709203F lc; #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ #define TIME_TO_SLEEP 60 /* Time ESP32 will go to sleep (in seconds) */ // Replace the next variables with your SSID/Password combination const char* ssid = "NGJFA"; const char* password = "9FD4D3&@xH"; // Add your MQTT Broker IP address, example: const char* mqtt_server = "192.168.0.25"; int status = WL_CONNECTED; WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; int value = 0; Adafruit_AHTX0 aht; // I2C float temperature = 0; float humid = 0; void setup_wifi() { // We start by connecting to a WiFi network Serial1.println(); Serial1.print("Connecting to "); Serial1.println(ssid); delay(100); status = WiFi.begin(ssid, password); while (status != WL_CONNECTED) { delay(1000); Serial1.print("."); status = WiFi.begin(ssid, password); } Serial1.println(""); Serial1.println("WiFi connected"); Serial1.println("IP address: "); Serial1.println(WiFi.localIP()); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial1.print("Attempting MQTT connection..."); if (client.connect("ESP32Client1")) { Serial1.println("connected"); } else { Serial1.print("failed, rc="); Serial1.print(client.state()); Serial1.println(" try again in 2 seconds"); // Wait 2 seconds before retrying delay(2000); } } } void setup() { Serial1.begin(115200); delay(1000); //Take some time to open up the Serial Monitor //status = aht.begin(); if (!aht.begin()) { Serial1.println("Could not find a valid AHT20 sensor, check wiring!"); while (1); } if (!lc.begin()) { Serial1.println(F("Couldnt find Adafruit LC709203F?\nMake sure a battery is plugged in!")); while (1) delay(10); } //Serial1.println(F("Found LC709203F")); //Serial1.print("Version: 0x"); Serial.println(lc.getICversion(), HEX); lc.setPackSize(LC709203F_APA_500MAH); setup_wifi(); client.setServer(mqtt_server, 1883); if (!client.connected()) { reconnect(); } sensors_event_t humidity, temp; aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data temperature = 1.8 * temp.temperature + 32.0; // Temperature in Fahrenheit humid = humidity.relative_humidity; // Convert the value to a char array char tempString[8]; dtostrf(temperature, 1, 1, tempString); // Convert the value to a char array char humString[8]; dtostrf(humid, 1, 1, humString); const char datastr1[20] = "[{\"Temperature\": "; char buf[100] = ""; strcat(buf, datastr1); strcat(buf, tempString); const char datastr2[20] = " , \"Humidity\": "; strcat(buf, datastr2); strcat(buf, humString); const char locationString[32] = "}, {\"Location\": \"5th_wheel\"}]"; strcat(buf, locationString); Serial1.println(buf); client.publish("environmentTopic", buf); strcpy(buf,""); char battvString[8]; dtostrf(lc.cellVoltage(), 1, 1, battvString); char battpercentString[8]; dtostrf(lc.cellPercent(), 1, 1, battpercentString); const char datastr4[21] = "[{\"Battery Volts\": "; strcat(buf, datastr4); strcat(buf, battvString); const char datastr5[25] = " , \"Battery Percent\": "; strcat(buf, datastr5); strcat(buf, battpercentString); strcat(buf, locationString); Serial1.println(buf); client.publish("environmentTopic", buf); esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); Serial1.println("ESP32 going to sleep for " + String(TIME_TO_SLEEP) + " Seconds"); delay(1000); Serial.flush(); esp_deep_sleep_start(); } void loop() { //This is not going to be called }