I am working with an Arduiono Board and an Ethernet Shield.
I followed the “Can’t connect to Losant” topic - using a modified version of the sketch described using PubSubClient.h I succeed in connecting to Losant:
> #include <SPI.h>
> #include <Ethernet.h>
> #include <PubSubClient.h>
> #define MQTT_MAX_TRANSFER_SIZE 80 //this was done as a result of troubleshooting
> byte mac[] = { 0x00, 0x21, 0x5C, 0xD7, 0x78, 0xEF };
> void callback(char* topic, byte* payload, unsigned int length) {
> Serial.print("Command received.");
> }
> EthernetClient client;
> PubSubClient pclient("broker.losant.com", 1883, callback, client);
> void setup() {
> Serial.begin(9600);
> // disable SD SPI
> pinMode(4,OUTPUT);
> digitalWrite(4,HIGH);
> Serial.print(F("Starting ethernet..."));
> if(!Ethernet.begin(mac)) Serial.println(F("failed"));
> else {
> Serial.println(Ethernet.localIP());
> Serial.println(Ethernet.gatewayIP());
> }
> if(pclient.connect("removed for security", "removed for security", "removed for security")) {
> Serial.println("Connected to Losant");
> }
> else {
> Serial.println("Failed to connect.");
> Serial.println(pclient.state());
> }
> }
> void loop() {
> }
I double check this by looking on the Serial monitor - I see “Connected to Losant” -
and on the Losant Application Communication log I see “Connection Succeeded” & “Authentication Succeeded”
I then switch to a sketch using the Losant.h library based on the arduino wifi example (again key details removed for security but exactly the same as those used successfully above
#include <SPI.h>
#include <Ethernet.h>
#include <Losant.h>
#define MQTT_MAX_TRANSFER_SIZE 80 //this was done as a result of troubleshooting
// ** Configure the Losant credentials to enable comms over mqtt **
const char* LOSANT_DEVICE_ID = "removed for security";
const char* LOSANT_ACCESS_KEY = "removed for security";
const char* LOSANT_ACCESS_SECRET = "removed for security";
byte mac[] = { 0x00, 0x21, 0x5C, 0xD7, 0x78, 0xEF };
const int BUTTON_PIN = 31;
const int LED_PIN = 12;
bool ledState = false;
EthernetClient client;
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 setup() {
Serial.begin(9600);
while(!Serial) { }
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
// disable SD SPI
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Serial.print(F("Starting ethernet..."));
if(!Ethernet.begin(mac)) Serial.println(F("failed"));
else {
Serial.println(Ethernet.localIP());
Serial.println(Ethernet.gatewayIP());
}
// Register the command handler to be called when a command is received
// from the Losant platform.
device.onCommand(&handleCommand);
connect();
}
void connect() {
// Connect to Losant.
Serial.println();
Serial.print("Connecting to Losant...");
device.connectSecure(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
while(!device.connected()) {
delay(500);
Serial.println(device.mqttClient.state());
Serial.print(".");
}
Serial.println("Connected!");
}
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(!device.connected()) {
Serial.println("Disconnected from Losant");
Serial.println(device.mqttClient.state());
toReconnect = true;
}
if(toReconnect) {
connect();
}
device.loop();
int currentRead = digitalRead(BUTTON_PIN);
if(currentRead != buttonState) {
buttonState = currentRead;
if(buttonState) {
buttonPressed();
}
}
delay(100);
}
I get -4 returned from Serial.println(device.mqttClient.state()); and there is no activity shown in the communication log- it feels like I have done something daft - can you help?