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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
#define BLINKER_WIFI #define BLINKER_MIOT_MULTI_OUTLET #define BLINKER_PRINT Serial #define BLINKER_WITHOUT_SSL #include <Blinker.h> char auth[] = "46e64f4684b9";
char ssid[] = "@Hyatt_WiFi"; char pswd[] = "";
#define Relay_1 D1 #define Relay_2 D2 #define Relay_3 D3 #define Relay_4 D4
BlinkerButton Button("MainSwitch"); BlinkerButton Button1("Switch1"); BlinkerButton Button2("Switch2"); BlinkerButton Button3("Switch3"); BlinkerButton Button4("Switch4"); BlinkerButton Button5("Refresh");
bool oState[5] = { false }; #define OUTLET_ALL 0 #define OUTLET_NO_1 1 #define OUTLET_NO_2 2 #define OUTLET_NO_3 3 #define OUTLET_NO_4 4
void RelayState(int num) { switch(num) { case 1: if(digitalRead(Relay_1)==LOW) { Button1.color("#FF0000"); Button1.text("插座1开"); Button1.print("on"); oState[1] = true; } else if(digitalRead(Relay_1==HIGH)) { Button1.color("#808080"); Button1.text("插座1关"); Button1.print("off"); oState[1] = false; } break; case 2: if(digitalRead(Relay_2)==LOW) { Button2.color("#FF0000"); Button2.text("插座2开"); Button2.print("on"); oState[2] = true; } else if(digitalRead(Relay_2==HIGH)) { Button2.color("#808080"); Button2.text("插座2关"); Button2.print("off"); oState[2] = false; } break; case 3: if(digitalRead(Relay_3)==LOW) { Button3.color("#FF0000"); Button3.text("插座3开"); Button3.print("on"); oState[3] = true; } else if(digitalRead(Relay_3==HIGH)) { Button3.color("#808080"); Button3.text("插座3关"); Button3.print("off"); oState[3] = false; } break; case 4: if(digitalRead(Relay_4)==LOW) { Button4.color("#FF0000"); Button4.text("插座4开"); Button4.print("on"); oState[4] = true; } else if(digitalRead(Relay_4==HIGH)) { Button4.color("#808080"); Button4.text("插座4关"); Button4.print("off"); oState[4] = false; } break; default: break; } }
void ctrl_multi_outlet(uint8_t num, uint8_t io_level) { switch (num) { case 0: digitalWrite(Relay_1, io_level); digitalWrite(Relay_2, io_level); digitalWrite(Relay_3, io_level); digitalWrite(Relay_4, io_level); break; case 1: digitalWrite(Relay_1, io_level); break; case 2: digitalWrite(Relay_2, io_level); break; case 3: digitalWrite(Relay_3, io_level); break; case 4: digitalWrite(Relay_4, io_level); break; default: break; } }
void miotPowerState(const String & state, uint8_t num) { BLINKER_LOG("need set outlet: ", num, ", power state: ", state); if (state == BLINKER_CMD_ON) { ctrl_multi_outlet(num, LOW); BlinkerMIOT.powerState("on", num); BlinkerMIOT.print(); RelayState(num); oState[num] = true; } else if (state == BLINKER_CMD_OFF) { ctrl_multi_outlet(num, HIGH); BlinkerMIOT.powerState("off", num); BlinkerMIOT.print(); RelayState(num); oState[num] = false; } }
void miotQuery(int32_t queryCode,uint8_t num) { BLINKER_LOG("插孔",num,"状态",",codes:", queryCode); switch (num) { case 0 : BLINKER_LOG("状态:"); BlinkerMIOT.powerState(oState[1] ? "on" : "off"); BlinkerMIOT.powerState(oState[2] ? "on" : "off"); BlinkerMIOT.powerState(oState[3] ? "on" : "off"); BlinkerMIOT.powerState(oState[4] ? "on" : "off"); BlinkerMIOT.print(); break; case 1 : BLINKER_LOG("插孔1状态:"); BlinkerMIOT.powerState(oState[1] ? "on" : "off"); BlinkerMIOT.print(); break; case 2 : BLINKER_LOG("插孔2状态:"); BlinkerMIOT.powerState(oState[2] ? "on" : "off"); BlinkerMIOT.print(); break; case 3 : BLINKER_LOG("插孔3状态:"); BlinkerMIOT.powerState(oState[3] ? "on" : "off"); BlinkerMIOT.print(); break; case 4 : BLINKER_LOG("插孔4状态:"); BlinkerMIOT.powerState(oState[4] ? "on" : "off"); BlinkerMIOT.print(); break; default : BlinkerMIOT.powerState(oState[1] ? "on" : "off"); BlinkerMIOT.powerState(oState[2] ? "on" : "off"); BlinkerMIOT.powerState(oState[3] ? "on" : "off"); BlinkerMIOT.powerState(oState[4] ? "on" : "off"); BlinkerMIOT.print(); break; } }
void button_callback(const String & state) { BLINKER_LOG("操作了MainSwitch: ", state); if (state=="on") { ctrl_multi_outlet(OUTLET_ALL, LOW); Button.color("#FF0000"); Button.text("开"); Button.print("on"); } else if(state=="off") { ctrl_multi_outlet(OUTLET_ALL, HIGH); Button.color("#808080"); Button.text("关"); Button.print("off"); } }
void button1_callback(const String & state) { BLINKER_LOG("get button state: ", state); if (state=="on") { ctrl_multi_outlet(OUTLET_NO_1, LOW); RelayState(1); } else if(state=="off") { ctrl_multi_outlet(OUTLET_NO_1, HIGH); RelayState(1); } }
void button2_callback(const String & state) { BLINKER_LOG("get button state: ", state); if (state=="on") { ctrl_multi_outlet(OUTLET_NO_2, LOW); RelayState(2); } else if(state=="off") { ctrl_multi_outlet(OUTLET_NO_2, HIGH); RelayState(2); } } void button3_callback(const String & state) { BLINKER_LOG("get button state: ", state); if (state=="on") { ctrl_multi_outlet(OUTLET_NO_3, LOW); RelayState(3); } else if(state=="off") { ctrl_multi_outlet(OUTLET_NO_3, HIGH); RelayState(3); } }
void button4_callback(const String & state) { BLINKER_LOG("get button state: ", state); if (state=="on") { ctrl_multi_outlet(OUTLET_NO_4, LOW); RelayState(4); } else if(state=="off") { ctrl_multi_outlet(OUTLET_NO_4, HIGH); RelayState(4); } }
void button5_callback(const String & state) { for(int i=0;i<5;i++) { RelayState(i); } }
void heartbeat() { for(int i=0;i<5;i++) { RelayState(i); } } void setup() { Serial.begin(115200); BLINKER_DEBUG.stream(Serial); BLINKER_DEBUG.debugAll(); pinMode(Relay_1, OUTPUT); pinMode(Relay_2, OUTPUT); pinMode(Relay_3, OUTPUT); pinMode(Relay_4, OUTPUT); digitalWrite(Relay_1, HIGH); digitalWrite(Relay_2, HIGH); digitalWrite(Relay_3, HIGH); digitalWrite(Relay_4, HIGH); Blinker.begin(auth, ssid, pswd); Button.attach(button_callback); Button1.attach(button1_callback); Button2.attach(button2_callback); Button3.attach(button3_callback); Button4.attach(button4_callback); Button5.attach(button5_callback); BlinkerMIOT.attachPowerState(miotPowerState); BlinkerMIOT.attachQuery(miotQuery); Blinker.attachHeartbeat(heartbeat); } void loop() { Blinker.run(); }
|