ATK-esp8266加arduino nano

智能

 1 #include <SoftwareSerial.h>
 2 
 3 
 4 int switchCircuit = 6;//定义switch引脚为6继电器开关
 5 SoftwareSerial mySerial(10 ,11);   //RX ,TX   软串口
 6 #define acs712 A0
 7 
 8 float vpp = 0.0048828125;
 9 float sensitivity = 0.066;
10 //char a=' ';//变量a用来存储数值
11 unsigned int Flag_val = 0;
12 unsigned int threshHold = 50;
13 char val = ' ';
14 
15 
16 void setup() {
17 
18 pinMode(switchCircuit,OUTPUT);//设定switch引脚为输出状态
19 pinMode(acs712,INPUT);
20 Serial.begin(115200);
21 mySerial.begin(115200);
22 //pwm_v = 0;
23 }
24 
25 void loop()
26 {
27   int counts = analogRead(acs712)+1;
28   float voltage = counts*vpp;
29   voltage -=2.5;// take away 2.5 v
30   Serial.println("voltage: " + String(voltage));
31   float amperage = voltage/sensitivity;
32   Serial.println("amperage: "+String(amperage));
33 
34   if ( Flag_val < threshHold )
35   {   
36     if (voltage > 1)
37     {
38       Flag_val = 0;
39 //      mySerial.print('v');    //v表示正在充电 ->8266
40     }
41     else 
42     {
43       Flag_val++;
44     }
45     digitalWrite(switchCircuit,LOW);
46   }
47   else 
48   {
49 //    Flag_val++;
50       digitalWrite(switchCircuit,HIGH);
51       mySerial.print('z');   //'z'表示无电流 并断电->8266
52       Serial.print("off");
53   }
54   
55   if (mySerial.available())
56   {
57     delay(100);
58     val = mySerial.read();
59     Serial.println(val);
60     if (val == 'x')           //x表示通电 ->arduino
61     {
62       Flag_val = 0;
63 //      val = ' ';
64     }
65     if (val == 'c')           //c表示断电 ->arduino
66     {
67       Flag_val = threshHold;
68 //      val = ' ';
69     }
70   }
71   
72   Serial.print(Flag_val);
73   delay(500);
74 
75 }

烧录时io需接地

 1 #define BLINKER_WIFI
 2 
 3 #include <EspSaveCrash.h>
 4 #include <Blinker.h>
 5 
 6 char auth[] = "a8b39f6a97a5";
 7 char ssid[] = "mi8";
 8 char pswd[] = "123456789";
 9 
10 // 新建组件对象
11 BlinkerButton Button1("btn-abc");
12 BlinkerButton Button2("btn-abb");
13 BlinkerButton Button3("btn-acc");
14 BlinkerNumber Number1("num-abc");
15 
16 int counter = 0;
17 char Flag1 = 'x';
18 char  Flag2 = 'c';
19 char *Flag3 = "x";
20 char Flag_switch = ' ';
21 
22 // 按下按键即会执行该函数
23 void button1_callback(const String & state)
24 {
25 
26       Blinker.print("on");
27       Serial.print(Flag1);
28    
29 
30 }
31 
32 void button2_callback(const String & state)
33 {
34 
35       Blinker.print("on2");
36       Serial.print(Flag2);
37    
38 
39 }
40 
41 void button3_callback(const String & state)
42 {
43 
44       Blinker.print("on3");
45       Serial.write(*Flag3);
46    
47 
48 }
49 
50 
51 void setup()
52 {
53     // 初始化串口
54     Serial.begin(115200);
55 
56     
57     Blinker.begin(auth, ssid, pswd);
58     //Blinker.attachData(dataRead);
59 
60     Button1.attach(button1_callback);
61     Button2.attach(button2_callback);
62     Button3.attach(button3_callback);
63 }
64 
65 void loop() {
66     Blinker.run();
67     if (Serial.available())
68     {
69       delay(100);
70       Flag_switch = Serial.read();
71       if (Flag_switch == 'z')
72       {
73         Blinker.print("fully charged");
74 
75       }
76       if (Flag_switch == 'v')
77       {
78         Blinker.print("charging");
79       }
80     }
81     
82     
83 }
原文地址:https://www.cnblogs.com/miaorn/p/14189369.html