Hi everyone ![]()
I am new here and I am starting this topic because I am being stuck with my (seemingly) straight-forward project: I want to track ambient data from two Ruuvi tags in my greenhouse.
Therefore I am trying to program an ESP32-WROOM-32U to receive the Ruuvi HEX string and forward it via my home-WiFi to a google sheet.
In the Google Apps script I have the logic to decode the Ruuvi hex strings and add them as a new row.
Everything works in principal and I do receive data in the google sheet, BUT:
- after the ESP is running successfully a while, I get the http error code -1 , which then repeats for minutes or even hours. Then it works couple of minutes, then stops working again for good.
- after restarting the ESP32, it again works for some time, but then stops as described above
- I am logging WiFi strength and it seems reasonable between -50 and -65, so not too bad.
- When it is working, I keep getting the http error code 302, not sure if that has to say anything...
So, does anyone have an idea what is causing the connection to "break" after some time of working normally?
I am totally new to programming, but this is my code so far (with help of google and microsoft copilot) :
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLEAddress.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <esp_task_wdt.h>
#include <time.h>
#define WDT_TIMEOUT 3600000 // = 1 hour - Timeout in milliseconds
const char* MAC_inside_sensor = "xx:B9:9A:E5:54:xx";
const char* MAC_outside_sensor = "xx:6A:CC:E0:7E:xx";
HTTPClient http; // Declare HTTPClient object globally
const char* ssid = "FRITZxxxxxxx2";
const char* password = "451xxxxxxxxxx710";
unsigned long previousMillis = 0;
const long interval = 30000; // 30 seconds in milliseconds
esp_task_wdt_config_t wdt_config = {
.timeout_ms = WDT_TIMEOUT,
.idle_core_mask = (1 << 2) - 1,
.trigger_panic = true,
};
int scanTime = 25; // BLE scan time in seconds
BLEScan* pBLEScan;
String googleURL = "https://script.google.com/macros/s/AKfycbxyxxxxxxxxxxxxxxiMlTAh1oXXEdZ--WXXa24PnMa/exec?";
String HEX_inside_sensor = "";
String HEX_outside_sensor = "";
bool insideSensorFound = false;
bool outsideSensorFound = false;
void connectToWiFi() {
WiFi.disconnect();
delay(1000);
WiFi.begin(ssid, password);
unsigned long startAttemptTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 10000) {
delay(1000);
Serial.println("Connecting to WiFi...");
logWiFiStrength(); // Log the WiFi signal strength during connection attempts
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to WiFi");
logWiFiStrength(); // Log the WiFi signal strength after connection
} else {
Serial.println("Failed to connect to WiFi");
}
}
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
String macAddress = advertisedDevice.getAddress().toString().c_str();
macAddress.toUpperCase();
macAddress.trim();
Serial.print("MAC Address: "); Serial.println(macAddress);
if (macAddress == MAC_inside_sensor) {
Serial.print("Inside sensor detected - ");
String raw_data = String(BLEUtils::buildHexData(nullptr, (uint8_t*)advertisedDevice.getManufacturerData().c_str(), advertisedDevice.getManufacturerData().length()));
raw_data.toUpperCase();
HEX_inside_sensor = raw_data;
Serial.print("Raw data: "); Serial.println(HEX_inside_sensor);
insideSensorFound = true;
} else if (macAddress == MAC_outside_sensor) {
Serial.print("Outside sensor detected - ");
String raw_data = String(BLEUtils::buildHexData(nullptr, (uint8_t*)advertisedDevice.getManufacturerData().c_str(), advertisedDevice.getManufacturerData().length()));
raw_data.toUpperCase();
HEX_outside_sensor = raw_data;
Serial.print("Raw data: "); Serial.println(HEX_outside_sensor);
outsideSensorFound = true;
}
// Stop the scan if both sensors are found
if (insideSensorFound && outsideSensorFound) {
pBLEScan->stop();
}
}
};
void setupBLE() {
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);
}
void logWiFiStrength() {
long rssi = WiFi.RSSI();
Serial.print("WiFi Signal Strength (RSSI): ");
Serial.print(rssi);
Serial.println(" dBm");
}
void setup() {
Serial.begin(115200);
WiFi.setSleep(false);
esp_task_wdt_deinit();
esp_task_wdt_init(&wdt_config);
esp_task_wdt_add(NULL);
delay(4000);
connectToWiFi();
setupBLE();
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
Serial.println("NTP initialized");
http.setTimeout(20000); // Set timeout to 20 seconds
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
insideSensorFound = false;
outsideSensorFound = false;
HEX_inside_sensor = "";
HEX_outside_sensor = "";
BLEScanResults* foundDevices = pBLEScan->start(scanTime, false);
pBLEScan->clearResults();
if (!HEX_inside_sensor.isEmpty() && !HEX_outside_sensor.isEmpty()) {
String googleURL_with_parameters = googleURL + "&HEXin=" + HEX_inside_sensor + "&HEXout=" + HEX_outside_sensor;
Serial.println("URL: " + googleURL_with_parameters);
if (WiFi.status() == WL_CONNECTED) {
http.begin(googleURL_with_parameters);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on HTTP request: ");
Serial.println(httpResponseCode);
if (httpResponseCode == -1) {
Serial.println("Failed to connect to server. Re-initializing WiFi...");
connectToWiFi();
}
}
http.end();
} else {
Serial.println("Error in WiFi connection. Re-initializing WiFi...");
connectToWiFi();
}
} else {
Serial.println("No BLE values received");
}
}
esp_task_wdt_reset();
}
Any tips, hints, ANYTHING is appreciated, as I do not know what else to look at...
thanks!

