//这是esp的
#define BLINKER_WIFI
#include <Blinker.h>
#include<SoftwareSerial.h>
SoftwareSerial mySerialA(D5,D6);
String Serial_String="";
char auth[] = "";
char ssid[] = "momo";
char pswd[] = "12345678";
BlinkerButton Button1("btn-gfq");
void button1_callback(const String & state)
{
if (state =="on") {
BLINKER_LOG("Toggle on!");
Button1.icon("icon_1");
Button1.color("#FFFFFF");
Button1.text("开灯");
// Button1.text("Your button name", "describe");
Button1.print("on");
mySerialA.print("Turn on LED");//程序的关键部分,串口通讯,至于你要发送的内容,不要管,只要在nano方面判断就好
}
else if (state =="off") {
BLINKER_LOG("Toggle off!");
Button1.icon("icon_1");
Button1.color("#FFFFFF");
Button1.text("关灯");
// Button1.text("Your button name", "describe");
Button1.print("off");
mySerialA.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);
mySerialA.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
}
void read_string(){
if(mySerialA.available()>0){
Serial_String+=(char)(mySerialA.read());
delay(2);}
}
void loop()
{
Blinker.run();
if(mySerialA.available()>0){
Serial_String="";
read_string();
}
if(Serial_String=="turn off")
{BLINKER_LOG("Button1:","off");
Button1.print("off");}
}
//这是arduino uno的
#include<SoftwareSerial.h>
SoftwareSerial mySerialA(5,6);
String Serial_String="";
int PIRpin=2;
int ledpin=12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
mySerialA.begin(9600);
pinMode(LED_BUILTIN,OUTPUT);
pinMode(PIRpin,INPUT); //人体红外探测
pinMode(ledpin,OUTPUT);
attachInterrupt(0,warning,LOW);
}
void read_string(){
while(mySerialA.available()>0){
Serial_String+=(char)(mySerialA.read());
delay(2);
}
}
void loop() {
// put your main code here, to run repeatedly:
if(mySerialA.available()>0){
Serial_String="";
read_string();
}
Serial.println(Serial_String);
if(Serial_String=="Turn on LED")
{digitalWrite(LED_BUILTIN,HIGH);
digitalWrite(12,HIGH);}
else if(Serial_String=="Turn off LED")
{digitalWrite(LED_BUILTIN,LOW);
digitalWrite(12,LOW);
}
}
//中断函数
void warning(){
Serial.println("turn off");
mySerialA.print("turn off");
digitalWrite(12,LOW);
} |