|
arduino端的代码:
[pre]#include<SoftwareSerial.h>
SoftwareSerial mySerial(5,6);
String Serial_String="";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
mySerial.begin(115200);
pinMode(3,OUTPUT);
}
void read_string(){
while(mySerial.available()>0){
Serial_String+=(char)(mySerial.read());
delay(2);}
}
void loop() {
// put your main code here, to run repeatedly:
if(mySerial.available()>0){
Serial_String="";
read_string();
}
Serial.println(Serial_String);
delay (1000);
if(Serial_String=="Turn on LED")
digitalWrite(3,HIGH);
else if(Serial_String=="Turn off LED")
digitalWrite(3,LOW);
}[/pre]
ESP8266端的代码
[pre]#define BLINKER_WIFI
#include <Blinker.h>
#include<SoftwareSerial.h>
SoftwareSerial mySerial(13,15);
char auth[] = "f785232a72e5";
char ssid[] = "TP-LINK_67C596";
char pswd[] = "07938208201";
#define BUTTON_1 "ABC"
BlinkerButton Button1(BUTTON_1);
void button1_callback(const String & state)
{
if (state == BLINKER_CMD_ON) {
BLINKER_LOG("Toggle on!");
Button1.icon("icon_1");
Button1.color("#FFFFFF");
Button1.text("Your button name or describe");
// Button1.text("Your button name", "describe");
Button1.print("on");
mySerial.print("Turn on LED");//程序的关键部分,串口通讯,至于你要发送的内容,不要管,只要在nano方面判断就好
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("Toggle off!");
Button1.icon("icon_1");
Button1.color("#FFFFFF");
Button1.text("Your button name or describe");
// Button1.text("Your button name", "describe");
Button1.print("off");
mySerial.print("Turn off LED");//程序的关键部分,串口通讯,至于你要发送的内容,不要管,只要在nano方面判断就好
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print(BlinkerTime);
Blinker.print("millis", BlinkerTime);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
mySerial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
}
void loop()
{
Blinker.run();
}[/pre]
设计思路就是ESP8266收到按钮的指令后,通过软串口向Arduino发送Turn on LED,在Adruino端识别指令控制灯点亮。
在串口监视器中反馈的数据竟然是 Toggle on! Toggle off! |
|