Data not being sent to dashboard from Wemos Mini

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
}

The first thing to check is the Device Communication Log.

My hunch is that the 60 second delay between device.loop calls is too long. I recommend ~100ms between calls to device.loop. This is where all network communication is performed and 60 seconds can lead to timeouts.

You’ll have to reduce the delay. You can still report every 60 seconds by keeping track of the last time you reported using millis.

Thanks for the quick reply Brandon,

I’ve changed the delay to 100, the device com log just shows “No logs yet, Listening…”

Ok, the next thing to try is connecting insecure. Sometimes embedded modules are very picky about TLS and certificates.

Change the following:

WiFiClientSecure wifiClient;
to
WiFiClient wifiClient;
device.connectSecure(...);
to
device.connect();

Thanks Brandon, that got me much closer, I’m now getting the following error on the connection log
Connected:6 Dec 2023 20:13:47Connected via MQTTIP: 46.65.114.147

Disconnected: Topic inbound throughput limit exceeded on losant/656e2ae294a67f854486024a/stateIP: 46.65.114.14737 messages sent / 0messages received

cracked it, changed the delay to 1 second and its staying connected.

thanks again for your help Brandon