-->
Page 22 of 22

Re: ESP8266 with easy webconfig

PostPosted: Fri Apr 22, 2016 3:44 am
by trcircuit
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");
  }
}

Re: ESP8266 with easy webconfig

PostPosted: Thu Apr 28, 2016 2:59 am
by trcircuit
Found the bug, in the loop part i changed
else{abort();}
to
else{delay(500);}

Re: ESP8266 with easy webconfig

PostPosted: Wed Jan 11, 2017 6:41 am
by staffa
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!!