this code receives a data from server(I tested the received data with UDPSENDRECEIVESTRING, it has no problem)
the received data is something like this "No1;No2;No3;No4;No5:01"
the code should change digital pins on XBee(s) with ID=No1,No2...
And based on 00 or 10 or 01 ... at the end of the received data it should decide to change the pin D1 or D3 or turn off all the XBee in the network. (as it is shown in the code)
but it doesn't work and turns some XBee(s) randomly!!!
can anyone find the problem please?
Here is the code(because the question became too long I had to delete extra void functions at the end)
#include <Ethernet2.h> //Load Ethernet Library
#include <EthernetUdp2.h> //Load UDP Library
#include <SPI.h> //Load the SPI Library
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE}; //Assign a mac address
IPAddress ip(192, 168, 100, 90); //Assign my IP adress
unsigned int localPort = 5555; //Assign a Port to talk over
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
String MsgData; //String for our data
int packetSize; //Size of Packet
EthernetUDP Udp; //Define UDP Object
void setup() {
Serial.begin(9600); //Turn on Serial Port
Ethernet.begin(mac, ip); //Initialize Ethernet
Udp.begin(localPort); //Initialize Udp
delay(1500); //delay
}
void loop() {
packetSize = Udp.parsePacket(); //Read theh packetSize
if(packetSize>0) { //Check to see if a request is present
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); //Reading the data request on the Udp
String MsgData(packetBuffer); //Convert packetBuffer array to string datReq
Serial.println(MsgData);
int No1 = MsgData.indexOf("No1");
int No2 = MsgData.indexOf("No2");
int No3 = MsgData.indexOf("No3");
int No4 = MsgData.indexOf("No4");
int No5 = MsgData.indexOf("No5");
int cri = MsgData.indexOf("00");
int med = MsgData.indexOf("01");
int m20 = MsgData.indexOf("10");
int m40 = MsgData.indexOf("20");
int m60 = MsgData.indexOf("60");
int reset = MsgData.indexOf("40");
while (reset >= 0){
OffAllAlarm(0x4);
OffAllLight(0x4);
}
while ((cri >= 0) || (med >= 0))
{
if(No1 >= 0)
{
Alarm1(0x5);
}
if(No2 >= 0)
{
Alarm2(0x5);
}
if(No3 >= 0)
{
Alarm3(0x5);
}
if(No4 >= 0)
{
Alarm4(0x5);
}
if(No5 >= 0)
{
Alarm5(0x5);
}
delay(10000);
Alarm1(0x4);
Alarm2(0x4);
Alarm3(0x4);
Alarm4(0x4);
Alarm5(0x4);
}
while ((m20 >= 0) || (m40 >= 0) || (m60 >= 0))
{
if(No1 >= 0)
{
Light1(0x5);
}
if(No2 >= 0)
{
Light2(0x5);
}
if(No3 >= 0)
{
Light3(0x5);
}
if(No4 >= 0)
{
Light4(0x5);
}
if(No5 >= 0)
{
Light5(0x5);
}
delay(10000);
Light1(0x4);
Light2(0x4);
Light3(0x4);
Light4(0x4);
Light5(0x4);
}
}
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
}
void OffAllAlarm(char value){
Serial.write(0x7E); //sync byte
Serial.write(0x00); //length
Serial.write(0x10);
Serial.write(0x17); //command type, 17 is AT
Serial.write(0x00); //command sequence number(???)
Serial.write(0x00); //destination serial number (64bit)
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0xFF);
Serial.write(0xFF);
Serial.write(0xFF); //destination network address(???)
Serial.write(0xFE);
Serial.write(0x02); //remote command options(???)
Serial.write('D'); //ASCII command name!!!
Serial.write('1');
Serial.write(value); //command
long sum = 0x17 + (0x00 + 0x13 + 0xA2 + 0x00 + 0x40 + 0xE4 + 0x2D + 0xC0) + 0xFF + 0xFE + 0x02 + 'D' + '1' + value;
Serial.write(0xFF - (sum & 0xFF)); //checksum from byte4 to previous one
}
void OffAllLight(char value){
Serial.write(0x7E); //sync byte
Serial.write(0x00); //length
Serial.write(0x10);
Serial.write(0x17); //command type, 17 is AT
Serial.write(0x00); //command sequence number(???)
Serial.write(0x00); //destination serial number (64bit)
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0xFF);
Serial.write(0xFF);
Serial.write(0xFF); //destination network address(???)
Serial.write(0xFE);
Serial.write(0x02); //remote command options(???)
Serial.write('D'); //ASCII command name!!!
Serial.write('3');
Serial.write(value); //command
long sum = 0x17 + (0x00 + 0x13 + 0xA2 + 0x00 + 0x40 + 0xE4 + 0x2D + 0xC0) + 0xFF + 0xFE + 0x02 + 'D' + '1' + value;
Serial.write(0xFF - (sum & 0xFF)); //checksum from byte4 to previous one
}
void Alarm1(char value){
Serial.write(0x7E); //sync byte
Serial.write(0x00); //length
Serial.write(0x10);
Serial.write(0x17); //command type, 17 is AT
Serial.write(0x00); //command sequence number(???)
Serial.write(0x00); //destination serial number (64bit)
Serial.write(0x13);
Serial.write(0xA2);
Serial.write(0x00);
Serial.write(0x40);
Serial.write(0xE4);
Serial.write(0x2D);
Serial.write(0xC0);
Serial.write(0xFF); //destination network address(???)
Serial.write(0xFE);
Serial.write(0x02); //remote command options(???)
Serial.write('D'); //ASCII command name!!!
Serial.write('1');
Serial.write(value); //command
long sum = 0x17 + (0x00 + 0x13 + 0xA2 + 0x00 + 0x40 + 0xE4 + 0x2D + 0xC0) + 0xFF + 0xFE + 0x02 + 'D' + '1' + value;
Serial.write(0xFF - (sum & 0xFF)); //checksum from byte4 to previous one
}
void Alarm2(char value){
}
void Alarm3(char value){
}
void Alarm4(char value){
}
void Alarm5(char value){
}
void Light1(char value){
Serial.write(0x7E); //sync byte
Serial.write(0x00); //length
Serial.write(0x10);
Serial.write(0x17); //command type, 17 is AT
Serial.write(0x00); //command sequence number(???)
Serial.write(0x00); //destination serial number (64bit)
Serial.write(0x13);
Serial.write(0xA2);
Serial.write(0x00);
Serial.write(0x40);
Serial.write(0xE4);
Serial.write(0x2D);
Serial.write(0xC0);
Serial.write(0xFF); //destination network address(???)
Serial.write(0xFE);
Serial.write(0x02); //remote command options(???)
Serial.write('D'); //ASCII command name!!!
Serial.write('3');
Serial.write(value); //command
long sum = 0x17 + (0x00 + 0x13 + 0xA2 + 0x00 + 0x40 + 0xC2 + 0x7D + 0xBF) + 0xFF + 0xFE + 0x02 + 'D' + '3' + value;
Serial.write(0xFF - (sum & 0xFF)); //checksum from byte4 to previous one
}
void Light2(char value){
}
void Light3(char value){
}
void Light4(char value){
}
void Light5(char value){
}