Good evening all, I need some help, I’ve just gotten back into playing with Losant and Wemos Mini’s.
I’m trying to upload my temperature and humidity readings from a DHT22, I’m showing the readings on IDE2 but they do not seem to be getting sent to my Losant account, I believe the device is connected to the account though.
Its been a long time since I’ve done this, so I apologise if ive overlooked something simple. My skills on this are basic.
My code is as follows:
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <Losant.h>
#include <ArduinoJson.h>
#define WIFI_SSID "deleted for this post"
#define WIFI_PASS "deleted for this post"
#define LOSANT_DEVICE_ID "656e2ae294a67f854486024a"
#define LOSANT_ACCESS_KEY "deleted for this post"
#define LOSANT_ACCESS_SECRET "deleted for this post"
#define DHT_PIN 4 // DHT22 data pin
DHT dht(DHT_PIN, DHT22);
WiFiClientSecure wifiClient;
LosantDevice device(LOSANT_DEVICE_ID);
void setup() {
Serial.begin(115200);
delay(10);
// Connect to Wi-Fi
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
// Connect to Losant
device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
dht.begin();
}
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
if (isnan(temp) || isnan(hum)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" °C | Humidity: ");
Serial.print(hum);
Serial.println(" %");
// Send data to Losant
device.loop();
if (device.connected()) {
// Construct the payload
DynamicJsonDocument payload(256);
payload["temp"] = temp;
payload["hum"] = hum;
// Convert DynamicJsonDocument to JsonObject
JsonObject jsonObject = payload.as<JsonObject>();
// Send the payload
device.sendState(jsonObject);
}
delay(60000); // Wait for a minute before sending the next data point
}