Hi All
I had this code working perfectly yesterday on a ESP32 dev board and battery out in the yard. For what ever reason it disconnected, possibly a flat battery. Now it can get the unit into deep sleep mode.
I haven’t changed anything hardware or software wise.
I have charged the battery and the readings are there but it won’t sleep now. I have reloaded the code and tried the second code which puts it into deep sleep.
Any ideas?
Cheers
[code]
#include <HX711.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>
#define calibration_factor -21500.0
#define zero_factor 8364129
#define LOADCELL_DOUT_PIN 22
#define LOADCELL_SCK_PIN 23
//#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
//#else
// #include <ESP8266WiFi.h>
// #include <ESP8266HTTPClient.h>
// #include <WiFiClient.h>
//#endif
// Replace with your network credentials
const char* ssid = “….”;
const char* password = “…..”;
// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = “……”;
// Keep this API Key value to be compatible with the PHP code provided in the project page.
// If you change the apiKeyValue value, the PHP file /post-data.php also needs to have the same key
String apiKeyValue = “……”;
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define ONE_WIRE_BUS 15 // Data wire is connected to GPIO15
HX711 scale;
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1 = { 0x28, 0x6A, 0x48, 0x7C, 0x0C, 0x00, 0x00, 0xC4 }; // long
DeviceAddress sensor2 = { 0x28, 0xFA, 0x14, 0x7B, 0x0C, 0x00, 0x00, 0x47 }; //short
DHT dht(DHTPIN, DHTTYPE);
// Time to sleep
uint64_t uS_TO_S_FACTOR = 1000000; // Conversion factor for micro seconds to seconds
// sleep for 30 minutes = 1800 seconds
uint64_t TIME_TO_SLEEP = 1800;
// Get Sensor Readings
String getSensorReadings(){
sensors.requestTemperatures();
}
void setup() {
Serial.begin(9600);
sensors.begin();
dht.begin();
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale.set_offset(zero_factor); //Zero out the scale using a previously known zero_factor
WiFi.begin(ssid, password);
Serial.println(“Connecting”);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.print(“Connected to WiFi network with IP Address: “);
Serial.println(WiFi.localIP());
// enable timer deep sleep
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
}
void loop(){
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
http.addHeader(“Content-Type”, “application/x-www-form-urlencoded”);
// Prepare your HTTP POST request data
String httpRequestData = “api_key=” + apiKeyValue + “&value1=” + String(dht.readTemperature())
+ “&value2=” + String(dht.readHumidity()) + “&value3=” + String(sensors.getTempC(sensor1)) + “&value4=” + String(sensors.getTempC(sensor2))+ “&value5=” + String(scale.get_units(), 1) +””;
Serial.print(“httpRequestData: “);
Serial.println(httpRequestData);
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
// If you need an HTTP request with a content type: text/plain
//http.addHeader(“Content-Type”, “text/plain”);
//int httpResponseCode = http.POST(“Hello, World!”);
// If you need an HTTP request with a content type: application/json, use the following:
//http.addHeader(“Content-Type”, “application/json”);
//int httpResponseCode = http.POST(“{\”value1\”:\”19\”,\”value2\”:\”67\”,\”value3\”:\”78\”}”);
if (httpResponseCode>0) {
Serial.print(“HTTP Response code: “);
Serial.println(httpResponseCode);
}
else {
Serial.print(“Error code: “);
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println(“WiFi Disconnected”);
}
// //Send an HTTP POST request every 30 seconds
// delay(30000);
// // Wait a few seconds between measurements.
// delay(20000);
//Serial.print(“Requesting temperatures…”);
sensors.requestTemperatures(); // Send the command to get temperatures
// Serial.println(“DONE”);
Serial.print(“8 Frame Hive: “);
Serial.print(sensors.getTempC(sensor1));
Serial.print(“°C: “);
Serial.print(“10 Frame Hive: “);
Serial.print(sensors.getTempC(sensor2));
Serial.print(“°C: “);
Serial.print(“Weight “);
Serial.print(scale.get_units(), 1);
Serial.print(” Kgs “);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F(“Failed to read from DHT sensor!”));
return;
}
Serial.print(F(“8 Frame Roof Temp: “));
Serial.print(t);
Serial.print(F(“°C “));
Serial.print(F(“8 Frame Roof Humidity: “));
Serial.print(h);
Serial.print(F(“% “));
Serial. print(‘\n’);
delay(5000);
Serial.println(“Going to sleep now”);
// start deep sleep for 1800 seconds (30 minutes)
esp_deep_sleep_start();
}
[/code]
Hi.
Was your first code working before?
Didn’t you change anything else?
Do you have another board to experiment with?
Otherwise, you can try to upload a blank sketch and then, your original sketch again.
Regards,
Sara
Hi Sara
Yes the code was working perfectly. I changed absolutely nothing except but put the unit out in the field to test. Worked perfectly giving me all the reading for around 12 hrs. The unit stopped sending data so I took it inside and re loaded the code with no sensors and it just sent data every 5 seconds.
I just decided to hook the sensors up right now and its working again…..
Ok So weird
Its stopped again and sending data every second…
the code was working perfectly for around 12hrs sending data every 30mins then changed to send every second…
Again I did nothing as I was asleep.
So its doing it again.
1 reading every 2 seconds or so.
Really frustrating me now.
11.30 at night while asleep.
just tried several boards and its doing on the test bench to now.
Help please
ESP32 devkit board.
Hi.
I’m not sure why that issue happens.
Do you know if the ESP32 prints something on the debugging window when that happens?
Regards,
Sara