Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By trcircuit
#45979 I try to add very basic off pubsubclient and mqtt function to the code. Code works well when alone but somehow the loop part off the mqtt code blocks all the web config code.

Code: Select all#include <PubSubClient.h>
#include <ESP8266WiFi.h>

#define MQTT_SERVER "192.168.1.10"
// from this part i don't take it to the web config code
const char* ssid = "xxxxx";
const char* password = "xxxxx";
// to this part i don't take it to the web config code

const int lightPin = 0;

char* lightTopic = "/house/light1";
char* clientname = "newesp";
char* lightConfirmTopic = "/house/light1confirm";


WiFiClient wifiClient;
PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);

void setup() {
  pinMode(lightPin, OUTPUT);
// from this part i don't take it to the web config code
  Serial.begin(115200);
  delay(100);
  WiFi.begin(ssid, password);
  if(WiFi.status() != WL_CONNECTED){
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
    }
  }
// to this part i don't take it to the web config code
}

void loop(){
  if (!client.connected()) {
    while (!client.connected()) {
      if (client.connect(clientname)) {
        client.subscribe(lightTopic);
      }
      else{abort();}
    }
    }
  client.loop();
  delay(10);
}


//MQTT callback
void callback(char* topic, byte* payload, unsigned int length) {
  if(payload[0] == '1'){
    digitalWrite(lightPin, HIGH);
    client.publish(lightConfirmTopic, "1");
  }
  else{
    digitalWrite(lightPin, LOW);
    client.publish(lightConfirmTopic, "0");
  }
}
User avatar
By staffa
#60835 Great work John!
I'm using the esp8266 as antenna for my atmega328 project and it works fine as it is. I wish to implement your code however I can't get to make it communicate in AT. Am I missing something? Is there a way to keep the AT communication? Thank you!!