Manage message with many arduinos

Hi everybody,

I'm come back to you again.

To secure my project i want to manage my messages.

I use 2 arduinos with different name 1=CAL, 2=BAL,

On my sketch to send message by serial my messages start by CAL* or BAL*.

So all message cal(star) need to go arduino CAL and bal(star) got to arduino BAL.
The forum can't take the symbol star ?

You can find my sketchs for arduinos and my node.
ok 1112.json (25.6 KB)

Sketch calendar :

/*
Typical pin layout used:

           MFRC522      Arduino
           Reader/PCD   Uno/101

Signal Pin Pin

RST/Reset RST 9
SPI SS SDA(SS) 10
SPI MOSI MOSI 11
SPI MISO MISO 12
SPI SCK SCK 13

*/

//DECLARATION DES VARIABLES
#include <SPI.h>
#include <MFRC522.h>

#define RelaiOuverture 7 // Pin 7 pour la ventouse magnétique
#define RelaiLeds 8 // Pin 8 pour le relai des LEDS
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above

String command;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
int phase = 0;
int on_position = 0;
const byte uids[4][7] = {
{0x04, 0xfe, 0x36, 0xba, 0x6d, 0x67, 0x80},
{0x04, 0x9f, 0x36, 0xba, 0x6d, 0x67, 0x80},
{0x04, 0xa5, 0x36, 0xba, 0x6d, 0x67, 0x80},
{0x04, 0xfe, 0x36, 0xba, 0x6d, 0x67, 0x80},
};
bool stringComplete = false;

MFRC522 mfrc522(SS_PIN, RST_PIN);

//DECLARATION DE LA CONFIGURATION INITIALE
void setup() {
Serial.begin(57600); // Configuration de la vitesse du port série 57600 bauds
SPI.begin(); // Initialisation du bus SPI
mfrc522.PCD_Init(); // Initialisation du capteur MFRC522 (RFID)
digitalWrite(RelaiOuverture, HIGH); // Pin 7 doit est être alimentée
pinMode(RelaiLeds, OUTPUT); // Pin 8 est une sortie

delay(2000); // Temps d'attente de l'initialisation 2 secondes

}

void reset_game() {
phase = 0;
on_position = 0;
}

void sendMessage(String mesg) {
char charBuf[50];
mesg.toCharArray(charBuf, 50);
Serial.println(mesg);

}

void loop() {

if (Serial.available()) {
command = Serial.readStringUntil('');
command.trim();
if (command.equals("CAL"
"LEDOPEN")) {
digitalWrite(RelaiLeds, HIGH);
digitalWrite(RelaiOuverture, LOW);
}
else if (command.equals("CAL"*"LEDCLOSE")) {
digitalWrite(RelaiLeds, LOW);
digitalWrite(RelaiOuverture, HIGH);
}
else {
Serial.print("Mauvais ordre");
}
Serial.print("Command: ");
Serial.println(command);
}

delay(10);
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// A new card is present : read it
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}

// for phase 0 to 3 read cards & go to next phase :
if (phase <= 3) {
if ( (memcmp(uids[phase], mfrc522.uid.uidByte, 7) == 0) /and (horaire == HIGH)/) {
switch (on_position) {
case 0:
previousMillis = millis();
sendMessage("CALPHASE_" + String(phase) + "_" + String(on_position));
on_position = 1;
break;
case 1:
currentMillis = millis();
if (currentMillis > previousMillis + 1000) {
sendMessage("CAL
PHASE_" + String(phase) + "_" + String(on_position));
phase++;
on_position = 0;
}
break;
}
}
return;
}

sendMessage("CALPLAYROCK");
sendMessage("CAL
FINISH");
reset_game();
}

Sketch Pierres:

#include "HX711.h"

//DECLARATION DES VARIABLES
int MINLOAD = 30; //Sensibilité de la pression à travailler

String message = "";
String command;
bool stringComplete = false;

//CLASS SCALE - Programmation des balances
class _Scale {
private :
public :
HX711 scale;
int D;
int S;
long zero;

_Scale(int D, int S) {
  this->D = D;
  this->S = S;
  this->scale.begin(D, S);

  this->scale.read();
  this->zero = this->scale.read();
  this->scale.tare();
}

int getData() {
  long reading = 0;
  if (scale.is_ready()) {
    reading = scale.read() - this->zero;
    reading = int((90000 - reading) / 10000);
  }
  return (reading);
}

};
//FIN CLASS SCALE

//BRANCHEMENT DES BALANCES ET LECTURE DE DONNEES
_Scale scales[6] = {
_Scale(2, 3),
_Scale(4, 5),
_Scale(6, 7),
_Scale(8, 9),
_Scale(10, 11),
_Scale(12, 13)
};
//FIN BRANCHEMENT BALANCE ET LECTURE DE DONNEES

void sendMessage(String mesg) {
char charBuf[50];
mesg.toCharArray(charBuf, 50);
Serial.println(mesg);

}

void setup() {
Serial.begin(57600);
}

void loop() {
if (stringComplete) {
// manage message here :
message.trim();
if (message.substring(0, 6) == "BALMIN") {
MINLOAD = message.substring(7).toInt();
}

//REset stuff :
stringComplete = false;
message = "";

}

int nbp = 0;
String bals = "";
for (int i = 0; i < 6; i++) {
int p = scales[i].getData();
bals = bals + "|" + String(p);
if (p > MINLOAD) {
nbp += 1;
}
delay(10);
}
delay(1000);
sendMessage("BALT" + String(nbp));
sendMessage("BAL
B" + String(bals));

}

void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
message += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}

So i need to your help for make this. Thanks in advance !

NOTE : I can't show my "" on my code so it's CAL""LEDOPEN, CAL"*"LEDCLOSE without quotes.

Rather than use equal in the switch node, try contains CAL or regex ^CAL

Thanks to your reply E1cid.

I tried your solution but is not work.

In fact, i think my issue come to my sketch. Because in the IDE when i type CAL"*"LEDOPEN i've :
Bad command : CAL
Bad command : LEDOPEN

Maybe i need to make something in my sketch no ?

Sorry miss read the switch you had already split the topic off. Sorry can not help with Arduino sketch. It may help if you supply the data from the serial in nodes. So others have an idea of the data going through the flow

OK, no problem.

In fact, my message is Split well because at Last debug i've "ledopen" only. But the message is not send to the arduino. Arduino don't know what is ledopen.

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