Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By ridge
#28637 @krzychb,

Now I am confused :?

I get a compile error of:
Code: Select all/home/ridge/.arduino15/packages/esp8266/hardware/esp8266/1.6.5-1044-g170995a/libraries/PubSubClient/PubSubClient.h:16:7: note:   candidate expects 1 argument, 2 provided
no matching function for call to 'PubSubClient::PubSubClient(WiFiClient&, IPAddress&)'


I am glad you have found a solution. Multiple libraries with the same name and poor version control = insanity. :D

Edit: I apparently have been using an older version of the lmroy/pubsubclient package. :o
When I deleted my existing pubsubclient library and followed my own instructions about installing the package, it would no longer compile until I made the changes suggested by @krzychb. Mystery solved! I'll load up a few nodes with the new library version and see if there is any change in system stability.
Last edited by ridge on Tue Sep 08, 2015 5:04 pm, edited 2 times in total.
User avatar
By krzychb
#28643 @ridge - to have constructor with single argument I would replace (in the code from post #27767):

Code: Select allPubSubClient mqttClient(mqttServer);

with
Code: Select allPubSubClient mqttClient(wifiClient);

It compiles but does not work for me - I does not connect with MQTT broker. The reason is likely explained in PubSubClient.h but I did not have time today to test it:
Code: Select allpublic:
   //! Simple constructor
   /*!
     Use set_server() before connect()
   */
   PubSubClient(Client& c);

In other words I think the issue may be with the above constructor call in post #27767, that requires another type of argument - not IPAddress but Client& and calling set_server() before connect(). This is assuming that you have the latest and not modified library https://github.com/Imroy/pubsubclient.

I hope I kept my reasoning short enough and this will move us to forward with troubleshooting :D

BTW: I believe Arduino IDE for ESP8266 by igrr is a masterpiece :D


Happy hacking, Krzysztof
User avatar
By ridge
#28649 @krzychb,

BTW: I believe Arduino IDE for ESP8266 by igrr is a masterpiece :D


I agree! I am very happy to accept all of the decisions that igrr has made in the massive amount of work to go from the native esp8266 compiler/hardware environment to the arduino 'form factor'. An amazing accomplishment, really.

Update: I apparently have been using an older version of the lmroy/pubsubclient package. :o
When I deleted my existing pubsubclient library and followed my own instructions about installing the package, it would no longer compile until I made the changes suggested by @krzychb. Mystery solved! I'll load up a few nodes with the new library version and see if there is any change in system stability.
User avatar
By ridge
#28669 I see no difference between nodes using the older library and the new one in operation.
Here is a small update version_6 for the 'new' lmroy/pubsubclient. :)


