Data separation from arduino

Hello, friends!
English is not my native language, I apologize for the clumsiness. I'm new to programming. The problem is as follows, there is a code for arduino time relay control. I wanted to use it to control the lighting in the aquarium. But I can't separate the data coming from arduino

Arduino_time _relay.txt (7.0 KB)
Viki.aia (232.8 KB)

Here's your sketch, for the board:

The sketch

#include <EEPROM.h>
#include <Wire.h>
#include <DS3231.h>
  DS3231 clock;
  RTCDateTime DateTime;
 
int buff[2];
int i,w,w0,rele=1;
unsigned long times;
 
int hh,mm,ss;
int sost1,sost2,sost3,sost4;
int reg1_hh,reg1_mm,reg2_hh,reg2_mm;
int reg3_hh,reg3_mm,reg4_hh,reg4_mm;
int reg5_hh,reg5_mm,reg6_hh,reg6_mm;
int reg7_hh,reg7_mm,reg8_hh,reg8_mm;

void setup(){ 
   Serial.begin(9600);
   Serial.setTimeout(10);
   Wire.begin(); clock.begin();
 // clock.setDateTime(__DATE__, __TIME__); // Setting the time on the clock based on the sketch compilation time
   pinMode(13, OUTPUT); // relay output
   pinMode(12, OUTPUT); // relay output
   pinMode(11, OUTPUT); // relay output
   pinMode(10, OUTPUT); // relay output
   reg1_hh = EEPROM.read(0);reg1_mm = EEPROM.read(1);reg2_hh = EEPROM.read(2);reg2_mm = EEPROM.read(3);
   reg3_hh = EEPROM.read(4);reg3_mm = EEPROM.read(5);reg4_hh = EEPROM.read(6);reg4_mm = EEPROM.read(7);
   reg5_hh = EEPROM.read(8);reg5_mm = EEPROM.read(9);reg6_hh = EEPROM.read(10);reg6_mm = EEPROM.read(11);
   reg7_hh = EEPROM.read(12);reg7_mm = EEPROM.read(13);reg8_hh = EEPROM.read(14);reg8_mm = EEPROM.read(15);
   Serial.println();
}
 
