Whats wrong with this Code ??
Posted: Fri Dec 30, 2016 10:22 am
Hello, I'm new in Arduino & IoT, I'm building simple project that can control electronic device using ESP12E AI Thinker, MQTT and HomeAssistant. The system & UI worked fine until I add/edit some GPIO as an input.
The "if function" that use "status3" as a trigger and publish into topic ("ins/room6/status", "1") or ("ins/room6/status", "0") make Serial monitor display "Attempting Connection .... MQTT COnnected" after Callback updated into topic ("ins/room6").
If I delete that "if function" that use "status3" the system works fine, but I need to use "status3" to send a topic payload.
Is there anything wrong with my code?
Big thanks for help.
The "if function" that use "status3" as a trigger and publish into topic ("ins/room6/status", "1") or ("ins/room6/status", "0") make Serial monitor display "Attempting Connection .... MQTT COnnected" after Callback updated into topic ("ins/room6").
If I delete that "if function" that use "status3" the system works fine, but I need to use "status3" to send a topic payload.
Is there anything wrong with my code?
Big thanks for help.
Code: Select all
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#define MQTT_SERVER "xx.xx.xx.xx"
const char* ssid = "xxxxxxxx";
const char* password = "xxxxxxx";
const int lightPin1 = 15;
const int lightPin2 = 12;
const int lightPin3 = 14;
const int push1 = 5;
const int push2 = 4;
const int push3 = 9;
char* lightTopic1 = "ins/room1";
char* lightTopic2 = "ins/room2";
char* lightTopic3 = "ins/room3";
char* lightTopic4 = "ins/room4";
char* lightTopic5 = "ins/room5";
char* lightTopic6 = "ins/room6";
WiFiClient wifiClient;
PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);
void setup() {
pinMode(lightPin1, OUTPUT);
pinMode(lightPin2, OUTPUT);
pinMode(lightPin3, OUTPUT);
pinMode(push1, INPUT_PULLUP);
pinMode(push2, INPUT_PULLUP);
pinMode(push3, INPUT_PULLUP);
Serial.begin(115200);
WiFi.begin(ssid, password);
reconnect();
delay(2000);
}
void loop(){
if (!client.connected() && WiFi.status() == 3) {reconnect();}
client.loop();
int status1 = digitalRead(push1);
int status2 = digitalRead(push2);
int status3 = digitalRead(push3);
if(status1==0){
client.publish("ins/room4/status", "1");
Serial.print("sensor1 : ");
Serial.println(status1);
}else if (status1==1){
client.publish("ins/room4/status", "0");
Serial.print("sensor1 : ");
Serial.println(status1);
}
if(status2==0){
client.publish("ins/room5/status", "1");
Serial.print("sensor2 : ");
Serial.println(status2);
}else if (status2==1){
client.publish("ins/room5/status", "0");
Serial.print("sensor2 : ");
Serial.println(status2);
}
if(status3==0){
client.publish("ins/room6/status", "1");
Serial.print("sensor3 : ");
Serial.println(status3);
}else if (status3==1){
client.publish("ins/room6/status", "0");
Serial.print("sensor3 : ");
Serial.println(status3);
}
delay(100);
}
void callback(char* topic, byte* payload, unsigned int length) {
String topicStr = topic;
Serial.println("Callback update.");
Serial.print("Topic: ");
Serial.println(topicStr);
if(topicStr.equals(lightTopic1)){
if(payload[0] == '1'){
digitalWrite(lightPin1, HIGH);
client.publish("ins/room1/status", "1");
}
else if (payload[0] == '0'){
digitalWrite(lightPin1, LOW);
client.publish("ins/room1/status", "0");
}
}
if(topicStr.equals(lightTopic2)){
if(payload[0] == '1'){
digitalWrite(lightPin2, HIGH);
client.publish("ins/room2/status", "1");
}
else if (payload[0] == '0'){
digitalWrite(lightPin2, LOW);
client.publish("ins/room2/status", "0");
}
}
if(topicStr.equals(lightTopic3)){
if(payload[0] == '1'){
digitalWrite(lightPin3, HIGH);
client.publish("ins/room3/status", "1");
}
else if (payload[0] == '0'){
digitalWrite(lightPin3, LOW);
client.publish("ins/room3/status", "0");
}
}
if(topicStr.equals(lightTopic4)){
if(payload[0] == '1'){
client.publish("ins/room4/status", "1");
}
else if (payload[0] == '0'){
client.publish("ins/room4/status", "0");
}
}
if(topicStr.equals(lightTopic5)){
if(payload[0] == '1'){
client.publish("ins/room5/status", "1");
}
else if (payload[0] == '0'){
client.publish("ins/room5/status", "0");
}
}
if(topicStr.equals(lightTopic6)){
if(payload[0] == '1'){
client.publish("ins/room6/status", "1");
}
else if (payload[0] == '0'){
client.publish("ins/room6/status", "0");
}
}
}
void reconnect() {
if(WiFi.status() != WL_CONNECTED){
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
if(WiFi.status() == WL_CONNECTED){
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
if (client.connect((char*) clientName.c_str())) {
Serial.print("\tMTQQ Connected");
client.subscribe(lightTopic1);
client.subscribe(lightTopic2);
client.subscribe(lightTopic3);
client.subscribe(lightTopic4);
client.subscribe(lightTopic5);
client.subscribe(lightTopic6);
}
else{Serial.println("\tFailed."); abort();}
}
}
}
String macToStr(const uint8_t* mac){
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5){
result += ':';
}
}
return result;
}