I want to use an ESP8266 as station(?) to forward detected sensor values to a webserver to be displayed in a browser via fastAPI. The fastAPI code is working fine, and the program for the Wemos D1 is doing good as well. I've tried several Wemos D1 R2 boards and a mini to no avail.
Wemos code:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
// Wi-Fi Credentials
const char* ssid = "LTE-WiFi_NTEK";
const char* password = "Ntek12345";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println(" Connected!");
}
void loop() {
// Call your device-specific function here
sendDeviceAData();
sendDeviceBData();
sendDeviceCData();
delay(5000); // Send data every 5 seconds
}
void sendDeviceAData() {
// DHT Sensor
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
WiFiClient client;
http.begin(client,"http://127.0.0.1:8000/deviceA");
http.addHeader("Content-Type", "application/json");
// Simulated water level value
float Temperature = 12.4;
float Moisture = 12.4;
String payload = "{\"temperature\": " + String(Temperature) + " \"moisture\": " + String(Moisture) + "}";
int httpResponseCode = http.POST(payload);
Serial.println("Device A Data Sent");
Serial.println(payload);
Serial.println("Response Code: " + String(httpResponseCode));
http.end();
}
}
void sendDeviceBData() {
// Soil Moisture Sensor
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
WiFiClient client;
http.begin(client, "http://127.0.0.1:8000/deviceB");
http.addHeader("Content-Type", "application/json");
// Simulated water level value
float soilMoisture = 12.4;
String payload = "{\"soil_moisture\": " + String(soilMoisture) + "}";
int httpResponseCode = http.POST(payload);
Serial.println("Device B Data Sent");
Serial.println(payload);
Serial.println("Response Code: " + String(httpResponseCode));
http.end();
}
}
void sendDeviceCData() {
// Ultrasonic Sensor for water level
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
WiFiClient client;
http.begin(client, "http://127.0.0.1:8000/deviceC");
http.addHeader("Content-Type", "application/json");
// Simulated water level value
float waterLevel = 12.4;
String payload = "{\"water_level\": " + String(waterLevel) + "}";
int httpResponseCode = http.POST(payload);
Serial.println("Device C Data Sent");
Serial.println(payload);
Serial.println("Response Code: " + String(httpResponseCode));
http.end();
}
}
fastAPI code:
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List
app = FastAPI()
# In-memory storage for received weather data
data_storage = {
"deviceA": [],
"deviceB": [],
"deviceC": []
}
# Request models for each device
class DeviceAData(BaseModel):
temperature: float
moisture: float
class DeviceBData(BaseModel):
soil_moisture: float
class DeviceCData(BaseModel):
water_level: float
@app.get("/")
def root():
return {"message": "Weather Data API is running"}
# Endpoint to receive data from Device A
@app.post("/deviceA")
def receive_device_a_data(data: DeviceAData):
data_storage["deviceA"].append(data)
return {"message": "Device A data received", "data": data}
# Endpoint to receive data from Device B
@app.post("/deviceB")
def receive_device_b_data(data: DeviceBData):
data_storage["deviceB"].append(data)
return {"message": "Device B data received", "data": data}
# Endpoint to receive data from Device C
@app.post("/deviceC")
def receive_device_c_data(data: DeviceCData):
data_storage["deviceC"].append(data)
return {"message": "Device C data received", "data": data}
# Endpoint to retrieve all stored weather data
@app.get("/data")
def get_all_data():
return data_storage
When the ESP8266 sends data automatically to the webserver, it gives a response code of -1, which according to the library means that connection has failed, yet I have checked by printing WiFi.status() that it is indeed connected. I am not sure about http.begin as I am not familiar with its functions. Could you help me with my problem?
Thank you!