MQTT switch freezing

8 years 9 months ago #2859 by simsonite
Replied by simsonite on topic MQTT switch freezing
@Henrik,

I have had it connected to an iPhone charger now for 4 days (DEBUG disabled) and counting with the "old" software and still no problem. By old software I mean the one where the myConnectedCb() function did not have the myMqtt.subscribe("/Db/" + instanceId + "/" + String(storage.moduleId) + "/Sensor.Parameter1"); line. So thanks EasyIoT for the updated function! Any other changes?

I did however have the same problem when having it connected to a cheap China charger. I thought everything was fine (DEBUG disabled), it was online for roughly 3 days but then I lost the connection to it. So there seems to be several causes for this...

1. First, I assume you need a really stable charger (will try again to add capacitor to NodeMCU and try the China charger once more). Otherwise the iPhone charger seems to do the trick.

2. Replace the old software with the new.

3. Have DEBUG disabled.


That should do it...? What do you think EasyIoT?

Please Log in or Create an account to join the conversation.

8 years 9 months ago #2861 by shmuck
Replied by shmuck on topic MQTT switch freezing

EasyIoT wrote:

HM wrote: Hi there

I got more the less the same problem.
After a while my ESP do not react on IOT commands, but I can ping the module and use the local button.

Does yours still run stable after deactivating "Debug" ?

Best
Henrik


Do you have myMqtt.subscribe("/Db/" + instanceId + "/" + String(storage.moduleId) + "/Sensor.Parameter1"); in myConnectedCb() function? In first version of program it was missing, and switch did not reconnect after disconnect. Proper function is like this:

void myConnectedCb() {
#ifdef DEBUG
Serial.println("connected to MQTT server");
#endif
myMqtt.subscribe("/Db/" + instanceId + "/" + String(storage.moduleId) + "/Sensor.Parameter1");
}


Hello,
could you post the final code for the switch on iot-playground?? ...
i have the code from the site iot-playground.com/2-uncategorised/77-es...syiot-cloud-mqtt-api - codebender.cc/sketch:184378?referrer=EasyIoT .
the debug function is not working and the switch could not be controlled from internet.

best regards

Please Log in or Create an account to join the conversation.

8 years 9 months ago #2874 by HM
Replied by HM on topic MQTT switch freezing
Hi again

After correcting the code, I have two ESP's running flawless for a couple of days now. Seems as if it works perfectly now.

Thanks again :cheer: :cheer:

Please Log in or Create an account to join the conversation.

8 years 9 months ago - 8 years 9 months ago #2875 by simsonite
Replied by simsonite on topic MQTT switch freezing

shmuck wrote: Hello,
could you post the final code for the switch on iot-playground?? ...
i have the code from the site iot-playground.com/2-uncategorised/77-es...syiot-cloud-mqtt-api - codebender.cc/sketch:184378?referrer=EasyIoT .
the debug function is not working and the switch could not be controlled from internet.

best regards


I have had my ESP online now for a couple of days and it seems fine. THANKS!!! I have a 230v -> 3.3v charger connected to the NodeMCU and a 3.3v relay. The final code is:

#include <ESP8266WiFi.h>
#include <MQTT.h>
#include <EEPROM.h>

//#define DEBUG

#define AP_SSID XXX
#define AP_PASSWORD XXX

#define EIOTCLOUD_USERNAME XXX
#define EIOTCLOUD_PASSWORD XXX

// create MQTT object
#define EIOT_CLOUD_ADDRESS "cloud.iot-playground.com"
MQTT myMqtt("", EIOT_CLOUD_ADDRESS, 1883);

#define CONFIG_START 0
#define CONFIG_VERSION "v01"

struct StoreStruct {
// This is for mere detection if they are your settings
char version[4];
// The variables of your settings
uint moduleId; // module id
bool state; // state
} storage = {
CONFIG_VERSION,
// The default module 0
0,
0 // off
};

bool stepOk = false;
String instanceId = "";
int buttonState;

boolean result;
String topic("");
String valueStr("");

boolean switchState;
const int buttonPin = 0;
const int outPin = 5;
int lastButtonState = LOW;

void loadConfig() {
// To make sure there are settings, and they are YOURS!
// If nothing is found it will use the default settings.
if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] &&
EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] &&
EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2])
for (unsigned int t=0; t<sizeof(storage); t++)
*((char*)&storage + t) = EEPROM.read(CONFIG_START + t);
}

