组网点灯实验
目的本教程使用Microduino-Zigbee组网结合Microduino-Core+,协调器控制四个节点LED灯,同时每个节点也能够控制协调器上的led灯,实现组网无线控制。 设备
将模块叠加在Plug上,再将按键接在A6上,D6,D7,D8,D9,D10分别接一个led灯
我们将搭建四组节点,但是只用一个Microduino-USBTTL/zh模块,因此你需要先叠加Core+、Zigbee 和USBTTL,下载完程序再拔下USBTTL,再叠加在Microduino-Cube-S1上通过USB数据线供电,再用USBTTL给别节点下载程序。 程序
#define led_pin_en 6
int led_pin[4]={
7,8,9,10};
#define key_pin A6
boolean led_power_01,led_power_02,led_power_03;
String led_switch_0="led_off";
String led_switch_1="led_on";
String myStringSerial1="";
String MacString="";
String MacString_rec="";
String myString="";
String StyleString="";
String myString_01[4]={
"","","",""};
String myString_02[4]={
"","","",""};
int num_01,num_02,num_03;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
pinMode(led_pin_en,OUTPUT);
for(int i=0;i<4;i++)
{
pinMode(led_pin[i],OUTPUT);
}
pinMode(key_pin,INPUT_PULLUP);
for(int i=0;i<4;i++)
{
digitalWrite(led_pin[i],LOW);
}
}
void loop() {
int sensorValue = analogRead(key_pin);
//Serial.println(sensorValue);
while (Serial1.available() > 0)
{
myStringSerial1 += char(Serial1.read());
delay(2);
}
if (myStringSerial1.length() > 20&&myStringSerial1[0]=='+'&&myStringSerial1[7]==','&&myStringSerial1[12]==',')
{
StyleString="";
MacString="";
for(int i=5;i<7;i++)
StyleString+=myStringSerial1[i];
for(int i=8;i<13;i++)
MacString+=myStringSerial1[i];
if(StyleString=="01")
{
num_01++;
myString_01[num_01-1]=MacString;
}
else if(StyleString=="02")
{
num_02++;
myString_02[num_02-1]=MacString;
}
digitalWrite(led_pin_en,HIGH);
delay(150);
digitalWrite(led_pin_en,LOW);
delay(150);
digitalWrite(led_pin_en,HIGH);
delay(150);
digitalWrite(led_pin_en,LOW);
/*
for(int i=0;i<num_01;i++)
{
Serial.print("myString_01[");
Serial.print(i);
Serial.print("]:");
Serial.println(myString_01[i]);
if(i==num_01-1)
Serial.println("/------------------------/");
}
for(int i=0;i<num_02;i++)
{
Serial.print("myString_02[");
Serial.print(i);
Serial.print("]:");
Serial.println(myString_02[i]);
if(i==num_02-1)
Serial.println("/------------------------/");
}
*/
}
else if(myStringSerial1.length()>0&&myStringSerial1.length() <20&&myStringSerial1[0]=='+'&&myStringSerial1[4]=='='&&myStringSerial1[9]==',')
{
MacString_rec="";
myString="";
for(int i=5;i<10;i++)
MacString_rec+=myStringSerial1[i];
for(int i=10;i<myStringSerial1.length()-2;i++)
myString+=myStringSerial1[i];
Serial.println(myString);
}
myStringSerial1="";
for(int i=0;i<num_02;i++)
{
if(MacString_rec==myString_02[i])
{
if(myString=="led_on")
digitalWrite(led_pin[i],HIGH);
else if(myString=="led_off")
digitalWrite(led_pin[i],LOW);
}
}
if(sensorValue<100)
{
led_power_01=!led_power_01;
for(int i=0;i<num_02;i++)
{
if(led_power_01==true)
Serial1.println("+ZBD="+myString_02[i]+led_switch_1);
else if(led_power_01==false)
Serial1.println("+ZBD="+myString_02[i]+led_switch_0);
delay(300);
}
}
}
#define led_pin 6
#define key_pin A6
boolean led_power;
String led_switch_0="led_off";
String led_switch_1="led_on";
String myStringSerial1="";
String myString="";
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
pinMode(led_pin,OUTPUT);
pinMode(key_pin,INPUT_PULLUP);
}
void loop() // run over and over
{
int sensorValue = analogRead(key_pin);
//Serial.println(sensorValue);
while (Serial1.available() > 0)
{
myStringSerial1 += char(Serial1.read());
delay(2);
}
if (myStringSerial1.length() > 0)
{
for(int i=10;i<myStringSerial1.length()-2;i++)
myString+=myStringSerial1[i];
Serial.println(myString);
}
if(myString=="led_on")
digitalWrite(led_pin,HIGH);
else if(myString=="led_off")
digitalWrite(led_pin,LOW);
if(sensorValue<500)
{
delay(300);
led_power=!led_power;
if(led_power==true)
Serial1.print(led_switch_0);
else
Serial1.print(led_switch_1);
}
myStringSerial1="";
myString="";
delay(100);
}
调试
按下按键,可以看到四个节点的led灯按照匹配的顺序依次亮起,每按一次按键,led状态切换一次。
按下S1底板上右边的按钮,可以对应控制协调器上的led。
注意
|