Ciao a tutti,
uso delle schede con wemos d1 mini in casa per leggere temperatura e umidità, accendere le luci, sono collegate tra loro tramite rete mesh con la libreria painlessmesh, ma ho un problema, ogni tanto accendo la luce sia tramite pulsante che da remoto, la scheda si riavvia, non riesco a capire cosa può avere, inserisco il codice, se a qualcuno viene qualche idea, inserisco anche lo schema della scheda, grazie a tutti
//************************************************************
// read temperature and humidity "camera" lolin D1 R2
//
// rele D7 -- 13
// AHT
// pulsante D5 -- 14
//
//************************************************************
#include "Button2.h"
#include <AHTxx.h>
float h,t;
AHTxx aht20(AHTXX_ADDRESS_X38, AHT2x_SENSOR);
#define BUTTON_1 D5
const byte relay_pin = D7;
bool relayState = LOW;
unsigned long now;
Button2 btn1(BUTTON_1);
//#include "painlessMesh.h"
#include "namedMesh.h"
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
Scheduler userScheduler; // to control your personal task
//painlessMesh mesh;
namedMesh mesh;
String msg,rel;
String nodeName = "camera"; // Name needs to be unique
String to = "bridgemqtt";
uint32_t root_id;
#define ROLE "camera"
#define VERSION "Camera v3.0.5"
#define MESSAGE "camera "
// User stub
void sendMessage() ; // Prototype so PlatformIO doesn't complain
void sendMessage1();
Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );
Task taskSendMessage1( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage1 );
void sendMessage() {
read_AHT();
msg = "temperatura/";
msg += t;
mesh.sendSingle(to, msg);
msg = "umidita/";
msg += h;
mesh.sendSingle(to, msg);
taskSendMessage.setInterval( random( TASK_SECOND * 300, TASK_SECOND * 400 ));
}
void sendMessage1() {
update_status();
taskSendMessage1.setInterval( random( TASK_SECOND * 450, TASK_SECOND * 550 ));
}
// Needed for painless library
void receivedCallback( uint32_t from, String &msg ) {
Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
rel=msg;
root_id=from;
if (strcmp(rel.c_str(),"1") == 0){
digitalWrite(relay_pin, HIGH);
relayState = HIGH;
msg = "output/";
msg += "1";
mesh.sendSingle(to, msg);
}
else if (strcmp(rel.c_str(),"0") == 0) {
digitalWrite(relay_pin, LOW);
relayState = LOW;
msg = "output/";
msg += "0";
mesh.sendSingle(to, msg);
}}
void newConnectionCallback(uint32_t nodeId) {
Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}
void changedConnectionCallback() {
Serial.printf("Changed connections\n");
}
void nodeTimeAdjustedCallback(int32_t offset) {
Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
}
void read_AHT() {
h = aht20.readHumidity();
t = aht20.readTemperature();
if (isnan(h) || isnan(t)) {
msg = "error/";
msg += "FailedtoreadfromAHTsensor";
mesh.sendSingle(to, msg);
return;
}}
void button_setup()
{
btn1.setPressedHandler([](Button2 & b)
{
relayState = !relayState;
digitalWrite(relay_pin,relayState);
if (relayState == HIGH){
msg = "output/";
msg += "1";
mesh.sendSingle(to, msg);
}
else if (relayState == LOW){
msg = "output/";
msg += "0";
mesh.sendSingle(to, msg);
}
});
}
void update_status()
{
long uptime = millis() / 60000L;
msg = "uptime/";
msg += uptime;
mesh.sendSingle(to, msg);
msg = "nodeid/";
msg += mesh.getNodeId();
mesh.sendSingle(to, msg);
msg = "freememory/";
msg += String(ESP.getFreeHeap());
mesh.sendSingle(to, msg);
msg = "version/";
msg += VERSION;
mesh.sendSingle(to, msg);
msg = "root/";
msg += root_id;
mesh.sendSingle(to, msg);
msg = "ip/";
msg += WiFi.localIP().toString();
mesh.sendSingle(to, msg);
msg = "wifisignal/";
msg += String(WiFi.RSSI());
mesh.sendSingle(to, msg);
}
void setup() {
Serial.begin(115200);
aht20.begin();
pinMode(relay_pin, OUTPUT);
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
mesh.setDebugMsgTypes( ERROR | STARTUP | DEBUG ); // set before init() so that you can see startup messages
mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT, WIFI_AP_STA, 11 );
mesh.initOTAReceive(ROLE);
mesh.setContainsRoot(true);
mesh.setName(nodeName);
mesh.onReceive(&receivedCallback);
mesh.onNewConnection(&newConnectionCallback);
mesh.onChangedConnections(&changedConnectionCallback);
mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
userScheduler.addTask( taskSendMessage );
userScheduler.addTask( taskSendMessage1 );
taskSendMessage.enable();
taskSendMessage1.enable();
button_setup();
}
void loop() {
// it will run the user scheduler as well
mesh.update();
btn1.loop();
if((millis() - now) > 600000) { //600000 - 10 min
now=millis();
if(!(mesh.isConnected(root_id))){
//Serial.printf("Reboot\n");
ESP.restart();
}}
}
Schema_V2.pdf (69.2 KB)