1

I've the following problem:
I want to create an MQTT-client on an Adafruit Huzzah ESP6288 and set the MAC-address as clientID.

This is my code:

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define WLAN_SSID       "Jupiter"
#define WLAN_PASS       "wilano123"
#define SERVER      "192.168.178.134"
#define CLIENTID      "abcde123456789"
#define SERVERPORT  1883          

WiFiClient client;
void MQTT_connect();
Adafruit_MQTT_Client mqtt;

void setup() {
  Serial.begin(115200);
  delay(10);


  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.mode(WIFI_STA);
  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
  }
  Serial.println();

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

  String mac = WiFi.macAddress(); 
  int length = mac.length() + 1;
  char macArray[length];
  mac.toCharArray(macArray, length);
  mqtt = Adafruit_MQTT_Client(&client, SERVER, SERVERPORT, macArray, "root", "pass");
}

void loop() {
    MQTT_connect();
    sendTemperature(12.5, "Werkstatt");
    String s = WiFi.macAddress();
    Serial.println(s);
    delay(5000);
}

void sendTemperature(double temperature, String sensorID) {
    .....
    mqtt.publish("ds18b20",char_array,0);
}

void MQTT_connect() {
    int8_t ret;

    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);
       }
   }
   Serial.println("MQTT Connected!");
}

I'm new to C and I think it isn't possible to initialize the Adafruit_MQTT_Client mqtt variable and set it in the setup loop.
Hope somebody can help, so I can create an Adafruit_MQTT_Client using the MAC-address as clientID.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
hengel28
  • 89
  • 2
  • 10
  • 1
    I started to look at this, as I was curious that "WiFi.macAddress()" would return a String. Most embedded code tends to stay away from Strings, as the String lib is large. I looked at ESP8266WiFi.h and couldn't fund a macAddress() method. Rather, a method called getMacAddress, which returns char *. Where did you get your version of ESP8266WiFi.h ? – Greycon Feb 09 '17 at 11:41
  • http://esp8266.github.io/Arduino/versions/2.0.0/doc/libraries.html – hengel28 Feb 09 '17 at 12:44
  • OK, in the code you show above, you are declaring mac as a String type. It needs to be : byte mac[6]; This will then populate the byte array with the 6 byte Mac address. You will then need to convert this to a standard C null-terminated character arrray to pass to the constructor of the Adafruit_MQTT_Client. Do you know how to go about this? – Greycon Feb 09 '17 at 13:36
  • 1
    @Greycon [`WiFi.macAddress()` also returns a `String` object.](https://github.com/esp8266/Arduino/blob/2.3.0/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp#L368) – gre_gor Feb 09 '17 at 14:40
  • My bad - missed it. – Greycon Feb 09 '17 at 14:43

0 Answers0