透传点灯实验
853387039@qq.com(讨论 | 贡献)2014年11月5日 (三) 08:59的版本
目的本教程展示了Microduino-Zigbee模块的数据透传。发送端为协调器节点,接收端为路由节点。路由节点的LED灯的亮灭受控于协调器端的开关控制,表征了透传的实现。 设备
实验说明
见下图 程序
//sender coordinator this example
#include <SoftwareSerial.h>
String myString="";
String IEEEAD="";
#define keyON 5
#define keyOFF 6
SoftwareSerial mySerial(2, 3); // RX, TX
void setup()
{
pinMode(keyON,INPUT_PULLUP);
pinMode(keyOFF,INPUT_PULLUP);
mySerial.begin(38400);
}
void loop()
{
while (mySerial.available() > 0)
{
myString += char(mySerial.read());
delay(2);
}
if (myString.length() > 0)
{
if(myString[0]=='+'&&myString[7]==','&&myString[12]==',')
{
for(int i=13;i<myString.length();i++)
IEEEAD+=myString[i];
myString="+ZBD=" +IEEEAD;
while(1)
{
if(digitalRead(keyON)==0)
{
delay(50);
if(digitalRead(keyON)==0)
{
mySerial.print(myString);
mySerial.println("ON");
delay(300);
}
}
if(digitalRead(keyOFF)==0)
{
delay(50);
if(digitalRead(keyOFF)==0)
{
mySerial.print(myString);
mySerial.println("OFF");
delay(300);
}
}
}
}
}
}
程序如下 //sender coordinator this example
String myString="";
String IEEEAD="";
#define keyON 5
#define keyOFF 6
void setup()
{
pinMode(keyON,INPUT_PULLUP);
pinMode(keyOFF,INPUT_PULLUP);
Serial1.begin(38400);
}
void loop()
{
while (Serial1.available() > 0)
{
myString += char(Serial1.read());
delay(2);
}
if (myString.length() > 0)
{
if(myString[0]=='+'&&myString[7]==','&&myString[12]==',')
{
for(int i=13;i<myString.length();i++)
IEEEAD+=myString[i];
myString="+ZBD=" +IEEEAD;
while(1)
{
if(digitalRead(keyON)==0)
{
delay(50);
if(digitalRead(keyON)==0)
{
Serial1.print(myString);
Serial1.println("ON");
delay(300);
}
}
if(digitalRead(keyOFF)==0)
{
delay(50);
if(digitalRead(keyOFF)==0)
{
Serial1.print(myString);
Serial1.println("OFF");
delay(300);
}
}
}
}
}
}
// receiver router this example
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
String myString="";
void setup()
{
pinMode(13,OUTPUT);
mySerial.begin(38400);
}
void loop()
{
while (mySerial.available() > 0)
{
myString += char(mySerial.read());
delay(2);
}
if (myString.length() > 0)
{
if(myString=="ON")
digitalWrite(13,HIGH);
if(myString=="OFF")
digitalWrite(13,LOW);
}
myString="";
}
调试
|