Hi! I have written code to make my arduino-xbee mesh communicate and exchange input states and set outputs on-off etc.
I need to send a sonar reading and generally a value through my mesh to another arduino! Can you help me? Also, i dont use any library, if you can make suggestions about the code it would be nice!
Here is the code on the sonar arduino.
long previousMillis = 0; //timer for sending the code
long interval = 2000; //timer for sending the code
//-for reading the packet
int readAddress1 = 0;
int readAddress2 = 0;
int readAddress3 = 0;
int readAddress4 = 0;
int readValue1 = 0;
int readValue2 = 0;
int readValue3 = 0;
int readValue4 = 0;
int readValue5 = 0;
int readValue6 = 0;
int readValue7 = 0;
int readValue8 = 0;
int readValue9 = 0;
int readValueA = 0;
//-for the sonar
const int pingPin = 8;
const int echoPin = 9;
//-inputs and outputs
const int irrigation = 5;
const int relay1 = 6;
int irrigationstate = 0;
int relay1state = 0;
//for data transmission
char irr;
char lvl;
char re1;
char re2;
char tank;
void setup() {
Serial.begin(9600);
//----------------------SONAR-------------------------------------------------------
pinMode(echoPin, INPUT);
pinMode(pingPin, OUTPUT);
//----------------------SONAR-------------------------------------------------------
//----------------------INPUTS-------------------------------------------------------
pinMode(irrigation, INPUT);
//----------------------INPUTS-------------------------------------------------------
//----------------------RELAYS-------------------------------------------------------
pinMode(relay1, OUTPUT);
//----------------------RELAYS-------------------------------------------------------
} //END VOID.SETUP
void loop() {
//-read the incoming packet---------------------------------
if (Serial.available() > 25) {
if (Serial.read() == 0x7E) {
byte discard1 = Serial.read();
byte discard2 = Serial.read();
byte discard3 = Serial.read();
byte discard4 = Serial.read();
byte discard5 = Serial.read();
byte discard6 = Serial.read();
byte discard7 = Serial.read();
byte discard8 = Serial.read();
readAddress1 = Serial.read();
readAddress2 = Serial.read();
readAddress3 = Serial.read();
readAddress4 = Serial.read();
byte discard9 = Serial.read();
byte discard10 = Serial.read();
byte discard11 = Serial.read();
readValue1 = Serial.read();
readValue2 = Serial.read();
readValue3 = Serial.read();
readValue4 = Serial.read();
readValue5 = Serial.read();
readValue6 = Serial.read();
readValue7 = Serial.read();
readValue8 = Serial.read();
readValue9 = Serial.read();
readValueA = Serial.read();
if (readValue1 == 32 && readValue3 == 32 && readValue5 == 32 && readValue7 == 32 && readValue9 == 32) {
//if the packet is from base do:
//-----base----------------------------------------------------------------------
if (readAddress1 == 64 && readAddress2 == 176 && readAddress3 == 20 && readAddress4 == 176) {
if (readValue2 = 0x31) {
digitalWrite(relay1, HIGH);
} else if (readValue2 = 0x30){
digitalWrite(relay1, LOW);
}
}
//if the packet is from another station do:
//-----Station1---------------------------------------------------------------------------
else if (readAddress1 == 64 && readAddress2 == 178 && readAddress3 == 145 && readAddress4 == 241) {
}
}
}
} //end receive
//sensor------------------------------------------------------------------------------------------------
long duration,cm;
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration); //THAT CM I NEED TO SEND<----------------------------------
//sensor-----------------------------------------------------------------------------------------------
//SET THE DATA TO SEND-----------------
//------------------------------------------------------------------------------------------------------
irrigationstate = digitalRead(irrigation);
if (irrigationstate == HIGH) {
// Serial.println("irrigation started");
irr = 0x31;
} else {
// Serial.println("irrigation is off");
irr = 0x30;
}
//-------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
relay1state = digitalRead(relay1);
if (relay1state == HIGH) {
re1 = 0x31;
} else if (relay1state == LOW) {
re1 = 0x30;
}
//-------------------------------------------------------------------------------------------------------
//--SEND DATA EVERY ___SECS------------------------------------------------------------------------------
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
sendDatatoBase(irr, 0x30, re1, 0x30, 0x30);
sendDatatoStation(lvl, 0x30, 0x30, 0x30, 0x30);
} //END SEND
} //END VOID.LOOP
//FOR SONAR TO CALCULATE CMS
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
//TO CREATE THE PACKET TO SEND TO BASE
void sendDatatoBase(int ONE, int TWO, int THREE, int FOUR, int FIVE) {
Serial.write(0x7E);
Serial.write(0x00);
Serial.write(0x18);
Serial.write(0x10);
Serial.write(0x01);
Serial.write(0x00);
Serial.write(0x13);
Serial.write(0xA2);
Serial.write(0x00);
Serial.write(0x40);
Serial.write(0xFD);
Serial.write(0x78);
Serial.write(0x00);
Serial.write(0xFF);
Serial.write(0xFE);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x20);
Serial.write(ONE);
Serial.write(0x20);
Serial.write(TWO);
Serial.write(0x20);
Serial.write(THREE);
Serial.write(0x20);
Serial.write(FOUR);
Serial.write(0x20);
Serial.write(FIVE);
long sum = 0x10 + 0x01 + 0x00 + 0x13 + 0xA2 + 0x00 + 0x40 + 0xFD + 0x78 + 0x00 + 0xFF + 0xFE + 0x00 + 0x00 +0x20 + ONE + 0x20 + TWO + 0x20 + THREE + 0x20 + FOUR + 0x20 + FIVE;
Serial.write( 0xFF - ( sum & 0xFF) );
}
void sendDatatoStation(int PONE, int PTWO, int PTHREE, int PFOUR, int PFIVE) {
Serial.write(0x7E);
Serial.write(0x00);
Serial.write(0x18);
Serial.write(0x10);
Serial.write(0x01);
Serial.write(0x00);
Serial.write(0x13);
Serial.write(0xA2);
Serial.write(0x00);
Serial.write(0x40);
Serial.write(0x00);
Serial.write(0x4D);
Serial.write(0x2F);
Serial.write(0xFF);
Serial.write(0xFE);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x20);
Serial.write(PONE);
Serial.write(0x20);
Serial.write(PTWO);
Serial.write(0x20);
Serial.write(PTHREE);
Serial.write(0x20);
Serial.write(PFOUR);
Serial.write(0x20);
Serial.write(PFIVE);
long sum = 0x10 + 0x01 + 0x00 + 0x13 + 0xA2 + 0x00 + 0x40 + 0x00 + 0x4D + 0x2F + 0xFF + 0xFE + 0x00 + 0x00 +0x20 + PONE + 0x20 + PTWO + 0x20 + PTHREE + 0x20 + PFOUR + 0x20 + PFIVE;
Serial.write( 0xFF - ( sum & 0xFF) );
}
As you can see i send data like that sendDatatoStation(lvl, 0x30, 0x30, 0x30, 0x30);
And the arduino that receives the data reads the data readValue1 = Serial.read();
can i send the cm value that i need? And how will i read it on the other arduino?
And some xbee-arduino questions. When i send a packet to Base and then to the station, is this the right way to do it? Should i insert a delay?
The arduinos are unos and megas, and the xbees are series 2 in api mode 2.