Hi everyone
I’m developing a solution that self-declares its own configuration and sends the data of the connected sensors in json format, through MQTT
The problem I have is that every time the code starts, runs a loop and starts again from the Setup
in practice it comes out of the loop as if it went wrong.
Can anyone help me.
#include <MQTT.h>
#include <ArduinoJson.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <EmonLib.h>
#define RELAY_PIN D7
#define PIR_PIN D1
#define BME_SCK D4
#define BME_MISO D3
#define BME_MOSI D2
#define BME_CS D5
#define SEALEVELPRESSURE_HPA (1013.25)
#define SENSORNUMBER (7)
#define SERSORNAME "Clientino"
void callback(char* topic, byte* payload, unsigned int length);
typedef struct
{
char* sensortype;
char* sensorunit;
char* sensorID;
} sensor_conf;
typedef struct
{
char macStr[19];
sensor_conf sens[SENSORNUMBER];
} tini_conf;
typedef struct
{
String topic;
String message;
} mqtt_message;
mqtt_message mmessage;
tini_conf tini;
byte mac[6];
char macStr[19];
bool pir_state = false;
uint32_t lastTime;
MQTT client("aisens.beiot.cloud", 1883, callback);
Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);
EnergyMonitor emon1;
void Tini_Configuration(){
tini.sens[1].sensortype = "Temperature";
tini.sens[1].sensorunit = "Celsius";
tini.sens[1].sensorID = "00";
tini.sens[2].sensortype = "Pressure";
tini.sens[2].sensorunit = "HPA";
tini.sens[2].sensorID = "00";
tini.sens[3].sensortype = "Altitude";
tini.sens[3].sensorunit = "Meter";
tini.sens[3].sensorID = "00";
tini.sens[4].sensortype = "Humidity";
tini.sens[4].sensorunit = "Rate";
tini.sens[4].sensorID = "00";
tini.sens[5].sensortype = "PIR";
tini.sens[5].sensorunit = "Digital";
tini.sens[5].sensorID = "00";
tini.sens[6].sensortype = "Apparent Power";
tini.sens[6].sensorunit = "Watt";
tini.sens[6].sensorID = "00";
tini.sens[7].sensortype = "Current";
tini.sens[7].sensorunit = "Ampere";
tini.sens[7].sensorID = "00";
tini.sens[8].sensortype = "Relay";
tini.sens[8].sensorunit = "Digital";
tini.sens[8].sensorID = "00";
}
void callback(char* topic, byte* payload, unsigned int length) {
/*char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
String t(topic);
mmessage.message = message;
mmessage.topic = t;*/
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
if (message.equals("RED"))
RGB.color(255, 0, 0);
else if (message.equals("GREEN"))
RGB.color(0, 255, 0);
else if (message.equals("BLUE"))
RGB.color(0, 0, 255);
else
RGB.color(255, 255, 255);
delay(1000);
}
void initcicle(const char* t, const char* u, const char* i) {
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["eventID"] = 1;
root["eventsensorname"] = tini.macStr;
root["eventsensortype"] = t;
root["eventsensorunit"] = u;
root["eventsensorID"] = i;
char buffer[300];
root.prettyPrintTo(buffer, sizeof(buffer));
client.publish("init", buffer);
delay(100);
}
void init (){
int i;
for (i=1; i<=SENSORNUMBER; i++){
initcicle(tini.sens[i].sensortype, tini.sens[i].sensorunit, tini.sens[i].sensorID);
delay(100);
}
}
void publishjson(int id, float value) {
String topic = String(tini.macStr) + "/" + tini.sens[id].sensortype + "/" + tini.sens[id].sensorID;
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["eventsensordatetime"] = millis();
root["eventsensorvalue"] = value;
/*JsonArray& data = root.createNestedArray("data");
data.add(value, 2); // 6 is the number of decimals to print*/
char buffer[200];
root.prettyPrintTo(buffer, sizeof(buffer));
client.publish(topic, buffer);
}
void bme_controller(){
publishjson(1, bme.readTemperature());
delay(100);
publishjson(2, bme.readPressure() / 100.00F);
delay(100);
publishjson(3, bme.readAltitude(SEALEVELPRESSURE_HPA));
delay(100);
publishjson(4, bme.readHumidity());
}
void pir_controller(){
if (digitalRead(PIR_PIN) == HIGH && !pir_state){
publishjson(5, 1);
pir_state = true;
}
else if (digitalRead(PIR_PIN) == LOW && pir_state){
publishjson(5, 0);
pir_state = false;
}
}
void ct_controller(){
double Irms = emon1.calcIrms(1480); // Calculate Irms only
publishjson(6, Irms*230.0); // Apparent power
publishjson(7, Irms); // Irms
delay(100);
}
// setup() runs once, when the device is first turned on.
void setup() {
Particle.syncTime();
Time.zone(1);
//while(!bme.begin());
emon1.current(1, 26.6);
WiFi.macAddress(mac);
snprintf(macStr, sizeof(macStr), "T%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
strcpy (tini.macStr, macStr);
client.connect(SERSORNAME);
Tini_Configuration();
if (client.isConnected()) {
String message6 = Time.timeStr();
char cstr[24];
strcpy(cstr, message6.c_str());
client.publish(String("lasttime/")+tini.macStr, "pluto"); //publish time starts up
client.subscribe(String(tini.macStr) + "/" + tini.sens[8].sensortype + "/" + tini.sens[8].sensorID);
delay(100);
}
init();
pinMode(RELAY_PIN, OUTPUT);
pinMode(PIR_PIN, INPUT);
pinMode(D7, OUTPUT);
lastTime = millis();
if (client.isConnected()) {
client.publish(String("lasttime/")+tini.macStr, "paperina");
}// Put initialization like pinMode and begin functions here.
}
// loop() runs over and over again, as quickly as it can execute.
void loop() {
if (client.isConnected()) {
client.loop();
delay(100);
pir_controller();
if(millis() - lastTime > 15000){
//init();
bme_controller();
delay(100);
ct_controller();
lastTime = millis();
}
delay(100);
}
else
{
if(!client.isConnected()){
client.connect(SERSORNAME);
String message7 = Time.timeStr();
char cstr[24];
strcpy(cstr, message7.c_str());
client.publish(String("lasttime/")+tini.macStr, "pippo"); //publish time starts up
delay(100);
}
}
// The core of your code will likely live here.
}