Relay won't stay on "Bool not staying true"

I am running this code and I can't keep the relay on? It will blink but not stay LOW or HIGH when I sent the payload. All the Values change but then change back in seconds.

I am a little new but trying to figure this out.

#include <painlessMesh.h>
#include <ArduinoJson.h>

// mesh credentials has to be same for all!
#define   MESH_PREFIX     "whateverYouLike"
#define   MESH_PASSWORD   "somethingSneaky"
#define   MESH_PORT       5555

#define button 26
#define AWP_51 23
#define ABP_51 22
#define AYP_51 19
#define ARP_51 18
#define AGP_51 21
#define AAP_51 25

bool BS_51 = 1;
bool AW_51 = 1;
bool AR_51 = 1;
bool AB_51 = 1;
bool AY_51 = 1;
bool AG_51 = 1;
bool AA_51 = 1;
Scheduler userScheduler; // to control your personal task
painlessMesh  mesh;


// User stub
void sendMessage() ; // Prototype so PlatformIO doesn't complain

Task taskSendMessage( TASK_SECOND * 3 , TASK_FOREVER, &sendMessage );

void sendMessage()
{
  // Serializing in JSON Format
  DynamicJsonDocument doc(192);
  //BS_51= digitalRead(button);
  doc["BS51"] = BS_51;
  doc["RS51"] = AR_51;
  doc["YS51"] = AY_51;
  doc["GS51"] = AG_51;
  doc["AB51"] = AB_51;
  doc["AW51"] = AW_51;
  doc["AA51"] = AA_51;

  String msg ;
  serializeJson(doc, msg);
  mesh.sendBroadcast(msg);
  Serial.println(msg);
  //taskSendMessage.setInterval((TASK_SECOND * 10));
}

void receivedCallback( uint32_t from, String &msg )
{
  //Deserializing
  String json;
  DynamicJsonDocument doc(192);
  json = msg.c_str();
  DeserializationError error = deserializeJson(doc, json);

  if (error)
  {
    Serial.print("Failed Deserializing ... ");
    Serial.println(error.c_str());
  }
  AW_51 = doc["AS51"];
  digitalWrite ( AW_51, AWP_51);
  AB_51 = doc["BS51"];
  digitalWrite( ABP_51, AB_51);
  AR_51 = doc["RS51"];
  digitalWrite( ARP_51, AR_51);
  AY_51 = doc["YS51"];
  digitalWrite( AYP_51, AY_51);
  AG_51 = doc["GS51"];
  digitalWrite( AGP_51, AG_51);
  AA_51 = doc["AS51"];
  digitalWrite( AAP_51, AA_51);

}
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 setup() {
  Serial.begin(115200);
  pinMode(button, INPUT_PULLUP);
  pinMode(AWP_51, OUTPUT);
  pinMode(ABP_51, OUTPUT);
  pinMode(ARP_51, OUTPUT);
  pinMode(AYP_51, OUTPUT);
  pinMode(AGP_51, OUTPUT);
  pinMode(AAP_51, OUTPUT);

  digitalWrite(AWP_51, HIGH);
  digitalWrite(ABP_51, HIGH);
  digitalWrite(ARP_51, HIGH);
  digitalWrite(AYP_51, HIGH);
  digitalWrite(AGP_51, HIGH);
  digitalWrite(AAP_51, HIGH);

  mesh.setDebugMsgTypes( ERROR | STARTUP );  // set before init() so that you can see startup messages
  mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT );
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);

  userScheduler.addTask( taskSendMessage );
  taskSendMessage.enable();
}

void loop() {

  mesh.update();
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Corrected in top post, sorry.

What relay? Several times a week, someone posts a similar problem, it's always a current surge from the relay turn-on, resetting the CPU and restarting the program.

The values even switch in serial monitor with no relay connected after a few moments. The values are not sticking?

Do the serial values switch with the relays connected?

Yes serial values switch with the relays connected and with out the relays connected, but quickly switch back. I can also see the same behavior in node red with the mqtt from debug node.

You aren't handling deserialization errors correctly - you print a message but then go and blindly set the bool's from the corrupt json anyway - don't do that!

You have an obvious typo here:

which is the wrong way round from all the other calls to digitalWrite.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.