void waitOk()
{
while(!stepOk)
delay(100);

stepOk = false;
}

String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac, 16);
if (i < 5)
result += ':';
}
return result;
}


/*
*
*/

void myConnectedCb() {
#ifdef DEBUG
Serial.println("connected to MQTT server");
#endif
myMqtt.subscribe("/Db/" + instanceId + "/" + String(storage.moduleId) + "/Sensor.Parameter1");
}

void myDisconnectedCb() {
#ifdef DEBUG
Serial.println("disconnected. try to reconnect...");
#endif
delay(500);
myMqtt.connect();
}

void myPublishedCb() {
#ifdef DEBUG
Serial.println("published.");
#endif
}

void myDataCb(String& topic, String& data) {
#ifdef DEBUG
Serial.print(topic);
Serial.print(": ");
Serial.println(data);
#endif
if (topic == String("/Db/InstanceId"))
{
instanceId = data;
stepOk = true;
}
else if (topic == String("/Db/"+instanceId+"/NewModule"))
{
storage.moduleId = data.toInt();
stepOk = true;
}
else if (topic == String("/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1/NewParameter"))
{
stepOk = true;
}
else if (topic == String("/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Settings.Icon1/NewParameter"))
{
stepOk = true;
}
else if (topic == String("/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1"))
{
switchState = (data == String("1"))? true: false;

#ifdef DEBUG
Serial.println("switch state");
Serial.println(switchState);
#endif
}
}

void saveConfig() {
for (unsigned int t=0; t<sizeof(storage); t++)
EEPROM.write(CONFIG_START + t, *((char*)&storage + t));

EEPROM.commit();
}
void setup()
{
#ifdef DEBUG
Serial.begin(115200);
Serial.println("Init...");
#endif
delay(1000);

pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);

EEPROM.begin(512);

switchState = false;

pinMode(BUILTIN_LED, OUTPUT);
//#ifndef DEBUG
pinMode(outPin, OUTPUT);
//#endif
loadConfig();

#ifdef DEBUG
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(AP_SSID);
#endif
WiFi.begin(AP_SSID, AP_PASSWORD);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
#ifdef DEBUG
Serial.print(".");
#endif
}

#ifdef DEBUG
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

Serial.println("Connecting to MQTT server");
#endif
//set client id
// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName;
//clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);
myMqtt.setClientId((char*) clientName.c_str());

#ifdef DEBUG
Serial.print("MQTT client id:");
Serial.println(clientName);
#endif
// setup callbacks
myMqtt.onConnected(myConnectedCb);
myMqtt.onDisconnected(myDisconnectedCb);
myMqtt.onPublished(myPublishedCb);
myMqtt.onData(myDataCb);

//////Serial.println("connect mqtt...");
myMqtt.setUserPwd(EIOTCLOUD_USERNAME, EIOTCLOUD_PASSWORD);
myMqtt.connect();

delay(500);

//get instance id
//////Serial.println("suscribe: Db/InstanceId");
myMqtt.subscribe("/Db/InstanceId");

waitOk();

#ifdef DEBUG
Serial.print("ModuleId: ");
Serial.println(storage.moduleId);
#endif

//create module if necessary
if (storage.moduleId == 0)
{
//create module
#ifdef DEBUG
Serial.println("create module: Db/"+instanceId+"/NewModule");
#endif
myMqtt.subscribe("/Db/"+instanceId+"/NewModule");
waitOk();

// create Sensor.Parameter1
#ifdef DEBUG
Serial.println("/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1/NewParameter");
#endif
myMqtt.subscribe("/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1/NewParameter");
waitOk();

// set module type
#ifdef DEBUG
Serial.println("Set module type");
#endif
valueStr = "MT_DIGITAL_OUTPUT";
topic = "/Db/" + instanceId + "/" + String(storage.moduleId) + "/ModuleType";
result = myMqtt.publish(topic, valueStr);
delay(100);

// save new module id
saveConfig();
}


//publish switch state
valueStr = String(!storage.state);
topic = "/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1";
result = myMqtt.publish(topic, valueStr);

switchState = storage.state;

myMqtt.subscribe("/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1");
}

void loop() {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
#ifdef DEBUG
Serial.print(".");
#endif
}