The lines:
if (mqttClient.connect("espClient_111","esp_111/available",0, 0, "0")){
mqttClient.publish("esp_111/available","1");

are worth mentioning.

mqttClient.connect("espClient_111","esp_111/available", 0, 0, "0")
initializes the 'last will and testament' for MQTT. This allows MQTT to automatically publish esp_111/available 0 as the topic and payload when communication with the node has disconnected for any reason.

mqttClient.publish("esp_111/available","1")
Announces to openHAB that the node is now available. The 5 minute update of this call as a heartbeat does not really seem necessary. I will leave it in as an example of two non-blocking timers.



Code: Select all/*
 mqtt_openHAB_111_6

 Example program for esp8266-12 on the yellow carrier board.
 Used to interact over local intranet with a mosquitto v1.4.3 server. OpenHAB also running on server computer.
 
 Ver. 6 Change PubSubClient instance creation to conform with most recent version of lmroy/PubSubClient.
        Add return statements in mqttCallback to save a few clock cycles.
 Ver. 5 Add full system reset if WiFi communications have become unstable.
 Ver. 4 Add last will and testament for openHAB notification of node health during MQTT initialization.
 Ver. 3 Support mosquitto 1.4.3 and the @lmroy PubSubClient library for esp8266/arduino.
      Implement lmroy's wifi and mqtt initialization within the main loop method.
 Ver. 2 Changed mqtt messaging structure to support multiple esp boards addressed by openHAB at different IP addresses
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

ADC_MODE(ADC_TOUT);

const char* ssid = "your ssid";
const char* password = "your password";

IPAddress mqttServer( 192, 168, 1, 114 );//Hardcode of mosquitto server IP on local intranet connected computer.
byte IP[]         = { 192, 168, 1, 111 };//Hardcode for this esp8266 node
byte Netmask[]    = { 255, 255, 255, 0 };//Local router info
byte Gateway[]    = { 192, 168, 1, 1 };  //Local router info

WiFiClient   wifiClient;
PubSubClient mqttClient(wifiClient,mqttServer);

long millis_now;         // For non-blocking delay
long millis_prev1;       // For non-blocking delay
long millis_prev2;       // For non-blocking delay

long mqtt_connect_count = 0;

long LEDredValue = 0;
long LEDgreenValue = 0;
long LEDblueValue = 0;

long lightsensor = 0;
long lightsensor_prev = 0;

//----------------------------------------------------------------------------
// Callback function
//----------------------------------------------------------------------------
void mqttCallback(const MQTT::Publish& pub) {
 
 String msgString = pub.payload_string();

 if (String(pub.topic()) == "openHAB/esp_111/RED") {
   LEDredValue = round(msgString.toInt() * 10.2);
   analogWrite(15,LEDredValue);
   return;
 }
 if (String(pub.topic()) == "openHAB/esp_111/GREEN") {
   LEDgreenValue = round(msgString.toInt() * 10.2);
   analogWrite(12,LEDgreenValue);
   return;
 }
 if (String(pub.topic()) == "openHAB/esp_111/BLUE") {
   LEDblueValue = round(msgString.toInt() * 10.2);
   analogWrite(13,LEDblueValue);
   return;
 }
 if (String(pub.topic()) == "openHAB/esp_111/led1") {
  if (msgString == "ON") digitalWrite( 2, 0 );  //red leds are inverted output
  else                   digitalWrite( 2, 1 ); 
  return;
 }
 if (String(pub.topic()) == "openHAB/esp_111/led2") {
  if (msgString == "ON") digitalWrite( 0, 0 );  //red leds are inverted output
  else                   digitalWrite( 0, 1 );
  return;
 }
 if (String(pub.topic()) == "openHAB/esp_111/led3") {
  if (msgString == "ON") digitalWrite( 4, 0 );  //red leds are inverted output
  else                   digitalWrite( 4, 1 ); 
  return;
 }
 if (String(pub.topic()) == "openHAB/esp_111/led4") {
  if (msgString == "ON") digitalWrite( 5, 0 );  //red leds are inverted output
  else                   digitalWrite( 5, 1 ); 
  return;
 }
 if (String(pub.topic()) == "openHAB/esp_111/led5") {
  if (msgString == "ON") digitalWrite( 14, 0 );  //red leds are inverted output
  else                   digitalWrite( 14, 1 ); 
  return;
 }
 if (String(pub.topic()) == "openHAB/esp_111/led6") {
  if (msgString == "ON") digitalWrite( 16, 0 );  //red leds are inverted output
  else                   digitalWrite( 16, 1 ); 
  return;
 }
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void setup()
{
  // ESP-12 yellow carrier board GPIO assignments
  pinMode(A0, INPUT);  // analog light sensor input
  pinMode(2, OUTPUT);  // 00000X  discrete red led position
  pinMode(0, OUTPUT);  // 0000X0  discrete red led position
  pinMode(4, OUTPUT);  // 000X00  discrete red led position
  pinMode(5, OUTPUT);  // 00X000  discrete red led position
  pinMode(14,OUTPUT);  // 0X0000  discrete red led position
  pinMode(16,OUTPUT);  // X00000  discrete red led position

  pinMode(15,OUTPUT);  // RGB led red
  pinMode(12,OUTPUT);  // RGB led green
  pinMode(13,OUTPUT);  // RGB led blue
 
  // Initialize GPIO 'inverted' led outputs to off
  digitalWrite(2, 1);  // red leds are inverted output
  digitalWrite(0, 1);  // red leds are inverted output
  digitalWrite(4, 1);  // red leds are inverted output
  digitalWrite(5, 1);  // red leds are inverted output
  digitalWrite(14,1);  // red leds are inverted output
  digitalWrite(16,1);  // red leds are inverted output
   
  digitalWrite(15,0);  // RGB led red not inverted
  digitalWrite(12,0);  // RGB led green not inverted
  digitalWrite(13,0);  // RGB led blue not inverted

  Serial.begin(115200);
  delay(10);
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void loop()
{
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println();
    Serial.print("WiFi connecting to ");
    Serial.print(ssid);
    Serial.println("...");
    yield();
    WiFi.begin(ssid, password);

    if (WiFi.waitForConnectResult() != WL_CONNECTED)
      return;
      Serial.println("WiFi connected.");
      WiFi.config(IPAddress(IP[0],IP[1],IP[2],IP[3] ), 
                  IPAddress(Gateway[0],Gateway[1],Gateway[2],Gateway[3] ),
                  IPAddress(Netmask[0],Netmask[1],Netmask[2],Netmask[3] ));
      WiFi.mode(WIFI_STA);   
      Serial.println("ESP8266 IP address: ");
      Serial.println(WiFi.localIP());
  }

  if (WiFi.status() == WL_CONNECTED) {
    if (!mqttClient.connected()){
      yield();
      //                    ( String id,      String willTopic,  uint8_t willQos, bool willRetain, String willMessage)
      if (mqttClient.connect("espClient_111","esp_111/available",0,               0,              "0")){
          mqttClient.publish("esp_111/available","1");
          mqttClient.set_callback(mqttCallback);
          mqttClient.subscribe("openHAB/esp_111/#");
          Serial.print("Connected to mqtt server.");
          mqtt_connect_count ++;
          if(mqtt_connect_count > 5){   //Something is out of sync somewhere... do a system reset.
            wifiClient.stop();
            wifiClient.flush();
            WiFi.mode(WIFI_OFF);
            WiFi.disconnect(1);
            yield();
            delay(5000);                //Give the router time to recognize disconnected node.
            ESP.restart();              //
          }
       }
    }

    if (mqttClient.connected()){
     
      yield();
      mqttClient.loop(); // Keep mqtt connection open and allow callbacks to work
      millis_now = millis();

      if (millis_now > (millis_prev1 + 5000)){ 
        millis_prev1 = millis_now;
        lightsensor = analogRead(A0);

        if ((lightsensor > lightsensor_prev + 25) ||
            (lightsensor < lightsensor_prev - 25)){
          char sensor[8];
          lightsensor_prev = lightsensor;
          itoa(lightsensor,sensor,10);  // Integer to string
   
          mqttClient.publish("esp_111/light_sensor", sensor);
        }
      }
     
      if (millis_now > (millis_prev2 + 300000)){ //5 min
        millis_prev2 = millis_now;
        mqttClient.publish("esp_111/available","1");
      }
    }
  }