Post by agecrabahaykubo on Jun 8, 2017 23:50:22 GMT
Hi everyone, it's been a while since my last post. Was busy tinkering with this home automation project and scouring the net for some answer to a MQTT error that I saw in the log.
Here's the error log from mosquitto.
Here's the sketch for nodeMCU board which i copied/stiched from the sample codes.
So basically I added two relay/openhab controlled lamp, one with uno & ethernet shield and the other one is nodeMCU board(esp8266) but not really connected to multi-choice node.
After some period of time I guess when the connection timed-out the virtual switch from openhab interface is not responding anymore but it is still do the switching only not turning on/off the light.
I hope I make sense with my explanation
Thanks.
Mark
Here's the error log from mosquitto.
1496961417: New client connected from 192.168.0.xx as mcuClient (c1, k15).
1496961423: New connection from 192.168.0.xx on port 1883.
1496961423: Client mcuClient already connected, closing old connection.
1496961423: Client mcuClient disconnected.
1496961423: New client connected from 192.168.0.xx as mcuClient (c1, k15).
1496961445: Client mcuClient has exceeded timeout, disconnecting.
1496961445: Socket error on client mcuClient, disconnecting.
1496961603: New connection from 192.168.0.xx on port 1883.
1496961603: New client connected from 192.168.0.xx as mcuClient (c1, k15).
1496962887: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1496961423: New connection from 192.168.0.xx on port 1883.
1496961423: Client mcuClient already connected, closing old connection.
1496961423: Client mcuClient disconnected.
1496961423: New client connected from 192.168.0.xx as mcuClient (c1, k15).
1496961445: Client mcuClient has exceeded timeout, disconnecting.
1496961445: Socket error on client mcuClient, disconnecting.
1496961603: New connection from 192.168.0.xx on port 1883.
1496961603: New client connected from 192.168.0.xx as mcuClient (c1, k15).
1496962887: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
Here's the sketch for nodeMCU board which i copied/stiched from the sample codes.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Time.h>
#include <TimeAlarms.h>
#define relayPin D1 // Pin of Relay Module
bool state=false;
void callback(char* topic, byte* payload, unsigned int length);
// Wifi settings
const char* ssid = "xxxxxxxxxx";
const char* password = "xxxxxxxxx";
const char* mqtt_server = "192.168.0.xx";
//const char* mqtt_port = "1883";
WiFiClient espClient;
PubSubClient client(mqtt_server, 1883, callback, espClient);
void setup_wifi() {
delay(100);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
if (strcmp(topic,"/home/room2/ardshield/lamp2/com")==0) {
if (payload[0] == '0')
{
digitalWrite(relayPin, LOW); // Turns OFF Relay and light
Serial.println ("Light switch OFF");
delay(1000);
client.publish("/home/room2/ardshield/lamp2/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(relayPin, HIGH); // Turns ON Relay and light
Serial.println ("Light switch ON");
delay(1000);
client.publish("/home/room2/ardshield/lamp2/state","1");
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
//if your MQTT broker has clientID,username and password
//please change following line to if (client.connect(clientId,userName,passWord))
if (client.connect(clientId.c_str()))
{
Serial.println("connected");
//once connected to MQTT broker, subscribe command if any
client.subscribe("LivingRoomLamp");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 6 seconds before retrying
delay(5000);
}
}
}
void setup(){
Serial.begin(115200);
pinMode(relayPin, OUTPUT); // Set Pin connected to Relay as an OUTPUT
// pinMode(ldrSensorPin, INPUT);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Serial.println(" Starting ");
delay(1000);
if (client.connect("mcuClient")) {
Serial.println("MQTT Connected");
client.publish("outTopic","hello world");
client.subscribe("/home/room2/ardshield/lamp2/#"); // Subscribe to all messages for this device
}
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
//set the time and date
setTime(18,40,0,6,8,2017); // set time to Wednesday, May 26, 2017
// create the alarms
Alarm.alarmRepeat(6,15,0, TurnOffLight); // 7:00am every day (summer)
Alarm.alarmRepeat(22,15,0, TurnOnLight); // Turn Light ON from Mon-Fri
}
void loop(){
if (!client.connected()) {
reconnect();
}
client.loop();
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
// functions to be called when an alarm triggers:
void TurnOffLight()
{
digitalWrite(relayPin, LOW); //turn OFF relay
Serial.println("Alarm: - turn lights off");
client.publish("/home/room2/ardshield/lamp2/state","0");
delay(1000);
}
void TurnOnLight()
{
digitalWrite(relayPin, HIGH); //turn on relay
Serial.println("Alarm: - turn lights on");
client.publish("/home/room2/ardshield/lamp2/state","1");
delay(1000);
}
void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
#include <PubSubClient.h>
#include <Wire.h>
#include <Time.h>
#include <TimeAlarms.h>
#define relayPin D1 // Pin of Relay Module
bool state=false;
void callback(char* topic, byte* payload, unsigned int length);
// Wifi settings
const char* ssid = "xxxxxxxxxx";
const char* password = "xxxxxxxxx";
const char* mqtt_server = "192.168.0.xx";
//const char* mqtt_port = "1883";
WiFiClient espClient;
PubSubClient client(mqtt_server, 1883, callback, espClient);
void setup_wifi() {
delay(100);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
if (strcmp(topic,"/home/room2/ardshield/lamp2/com")==0) {
if (payload[0] == '0')
{
digitalWrite(relayPin, LOW); // Turns OFF Relay and light
Serial.println ("Light switch OFF");
delay(1000);
client.publish("/home/room2/ardshield/lamp2/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(relayPin, HIGH); // Turns ON Relay and light
Serial.println ("Light switch ON");
delay(1000);
client.publish("/home/room2/ardshield/lamp2/state","1");
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
//if your MQTT broker has clientID,username and password
//please change following line to if (client.connect(clientId,userName,passWord))
if (client.connect(clientId.c_str()))
{
Serial.println("connected");
//once connected to MQTT broker, subscribe command if any
client.subscribe("LivingRoomLamp");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 6 seconds before retrying
delay(5000);
}
}
}
void setup(){
Serial.begin(115200);
pinMode(relayPin, OUTPUT); // Set Pin connected to Relay as an OUTPUT
// pinMode(ldrSensorPin, INPUT);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Serial.println(" Starting ");
delay(1000);
if (client.connect("mcuClient")) {
Serial.println("MQTT Connected");
client.publish("outTopic","hello world");
client.subscribe("/home/room2/ardshield/lamp2/#"); // Subscribe to all messages for this device
}
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
//set the time and date
setTime(18,40,0,6,8,2017); // set time to Wednesday, May 26, 2017
// create the alarms
Alarm.alarmRepeat(6,15,0, TurnOffLight); // 7:00am every day (summer)
Alarm.alarmRepeat(22,15,0, TurnOnLight); // Turn Light ON from Mon-Fri
}
void loop(){
if (!client.connected()) {
reconnect();
}
client.loop();
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
// functions to be called when an alarm triggers:
void TurnOffLight()
{
digitalWrite(relayPin, LOW); //turn OFF relay
Serial.println("Alarm: - turn lights off");
client.publish("/home/room2/ardshield/lamp2/state","0");
delay(1000);
}
void TurnOnLight()
{
digitalWrite(relayPin, HIGH); //turn on relay
Serial.println("Alarm: - turn lights on");
client.publish("/home/room2/ardshield/lamp2/state","1");
delay(1000);
}
void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
So basically I added two relay/openhab controlled lamp, one with uno & ethernet shield and the other one is nodeMCU board(esp8266) but not really connected to multi-choice node.
After some period of time I guess when the connection timed-out the virtual switch from openhab interface is not responding anymore but it is still do the switching only not turning on/off the light.
I hope I make sense with my explanation
Thanks.
Mark