int reading = digitalRead(buttonPin);

if (reading != lastButtonState) {
if (reading == LOW)
{
switchState = !switchState;
#ifdef DEBUG
Serial.println("button pressed");
#endif
}

valueStr = String(switchState);
topic = "/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1";
result = myMqtt.publish(topic, valueStr);

delay(200);


#ifdef DEBUG
Serial.println("set state: ");
Serial.println(valueStr);
#endif

lastButtonState = reading;
}


digitalWrite(BUILTIN_LED, !switchState);
//#ifndef DEBUG
digitalWrite(outPin, switchState);
//#endif
if (switchState != storage.state)
{
storage.state = switchState;
// save button state
saveConfig();
}
}

Please Log in or Create an account to join the conversation.

8 years 9 months ago - 8 years 9 months ago #2876 by HM
Replied by HM on topic MQTT switch freezing
Sure :)

Be aware that I modyfied the switchcase - I put in some dimming effect, but it is commented out in the code (line 215 to245). Just replace it with the original.


/*
* If module not creates, run EEPROMClear.ino
*/

#include <ESP8266WiFi.h>
#include <MQTT.h>
#include <EEPROM.h>

#define DEBUG

#define AP_SSID "xxxxxxxx"
#define AP_PASSWORD "xxxxxxxx"

#define EIOTCLOUD_USERNAME "xxxxxxxx"
#define EIOTCLOUD_PASSWORD "xxxxxxxx"

// create MQTT object
#define EIOT_CLOUD_ADDRESS "cloud.iot-playground.com"
MQTT myMqtt("", EIOT_CLOUD_ADDRESS, 1883);

#define CONFIG_START 0
#define CONFIG_VERSION "v01"

struct StoreStruct {
// This is for mere detection if they are your settings
char version[4];
// The variables of your settings
uint moduleId; // module id
bool state; // state
} storage = {
CONFIG_VERSION,
// The default module 0
0,
0 // off
};

bool stepOk = false;
String instanceId = "";
int buttonState;

boolean result;
String topic("");
String valueStr("");

boolean switchState;
const int buttonPin = 0;
const int outPin = 2;
int lastButtonState = LOW;

boolean onoff=LOW;

void setup()
{
#ifdef DEBUG
Serial.begin(115200);
#endif
delay(1000);

pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);

// EEPROM.begin(512);

switchState = false;

// pinMode(BUILTIN_LED, OUTPUT);
#ifndef DEBUG
pinMode(outPin, OUTPUT);
#endif
loadConfig();

#ifdef DEBUG
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(AP_SSID);
#endif
WiFi.begin(AP_SSID, AP_PASSWORD);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
#ifdef DEBUG
Serial.print(".");
#endif
}

#ifdef DEBUG
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

Serial.println("Connecting to MQTT server");
#endif
//set client id
// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName;
//clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);
myMqtt.setClientId((char*) clientName.c_str());

#ifdef DEBUG
Serial.print("MQTT client id:");
Serial.println(clientName);
#endif
// setup callbacks
myMqtt.onConnected(myConnectedCb);
myMqtt.onDisconnected(myDisconnectedCb);
myMqtt.onPublished(myPublishedCb);
myMqtt.onData(myDataCb);

//////Serial.println("connect mqtt...");
myMqtt.setUserPwd(EIOTCLOUD_USERNAME, EIOTCLOUD_PASSWORD);
myMqtt.connect();

delay(500);

//get instance id
//////Serial.println("suscribe: Db/InstanceId");
myMqtt.subscribe("/Db/InstanceId");

waitOk();

#ifdef DEBUG
Serial.print("ModuleId: ");
Serial.println(storage.moduleId);
#endif

//create module if necessary
if (storage.moduleId == 0)
{
//create module
#ifdef DEBUG
Serial.println("create module: Db/"+instanceId+"/NewModule");
#endif
myMqtt.subscribe("/Db/"+instanceId+"/NewModule");
waitOk();

// create Sensor.Parameter1
#ifdef DEBUG
Serial.println("/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1/NewParameter");
#endif
myMqtt.subscribe("/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1/NewParameter");
waitOk();

// set module type
#ifdef DEBUG
Serial.println("Set module type");
#endif
valueStr = "MT_DIGITAL_OUTPUT";
topic = "/Db/" + instanceId + "/" + String(storage.moduleId) + "/ModuleType";
result = myMqtt.publish(topic, valueStr);
delay(100);

// save new module id
saveConfig();
}


