-->
Page 1 of 1

MQTTS issues

PostPosted: Sun Aug 04, 2019 9:06 am
by batstru
Hi all,

I'm using the PubSubClient library to connect to Mosquitto, and I'm having a problems to connect.
The client.connect function returns -2 which means generic network error (see: https://pubsubclient.knolleary.net/api.html#state)

To check the certs, I've loaded them into mqtt-explorer and they work, I can connect to my host with such certs and credentials.

So.. it must be something wrong with the code: do you spot out anything bad from the code below?

FYI:
- the structure of the code is to use the deep sleep, so the loop() is empty.
- the network configuration is ok. To test it out, I'm doing an api call to a public web server to get my public ip. This code is cut from the snippet below.

Regards


Code: Select all
#include <Adafruit_BMP085.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <PubSubClient.h>
#include <stdlib.h>
#include <WiFiUdp.h>
#include <Wire.h>
#include "certificates.h"
#include <WiFiClientSecure.h>

bool logging = true;

IPAddress ip( 192, 168, 2, 9 );
IPAddress gateway( 192, 168, 2, 1 );
IPAddress subnet( 255, 255, 255, 0 );
IPAddress dns(192,168,2,1 );

//vcc read
ADC_MODE(ADC_VCC);
String v_str;

//Wifi details
const char* ssid = "rete";
const char* password = "0rgani,,a,Ion3!";

const int sclPin = D2;
const int sdaPin = D1;

//Instantiate TLS connection
WiFiClientSecure espClient;
PubSubClient client(espClient);

//VARIABLES
long lastReconnectAttempt = 0;
//sensors data
const char* sensor_account = "account";
const char* sensor_version = "version";
const char* sensor_id = "8";

//MQTT connection details
const char* mqtt_server = "host";
const int   mqtt_server_port = port;
const char* mqtt_username = sensor_account;
const char* mqtt_clientID = sensor_id;
const char* mqtt_password = "password";
const char* mqtt_topic = sensor_version;

// sleep for this many seconds
const int sleepSeconds = 3600;

// Instantiate new BMP chip
Adafruit_BMP085 bmp;
byte address_BMP085 = 119;

//FUNCTIONS
void setup_wifi() {
  if (logging) { Serial.print(millis()); Serial.println(" - Connecting to WIFI - Starting"); }
  WiFi.config(ip, dns, gateway, subnet);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  if (logging) { Serial.print(millis()); Serial.println(" - Connecting to WIFI - Connected");}
}

boolean checkConnection() {
  int count = 0;
  if (logging) { Serial.print(millis()); Serial.println(" - Waiting for Wi-Fi connection - Starting"); }
  while ( count < 30 ) {
    if (WiFi.status() == WL_CONNECTED) {
      if (logging) { Serial.print(millis()); Serial.println(" - Waiting for Wi-Fi connection - Connected"); }
      return (true);
    }
    delay(500);
    if (logging) { Serial.print(millis()); Serial.println(" - Waiting for Wi-Fi connection - Again"); }
    count++;
  }
  if (logging) { Serial.print(millis()); Serial.println(" - Waiting for Wi-Fi connection - Timed out!"); }
  return false;
}

boolean reconnect() {
  if (client.connect(mqtt_clientID)) {
    client.subscribe(mqtt_topic);
  }
  return client.connected();
}

void sendData(float temperature, String voltage) {
  String message = sensor_account;
  message += ", ";
  message += mqtt_topic;
  message += ", ";
  message += sensor_id;
  message += ", ";
  message += temperature;
  message += ", ";
  message += voltage;

  if (logging) { Serial.print(millis()); Serial.print(" - MQTT Message: "); Serial.println(String(message).c_str()); }
  if (client.publish(mqtt_topic, String(message).c_str())) {
     if (logging) { Serial.print(millis()); Serial.print(" - Message sent "); }
  } else {
     if (logging) { Serial.print(millis()); Serial.print(" - Can't send the message"); }
    }
 

}

void readvcc() {
  uint16_t v = ESP.getVcc();
  float_t v_cal = ((float)v / 1024.0f);
  v_str = String(v_cal);
}

void setup() {

  WiFi.mode(WIFI_OFF);
  WiFi.forceSleepBegin();
  delay(1);
 
  Serial.begin(9600);


  if (logging) { Serial.print(millis()); Serial.println(" - I2C interface - Starting"); }
  Wire.begin(sdaPin, sclPin);
  if (logging) { Serial.print(millis()); Serial.println(" - I2C interface - Completed"); }
 
  if (logging) { Serial.print(millis()); Serial.println(" - Checking BMP085 responses - Starting"); }
  Wire.beginTransmission(address_BMP085);
  if (!bmp.begin()) {
    if (logging) { Serial.println("Could not find a valid BMP085 sensor, check wiring!"); }
    while (1) {}
  }
  Wire.endTransmission();
  if (logging) { Serial.print(millis()); Serial.println(" - Checking BMP085 responses - Completed"); }

  if (logging) { Serial.print(millis()); Serial.println(" - Getting temperature - Starting"); }
  Wire.beginTransmission(address_BMP085);
  float temperature = bmp.readTemperature();
  Wire.endTransmission();
  if (logging) { Serial.print(millis()); Serial.println(" - Getting temperature - Completed"); }

  if (logging) { Serial.print(millis()); Serial.println(" - Getting voltage - Starting"); }
  readvcc();
  if (logging) { Serial.print(millis()); Serial.println(" - Getting voltage - Completed"); }

  WiFi.forceSleepWake();
  delay( 1 );
  setup_wifi();

  if (logging) { Serial.print(millis()); Serial.println(" - Setup MQTT - Starting"); }
  //MQTT Set server
  espClient.setCertificate(certificates_bin_crt, certificates_bin_crt_len);
  espClient.setPrivateKey(certificates_bin_key, certificates_bin_key_len);
 
  client.setClient(espClient);
  client.setServer(mqtt_server, mqtt_server_port);
  if (logging) { Serial.print(millis()); Serial.println(" - Setup MQTT - Completed"); }
  if (logging) { Serial.print(millis()); Serial.println(" - Connect to MQTT - Starting"); }

  if (client.connect(mqtt_clientID, mqtt_username, mqtt_password)) {
    if (logging) { Serial.print(millis()); Serial.print(" - connected to mqtt to topic: "); Serial.println(mqtt_topic);  }
    client.loop();
  } else {
    if (logging) { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println("");}
    if (logging) { Serial.print(millis()); Serial.print(" - mqtt_server: "); + Serial.print(mqtt_server); Serial.println(""); }
    if (logging) { Serial.print(millis()); Serial.print(" - mqtt_server_port: "); + Serial.print(mqtt_server_port); Serial.println("");}
    if (logging) { Serial.print(millis()); Serial.print(" - mqtt_clientID: "); + Serial.print(mqtt_clientID); Serial.println("");}
    if (logging) { Serial.print(millis()); Serial.print(" - mqtt_username: "); + Serial.print(mqtt_username); Serial.println("");}
    if (logging) { Serial.print(millis()); Serial.print(" - mqtt_password: "); + Serial.print(mqtt_password); Serial.println("");}
  }
  client.loop();
  if (logging) { Serial.print(millis()); Serial.println(" - Connect to MQTT - Completed"); }
  if (logging) { Serial.print(millis()); Serial.println(" - Send data to MQTT - Starting"); }

  sendData(temperature, v_str);   
  delay(500);   
  if (logging) { Serial.print(millis()); Serial.println(" - Send data to MQTT (Including 500ms delay) - Completed"); }

  if (logging) { Serial.print(millis()); Serial.println(" - Disconnect wifi and sleep"); }
  WiFi.disconnect( true );
  delay( 1 );

  // Connect D0 to RST to wake up
  pinMode(D0, WAKEUP_PULLUP);
  ESP.deepSleep(sleepSeconds * 1000000, WAKE_RF_DISABLED);
   
}

void loop() {

}