Hi All
When I run this code it doesn’t go past the connection and not reading the sensors.
It used to work but I cant seem to get it running now
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#endif
#include <HX711.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>
// 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 = “http://buzzyhunny.com/post-data.php”;
// 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 calibration_factor -23700.0
#define zero_factor -121400
#define LOADCELL_DOUT_PIN 22
#define LOADCELL_SCK_PIN 23
#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(115200);
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());
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
#ifdef ESP32
// enable timer deep sleep
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
// start deep sleep for 1800 seconds (30 minutes)
#else
// Deep sleep mode for 3600 seconds (60 minutes)
Serial.println(“Going to sleep now”);
ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR);
#endif
}
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’);
Serial.println(“Going to sleep now”);
esp_deep_sleep_start();
}
Hi.
Can you provide more details?
What are exactly the errors that you’re getting?
Regards,
Sara
These no error as such.
it prints the lines below and then does nothing after
void setup() {
Serial.begin(115200);
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());
in other words it doesn’t go to the loop and print the sensor readings
Hi.
That means that the board is not able to connect to your wi-fi network.
Double-check your network credentials and make sure that your board is relatively close to the router to be able to catch the wi-fi signal.
Regards,
Sara
So the board is connecting as it gives me an IP address but then not showing sensor readings.Its as if it connected and then doesn’t go to the next instruction.