Hey everyone,
I've recently embarked on a simple project to monitor the temperature of my pool. To do this, I acquired a DS18B20 sensor along with a board to connect it to my Arduino. The sensor itself isn't causing any issues; rather, it's what I'm trying to do with it that's proving tricky.
I use a home automation system called : eedomus which is a straightforward and handy device. Eedomus API - eedomus - Documentation
To integrate the temperature data into my setup, I created a virtual sensor and obtained the necessary URL to send requests for updating its value based on the DS18B20 readings.
Everything seemed to work smoothly with standard HTTP requests. However, eedomus requires HTTPS for communication, and this is where the trouble began. Simply swapping WiFiClient
for WiFiClientSecure
caused everything to stop functioning.
It's puzzling because the project was initially operational with the initial code (with HTTPS request).
But the only change I made was in the URL within the code because I deleted the virtual sensor by mistake along with the sensor ID .
But after uploading the modified code with the new url, it ceased to work altogether.
My current code is facing an issue with the HTTPS request, but I'm having trouble pinpointing the problem. I've narrowed it down to the HTTPS request because even when I tested it with a new code using the Postman API : https://postman-echo.com/get, it didn't work either.
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFiClientSecure.h>
// Replace the following values with your own WiFi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// IP address of the eedomus server
const char* serverIP = "192.168.1.86";
// Define the DS18B20 sensor address
#define ONE_WIRE_BUS D4 // Connect the DS18B20 sensor to pin D4 (GPIO2)
// Create an instance of the OneWire class to manage the 1-Wire bus
OneWire oneWire(ONE_WIRE_BUS);
// Pass the OneWire instance to the DallasTemperature library
DallasTemperature sensors(&oneWire);
// Address of your eedomus device
const String eedomusURL = "/api/set?api_user=YOUR_API_USER&api_secret=YOUR_API_SECRET&action=periph.value&periph_id=YOUR_PERIPH_ID&value=";
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Initialize the DS18B20 sensor
sensors.begin();
}
void loop() {
// Get the temperature from the DS18B20 sensor
sensors.requestTemperatures(); // Request temperature from the sensor
float temperatureC = sensors.getTempCByIndex(0);
// Check if the reading is valid (temperature is -127 if reading failed)
if (temperatureC != -127.00) {
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
// Send the temperature value to the eedomus API
sendToEedomus(temperatureC);
} else {
Serial.println("Sensor reading failed");
}
delay(5000); // Wait 5 seconds before reading again
}
void sendToEedomus(float temperature) {
// Create the full URL for the eedomus API with the temperature as value
String url = eedomusURL + String(temperature);
// Use the WiFiClientSecure library to establish a secure HTTPS connection
WiFiClientSecure client;
// Send the HTTPS GET request to the eedomus API
if (client.connect(serverIP, 443)) {
Serial.print("Request sent to eedomus API: ");
Serial.println("https://" + String(serverIP) + url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + String(serverIP) + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println("Request sent to eedomus API");
// Wait for server response
while (client.connected()) {
if (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
}
// Close the connection
client.stop();
} else {
Serial.println("Failed to connect to eedomus API");
}
}
If anyone has insights into why this might be happening, I'd greatly appreciate your input. Thanks!