//publish switch state
valueStr = String(!storage.state);
topic = "/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1";
result = myMqtt.publish(topic, valueStr);

switchState = storage.state;

myMqtt.subscribe("/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1");
}

void loop() {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
#ifdef DEBUG
Serial.print(".");
#endif
}

int reading = digitalRead(buttonPin);

if (reading != lastButtonState) {
if (reading == LOW)
{
switchState = !switchState;
#ifdef DEBUG
Serial.println("button pressed");
#endif
}

valueStr = String(switchState);
topic = "/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1";
result = myMqtt.publish(topic, valueStr);

delay(200);


#ifdef DEBUG
Serial.println("set state: ");
Serial.println(switchState);
Serial.println(valueStr);
#endif

lastButtonState = reading;
}


// digitalWrite(BUILTIN_LED, !switchState);
//#ifndef DEBUG



if ( switchState && !onoff){

analogWrite(outPin,0);
/*
for (int fadeValue = 1024 ; fadeValue >= 0; fadeValue -= 10) {
// sets the value (range from 0 to 255):
analogWrite(outPin, fadeValue);
Serial.println(fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(10);
}
analogWrite(outPin,0);
*/
onoff=HIGH;
}
if (!switchState && onoff){

analogWrite(outPin,1024);
/*
for (int fadeValue = 0 ; fadeValue <= 1024; fadeValue += 10) {
// sets the value (range from 0 to 255):
analogWrite(outPin, fadeValue);
Serial.println(fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(10);
}
analogWrite(outPin,1024);
*/
onoff=LOW;
}

// digitalWrite(outPin, switchState);
//#endif
if (switchState != storage.state)
{
storage.state = switchState;
// save button state
saveConfig();
}
}


void waitOk()
{
while(!stepOk)
delay(100);

stepOk = false;
}

String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac, 16);
if (i < 5)
result += ':';
}
return result;
}


/*
*
*/
void myConnectedCb() {
#ifdef DEBUG
Serial.println("connected to MQTT server");
#endif
myMqtt.subscribe("/Db/" + instanceId + "/" + String(storage.moduleId) + "/Sensor.Parameter1");
}

void myDisconnectedCb() {
#ifdef DEBUG
Serial.println("disconnected. try to reconnect...");
#endif
delay(500);
myMqtt.connect();
}

void myPublishedCb() {
#ifdef DEBUG
Serial.println("published.");
#endif
}

void myDataCb(String& topic, String& data) {
#ifdef DEBUG
Serial.print(topic);
Serial.print(": ");
Serial.println(data);
#endif
if (topic == String("/Db/InstanceId"))
{
instanceId = data;
stepOk = true;
}
else if (topic == String("/Db/"+instanceId+"/NewModule"))
{
storage.moduleId = data.toInt();
stepOk = true;
}
else if (topic == String("/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1/NewParameter"))
{
stepOk = true;
}
else if (topic == String("/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Settings.Icon1/NewParameter"))
{
stepOk = true;
}
else if (topic == String("/Db/"+instanceId+"/"+String(storage.moduleId)+ "/Sensor.Parameter1"))
{
switchState = (data == String("1"))? true: false;

#ifdef DEBUG
Serial.println("switch state");
Serial.println(switchState);
#endif
}
}

void loadConfig() {
EEPROM.begin(512);
// To make sure there are settings, and they are YOURS!
// If nothing is found it will use the default settings.
if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] &&
EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] &&
EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2])
for (unsigned int t=0; t<sizeof(storage); t++)
*((char*)&storage + t) = EEPROM.read(CONFIG_START + t);
EEPROM.end();
}

void saveConfig() {
EEPROM.begin(512);
for (unsigned int t=0; t<sizeof(storage); t++)
EEPROM.write(CONFIG_START + t, *((char*)&storage + t));

EEPROM.commit();
EEPROM.end();
}

Please Log in or Create an account to join the conversation.

8 years 9 months ago #2877 by shmuck
Replied by shmuck on topic MQTT switch freezing
is debug working in this code?

Please Log in or Create an account to join the conversation.

Time to create page: 0.847 seconds

Forum latest

  • No posts to display.