I’ve been working on getting a simple Arduino board connected to Losant. I’m using the older Arduino Wifi shield and I updated the firmware so now it connects to the wifi with no problems. However, it has yet to connect to Losant. It just gets stuck in the connecting loop. I even tried changing the Losant.h file like in the “MQTT Not Connecting” help topic (until I realized that it was no longer relevant haha). Any suggestions? Code I’m using is below:
include WiFi.h
include Losant.h
// WiFi credentials.
char* WIFI_SSID = "my_network";
char* WIFI_PASS = "my_password";
// Losant credentials.
char* LOSANT_DEVICE_ID = "my_id";
char* LOSANT_ACCESS_KEY = "my_key";
char* LOSANT_ACCESS_SECRET = "my_secret";
// Create a secure WiFi client. This can also be an instance of the unsecured
// WiFiClient class, but the secure TLS client is recommended.
WiFiClient client;
// Create an instance of a Losant device.
LosantDevice device(LOSANT_DEVICE_ID);
// Connects to WiFi and then to the Losant platform.
void connect() {
// Connect to WiFi.
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());
// Connect to Losant.
Serial.println();
Serial.print("Connecting to Losant...");
// Connect the device instance to Losant using TLS encryption.
device.connect(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
while(!device.connected()) {
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
}
void setup() {
Serial.begin(115200);
while(!Serial) { }
connect();
}
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");
toReconnect = true;
}
if(toReconnect) {
connect();
}
device.loop();
}
The first thing I would check is the communications log on the application page. With the application page open, attempt to connect your device. If you see some messages, we at least know the board is reaching Losant. Here’s a screenshot of what I’m referring to:
Ok, let’s log the underlying mqtt client’s state to try to see why the board is not reaching Losant. Add a println to the loop where it’s waiting to connect:
while(!device.connected()) {
delay(500);
Serial.println(device.mqttClient.state()); // HERE
Serial.print(".");
}
This should log out some number that we can use for more debugging.
-4 is MQTT_CONNECTION_TIMEOUT, which doesn’t particularly help, but I have another thing to try. Since you said you’re using an older WiFi, you may have to enable an option in the underlying PubSubClient library that we use. From their readme:
Arduino WiFi Shield - if you want to send packets > 90 bytes with this shield,
enable the MQTT_MAX_TRANSFER_SIZE define in PubSubClient.h.
PubSubClient.h will be located in your Arduino’s library folder. On a Mac, it’s at: ~/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h. Open this file and uncomment out the following line (roughly line 42):
#define MQTT_MAX_TRANSFER_SIZE 80
Save the file and recompile and upload your sketch to the Arduino to pick up the change.
Hmm…just gave that a go and it took a little longer to connect to the WiFi and then got stuck in the same “Connecting to Losant” loop. (still reporting a client state of -4 btw)
Can you please try the basic web request example, just to rule out the possibility that the wifi shield is unable to connect to any external source: https://www.arduino.cc/en/Tutorial/WiFiWebClient
Could you please provide some screenshots of what you are seeing? We also have multiple forum posts with the same issue, so maybe one of them could lead to a solution. Here are a couple: