# Manage message with many arduinos

**URL:** <https://discourse.nodered.org/t/manage-message-with-many-arduinos/55075>\
**Category:** General\
**Created:** [12 December 2021 11:14 UTC](https://discourse.nodered.org/t/manage-message-with-many-arduinos/55075 "2021-12-12T11:14:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![azertym](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/azertym/32/52108_2.png) [@azertym](https://discourse.nodered.org/u/azertym)\
**Post date:** [12 December 2021 11:14 UTC](https://discourse.nodered.org/t/manage-message-with-many-arduinos/55075/1 "2021-12-12T11:14:25Z")

</div>

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](https://discourse.nodered.org/uploads/short-url/9NQEdDZEkQ9KWhJsJUJnSDfZjah.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("CAL_PHASE\_" + 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("CAL_PLAYROCK");  
> 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("BAL_T" + 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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 December 2021 11:33 UTC](https://discourse.nodered.org/t/manage-message-with-many-arduinos/55075/2 "2021-12-12T11:33:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![azertym](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/azertym/32/52108_2.png) [@azertym](https://discourse.nodered.org/u/azertym)\
**Post date:** [12 December 2021 11:45 UTC](https://discourse.nodered.org/t/manage-message-with-many-arduinos/55075/3 "2021-12-12T11:45:32Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 December 2021 11:58 UTC](https://discourse.nodered.org/t/manage-message-with-many-arduinos/55075/4 "2021-12-12T11:58:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![azertym](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/azertym/32/52108_2.png) [@azertym](https://discourse.nodered.org/u/azertym)\
**Post date:** [12 December 2021 12:24 UTC](https://discourse.nodered.org/t/manage-message-with-many-arduinos/55075/5 "2021-12-12T12:24:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 February 2022 12:25 UTC](https://discourse.nodered.org/t/manage-message-with-many-arduinos/55075/6 "2022-02-10T12:25:00Z")

</div>

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