My code crashes after I publish a command via mqtt. It is 5 scales that are added up to make the total weight of my bed. It works till I send the command to change calibration_factor or tare the scales. Any help would be appreciated. This uses a Mega and ESP8266
ESP Code (scroll down for Mega code )
#include <Arduino.h>
#include "EspMQTTClient.h"
unsigned long timer;
String weight1 , weight2 , weight3 , weight4 , weight5 , weight1cal , weight2cal , weight3cal , weight4cal , weight5cal;
EspMQTTClient client(
"ssid",
"password",
"000.000.00.000", // MQTT Broker server ip
"MbedScale", // Client name that uniquely identify your device
1883 // The MQTT port, default to 1883. this line can be omitted
);
void setup(){
Serial.begin(57600,SERIAL_8N1);
Serial.println("Hello");
Serial1.begin(57600,SERIAL_8N1);
Serial1.println("Hello");
delay(24000);
// Optional functionalities of EspMQTTClient
client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overridded with enableHTTPWebUpdater("user", "password").
client.enableOTA(); // Enable OTA (Over The Air) updates. Password defaults to MQTTPassword. Port is the default OTA port. Can be overridden with enableOTA("password", port).
}
// This function is called once everything is connected (Wifi and MQTT)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished(){
// Subscribe to "mytopic/test" and display received message to Serial
client.subscribe("home/mbedroomscale/remote", [](const String & payload) {
Serial1.println(payload);
});
}
void loop() {
client.loop();
if (Serial.available()) {
String s = Serial.readString();
weight1 = s.substring(0 , s.indexOf(","));
s.remove(0 , s.indexOf(",") + 1);
weight2 = s.substring(0 , s.indexOf(","));
s.remove(0 , s.indexOf(",") + 1);
weight3 = s.substring(0 , s.indexOf(","));
s.remove(0 , s.indexOf(",") + 1);
weight4 = s.substring(0 , s.indexOf(","));
s.remove(0 , s.indexOf(",") + 1);
weight5 = s.substring(0 , s.indexOf(","));
s.remove(0 , s.indexOf(",") + 1);
weight1cal = s.substring(0 , s.indexOf(","));
s.remove(0 , s.indexOf(",") + 1);
weight2cal = s.substring(0 , s.indexOf(","));
s.remove(0 , s.indexOf(",") + 1);
weight3cal = s.substring(0 , s.indexOf(","));
s.remove(0 , s.indexOf(",") + 1);
weight4cal = s.substring(0 , s.indexOf(","));
s.remove(0 , s.indexOf(",") + 1);
weight5cal = s.substring(0 , s.indexOf(","));
s.remove(0 , s.indexOf(",") + 1);
if (millis() - timer > 5000) {
char buf1[10];
char buf2[10];
char buf3[10];
char buf4[10];
char buf5[10];
char buf6[10];
char buf7[10];
char buf8[10];
char buf9[10];
char buf10[10];
weight1.toCharArray(buf1 , weight1.length() + 1);
weight2.toCharArray(buf2 , weight2.length() + 1);
weight3.toCharArray(buf3 , weight3.length() + 1);
weight4.toCharArray(buf4 , weight4.length() + 1);
weight5.toCharArray(buf5 , weight5.length() + 1);
weight1cal.toCharArray(buf6 , weight1cal.length() + 1);
weight2cal.toCharArray(buf7 , weight2cal.length() + 1);
weight3cal.toCharArray(buf8 , weight3cal.length() + 1);
weight4cal.toCharArray(buf9 , weight4cal.length() + 1);
weight5cal.toCharArray(buf10 , weight5cal.length() + 1);
client.publish("home/mbedroomscale/bed/lr", buf1);
client.publish("home/mbedroomscale/bed/ll", buf2);
client.publish("home/mbedroomscale/bed/ur", buf4);
client.publish("home/mbedroomscale/bed/ul", buf5);
client.publish("home/mbedroomscale/bed/center", buf3);
client.publish("home/mbedroomscale/bed/lrcal", buf6);
client.publish("home/mbedroomscale/bed/llcal", buf7);
client.publish("home/mbedroomscale/bed/urcal", buf9);
client.publish("home/mbedroomscale/bed/ulcal", buf10);
client.publish("home/mbedroomscale/bed/centercal", buf8);
}
}
}
void callback(char* topic , byte* payload, unsigned int length) {
/// remote tare and send to arduino to tare scales
if (strcmp(topic, "tare") == 0) {
Serial1.write("tare");
//scale 1 remote calibrate using mqtt server
if (strcmp(topic, "1cal1up") == 0)
Serial1.write("1cal1up");
else if (strcmp(topic, "1cal1down") == 0)
Serial1.write("1cal1down");
if (strcmp(topic, "1cal10up") == 0)
Serial1.write("1cal10up");
else if (strcmp(topic, "1cal10down") == 0)
Serial1.write("1cal10down");
if (strcmp(topic, "1cal100up") == 0)
Serial1.write("1cal100up");
else if (strcmp(topic, "1cal100down") == 0)
Serial1.write("1cal100down");
if (strcmp(topic, "1cal1000up") == 0)
Serial1.write("1cal1000up");
else if (strcmp(topic, "1cal1000down") == 0)
Serial1.write("1cal1000down");
//scale 2
if (strcmp(topic, "2cal1up") == 0)
Serial1.write("2cal1up");
else if (strcmp(topic, "2cal1down") == 0)
Serial1.write("2cal1down");
if (strcmp(topic, "2cal10up") == 0)
Serial1.write("2cal10up");
else if (strcmp(topic, "2cal10down") == 0)
Serial1.write("2cal10down");
if (strcmp(topic, "2cal100up") == 0)
Serial1.write("2cal100up");
else if (strcmp(topic, "2cal100down") == 0)
Serial1.write("2cal100down");
if (strcmp(topic, "2cal1000up") == 0)
Serial1.write("2cal1000up");
else if (strcmp(topic, "2cal1000down") == 0)
Serial1.write("2cal1000down");
//scale 3
if (strcmp(topic, "3cal1up") == 0)
Serial1.write("3cal1up");
else if (strcmp(topic, "3cal1down") == 0)
Serial1.write("3cal1down");
if (strcmp(topic, "3cal10up") == 0)
Serial1.write("3cal10up");
else if (strcmp(topic, "3cal10down") == 0)
Serial1.write("3cal10down");
if (strcmp(topic, "3cal100up") == 0)
Serial1.write("3cal100up");
else if (strcmp(topic, "3cal100down") == 0)
Serial1.write("3cal100down");
if (strcmp(topic, "3cal1000up") == 0)
Serial1.write("3cal1000up");
else if (strcmp(topic, "3cal1000down") == 0)
Serial1.write("3cal1000down");
// scale 4
if (strcmp(topic, "4cal1up") == 0)
Serial1.write("4cal1up");
else if (strcmp(topic, "4cal1down") == 0)
Serial1.write("4cal1down");
if (strcmp(topic, "4cal10up") == 0)
Serial1.write("4cal10up");
else if (strcmp(topic, "4cal10down") == 0)
Serial1.write("4cal10down");
if (strcmp(topic, "4cal100up") == 0)
Serial1.write("4cal100up");
else if (strcmp(topic, "4cal100down") == 0)
Serial1.write("4cal100down");
if (strcmp(topic, "4cal1000up") == 0)
Serial1.write("4cal1000up");
else if (strcmp(topic, "4cal1000down") == 0)
Serial1.write("4cal1000down");
// scale 5
if (strcmp(topic, "5cal1up") == 0)
Serial1.write("5cal1up");
else if (strcmp(topic, "5cal1down") == 0)
Serial1.write("5cal1down");
if (strcmp(topic, "5cal10up") == 0)
Serial1.write("5cal10up");
else if (strcmp(topic, "5cal10down") == 0)
Serial1.write("5cal10down");
if (strcmp(topic, "5cal100up") == 0)
Serial1.write("5cal100up");
else if (strcmp(topic, "5cal100down") == 0)
Serial1.write("5cal100down");
if (strcmp(topic, "5cal1000up") == 0)
Serial1.write("5cal1000up");
else if (strcmp(topic, "5cal1000down") == 0)
Serial1.write("5cal1000down");
}
}
Mega code
if (Serial3.available()) {
data = Serial3.readString();
}
// tare
if (data.indexOf("tare") > -1) { // remote tare from esp to arduino
scale1.tare();
scale2.tare();
scale3.tare();
scale4.tare();
scale5.tare();
}
/// remote calibrations from esp to arduino
// scale 1 - 1up or down
if (data.indexOf("1cal1up") > -1)
calibration_factor1 += 1;
else if(data.indexOf("1cal1down") > -1)
calibration_factor1 = calibration_factor1 - 1;
// scale 1 - 10up or down
if (data.indexOf("1cal10up") > -1)
calibration_factor1 += 10;
else if(data.indexOf("1cal10down") > -1)
calibration_factor1 -= 10;
// scale 1 - 100up or down
if (data.indexOf("1cal100up") > -1)
calibration_factor1 += 100;
else if(data.indexOf("1cal100down") > -1)
calibration_factor1 -= 100;
// scale 1 - 1000up or down
if (data.indexOf("1cal1000up") > -1)
calibration_factor1 += 1000;
else if(data.indexOf("1cal1000down") > -1)
calibration_factor1 -= 1000;
// scale 2 - 1up or down
if (data.indexOf("2cal1up") > -1)
calibration_factor2 += 1;
else if(data.indexOf("2cal1down") > -1)
calibration_factor2 -= 1;
// scale 2 - 10up or down
if (data.indexOf("2cal10up") > -1)
calibration_factor2 += 10;
else if(data.indexOf("2cal10down") > -1)
calibration_factor2 -= 10;
// scale 2 - 100up or down
if (data.indexOf("2cal100up") > -1)
calibration_factor2 += 100;
else if(data.indexOf("2cal100down") > -1)
calibration_factor2 -= 100;
// scale 2 - 1000up or down
if (data.indexOf("2cal1000up") > -1)
calibration_factor2 += 1000;
else if(data.indexOf("2cal1000down") > -1)
calibration_factor2 -= 1000;
// scale 3 - 1up or down
if (data.indexOf("3cal1up") > -1)
calibration_factor3 += 1;
else if(data.indexOf("3cal1down") > -1)
calibration_factor3 -= 1;
// scale 3 - 10up or down
if (data.indexOf("3cal10up") > -1)
calibration_factor3 += 10;
else if(data.indexOf("3cal10down") > -1)
calibration_factor3 -= 10;
// scale 3 - 100up or down
if (data.indexOf("3cal100up") > -1)
calibration_factor3 += 100;
else if(data.indexOf("3cal100down") > -1)
calibration_factor3 -= 100;
// scale 3 - 1000up or down
if (data.indexOf("3cal1000up") > -1)
calibration_factor3 += 1000;
else if(data.indexOf("3cal1000down") > -1)
calibration_factor3 -= 1000;
// scale 4 - 1up or down
if (data.indexOf("4cal1up") > -1)
calibration_factor4 += 1;
else if(data.indexOf("4cal1down") > -1)
calibration_factor4 -= 1;
// scale 4 - 10up or down
if (data.indexOf("4cal10up") > -1)
calibration_factor4 += 10;
else if(data.indexOf("4cal10down") > -1)
calibration_factor4 -= 10;
// scale 4 - 100up or down
if (data.indexOf("4cal100up") > -1)
calibration_factor4 += 100;
else if(data.indexOf("4cal100down") > -1)
calibration_factor4 -= 100;
// scale 4 - 1000up or down
if (data.indexOf("4cal1000up") > -1)
calibration_factor4 += 1000;
else if(data.indexOf("4cal1000down") > -1)
calibration_factor4 -= 1000;
// scale 5 - 1up or down
if (data.indexOf("5cal1up") > -1)
calibration_factor5 += 1;
else if(data.indexOf("5cal1down") > -1)
calibration_factor5 -= 1;
// scale 5 - 10up or down
if (data.indexOf("5cal10up") > -1)
calibration_factor5 += 10;
else if(data.indexOf("5cal10down") > -1)
calibration_factor5 -= 10;
// scale 5 - 100up or down
if (data.indexOf("5cal100up") > -1)
calibration_factor5 += 100;
else if(data.indexOf("5cal100down") > -1)
calibration_factor5 -= 100;
// scale 5 - 1000up or down
if (data.indexOf("5cal1000up") > -1)
calibration_factor5 += 1000;
else if(data.indexOf("5cal1000down") > -1)
calibration_factor5 -= 1000;
}