Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help porting library to ESP32 #2

Open
krupis opened this issue Sep 10, 2021 · 0 comments
Open

Need help porting library to ESP32 #2

krupis opened this issue Sep 10, 2021 · 0 comments

Comments

@krupis
Copy link

krupis commented Sep 10, 2021

Hello. I am trying to port the code to ESP32 since ESP8266 is very old and rarely used anymore. I have deleted the temperature part of the project since I am not really worried about that at this moment.

  1. I have downloaded Arduino_JSON library
  2. I have downloaded Adafruit_MQTT_Client library
  3. I have included "WiFi.h" (ESP32 alternative to ESP8266WiFi.h)

4.The next thing I have done is I have replaced the FLESPI_TOKEN to the TOKEN that I have on my flespi account

#define FLESPI_TOKEN    "FlespiToken dmHNhdqXEO4nrJfT0kZ6rKMriITrTiuUuZyB2OnxrhIIOOsEWfAcNbtVJacqiubC"
  1. And finally I have changed the SSID and PASS credentials for my WiFi network.

The issue that I am having at the moment is with this line of code:

client.setFingerprint(FLESPI_CERT_FINGERPRINT);

The error returned:

'class WiFiClientSecure' has no member named 'setFingerprint'

If I comment this line out, then my program will just retry connecting to the MQTT:

10:39:32.608 -> Retrying MQTT connection in 5 seconds...
10:39:37.594 -> Connection failed
10:39:37.594 -> Retrying MQTT connection in 5 seconds...
10:39:42.580 -> Connection failed
10:39:42.580 -> Retrying MQTT connection in 5 seconds...

My full code:

// This project shows how to send DS18B20 temperature data from ESP8266 board to flespi MQTT broker
// Read more here https://flespi.com/blog/how-to-connect-esp8266-to-secure-mqtt-broker-know-it-all-and-get-it-done-approach

#include <WiFi.h>
#include <Arduino_JSON.h>
#include <WiFiClientSecure.h>
#include <Adafruit_MQTT_Client.h>
/************************* MAIN DEFINITIONS *********************************/

#define ONEWIRE_PIN      14
#define STATIC_MESSAGE_BUF_LEN 256

#define MQTT_SERVER      "mqtt.flespi.io"
#define MQTT_SERVERPORT  8883
#define FLESPI_CERT_FINGERPRINT "6B 4B 7D 8B 78 EC D7 B7 DF 25 3E 96 9D 5F 1F 9D 3C B1 51 57"
#define DEST_TOPIC       "ESP8266/test"
#define FLESPI_TOKEN    "FlespiToken dmHNhdqXEO4nrJfT0kZ6rKMriITrTiuUuZyB2OnxrhIIOOsEWfAcNbtVJacqiubC"#define DEVICE_IDENTIFICATION_STRING "flespi"

#define WLAN_SSID       "MY_ID"
#define WLAN_PASS       "MY_PASS"

/**************************** Local files ***********************************/



/********************* Global connection instances **************************/
// WiFiFlientSecure for SSL/TLS support
WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, FLESPI_TOKEN, "");
// Setup a feed 'flespi' to publish messages to flespi MQTT broker.
Adafruit_MQTT_Publish flespi = Adafruit_MQTT_Publish(&mqtt, DEST_TOPIC);

// JSON message creation part
JSONVar message_object;         // to store message parameters
String json_msg_string;         // to stringify JSON message object
char message_string_buf[STATIC_MESSAGE_BUF_LEN];   // to use in mqtt publish function

/*************************** Sketch Code ************************************/
// Generic example for ESP8266 wifi connection
void setup() {
  Serial.begin(115200);
  delay(10);

  Serial.println("ESP8266: flespi MQTT over SSL example");

  // Connect to WiFi access point.
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  delay(1000);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  delay(2000);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());

  client.setFingerprint(FLESPI_CERT_FINGERPRINT);

  message_object["ident"] = DEVICE_IDENTIFICATION_STRING;
}

void loop() {
  float cels_degrees;             // temperature sensor value
  int8_t rc;                      // return code for get temperature function
  // add your sensor variable here e.g.
  // int my_sensor_value;

  // establish MQTT connection
  MQTT_connect(mqtt);

  // check for new value on OneWire connected pin
  rc = 10;
  if (rc != 0) {
    Serial.print("Failed to read one-wire temperature sensor with code: ");
    Serial.println(rc);
  } else {
    // add temperature sensor value to message object
    message_object["temperature"] = rc;
  }

  // read add your sensor value and add it to message object, e.g.
  //my_sensor_value = analogRead(A0);
  //message_object["analog.input"] = my_sensor_value;

  // send data to flespi MQTT broker via secure connection
  json_msg_string = JSON.stringify(message_object);
  json_msg_string.toCharArray(message_string_buf, json_msg_string.length() + 1);
  Serial.print("Publishing message to broker: ");
  Serial.println(message_string_buf);
  flespi.publish(message_string_buf);

  // cleanup memory used
  memset(message_string_buf, 0, STATIC_MESSAGE_BUF_LEN);

  // wait a second before repeat
  delay(1000);
}



void MQTT_connect(Adafruit_MQTT_Client mqtt) {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected())
    return;

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0)
         while (1); // basically die and wait for WDT to reset me
  }

  Serial.println("MQTT Connected!");
}

Hoping to get some clarification please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant