I am currently trying to make my Arduino and my pyboard communicate with two XBEE series 2 modules.
The context of my project is to control 2 relays and monitorise some data coming from different sensors. There will be an Arduino that will concatenate all this data and send it via an XBEE in AT mode to the pyboard with another XBEE module.
My frame is composed of: "D" + datax + "," + datay + "," + ....
I separated my data by comas because I used a second arduino to receive the data and I used the serial.parseInt() method to parse the data. I putted a "D" character at the begining in order to use serial.find("D") method to detect a new message.
I have achieved to make the pyboard receive part of the messages from the Arduino but since there is only de uart.recv method, there is no parsing or serial.readline method. So for example if I send a message with the Serial.println method in my Arduino, the pyboard will receive the frame but will add "/n" to the message and wil not recognise that there is a new line that is equivalent to a new message.
I will try to make a function that reads the frame until "/n" appears but I wanted to submit the problem in order to see if there are better performing ideas.
You can find attached my arduino code (the importatn part is in bold characters". Just to explain, the XBEE is connected to the Serial1 in the arduno Mega.
The pyboard code side there is just an uart.init() and uart .recv() method to print what it receives (using the REPL tool).
Thank you for your support.
Code: Select all
//Relay configuration
int relay1=3;
int relay2=4;
int armRelays=11;
int relayRadioInput1=12;
int relayRadioInput2=13;
int armRelaySig=7;
int relay1Sig = 8;
int relay2Sig = 9;
int relay1SignalPinM1;
int relay2SignalPinM1;
int relayRadioSignal1;
int relayRadioSignal2;
int armRelaysSignal;
//Temparature Sensor DHT
#include "DHT.h"
#define DHTPIN A0
#define DHTTYPE DHT11
int t;
int h;
DHT dht(DHTPIN, DHTTYPE);
//Data transmission
String c = ",";
String d= "D";
int r1Out;
int r2Out;
String data2;
void setup(){
Serial.begin(9600);
//Relay configuration
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
pinMode(armRelays,OUTPUT);
pinMode(armRelaySig,INPUT);
pinMode(relay1Sig,INPUT);
pinMode(relay2Sig,INPUT);
pinMode(relayRadioInput1,INPUT);
pinMode(relayRadioInput2,INPUT);
digitalWrite(armRelays,LOW);
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
//Temperature sensor
dht.begin();
//Data transmission
Serial1.begin(9600);
}
void loop(){
//Relay configuration
armRelaysSignal =digitalRead(armRelaySig);
relay1SignalPinM1 = digitalRead(relay1Sig);
relay2SignalPinM1 = digitalRead(relay2Sig);
relayRadioSignal1= digitalRead(relayRadioInput1);
relayRadioSignal2= digitalRead(relayRadioInput2);
if (armRelaysSignal==HIGH){
digitalWrite(armRelays,HIGH);
if((relay1SignalPinM1==HIGH&& armRelaysSignal==HIGH) || (relayRadioSignal1==HIGH&& armRelaysSignal==HIGH)){
digitalWrite(relay1,HIGH);
} else {
digitalWrite(relay1,LOW);
}
if((relay2SignalPinM1==HIGH&& armRelaysSignal==HIGH) || (relayRadioSignal2==HIGH&& armRelaysSignal==HIGH) ){
digitalWrite(relay2,HIGH);
}else{
digitalWrite(relay2,LOW);
}
} else {
digitalWrite(armRelays,LOW);
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
}
//Temperature sensor
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to A0 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h))
{
Serial.println("Failed to read from DHT");
}
else
{
}
//Sensor serial output
r1Out = digitalRead(relay1);
r2Out = digitalRead(relay2);
[b]data2 = d + armRelaysSignal + c + relay1SignalPinM1 + c + relay2SignalPinM1 + c + relayRadioSignal1 + c + relayRadioSignal2 + c +r1Out + c + r2Out + c + t + c + h;
Serial1.println(data2); //OK to be replaced by data2[/b]
delay(50);
Serial.print("Arm Sig=");
Serial.print(digitalRead(armRelaySig));
Serial.print(" Relay1Sig=");
Serial.print(digitalRead(relay1Sig));
Serial.print(" Relay2Sig=");
Serial.print(digitalRead(relay2Sig));
Serial.print(" Relay1RadioSig=");
Serial.print(digitalRead(relayRadioInput1));
Serial.print(" Relay2RadioSig=");
Serial.print(digitalRead(relayRadioInput2));
Serial.print(" Relay1State=");
Serial.print(digitalRead(relay1));
Serial.print(" Relay2State=");
Serial.print(digitalRead(relay2));
Serial.print(" Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print(" Temperature: ");
Serial.print(t);
Serial.println(" *C");
}