Device disconnect - Builder kit workshop

#1

Hello, I am new to this community and I have started reading the documentation and doing the workshops.
After uploading the code in the workshop to the ESP8266 and completed all configuration, I was able to control the LED and read the button state from my dashboard / workflow.
The problem is that after some time (couple of hours) the ESP8266 disconnect from the losant server and will not connect again (although the code check if the server is disconnected and try to connect again).
The ESP8266 still connected to the Wifi but disconnected from losant server.
Help please.

0 Likes

#2

We’ve had some ongoing issues with the ESP8266 Arduino core when using TLS. The underlying TLS library causes intermittent connectivity issues, but usually the device simply reconnects without any issues.

Can you please provide the github link to the workshop source code that you flashed to the device (or simply copy-paste the actual code here). I want to double-check and make sure it has the latest reconnect logic.

0 Likes

#3
/**
 * Workshop example for a remote control LED.
 *
 * Visit https://www.losant.com/kit for full instructions.
 *
 * Copyright (c) 2016 Losant IoT. All rights reserved.
 * https://www.losant.com
 */

'#include <ESP8266WiFi.h
'#include <Losant.h

// WiFi credentials.
const char* WIFI_SSID = "CWB";
const char* WIFI_PASS = "cwb12345";

// Losant credentials.
const char* LOSANT_DEVICE_ID = "57b80b40eb31750100f3cbf7";
const char* LOSANT_ACCESS_KEY = "";
const char* LOSANT_ACCESS_SECRET = "";

const int LED_PIN = 12;

bool ledState = false;

// For a secure connection to Losant.
WiFiClientSecure wifiClient;

LosantDevice device(LOSANT_DEVICE_ID);

void toggle() {
  Serial.println("Toggling LED.");
  ledState = !ledState;
  digitalWrite(LED_PIN, ledState ? HIGH : LOW);
}

// Called whenever the device receives a command from the Losant platform.
void handleCommand(LosantCommand *command) {
  Serial.print("Command received: ");
  Serial.println(command->name);

  if(strcmp(command->name, "toggle") == 0) {
    toggle();
  }
}

void connect() {
  // Connect to Wifi.
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);

  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.println();
  Serial.println("Connecting to Losant...");
  device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
  
  unsigned long connectionStart = millis();
  while(!device.connected()) {
    delay(500);
    Serial.print(".");

    // If we can't connect after 5 seconds, restart the board.
    if(millis() - connectionStart > 5000) {
      Serial.println("Failed to connect to Losant, restarting board.");
      ESP.restart();
    }
  }

  Serial.println("Connected!");
  Serial.println("This device is now ready for use!");
}

void setup() {
  Serial.begin(115200);

  // Giving it a little time because the serial monitor doesn't
  // immediately attach. Want the workshop that's running to
  // appear on each upload.
  delay(2000);

  Serial.println();
  Serial.println("Running Workshop 2 Firmware.");

  pinMode(LED_PIN, OUTPUT);

  // Register the command handler to be called when a command is received
  // from the Losant platform.
  device.onCommand(&handleCommand);

  connect();
  }


void buttonPressed() {
  Serial.println("Button Pressed!");

  // Losant uses a JSON protocol. Construct the simple state object.
  // { "button" : true }
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["button"] = true;

  // Send the state to Losant.
  device.sendState(root);
}

int buttonState = 0;

void loop() {

  bool toReconnect = false;
  if(WiFi.status() != WL_CONNECTED) {
    Serial.println("Disconnected from WiFi");
    toReconnect = true;
  }
  
  if(!device.connected()) {
    Serial.println("Disconnected from Losant");
    Serial.println(device.mqttClient.state());
    toReconnect = true;
  }
  
  if(toReconnect) {
    connect();
  }

  device.loop();
  
  delay(1000);
}
0 Likes

#4

This code looks correct. There is another known bug in the ESP8266/Arduino core that causes the board to not boot properly after it’s been flashed. Please try flashing the board, unplugging it, and plugging it back in. That will cause the board to boot properly and if an expected reset occurs it will boot properly again.

0 Likes