Re: ESP8266 with easy webconfig
Posted: Fri Apr 22, 2016 3:44 am
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");
}
}