[Solved] Can't connect to Losant with ESP32

mqtt

#1

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;
}

#2

It looks like at the moment, the Arduino MQTT client is unable to connect over SSL - we are looking into why internally. In the meantime, you can connect over an insecure channel - you want to use WiFiClient instead of WiFiClientSecure and use device.connect instead of device.connectSecure.


#3

I have changed code that use WiFiClient instead of WiFiClientSecure and use device.connect instead of device.connectSecure.

unfortunately, Errors continue to occur.
Serial log is below.

======================
Connecting to MakeySQ
.
WiFi connected
IP address:
192.168.0.20
Connecting to Losant…-1
.-1
.-1
.-1
.-1
.-1
.-1
.-1
.-1
.-1


#4

Are you able to connect to brokers other than Losant with the PubSub client directly? Are you perhaps behind a firewall that is preventing connections over ports 1883 (insecure MQTT) or 8883 (secure MQTT)?


#5

I don’t know how to connect directly to brokers. Please, Can you give sample code?


#6

I run code in below link using WIFI and SmartPhone tethering.
the result is same.

https://forums.losant.com/t/cant-connect-to-losant written by Brandon_Cannaday

and serial log is


rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun 8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:8488
ho 0 tail 12 room 4
load:0x40080400,len:5832
entry 0x40080698
…Connected to WiFi
Failed to connect.
-1


#7

I have tested WIndow app. app name is MQTT.fx.
When using Window app, it is success.

Connection profile setting

MQTT Publish screeshot

MQTT subscribe screenshot

I don’t know why can’t connect to losant MQTT broker?

Please help me.


#8

Hey Michael, i’m still unable to connect securely but am able to connect unsecure.

When will that be fixed? if it is fixed - what should i do?

Thanks,

Roy


#9

We have both the ESP32 and MKR1000 connecting successfully both securely and insecurely now: MKR1000 hangs on trying to connect to Losant