ESP8266 Wifi Shield and pubsub library

Hi,

I’m trying to use the pubsub.h MQTT library with the ESP8266 Wifi shield and cannot make the connection to the broker here’s the code:

//////////////////////

// Library Includes //

//////////////////////

#include <SparkFunESP8266WiFi.h>

#include <SoftwareSerial.h>

#include <PubSubClient.h>

const char* mqtt_server = “broker.mqttdashboard.com”;

long lastMsg = 0;

char msg[50];

ESP8266Client espClient;

PubSubClient client(espClient);

void setup() {

// put your setup code here, to run once:

char mySSID = “MySSD”;

char myPSK = “********”;

Serial.begin(19200);

initialiseESP8266();

connectESP8266(mySSID, myPSK);

client.setServer(mqtt_server, 1883);

}

void loop() {

// put your main code here, to run repeatedly:

int value;

if (!client.connected()) {

reconnect();

}

client.loop();

long now = millis();

if (now - lastMsg > 2000) {

lastMsg = now;

++value;

snprintf (msg, 75, “message from arduino #%ld”, value);

Serial.print("Publish message: ");

Serial.println(msg);

client.publish(“hello/world”, msg);

}

}

void reconnect() {

// Loop until we’re reconnected

while (!client.connected()) {

Serial.print(“Attempting MQTT connection…”);

// Attempt to connect

if (client.connect(“ESP8266Client”)) {

Serial.println(“connected”);

// Once connected, publish an announcement…

client.publish(“outTopic”, “hello world”);

// … and resubscribe

client.subscribe(“hello/world”);

} else {

Serial.print(“failed, rc=”);

Serial.print(client.state());

Serial.println(" try again in 5 seconds");

// Wait 5 seconds before retrying

delay(5000);

}

}

}

void initialiseESP8266(){

if(esp8266.begin()){

Serial.println(F(“ESP8266 has been detected”));

}

else{

Serial.println(F(“Could not detect ESP8266, check connections!”));

}

}

void connectESP8266(char *mySSID,char *myPSK){

// The ESP8266 can be set to one of three modes:

// 1 - ESP8266_MODE_STA - Station only

// 2 - ESP8266_MODE_AP - Access point only

// 3 - ESP8266_MODE_STAAP - Station/AP combo

// Use esp8266.getMode() to check which mode it’s in:

int mode = esp8266.getMode();

if(mode != ESP8266_MODE_STA){

//set it to station mode

if(esp8266.setMode(ESP8266_MODE_STA) < 0){

Serial.println(F(“There was a problem setting to station mode.”));

}

else{

Serial.println(F(“ESP8266 is set to station mode”));

}

}

// esp8266.status() indicates the ESP8266’s WiFi connect

// status.

// A return value of 1 indicates the device is already

// connected. 0 indicates disconnected. (Negative values

// equate to communication errors.)

if(esp8266.status() <= 0){

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

Serial.println(mySSID);

// esp8266.connect([ssid], [psk]) connects the ESP8266

// to a network.

// On success the connect function returns a value >0

// On fail, the function will either return:

// -1: TIMEOUT - The library has a set 30s timeout

// -3: FAIL - Couldn’t connect to network.

if(esp8266.connect(mySSID, myPSK) < 0){

Serial.print(F("There ws an error trying to connect to "));

Serial.println(mySSID);

}

else{

Serial.print(F("Successfully connected to "));

Serial.println(mySSID);

}

}

else{

Serial.print(F("Already connected to "));

Serial.print(F("AP: "));

Serial.println(mySSID);

Serial.print(F("and has IP: ")); Serial.println(esp8266.localIP());

}

}

Any help would be very much appreciated.

Regards

Any debug transmission from the serial port would be very much appreciated.

Whoops, posted in the wrong browser tab

Does that mean you have no solution for me?

I have no solution because I don’t know what the problem is. “It doesn’t work” could be due to many causes in between your ESP and the server that you are trying to connect with. That program should send out progress reports to the serial port as it is trying to establish contact to that broker. It is crucial to find out where the problem is. Only posting the code is of little help if others made this code and it apparently was good enough to publish. It is good that you did though, or else there would be nothing. I’m sure it will compile correctly on my side (hint: IF I know where to find that pubsub library you are using). But I am not in your internet-neighbourhood, or on your Wifi network, so I will not likely encounter the same roadblocks as you do.

The question is whether the Sparkfun library was created to work with other library’s such as pubsub and I was really hoping that someone would let me know either way whether the library client could be handled like the Ethernet client and so could be passed to the pubsub library https://github.com/knolleary/pubsubclient

As it is the code above has no errors and works with the Ethernet library but with the SparkFun Library (!client.connected()) is returning false, so I’m guessing it is incompatible?

I have the same project/problem with getting mqtt samples to run with sparkfun 8266 library, anybody figured this out?

Something how the client.h is mis-implemented, see this thread:

https://github.com/knolleary/pubsubclient/issues/107

Hey,

Just want to let you guys know that I had the same problem and fixed some issues regarding SubSub & Sparkfun AT Library:

  • Enabled ESP8266::write() to post binary payload instead of only terminated strings.

  • Introduced lookahead buffer between ESP8266Client and ESP8266. Buffer will filter AT commands from esp-responses (today only “+IPD”).

  • Misinterpreration (?) and mixup of CIPStatus/status() within ESP8266Client will suggest no connection to PubSub

I am now able to connect and publish using Sparkfun ESP8266Client and PubSub. Hope the day I have sacrificed debugging saves a few headaches! :wink:

https://github.com/mararn1618/SparkFun_ … no_Library