Hello, I am trying to get sensor data from an ecowitt webpage to an esp32 with integrated e-ink display ( https://soldered.com/product/soldered-inkplate-5-5-2″-e-paper-board/).
I use the following code but the display just shows connected to WIFI with an IP address.
Any help appreciated
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include <Inkplate.h>
// Create an Inkplate object for display (use INKPLATE_1BIT for monochrome mode)
Inkplate display(INKPLATE_1BIT);
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// Your server details with URL path
const char* serverName = "http://192.168.1.106:1880/get-sensor";
// Username and Password for HTTP Basic Authentication
const char* serverUsername = "YOUR_USERNAME";
const char* serverPassword = "YOUR_PASSWORD";
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 5000; // Set timer to 5 seconds
String sensorReadings;
float sensorReadingsArr[3]; // Array to store parsed sensor readings
void setup() {
Serial.begin(115200);
// Initialize the Inkplate display
display.begin();
display.clearDisplay();
display.display(); // Refresh the display to clear any previous data
// Display a message indicating the connection process
display.setTextSize(3); // Set a larger font size
display.setTextColor(BLACK, WHITE);
display.setCursor(50, 50);
display.print("Connecting to WiFi...");
display.display(); // Show the message on the e-paper display
// Connecting to WiFi
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi with IP Address: ");
Serial.println(WiFi.localIP());
// Update display with the IP Address
display.clearDisplay();
display.setCursor(50, 50);
display.print("Connected to WiFi!");
display.setCursor(50, 100);
display.print("IP: ");
display.print(WiFi.localIP().toString()); // Display IP address
display.display(); // Update the display
Serial.println("Timer set to 5 seconds (timerDelay variable).");
}
void loop() {
// Send an HTTP GET request every 5 seconds
if ((millis() - lastTime) > timerDelay) {
// Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
sensorReadings = httpGETRequest(serverName, serverUsername, serverPassword); // Fetch sensor data with auth
Serial.println(sensorReadings);
JSONVar myObject = JSON.parse(sensorReadings); // Parse JSON response
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
// Print the full JSON object to Serial
Serial.print("JSON object = ");
Serial.println(myObject);
// Get keys of the JSON object
JSONVar keys = myObject.keys();
for (int i = 0; i < keys.length(); i++) {
JSONVar value = myObject[keys[i]];
Serial.print(keys[i]);
Serial.print(" = ");
Serial.println(value);
sensorReadingsArr[i] = double(value); // Store each sensor reading in array
}
// Print each sensor reading to Serial
Serial.print("Sensor 1 = ");
Serial.println(sensorReadingsArr[0]);
Serial.print("Sensor 2 = ");
Serial.println(sensorReadingsArr[1]);
Serial.print("Sensor 3 = ");
Serial.println(sensorReadingsArr[2]);
// Update the Inkplate display with sensor data
display.clearDisplay(); // Clear the screen before displaying new data
display.setCursor(50, 50);
display.print("Sensor 1 (Temp): ");
display.print(sensorReadingsArr[0]);
display.print(" C");
display.setCursor(50, 100);
display.print("Sensor 2 (Humidity): ");
display.print(sensorReadingsArr[1]);
display.print(" %");
display.setCursor(50, 150);
display.print("Sensor 3 (Pressure): ");
display.print(sensorReadingsArr[2]);
display.print(" hPa");
display.display(); // Refresh the display to show updated data
} else {
Serial.println("WiFi Disconnected");
// Display WiFi disconnected message on Inkplate
display.clearDisplay();
display.setCursor(50, 50);
display.print("WiFi Disconnected");
display.display();
}
lastTime = millis(); // Update lastTime
}
}
// Function to make HTTP GET request with username and password
String httpGETRequest(const char* serverName, const char* username, const char* password) {
WiFiClient client;
HTTPClient http;
http.begin(client, serverName); // Initialize HTTP connection
// Set basic authentication credentials
http.setAuthorization(username, password);
int httpResponseCode = http.GET(); // Send GET request
String payload = "{}"; // Default empty payload in case of failure
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString(); // Get the response as a string
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end(); // Free resources
return payload;
}