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