void loop() {
  DateTime=clock.getDateTime();
 /////// RECEIVING DATA FROM SERIAL PORT /////////////////////////
  while (Serial.available()>0){ 
   for (i=0;i<1;i++) { 
     buff[0] = Serial.parseInt(); 
     buff[1] = Serial.parseInt();}    
     w=1;w0=1;times=millis();}
 
   ///////////// command processing ///////////////////
  if(buff[0]>10&&w==1){ w=0;
//// relay 1 /// time on
  if(buff[0]==101){w0=1;reg1_hh++;if(reg1_hh>23){reg1_hh=0;}}
  if(buff[0]==102){w0=1;reg1_hh--;if(reg1_hh<0){reg1_hh=23;}}
  if(buff[0]==103){w0=1;reg1_mm++;if(reg1_mm>59){reg1_mm=0;}}
  if(buff[0]==104){w0=1;reg1_mm--;if(reg1_mm<0){reg1_mm=59;}}
  //// relay 1 /// off time
  if(buff[0]==105){w0=1;reg2_hh++;if(reg2_hh>23){reg2_hh=0;}}
  if(buff[0]==106){w0=1;reg2_hh--;if(reg2_hh<0){reg2_hh=23;}}
  if(buff[0]==107){w0=1;reg2_mm++;if(reg2_mm>59){reg2_mm=0;}}
  if(buff[0]==108){w0=1;reg2_mm--;if(reg2_mm<0){reg2_mm=59;}}
 
//// relay 2 /// time on
  if(buff[0]==109){w0=1;reg3_hh++;if(reg3_hh>23){reg3_hh=0;}}
  if(buff[0]==110){w0=1;reg3_hh--;if(reg3_hh<0){reg3_hh=23;}}
  if(buff[0]==111){w0=1;reg3_mm++;if(reg3_mm>59){reg3_mm=0;}}
  if(buff[0]==112){w0=1;reg3_mm--;if(reg3_mm<0){reg3_mm=59;}}
  //// relay 2 /// off time
  if(buff[0]==113){w0=1;reg4_hh++;if(reg4_hh>23){reg4_hh=0;}}
  if(buff[0]==114){w0=1;reg4_hh--;if(reg4_hh<0){reg4_hh=23;}}
  if(buff[0]==115){w0=1;reg4_mm++;if(reg4_mm>59){reg4_mm=0;}}
  if(buff[0]==116){w0=1;reg4_mm--;if(reg4_mm<0){reg4_mm=59;}}
 
  //// relay 3 /// time on
  if(buff[0]==117){w0=1;reg5_hh++;if(reg5_hh>23){reg5_hh=0;}}
  if(buff[0]==118){w0=1;reg5_hh--;if(reg5_hh<0){reg5_hh=23;}}
  if(buff[0]==119){w0=1;reg5_mm++;if(reg5_mm>59){reg5_mm=0;}}
  if(buff[0]==120){w0=1;reg5_mm--;if(reg5_mm<0){reg5_mm=59;}}
  //// relay 3 /// off time
  if(buff[0]==121){w0=1;reg6_hh++;if(reg6_hh>23){reg6_hh=24;}}
  if(buff[0]==122){w0=1;reg6_hh--;if(reg6_hh<0){reg6_hh=23;}}
  if(buff[0]==123){w0=1;reg6_mm++;if(reg6_mm>59){reg6_mm=0;}}
  if(buff[0]==124){w0=1;reg6_mm--;if(reg6_mm<0){reg6_mm=59;}}
 
  //// relay  4 ///  time on
  if(buff[0]==125){w0=1;reg7_hh++;if(reg7_hh>23){reg7_hh=0;}}
  if(buff[0]==126){w0=1;reg7_hh--;if(reg7_hh<0){reg7_hh=23;}}
  if(buff[0]==127){w0=1;reg7_mm++;if(reg7_mm>59){reg7_mm=0;}}
  if(buff[0]==128){w0=1;reg7_mm--;if(reg7_mm<0){reg7_mm=59;}}
  //// реле 4 ///  время выкл
  if(buff[0]==129){w0=1;reg8_hh++;if(reg8_hh>23){reg8_hh=0;}}
  if(buff[0]==130){w0=1;reg8_hh--;if(reg8_hh<0){reg8_hh=23;}}
  if(buff[0]==131){w0=1;reg8_mm++;if(reg8_mm>59){reg8_mm=0;}}
  if(buff[0]==132){w0=1;reg8_mm--;if(reg8_mm<0){reg8_mm=59;}}
 
  if(buff[0]==200){w0=1;rele = 1;}
  if(buff[0]==201){w0=1;rele = 2;}
  if(buff[0]==202){w0=1;rele = 3;}
  if(buff[0]==203){w0=1;rele = 4;}
 
  if(buff[0]==301){w0=1;reg1_hh=0,reg1_mm=0,reg2_hh=0,reg2_mm=0;}
  if(buff[0]==302){w0=1;reg3_hh=0,reg3_mm=0,reg4_hh=0,reg4_mm=0;}
  if(buff[0]==303){w0=1;reg5_hh=0,reg5_mm=0,reg6_hh=0,reg6_mm=0;}
  if(buff[0]==304){w0=1;reg7_hh=0,reg7_mm=0,reg8_hh=0,reg8_mm=0;}
     }   
///////////////////////////////////////////////////////////////////
hh = DateTime.hour;
mm = DateTime.minute;
ss = DateTime.second;
 
   /// sending cash to android
   Serial.print(hh/10);Serial.print(hh%10);Serial.print(":");Serial.print(mm/10);Serial.print(mm%10);Serial.print(":");Serial.print(ss/10);Serial.print(ss%10);
 
   Serial.print("::");
   Serial.print(reg1_hh/10);Serial.print(reg1_hh%10);
   Serial.print("::");
   Serial.print(reg1_mm/10);Serial.print(reg1_mm%10);
   Serial.print("::");
   Serial.print(reg2_hh/10);Serial.print(reg2_hh%10);
   Serial.print("::");
   Serial.print(reg2_mm/10);Serial.print(reg2_mm%10);
 
   Serial.print("::");
   Serial.print(reg3_hh/10);Serial.print(reg3_hh%10);
   Serial.print("::");
   Serial.print(reg3_mm/10);Serial.print(reg3_mm%10);
   Serial.print("::");
   Serial.print(reg4_hh/10);Serial.print(reg4_hh%10);
   Serial.print("::");
   Serial.print(reg4_mm/10);Serial.print(reg4_mm%10);
 
   Serial.print("::");
   Serial.print(reg5_hh/10);Serial.print(reg5_hh%10);
   Serial.print("::");
   Serial.print(reg5_mm/10);Serial.print(reg5_mm%10);
   Serial.print("::");
   Serial.print(reg6_hh/10);Serial.print(reg6_hh%10);
   Serial.print("::");
   Serial.print(reg6_mm/10);Serial.print(reg6_mm%10);
 
   Serial.print("::");
   Serial.print(reg7_hh/10);Serial.print(reg7_hh%10);
   Serial.print("::");
   Serial.print(reg7_mm/10);Serial.print(reg7_mm%10);
   Serial.print("::");
   Serial.print(reg8_hh/10);Serial.print(reg8_hh%10);
   Serial.print("::");
   Serial.print(reg8_mm/10);Serial.print(reg8_mm%10);
 
   Serial.print("::");
   Serial.print(rele);
   Serial.print("::");
   Serial.print(sost1);
   Serial.print("::");
   Serial.print(sost2);
   Serial.print("::");
   Serial.print(sost3);
   Serial.print("::");
   Serial.println(sost4);
 
    if(w0==1&&millis()-times>10000){w0=0;
    EEPROM.update(0,reg1_hh);EEPROM.update(1,reg1_mm);EEPROM.update(2,reg2_hh);EEPROM.update(3,reg2_mm);
    EEPROM.update(4,reg3_hh);EEPROM.update(5,reg3_mm);EEPROM.update(6,reg4_hh);EEPROM.update(7,reg4_mm);
    EEPROM.update(8,reg5_hh);EEPROM.update(9,reg5_mm);EEPROM.update(10,reg6_hh);EEPROM.update(11,reg6_mm);
    EEPROM.update(12,reg7_hh);EEPROM.update(13,reg7_mm);EEPROM.update(14,reg8_hh);EEPROM.update(15,reg8_mm);
    }
 
    if(hh*100+mm>=reg1_hh*100+reg1_mm&&hh*100+mm<reg2_hh*100+reg2_mm){digitalWrite(13,HIGH);sost1=1;}else{digitalWrite(13,LOW);sost1=0;}
    if(hh*100+mm>=reg3_hh*100+reg3_mm&&hh*100+mm<reg4_hh*100+reg4_mm){digitalWrite(12,HIGH);sost2=1;}else{digitalWrite(12,LOW);sost2=0;}
    if(hh*100+mm>=reg5_hh*100+reg5_mm&&hh*100+mm<reg6_hh*100+reg6_mm){digitalWrite(11,HIGH);sost3=1;}else{digitalWrite(11,LOW);sost3=0;}
    if(hh*100+mm>=reg7_hh*100+reg7_mm&&hh*100+mm<reg8_hh*100+reg8_mm){digitalWrite(10,HIGH);sost4=1;}else{digitalWrite(10,LOW);sost4=0;}
}

Here's the place where you try to read from BlueTooth:

You made several mistakes:

  • You used a Button Click event instead of a Clock Timer to read incoming BlueTooth
  • In your Clock Timer, you did not check if there was any incoming data (Bytes Available > 0)
  • You expected an instantaneous response to your sent text from the sketch
  • You used a List From CSV Row block against incoming messages that do not contain commas. (CSV = Comma Delimited Values)
  • You did not check if the resulting list had at least 2 items before trying to select items 1 and 2 from it.

Instead of CSV conversion, you could use Split At '::' to match your field delimiters.

Here is standard advice for BlueTooth Delimiters:

Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

Some people send temperature and humidity in separate messages with distinctive prefixes like "t:" (for temperature) and "h:" (for humidity).
(That's YAML format.)

The AI2 Charts component can recognize these and graph them. See Bluetooth Client Polling Rate - #12 by ABG

To receive YAML format messages, test if the incoming message contains ':' . If true, split it at ':' into a list variable, and find the prefix in item 1 and the value in item 2.

...

Thank you so much for your help. Everything worked out. I found your advice.

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