I can’t connect to Lasant with ESP32
mqttClient.state -1.
Losant application log is “No logs yet Listening”
serial log is
=================================
Running Workshop 3 Firmware.
Connecting to MakeySQ
.....
WiFi connected
IP address:
192.168.0.20
Connecting to Losant...-2
.-2
.-2
===============================
source code
===============================
/**
* Workshop example for periodically sending temperature data.
*
* Visit https://www.losant.com/kit for full instructions.
*
* Copyright (c) 2016 Losant IoT. All rights reserved.
* https://www.losant.com
*/
#include <WiFiClientSecure.h>
#include <Losant.h>
// WiFi credentials.
const char* WIFI_SSID = "";
const char* WIFI_PASS = "";
// Losant credentials.
const char* LOSANT_DEVICE_ID = "";
const char* LOSANT_ACCESS_KEY = "";
const char* LOSANT_ACCESS_SECRET = "";
const int BUTTON_PIN = 33;
const int LED_PIN = 27;
const int TEMP_PIN = 39;
bool ledState = false;
WiFiClientSecure wifiClient;
LosantDevice device(LOSANT_DEVICE_ID);
void toggle() {
Serial.println("Toggling LED.");
ledState = !ledState;
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
}
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(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("Connecting to Losant...");
device.connect(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
while(!device.connected()) {
delay(500);
Serial.println(device.mqttClient.state()); // HERE
Serial.print(".");
}
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 3 Firmware.");
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
device.onCommand(&handleCommand);
connect();
}
void buttonPressed() {
Serial.println("Button Pressed!");
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["button"] = true;
device.sendState(root);
}
void reportTemp(double degreesC, double degreesF) {
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["tempC"] = degreesC;
root["tempF"] = degreesF;
device.sendState(root);
}
int buttonState = 0;
int timeSinceLastRead = 0;
int tempSum = 0;
int tempCount = 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 MQTT");
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();
}
}
tempSum += analogRead(TEMP_PIN);
tempCount++;
// Report every 15 seconds.
if(timeSinceLastRead > 2000) {
// Take the average reading over the last 15 seconds.
double raw = (double)tempSum / (double)tempCount;
// The tmp36 documentation requires the -0.5 offset, but during
// testing while attached to the Feather, all tmp36 sensors
// required a -0.52 offset for better accuracy.
double degreesC = (((raw / 1023.0)) - 0.5) * 100.0;
double degreesF = degreesC * 1.8 + 32;
Serial.println();
Serial.print("Raw: ");
Serial.println(raw);
Serial.print("Temperature C: ");
Serial.println(degreesC);
Serial.print("Temperature F: ");
Serial.println(degreesF);
Serial.println();
reportTemp(degreesC, degreesF);
timeSinceLastRead = 0;
tempSum = 0;
tempCount = 0;
}
delay(100);
timeSinceLastRead += 100;
}