透传点灯实验
Bdharduino@126.com(讨论 | 贡献)2014年10月29日 (三) 15:51的版本 (Created page with "Microduino-Zigbee透传点灯实验 {| style="width: 800px;" |- | ==目的== 本教程展示了Microduino-Zigbee模块的数据透传。发送端为协调器节点,接收端...")
Microduino-Zigbee透传点灯实验
目的本教程展示了Microduino-Zigbee模块的数据透传。发送端为协调器节点,接收端为路由节点。路由节点的LED灯的亮灭受控于协调器端的开关控制,表征了透传的实现。 设备
=实验说明
一根导线的一头接GND,另一头甩出。用甩出的一头去触碰D5为灯开指令,触碰D6为灯关指令。
LED与电阻串接与GND与D13之间,需注意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(9600);
}
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(9600);
}
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(9600);
}
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="";
}
调试
|