1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| #define BLINKER_WIFI #define BLINKER_MIOT_OUTLET
#include <Blinker.h> #include<ESP8266WiFi.h> #include<Servo.h> #include<ESP8266HTTPClient.h> #include<ESP8266httpUpdate.h> #include<ESP8266WebServer.h>
char auth[] = "blinker-auth-key"; char ssid[] = "PDCN"; char passwd[] = "wifi-password";
ESP8266HTTPUpdate httpUpdate; ESP8266WebServer webServer(80);
bool updateState = false; bool oState = false; Servo servo; int pos = 0;
BlinkerButton btn1 = BlinkerButton("btn1");
void doAction(const String & state) {
BLINKER_LOG("doAction : ", state); if(state == BLINKER_CMD_ON) { BLINKER_LOG("开关 开了"); } else if(state == BLINKER_CMD_OFF) { BLINKER_LOG("开关 关了");
for (pos = 90; pos >= 0; pos --) { servo.write(pos); delay(15); } delay(200); for (pos = 0; pos <= 90; pos ++) { servo.write(pos); delay(15); } delay(200); } } void miotPowerState(const String & state) { BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) { digitalWrite(LED_BUILTIN, LOW); doAction(state); BlinkerMIOT.powerState("on"); BlinkerMIOT.print();
oState = true; } else if (state == BLINKER_CMD_OFF) { digitalWrite(LED_BUILTIN, HIGH); doAction(state); BlinkerMIOT.powerState("off"); BlinkerMIOT.print(); oState = false; } }
void miotQuery(int32_t queryCode) { BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode) { case BLINKER_CMD_QUERY_ALL_NUMBER : BLINKER_LOG("MIOT Query All"); BlinkerMIOT.powerState(oState ? "on" : "off"); BlinkerMIOT.print(); break; case BLINKER_CMD_QUERY_POWERSTATE_NUMBER : BLINKER_LOG("MIOT Query Power State"); BlinkerMIOT.powerState(oState ? "on" : "off"); BlinkerMIOT.print(); break; default : BlinkerMIOT.powerState(oState ? "on" : "off"); BlinkerMIOT.print(); break; } }
void dataRead(const String & data) { BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate(); uint32_t BlinkerTime = millis(); Blinker.print("millis", BlinkerTime); }
void update_started() { BLINKER_LOG("8266 update start"); }
void update_finished() { BLINKER_LOG("8266 update finished"); } void update_process(int cur, int total) { BLINKER_LOG("update process at ", cur, " of ", total, " bytes"); }
void update_error(int err) { BLINKER_LOG("update error code ", err); }
void update(String url) {
WiFi.mode(WIFI_STA); WiFi.begin(ssid, passwd); while(WiFi.status() != WL_CONNECTED) { delay(500); }
WiFiClient client; httpUpdate.onStart(update_started); httpUpdate.onEnd(update_finished); httpUpdate.onError(update_error); httpUpdate.onProgress(update_process);
t_httpUpdate_return ret = httpUpdate.update(client, url); switch(ret) { case HTTP_UPDATE_FAILED: BLINKER_LOG("update failed ", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str()); break; case HTTP_UPDATE_NO_UPDATES: BLINKER_LOG("update result no updates"); break; case HTTP_UPDATE_OK: BLINKER_LOG("update ok"); break; } }
void handleUpdate() { webServer.send(200, "text/plain", "ok"); String url = webServer.arg("url"); Serial.print("url: " + url); update(url); }
void handleRoot() { webServer.send(200, "text/html;charset=utf-8", "<form action='/update'><input name='url' type='input'></input> <input type='submit'> submit</input></form>"); }
void setup() { Serial.begin(115200); BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH);
servo.attach(D0);
Blinker.begin(auth, ssid, passwd); Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState); BlinkerMIOT.attachQuery(miotQuery); doAction("off"); webServer.begin(); webServer.on("/", handleRoot); webServer.on("/update", handleUpdate); }
void loop() { Blinker.run(); webServer.handleClient(); }
|