Components and supplies
LYTWiFi Arduino shield
LYT led bulb
Arduino UNO
Tools and machines
USB to serial converter
Apps and platforms
IFTTT - Weather channel
IFTTT -GMail channel
Email provider (generic)
Maker service
Arduino IDE
Project description
Code
demo_firmware_lytwifi_2
c_cpp
The code to be uploaded to Arduino UNO board. Minor modifications to the official Authometion example code for LYT bulb control.
1/* 2 * This program is free software; you can redistribute it and/or modify 3 * it under the terms of the GNU General Public License as published by 4 * the Free Software Foundation; either version 2 of the License, or 5 * (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 15 * MA 02110-1301, USA. 16 */ 17 18/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * 19 Code by AUTHOMETION S.r.l. 20 Version: 1.02 21 Date: 14.10.2015 22 * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 23 24/**************************************************************************************** 25 * * 26 * !!!! INPORTANT NOTICE !!! * 27 * * 28 **************************************************************************************** 29 * * 30 * Please change the _SS_MAX_RX_BUFF value to 128 to use this sketch * 31 * You can find _SS_MAX_RX_BUFF value in the following header file * 32 * <Program dir>\\Arduino\\hardware\\arduino\\avr\\libraries\\SoftwareSerial\\SoftwareSerial.h * 33 * * 34 **************************************************************************************** 35 * * 36 * To use LYT Shield with Arduino MEGA 2560 please connect following pins: * 37 * * 38 * Shield MEGA 2560 * 39 * 5 A8 * 40 * 10 53 * 41 * 11 51 * 42 * 12 50 * 43 * 13 52 * 44 * * 45 * In JP1, JP2 and JP3 insert jumpers to left side * 46 * * 47 ****************************************************************************************/ 48 49#include <SPI.h> 50#include <EEPROM.h> 51#include <PL1167.h> 52#include <LYTWiFi.h> 53#include <Messenger.h> 54#include <string.h> 55#include "SoftwareSerial.h" 56#include <WiFiInterrupt.h> 57#include <stdlib.h> 58 59#define PL1167_CS_PIN 10 // 10 for Arduino Uno, 53 for Arduino MEGA 2560 60#define ESP8266_TCP_MUX 0 61#define ESP8266_TCP_SEVER_PORT 5001 62#define BUFFER_LENGTH 16 63 64SoftwareSerial mySerial(5, 6); // RX (5 for Arduino Uno / 62(A8) for Arduino Mega 2560), TX (6) 65LYTWiFi myNetWork(mySerial); 66//LYTWiFi myNetWork; 67 68//Redirect serial chars from Virt to WiFi serial port and vice versa 69uint8_t ui8BridgeOn=0; 70 71//Where commands are coming from 72uint8_t ui8WhoIs=0; 73 74// Instantiate Messenger object with the message function and the default separator (the comma character) 75Messenger SerWiFi=Messenger(','); 76Messenger SerHW=Messenger(','); 77 78char cBuf; 79uint8_t ui8Counter; 80 81// Define messenger function 82void messageWiFi() 83{ 84 char cBuffer[64]; 85 86 if(String(SerWiFi.buffer).indexOf(":")!=-1) 87 { 88 memset(cBuffer, '\\0', MESSENGERBUFFERSIZE); 89 strcpy(cBuffer, (String(SerWiFi.buffer).substring(String(SerWiFi.buffer).indexOf(":")+1, String(SerWiFi.buffer).indexOf("\ \ 90"))).c_str()); 91 strcpy(SerWiFi.buffer, cBuffer); 92 ui8WhoIs=1; 93 SerHW.setBufferString(SerWiFi.readBufferString()); 94 95 } 96} 97 98// Define messenger function 99void messageArduino() 100{ 101 uint8_t ui8Counter1, ui8Counter2, ui8Total; 102 uint8_t ui8MODE =0; 103 uint8_t ui8ADDR_A =0; 104 uint8_t ui8ADDR_B =0; 105 uint8_t ui8ADDR_A1=0; 106 uint8_t ui8ADDR_B1=0; 107 uint8_t ui8PAR1 =0; 108 uint8_t ui8PAR2 =0; 109 uint8_t ui8PAR3 =0; 110 uint8_t ui8DIM =0; 111 char cBuffer1[BUFFER_LENGTH]; 112 char cBuffer2[BUFFER_LENGTH]; 113 String sBuffer1; 114 String sBuffer2; 115 long lBaud; 116 117 // POWER ON LYT: PON,ADDR_A,ADDR_B,MODE 118 if (SerHW.checkString("PON")) 119 { 120 ui8ADDR_A=SerHW.readInt(); 121 ui8ADDR_B=SerHW.readInt(); 122 ui8MODE=SerHW.readInt(); 123 ui8PAR1=SerHW.readInt(); 124 memset(cBuffer2, '\\0', BUFFER_LENGTH); 125 if(myNetWork.ui8fSwitchOnAndCheck(ui8ADDR_A, ui8ADDR_B, ui8MODE, ui8PAR1)==C_ACK) 126 memcpy(cBuffer2, F(" OK!"), 4); 127 else 128 memcpy(cBuffer2, F(" KO!"), 4); 129 memset(cBuffer1, '\\0', BUFFER_LENGTH); 130 memcpy(cBuffer1, F("PON:"), 4); 131 strcat(cBuffer1, cBuffer2); 132 if(ui8WhoIs==1) 133 { 134 Serial.println(cBuffer1); 135 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 136 } 137 else 138 Serial.println(cBuffer1); 139 } 140 // POWER OFF LYT: POF,ADDR_A,ADDR_B,MODE 141 else if(SerHW.checkString("POF")) 142 { 143 ui8ADDR_A=SerHW.readInt(); 144 ui8ADDR_B=SerHW.readInt(); 145 ui8MODE=SerHW.readInt(); 146 ui8PAR1=SerHW.readInt(); 147 memset(cBuffer2, '\\0', BUFFER_LENGTH); 148 if(myNetWork.ui8fSwitchOffAndCheck(ui8ADDR_A, ui8ADDR_B, ui8MODE, ui8PAR1)==C_ACK) 149 memcpy(cBuffer2, F(" OK!"), 4); 150 else 151 memcpy(cBuffer2, F(" KO!"), 4); 152 memset(cBuffer1, '\\0', BUFFER_LENGTH); 153 memcpy(cBuffer1, F("POF:"), 4); 154 strcat(cBuffer1, cBuffer2); 155 if(ui8WhoIs==1) 156 { 157 Serial.println(cBuffer1); 158 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 159 } 160 else 161 Serial.println(cBuffer1); 162 } 163 // SET RGB VALUES: RGB,ADDR_A,ADDR_B,PAR1,PAR2,PAR3,MODE 164 else if(SerHW.checkString("RGB")) 165 { 166 ui8ADDR_A=SerHW.readInt(); 167 ui8ADDR_B=SerHW.readInt(); 168 ui8PAR1=SerHW.readInt(); 169 ui8PAR2=SerHW.readInt(); 170 ui8PAR3=SerHW.readInt(); 171 ui8MODE=SerHW.readInt(); 172 ui8DIM=SerHW.readInt(); 173 memset(cBuffer2, '\\0', BUFFER_LENGTH); 174 if(myNetWork.ui8fSetRGBValuesAndCheck(ui8ADDR_A, ui8ADDR_B, ui8PAR1, ui8PAR2, ui8PAR3, ui8MODE, ui8DIM)==C_ACK) 175 memcpy(cBuffer2, F(" OK!"), 4); 176 else 177 memcpy(cBuffer2, F(" KO!"), 4); 178 memset(cBuffer1, '\\0', BUFFER_LENGTH); 179 memcpy(cBuffer1, F("RGB:"), 4); 180 strcat(cBuffer1, cBuffer2); 181 if(ui8WhoIs==1) 182 { 183 Serial.println(cBuffer1); 184 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 185 } 186 else 187 Serial.println(cBuffer1); 188 } 189 // SET BRIGHTNESS VALUE: SBR,ADDA,ADDB,PAR1,MODE,DIM 190 else if(SerHW.checkString("SBR")) 191 { 192 ui8ADDR_A=SerHW.readInt(); 193 ui8ADDR_B=SerHW.readInt(); 194 ui8PAR2=SerHW.readInt(); 195 ui8MODE=SerHW.readInt(); 196 ui8DIM=SerHW.readInt(); 197 memset(cBuffer2, '\\0', BUFFER_LENGTH); 198 if(myNetWork.ui8fSetBrightnessValueAndCheck(ui8ADDR_A, ui8ADDR_B, ui8PAR2, ui8MODE, ui8DIM)==C_ACK) 199 memcpy(cBuffer2, F(" OK!"), 4); 200 else 201 memcpy(cBuffer2, F(" KO!"), 4); 202 memset(cBuffer1, '\\0', BUFFER_LENGTH); 203 memcpy(cBuffer1, F("SBR:"), 4); 204 strcat(cBuffer1, cBuffer2); 205 if(ui8WhoIs==1) 206 { 207 Serial.println(cBuffer1); 208 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 209 } 210 else 211 Serial.println(cBuffer1); 212 } 213 // SET NEW ADDRESS: SEA,ADDR_A,ADDR_B,ADDR_A1,ADDR_B1 214 else if(SerHW.checkString("SEA")) 215 { 216 ui8ADDR_A=SerHW.readInt(); 217 ui8ADDR_B=SerHW.readInt(); 218 ui8ADDR_A1=SerHW.readInt(); 219 ui8ADDR_B1=SerHW.readInt(); 220 memset(cBuffer2, '\\0', BUFFER_LENGTH); 221 if(myNetWork.ui8fAddLampAddressAndCheck(ui8ADDR_A, ui8ADDR_B, ui8ADDR_A1, ui8ADDR_B1)==C_ACK) 222 memcpy(cBuffer2, F(" OK!"), 4); 223 else 224 memcpy(cBuffer2, F(" KO!"), 4); 225 memset(cBuffer1, '\\0', BUFFER_LENGTH); 226 memcpy(cBuffer1, F("SEA: "), 4); 227 strcat(cBuffer1, cBuffer2); 228 if(ui8WhoIs==1) 229 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 230 else 231 Serial.println(cBuffer1); 232 } 233 // DELETE ADDRESS: DEA,ADDR_A,ADDR_B,ADDR_A1,ADDR_B1 234 else if(SerHW.checkString("DEA")) 235 { 236 ui8ADDR_A=SerHW.readInt(); 237 ui8ADDR_B=SerHW.readInt(); 238 ui8ADDR_A1=SerHW.readInt(); 239 ui8ADDR_B1=SerHW.readInt(); 240 memset(cBuffer2, '\\0', BUFFER_LENGTH); 241 if(myNetWork.ui8fDeleteLampAddressAndCheck(ui8ADDR_A, ui8ADDR_B, ui8ADDR_A1, ui8ADDR_B1)==C_ACK) 242 memcpy(cBuffer2, F(" OK!"), 4); 243 else 244 memcpy(cBuffer2, F(" KO!"), 4); 245 memset(cBuffer1, '\\0', BUFFER_LENGTH); 246 memcpy(cBuffer1, F("DEA:"), 4); 247 strcat(cBuffer1, cBuffer2); 248 if(ui8WhoIs==1) 249 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 250 else 251 Serial.println(cBuffer1); 252 } 253 // RESET BULB: RES 254 else if(SerHW.checkString("RES")) 255 { 256 memset(cBuffer2, '\\0', BUFFER_LENGTH); 257 if(myNetWork.ui8fResetLampAddressAndCheck()==C_ACK) 258 memcpy(cBuffer2, F(" OK!"), 4); 259 else 260 memcpy(cBuffer2, F(" KO!"), 4); 261 memset(cBuffer1, '\\0', BUFFER_LENGTH); 262 memcpy(cBuffer1, F("RES:"), 4); 263 strcat(cBuffer1, cBuffer2); 264 if(ui8WhoIs==1) 265 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 266 else 267 Serial.println(cBuffer1); 268 } 269 // READ ADDRESS TABLE: RAT,ADDR_A,ADDR_B 270 else if(SerHW.checkString("RAT")) 271 { 272 ui8ADDR_A=SerHW.readInt(); 273 ui8ADDR_B=SerHW.readInt(); 274 memset(cBuffer2, '\\0', BUFFER_LENGTH); 275 if(myNetWork.ui8fReadLampAddressTableAndCheck(ui8ADDR_A, ui8ADDR_B)==C_ACK) 276 memcpy(cBuffer2, F(" OK!"), 4); 277 else 278 memcpy(cBuffer2, F(" KO!"), 4); 279 if(ui8WhoIs==1) 280 { 281 for(ui8Counter1=0; ui8Counter1<PROTOCOL_ADDRESS_TABLE_POSITIONS; ui8Counter1++) 282 { 283 memset(cBuffer1, '\\0', BUFFER_LENGTH); 284 sprintf(cBuffer1, "(%2d,%2d)", myNetWork.LampAddressTable.ui8AddressTable[ui8Counter1][0], myNetWork.LampAddressTable.ui8AddressTable[ui8Counter1][1]); 285 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 286 delay(100); 287 } 288 memset(cBuffer1, '\\0', BUFFER_LENGTH); 289 memcpy(cBuffer1, F("RAT:"), 4); 290 strcat(cBuffer1, cBuffer2); 291 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 292 } 293 else 294 { 295 for(ui8Counter1=0; ui8Counter1<PROTOCOL_ADDRESS_TABLE_POSITIONS; ui8Counter1++) 296 { 297 Serial.print(F("(")); 298 Serial.print(myNetWork.LampAddressTable.ui8AddressTable[ui8Counter1][0], DEC); 299 Serial.print(F(", ")); 300 Serial.print(myNetWork.LampAddressTable.ui8AddressTable[ui8Counter1][1], DEC); 301 Serial.print(F(") ")); 302 } 303 Serial.println(); 304 memset(cBuffer1, '\\0', BUFFER_LENGTH); 305 memcpy(cBuffer1, F("RAT:"), 4); 306 strcat(cBuffer1, cBuffer2); 307 Serial.println(cBuffer1); 308 } 309 } 310 // SAVE CURRENT STATE OF BULB: SAV,ADDR_A,ADDR_B, 311 else if(SerHW.checkString("SAV")) 312 { 313 ui8ADDR_A=SerHW.readInt(); 314 ui8ADDR_B=SerHW.readInt(); 315 memset(cBuffer2, '\\0', BUFFER_LENGTH); 316 if(myNetWork.ui8fSaveLampSettingsAndCheck(ui8ADDR_A, ui8ADDR_B)==C_ACK) 317 memcpy(cBuffer2, F(" OK!"), 4); 318 else 319 memcpy(cBuffer2, F(" KO!"), 4); 320 memset(cBuffer1, '\\0', BUFFER_LENGTH); 321 memcpy(cBuffer1, F("SAV:"), 4); 322 strcat(cBuffer1, cBuffer2); 323 if(ui8WhoIs==1) 324 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 325 else 326 Serial.println(cBuffer1); 327 } 328 // READ INFO STATUS: RIS,ADDR_A,ADDR_B 329 else if(SerHW.checkString("RIS")) 330 { 331 ui8ADDR_A=SerHW.readInt(); 332 ui8ADDR_B=SerHW.readInt(); 333 memset(cBuffer2, '\\0', BUFFER_LENGTH); 334 if(myNetWork.ui8fAskLampInfoStatusAndCheck(ui8ADDR_A, ui8ADDR_B)==C_ACK) 335 memcpy(cBuffer2, F(" OK!"), 4); 336 else 337 memcpy(cBuffer2, F(" KO!"), 4); 338 memset(cBuffer1, '\\0', BUFFER_LENGTH); 339 memcpy(cBuffer1, F("RIS:"), 4); 340 strcat(cBuffer1, cBuffer2); 341 if(ui8WhoIs==1) 342 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 343 else 344 { 345 Serial.print(F("Answers: ")); 346 for(ui8Counter1=0; ui8Counter1<PROTOCOL_ANSWER_LENGHT; ui8Counter1++) 347 { 348 Serial.print(myNetWork.ReceivedAnswer.AnswerStruct.ui8Answer[ui8Counter1], DEC); 349 Serial.print(F(" ")); 350 } 351 Serial.println(); 352 Serial.println(cBuffer1); 353 } 354 } 355 // SET SYNC WORD: SSW,ADDR_A,ADDR_B,ADDR_A1,ADDR_B1 356 else if(SerHW.checkString("SSW")) 357 { 358 ui8ADDR_A=SerHW.readInt(); 359 ui8ADDR_B=SerHW.readInt(); 360 ui8ADDR_A1=SerHW.readInt(); 361 ui8ADDR_B1=SerHW.readInt(); 362 memset(cBuffer2, '\\0', BUFFER_LENGTH); 363 if(myNetWork.ui8fSetSyncWordAndCheck(ui8ADDR_A, ui8ADDR_B, ui8ADDR_A1, ui8ADDR_B1)==C_ACK) 364 memcpy(cBuffer2, F(" OK!"), 4); 365 else 366 memcpy(cBuffer2, F(" KO!"), 4); 367 memset(cBuffer1, '\\0', BUFFER_LENGTH); 368 memcpy(cBuffer1, F("SSW:"), 4); 369 strcat(cBuffer1, cBuffer2); 370 if(ui8WhoIs==1) 371 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 372 else 373 Serial.println(cBuffer1); 374 } 375 // SET LOCAL SYNC WORD: SLSW,ADDR_A,ADDR_B,MODE 376 else if(SerHW.checkString("SLSW")) 377 { 378 ui8ADDR_A=SerHW.readInt(); 379 ui8ADDR_B=SerHW.readInt(); 380 ui8MODE=SerHW.readInt(); 381 myNetWork.vfSetLocalSyncWord(ui8ADDR_A, ui8ADDR_B, ui8MODE); 382 if(ui8WhoIs==1) 383 { 384 memset(cBuffer1, '\\0', BUFFER_LENGTH); 385 memcpy(cBuffer1, F("SLSW!"), 5); 386 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, 5); 387 } 388 else 389 Serial.println(F("SLSW!")); 390 } 391 // SET RGB VALUE IN FAST MODE: FRGB,ADDR_A,ADDR_B,PAR1,PAR2,PAR3 392 else if(SerHW.checkString("FRGB")) 393 { 394 ui8ADDR_A=SerHW.readInt(); 395 ui8ADDR_B=SerHW.readInt(); 396 ui8PAR1=SerHW.readInt(); 397 ui8PAR2=SerHW.readInt(); 398 ui8PAR3=SerHW.readInt(); 399 while(Serial.available()) 400 Serial.read(); 401 while(Serial.available()==0) 402 { 403 myNetWork.ui8fSetRGBValuesAndCheck(ui8ADDR_A, ui8ADDR_B, ui8PAR1, 0, 0, C_MULTICAST); 404 delay(100); 405 myNetWork.ui8fSetRGBValuesAndCheck(ui8ADDR_A, ui8ADDR_B, 0, ui8PAR2, 0, C_MULTICAST); 406 delay(100); 407 myNetWork.ui8fSetRGBValuesAndCheck(ui8ADDR_A, ui8ADDR_B, 0, 0, ui8PAR3, C_MULTICAST); 408 delay(100); 409 } 410 if(ui8WhoIs==1) 411 { 412 memset(cBuffer1, '\\0', BUFFER_LENGTH); 413 memcpy(cBuffer1, F("FRGB!"), 5); 414 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, 5); 415 } 416 else 417 Serial.println(F("FRGB!")); 418 } 419 // SET LOCAL RADIO CHANNEL: SLRC,CHAN,MODE 420 else if(SerHW.checkString("SLRC")) 421 { 422 ui8PAR1=SerHW.readInt(); 423 ui8MODE=SerHW.readInt(); 424 myNetWork.vfSetLocalChannel(ui8PAR1, ui8MODE); 425 if(ui8WhoIs==1) 426 { 427 memset(cBuffer1, '\\0', BUFFER_LENGTH); 428 memcpy(cBuffer1, F("SLRC!"), 5); 429 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, 5); 430 } 431 else 432 Serial.println(F("SLRC!")); 433 } 434 // SET RADIO CHANNEL OF LYT SHIELD: SRC,ADDR_A,ADDR_B,CHAN 435 else if(SerHW.checkString("SRC")) 436 { 437 ui8ADDR_A=SerHW.readInt(); 438 ui8ADDR_B=SerHW.readInt(); 439 ui8PAR1=SerHW.readInt(); 440 memset(cBuffer2, '\\0', BUFFER_LENGTH); 441 if(myNetWork.ui8fSetChannelAndCheck(ui8ADDR_A, ui8ADDR_B, ui8PAR1)==C_ACK) 442 memcpy(cBuffer2, F(" OK!"), 4); 443 else 444 memcpy(cBuffer2, F(" KO!"), 4); 445 memset(cBuffer1, '\\0', BUFFER_LENGTH); 446 memcpy(cBuffer1, F("SRC:"), 4); 447 strcat(cBuffer1, cBuffer2); 448 if(ui8WhoIs==1) 449 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 450 else 451 Serial.println(cBuffer1); 452 } 453 // TEST RADIO CHANNEL EFFICENCY: TEST,ADDR_A,ADDR_B,CHAN 454 else if(SerHW.checkString("TEST")) 455 { 456 ui8ADDR_A=SerHW.readInt(); 457 ui8ADDR_B=SerHW.readInt(); 458 ui8Counter1=SerHW.readInt(); 459 while(Serial.available()) 460 Serial.read(); 461 while((Serial.available()==0)&&(ui8Counter1<=127)) 462 { 463 ui8Counter2=0; 464 while((myNetWork.ui8fSetChannelAndCheck(ui8ADDR_A, ui8ADDR_B, ui8Counter1)!=C_ACK)&&(ui8Counter2<3)) 465 { 466 delay(1000); 467 ui8Counter2++; 468 } 469 if(ui8Counter2<3) 470 { 471 ui8Total=0; 472 for(ui8Counter2=0; ui8Counter2<20; ui8Counter2++) 473 { 474 myNetWork.ui8fSetRGBValuesAndCheck(ui8ADDR_A, ui8ADDR_B, 10*ui8Counter2, 0, 0, C_UNICAST); 475 ui8Total+=myNetWork.ui8SentCommandCounterMem; 476 delay(500); 477 if(Serial.available()!=0) 478 break; 479 } 480 Serial.print("Channel: "); 481 Serial.print(ui8Counter1); 482 Serial.print(", Total number of sent commands: "); 483 Serial.println(ui8Total); 484 ui8Counter1++; 485 } 486 else 487 break; 488 } 489 if(ui8WhoIs==1) 490 { 491 memset(cBuffer1, '\\0', BUFFER_LENGTH); 492 memcpy(cBuffer1, F("TEST!"), 5); 493 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, 5); 494 } 495 else 496 Serial.println(F("TEST!")); 497 } 498 // ACTVATE BRIDGE between Serial and Virtual serial: BRIDGE 499 else if(SerHW.checkString("BRIDGE")) 500 { 501 ui8BridgeOn=1; 502 if(ui8WhoIs==1) 503 { 504 memset(cBuffer1, '\\0', BUFFER_LENGTH); 505 memcpy(cBuffer1, F("BRIDGE!"), 7); 506 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, 7); 507 } 508 else 509 Serial.println(F("BRIDGE!")); 510 } 511 // Discover all available WiFi routers: LAP 512 else if(SerHW.checkString("LAP")) 513 { 514 if(ui8WhoIs==0) 515 { 516 Serial.println(myNetWork.sfGetAPList_ESP8266()); 517 Serial.println(F("LAP!")); 518 } 519 } 520 // Join ESP8266: JOI,SSID,PWD 521 else if(SerHW.checkString("JOI")) 522 { 523 if(ui8WhoIs==0) 524 { 525 if(myNetWork.bfSetOprToStationSoftAP_ESP8266()) 526 { 527 myNetWork.bfRestart_ESP8266(); 528 SerHW.copyString(cBuffer1, BUFFER_LENGTH); 529 SerHW.copyString(cBuffer2, BUFFER_LENGTH); 530 if(myNetWork.bfJoinAP_ESP8266(String(cBuffer1), String(cBuffer2))) 531 { 532 Serial.println(F("Join: OK")); 533 delay(4000); 534 myNetWork.vfGetLocalIP_ESP8266(sBuffer1, sBuffer2); 535 Serial.print(F("Local IP Address :")); 536 Serial.println(sBuffer1); 537 Serial.print(F("Local Mac Address:")); 538 Serial.println(sBuffer2); 539 delay(2000); 540 if(myNetWork.bfEnableMUX_ESP8266()) 541 { 542 delay(2000); 543 if(myNetWork.bfStartTCPServer_ESP8266(ESP8266_TCP_SEVER_PORT)) 544 Serial.println(F("TCP Server: OK")); 545 else 546 Serial.println(F("TCP Server: FAIL")); 547 } 548 else 549 Serial.println(F("TCP Server: FAIL")); 550 } 551 else 552 Serial.println(F("Join: FAIL")); 553 } 554 else 555 Serial.println(F("FAIL: ESP8266 not configured")); 556 Serial.println(F("JOI!")); 557 } 558 } 559 // Start a ESP8266 TCP server: STCP 560 else if(SerHW.checkString("STCP")) 561 { 562 if(ui8WhoIs==0) 563 { 564 delay(2000); 565 myNetWork.vfGetLocalIP_ESP8266(sBuffer1, sBuffer2); 566 Serial.print(F("Local IP Address :")); 567 Serial.println(sBuffer1); 568 Serial.print(F("Local Mac Address:")); 569 Serial.println(sBuffer2); 570 delay(2000); 571 if(atoi(sBuffer1.c_str())>0) 572 { 573 if(myNetWork.bfEnableMUX_ESP8266()) 574 { 575 delay(2000); 576 if(myNetWork.bfStartTCPServer_ESP8266(ESP8266_TCP_SEVER_PORT)) 577 Serial.println(F("TCP Server: OK")); 578 else 579 Serial.println(F("TCP Server: FAIL")); 580 } 581 else 582 Serial.println(F("TCP Server: FAIL")); 583 } 584 else 585 Serial.println(F("FAIL: ESP8266 not connected to network")); 586 Serial.println(F("STCP!")); 587 } 588 } 589 // Read IP and Mac Addresses of ESP8266: RIP 590 else if(SerHW.checkString("RIP")) 591 { 592 if(ui8WhoIs==0) 593 { 594 myNetWork.vfGetLocalIP_ESP8266(sBuffer1, sBuffer2); 595 Serial.print(F("Local IP Address :")); 596 Serial.println(sBuffer1); 597 Serial.print(F("Local Mac Address:")); 598 Serial.println(sBuffer2); 599 Serial.println(F("RIP!")); 600 } 601 } 602 // Change Serial port baud rate (***SE ARRIVA DA WIFI CHE SUCCEDE***) 603 else if(SerHW.checkString("UART")) 604 { 605 lBaud=SerHW.readLong(); 606 Serial.end(); 607 delay(200); 608 Serial.begin(lBaud); 609 } 610 else if(SerHW.checkString("WDG")) 611 { 612 613 } 614 else 615 { 616 if(ui8WhoIs==1) 617 { 618 Serial.print(F("UNK FROM ESP! ")); 619 Serial.println(SerHW.readBufferString()); 620 621 memset(cBuffer1, '\\0', BUFFER_LENGTH); 622 memcpy(cBuffer1, F("UNK!"), 4); 623 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, 4); 624 625 } 626 else 627 Serial.println(F("UNK!")); 628 } 629 ui8WhoIs=0; 630} 631 632void setup() 633{ 634 // Initialize Serial Communications 635 mySerial.begin(9600); 636 Serial.begin(9600, SERIAL_8N1); 637 638 myNetWork.vfInitialize(PL1167_CS_PIN); 639// vfISRInit(&myNetWork); 640 vfISRInit(); 641 642 SerWiFi.attach(messageWiFi); 643 SerHW.attach(messageArduino); 644 delay(500); 645} 646 647void loop() 648{ 649 while (Serial.available()) 650 { 651 if(ui8BridgeOn==0) 652 SerHW.process(Serial.read()); 653 else 654 { 655 cBuf=Serial.read(); 656 if(cBuf=='!') 657 ui8BridgeOn=0; 658 else 659 mySerial.print(cBuf); 660 } 661 } 662 while (mySerial.available()) 663 { 664 if(ui8BridgeOn==0) 665 SerWiFi.process(mySerial.read()); 666 else 667 { 668 cBuf=mySerial.read(); 669 if(cBuf=='!') 670 ui8BridgeOn=0; 671 else 672 Serial.print(cBuf); 673 } 674 } 675} 676
demo_firmware_esp8266_ifttt_thermometer
c_cpp
The code to be uploaded to ESP8266 module of the LYTWiFi arduino shield for using of LYT led bulb as a thermometer.
1/* 2 * This program is free software; you can redistribute it and/or modify 3 * it under the terms of the GNU General Public License as published by 4 * the Free Software Foundation; either version 2 of the License, or 5 * (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 15 * MA 02110-1301, USA. 16 */ 17 18/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * 19 Code by AUTHOMETION S.r.l. 20 Version: 0.00 21 Date: 01.08.2016 22 * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 23 24// Using EmailPop3 sketch from Arduino playground as guideline: 25 26#include <Arduino.h> 27 28#include <ESP8266WiFi.h> 29#include <ESP8266WiFiMulti.h> 30 31#include <ESP8266HTTPClient.h> 32#include <SoftwareSerial.h> 33 34//#define USE_SERIAL Serial 35#define USE_SERIAL srvTerm 36#define ARDUINO_IF Serial 37 38SoftwareSerial srvTerm(14,13); 39 40 41// Use comments to enable or disable this define for debug messages 42//#define DEBUG_POP 43 44// Use comments to enable or disable the deleting of the mail 45#define ENABLE_DELETE_POP 46 47ESP8266WiFiMulti WiFiMulti; 48bool sent(false); 49 50 51// The mac address must be an unique number 52byte mac[] = { 0xDE, 0x36, 0x5F, 0x0A, 0x19, 0x83 }; 53// change network settings to yours 54IPAddress ip( 0, 0, 0, 0); 55IPAddress gateway( 0, 0, 0, 0 ); 56IPAddress subnet( 0, 0, 0, 0 ); 57 58// Set the server POP3 address, the port, the user and password. 59// The POP3 mail server is something like this: 60// mail.yourdomain.com, pop.yourdomain.com, pop3.yourdomain.com 61// Using PROGMEM for these causes a fail when trying to connect and log in. 62const char pop_server[] = <pop server name>; 63const int pop_port = 110; 64const char pop_user[] = <email address>; 65const char pop_pass[] = <email password>; 66 67 68// The number of milliseconds timeout for parseInt() and find(). 69// The response time for the Server can still be 10 seconds. 70#define POP_TIMEOUT 10000 71#define POP_TIMEOUT_DEFAULT 10000 72 73WiFiClient client; 74 75void CheckEmail(); 76int getEmail( char *pBuf, int nBufSize); 77boolean readOk( boolean readAll); 78void readRemaining(); 79void popFail(); 80 81void setup() { 82 83 ARDUINO_IF.begin(9600); 84 ARDUINO_IF.flush(); 85 86 USE_SERIAL.begin(9600); 87 USE_SERIAL.flush(); 88 // USE_SERIAL.setDebugOutput(true); 89 90 USE_SERIAL.println(); 91 USE_SERIAL.println(); 92 USE_SERIAL.println(); 93 94 for(uint8_t t = 4; t > 0; t--) 95 { 96 USE_SERIAL.printf("[SETUP] WAIT %d...\ 97", t); 98 USE_SERIAL.flush(); 99 delay(1000); 100 } 101 102 // Connect to the specified AccessPoint 103 WiFiMulti.addAP(<SSID>, <password>); 104 105 delay(3000); 106 107 if((WiFiMulti.run() == WL_CONNECTED)) 108 { 109 // Get current connection parameters. 110 ip = WiFi.localIP(); 111 gateway = WiFi.gatewayIP(); 112 subnet = WiFi.subnetMask(); 113 114 // print your local IP address. 115 USE_SERIAL.println(F( "WiFi started.")); 116 USE_SERIAL.print(F( "Local IP = ")); 117 USE_SERIAL.println(ip); 118 USE_SERIAL.print(F( "Gateway IP = ")); 119 USE_SERIAL.println(gateway); 120 USE_SERIAL.print(F( "Subnet Mask = ")); 121 USE_SERIAL.println(subnet); 122 } 123} 124 125void loop() 126{ 127 128 if (!sent) 129 { 130 // wait for WiFi connection 131 if((WiFiMulti.run() == WL_CONNECTED)) 132 { 133 HTTPClient http; 134 135 USE_SERIAL.print("[HTTP] begin...\ 136"); 137 138 http.begin("https://maker.ifttt.com/trigger/lytwifi_thermometer_on/with/key/<maker channel key>"); //HTTP 139 140 141 USE_SERIAL.print("[HTTP] GET...\ 142"); 143 // start connection and send HTTP header 144 int httpCode = http.GET(); 145 if(httpCode) 146 { 147 // HTTP header has been send and Server response header has been handled 148 USE_SERIAL.printf("[HTTP] GET... code = %d\ 149", httpCode); 150 151 // file found at server 152 if(httpCode == 200) 153 { 154 String payload = http.getString(); 155 USE_SERIAL.println(payload); 156 157 sent = true; 158 } 159 } 160 else 161 { 162 USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\ 163"); 164 } 165 } 166 else USE_SERIAL.print("No connection...\ 167"); 168 } 169 170 delay(5000); 171 172 CheckEmail(); 173 174} 175 176void CheckEmail() 177{ 178 // Create a buffer to receive the commands in (that is the Subject of the mail). 179 char buffer[32]; 180 181 // The getEmail gets the text of the mail Subject into the buffer. 182 // The valid number of received characters are returned. 183 // If the return value is < 0, it is an error. 184 int n = getEmail( buffer, sizeof(buffer)); 185 186 if( n<0) 187 { 188 USE_SERIAL.print(F("Email POP3 failed, error = ")); 189 USE_SERIAL.println( n); 190 } 191 else 192 { 193 if( n == 0) 194 { 195 USE_SERIAL.println(F("Ready, nothing to do.")); 196 } 197 else 198 { 199 // 'n' is > 0, a command received. 200 //Serial.print(F("Email checked, Command = \\"")); 201 //Serial.print( buffer); 202 //Serial.println(F("\\"")); 203 204 //Serial.print(F("Email checked, Command: \ \ 205")); 206 char cmd[32] = ":"; 207 int temp = atoi(buffer); 208 if (temp < 18) 209 strcat(cmd,"RGB,0,0,0,0,255,1"); 210 else if ((temp>=18)&&(temp<25)) 211 strcat(cmd,"RGB,0,0,0,255,0,1"); 212 else if ((temp>=25)&&(temp<30)) 213 strcat(cmd,"RGB,0,0,127,127,0,1"); 214 else if (temp>=30) 215 strcat(cmd,"RGB,0,0,255,0,0,1"); 216 217 218 USE_SERIAL.println(cmd); 219 ARDUINO_IF.println(cmd); 220 221 } 222 } 223} 224 225// getEmail 226// -------- 227// Find an email on a mail server, using POP3. 228// The Subject should start with "ARDUINO " and the text 229// after that is copied into pBuf. 230// 231// The data in pBuf is only valid if the return value is not an error 232// (an error is return value less than zero). 233// 234int getEmail( char *pBuf, int nBufSize) 235{ 236 // nBytes is the number of bytes that is returned by getEmail. 237 int nBytes = 0; 238 239 // Connect to server 240 // client.connect returns '1' if okay, or negative number if error. 241 // SUCCESS 1 242 // 0 (error, unknown timeout, perhaps an error in the library) 243 // TIMED_OUT -1 244 // INVALID_SERVER -2 245 // TRUNCATED -3 246 // INVALID_RESPONSE -4 247 // -5 (there is no mail server at that IP address and port) 248 // The string for the server must be a normal string in sram, no PROGMEM allowed. 249 int nError = client.connect( pop_server, pop_port); 250 251 // During testing, a value of zero was sometimes returned. 252 // This is not according to the documentation and it is an error. 253 // Therefor the non-error value '0' is turned into a negative number to 254 // indicate an error. 255 if( nError == 0) 256 return( -200); 257 258 // Only a value of 1 is okay. 259 if( nError != 1) 260 return( nError); 261 262#ifdef DEBUG_POP 263 USE_SERIAL.println(F("connected")); 264#endif 265 266 // The server should respond with "+OK" and maybe more text after that. 267 // Check if "+OK" can be read. 268 // The parameter 'true' is to read also everything after the "+OK". 269 if(!readOk( true)) 270 return -102; 271 272#ifdef DEBUG_POP 273 USE_SERIAL.println(F("command USER")); 274#endif 275 client.print(F( "USER ")); 276 client.println( pop_user); 277 if(!readOk( true)) 278 return -103; 279 280#ifdef DEBUG_POP 281 USE_SERIAL.println(F("command PASS")); 282#endif 283 client.print(F( "PASS ")); 284 client.println( pop_pass); 285 if(!readOk( true)) 286 return -104; 287 288#ifdef DEBUG_POP 289 USE_SERIAL.println(F("command STAT")); 290#endif 291 client.println(F( "STAT")); 292 if(!readOk( false)) 293 return -105; 294 295 // The whole line was like this: "+OK 3 15343" 296 // It means that 3 emails are waiting with a total size of 15343. 297 // At this moment, the "+OK" is read, but nothing else. 298 // Check if there is a space after "+OK". 299 char c = client.read(); 300 if( c != ' ') 301 return -106; 302 303 client.setTimeout( POP_TIMEOUT); // set timeout lower for parseInt 304 // Read the number of emails that are on the server. 305 int nMails = client.parseInt(); 306 client.setTimeout( POP_TIMEOUT_DEFAULT); // restore timeout to 1 second 307 308 // Read the remaining of the response to STAT. 309 readRemaining(); 310 311#ifdef DEBUG_POP 312 USE_SERIAL.print(F( "Number of emails=")); 313 USE_SERIAL.println( nMails); 314#endif 315 316 // Test if there are emails waiting. 317 if( nMails == 0) 318 { 319 // No emails, but no error. Set buffer to empty string. 320 nBytes = 0; // the returned value 321 pBuf[0] = '\\0'; // set empty string 322 } 323 else if( nMails > 0) 324 { 325 // emails are waiting. 326 // Scan the emails until the first is found with the keyword "ARDUINO " at the 327 // beginning of the "Subject: ". 328 329 boolean found_and_ready = false; 330 for( int nMailNumber = 1; nMailNumber <= nMails && !found_and_ready; nMailNumber++) 331 { 332 // The command RETR <x> gets the whole mail. 333 // The command TOP <x> <size> gets the header plus 'size' of the body. 334 335#ifdef DEBUG_POP 336 USE_SERIAL.print(F( "command TOP ")); 337 USE_SERIAL.print( nMailNumber); 338 USE_SERIAL.println(F( " 0")); 339#endif 340 client.print(F( "TOP ")); 341 client.print( nMailNumber); 342 client.println(F( " 0")); 343 344 // Use readOk with parameter 'false' to stop reading after "+OK". 345 if(!readOk( false)) 346 return -107; 347 // The header of the email is waiting to be read, use the Stream.find() to look for the Subject. 348 // The text "Subject: " should be at the beginning of a line, but that is not tested. 349 // The first found text "Subject: " is assumed to be the real subject. 350 // I have checked many years of emails, and the text is always "Subject: ", and never "SUBJECT: ". 351 // At the moment, it is not possible to use the F() macro for Stream.find 352 // Only the email that starts with "ARDUINO " at the start of the Subject is used. 353 354 client.setTimeout( POP_TIMEOUT); // set short timeout for find(). 355 // find() returns true if found and false if not. 356 boolean foundsubject = client.find( "Subject: ARDUINO "); 357 client.setTimeout( POP_TIMEOUT_DEFAULT); // restore timeout to 1 second 358 359 if( foundsubject) 360 { 361#ifdef DEBUG_POP 362 USE_SERIAL.println(F("Found an email for me")); 363#endif 364 365 // Read the remaining subject (that is the command for the Arduino) into a buffer. 366 // Every line from the mail server should end with CR + LF, 367 // but to be sure, both CR and LF are checked. 368 // Alternative: 369 // client.readBytesUntil('\ ', pBuf, nBufSize); 370 371 // The last position in the buffer is reserved for the zero terminator. 372 // So read data until (nBufSize-1). 373 int i; 374 for( i = 0; i < (nBufSize-1) && client.available(); i++) 375 { 376 char c = client.read(); 377 if (c == '\ ' || c == '\ 378') 379 break; 380 pBuf[i] = c; 381 } 382 // Add zero terminator 383 pBuf[i] = '\\0'; 384 nBytes = i; // the number of received bytes is returned by the getEmail() function. 385 386 // More text of the header could be following the Subject. 387 // That is read and disregarded. 388 readRemaining(); 389 390#ifdef DEBUG_POP 391 USE_SERIAL.print(F( "Subject = \\"ARDUINO ")); 392 USE_SERIAL.print( pBuf); 393 USE_SERIAL.println(F( "\\"")); 394#endif 395 396#ifdef ENABLE_DELETE_POP 397 // Delete the just read message. 398#ifdef DEBUG_POP 399 USE_SERIAL.print(F( "command DELE ")); 400 USE_SERIAL.println( nMailNumber); 401#endif 402 client.print(F( "DELE ")); 403 client.println( nMailNumber); 404 if(!readOk( true)) 405 return -108; 406#endif 407 408 // Everything is okay, the mail is read and deleted. 409 // Ready for now, don't process the remaining emails. 410 found_and_ready = true; 411 } 412 else 413 { 414#ifdef DEBUG_POP 415 USE_SERIAL.println(F("No ARDUINO keyword in subject")); 416#endif 417 // This email has no "Subject: ARDUINO ". 418 // But the remaining text has still to be read and disregarded. 419 readRemaining(); 420 } 421 } 422 } 423 424#ifdef DEBUG_POP 425 USE_SERIAL.println(F( "Sending QUIT")); 426#endif 427 client.println(F( "QUIT")); 428 429 // After "QUIT", the server still respons with "+OK", 430 // but after that, the connection might get lost. 431 // So don't read everything after "+OK" (use parameter 'false' for readOk). 432 if(!readOk( false)) 433 return -109; 434 435 client.stop(); 436 437#ifdef DEBUG_POP 438 USE_SERIAL.println(F("normally disconnected")); 439#endif 440 441 return( nBytes); 442} 443 444 445// Read the response from the mail server. 446// That is "+OK" if everything is okay. 447// Parameter 'readAll' is to read every character after the "+OK". 448boolean readOk( boolean readAll) 449{ 450 // Check the response "+OK" from the mail server 451 // In most cases that is followed by a space and more text, but not always. 452 // In case of an error the text "-ERR" is received. 453 454 int loopCount = 0; 455 char bufOk[4]; 456 457 // Wait for response of mail server, with a timout. 458 while(!client.available()) 459 { 460 delay(1); 461 loopCount++; 462 463 // if nothing received for 10 seconds, timeout 464 if(loopCount > 20000) 465 { 466 client.stop(); 467#ifdef DEBUG_POP 468 USE_SERIAL.println(F("\ 469Timeout")); 470#endif 471 return false; 472 } 473 } 474 475 // Read the first three bytes. 476 client.readBytes(bufOk, 3); 477 478#ifdef DEBUG_POP 479 USE_SERIAL.write(bufOk, 3); 480 USE_SERIAL.println(); 481#endif 482 483 // Is it "+OK" ? 484 if( strncmp( bufOk, "+OK", 3) != 0) 485 { 486 popFail(); 487 return false; 488 } 489 490 // When the text after "+OK" is not needed, everything 491 // else can be read and disregarded 492 // (or shown in the serial monitor during debugging). 493 if( readAll) 494 readRemaining(); 495 496 return true; 497} 498 499 500void readRemaining() 501{ 502 // This function is called after checking the "+OK". 503 // It reads everything from the server, until no more text is 504 // available. 505 506 while(client.available()) 507 { 508 char c = client.read(); 509#ifdef DEBUG_POP_EXTRA 510 USE_SERIAL.print( c); 511#endif 512 } 513 return; 514} 515 516 517void popFail() 518{ 519 int loopCount = 0; 520 521#ifdef DEBUG_POP 522 USE_SERIAL.println(F("popFail")); 523#endif 524 525 while(!client.available()) 526 { 527 delay(1); 528 loopCount++; 529 530 // if nothing received for 10 seconds, timeout 531 if(loopCount > 10000) 532 { 533 client.stop(); 534#ifdef DEBUG_POP 535 USE_SERIAL.println(F("\ 536Timeout")); 537#endif 538 return; 539 } 540 } 541 542 client.stop(); 543 544#ifdef DEBUG_POP 545 USE_SERIAL.println(F("disconnected due to fail")); 546#endif 547} 548 549
demo_firmware_esp8266_ifttt_remote_control
c_cpp
The code to be uploaded to ESP8266 module of the LYTWiFi Arduino shield for complete LYT led bulb remote control and weather forecast usage.
1/* 2 * This program is free software; you can redistribute it and/or modify 3 * it under the terms of the GNU General Public License as published by 4 * the Free Software Foundation; either version 2 of the License, or 5 * (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 15 * MA 02110-1301, USA. 16 */ 17 18/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * 19 Code by AUTHOMETION S.r.l. 20 Version: 0.00 21 Date: 01.08.2016 22 * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 23 24// Using EmailPop3 sketch from Arduino playground as guideline: 25 26#include <Arduino.h> 27 28#include <ESP8266WiFi.h> 29#include <ESP8266WiFiMulti.h> 30 31#include <ESP8266HTTPClient.h> 32#include <SoftwareSerial.h> 33 34//#define USE_SERIAL Serial 35#define USE_SERIAL srvTerm 36#define ARDUINO_IF Serial 37 38SoftwareSerial srvTerm(14,13); 39 40 41// Use comments to enable or disable this define for debug messages 42//#define DEBUG_POP 43 44// Use comments to enable or disable the deleting of the mail 45#define ENABLE_DELETE_POP 46 47ESP8266WiFiMulti WiFiMulti; 48bool sent(false); 49 50 51// The mac address must be an unique number 52byte mac[] = { 0xDE, 0x36, 0x5F, 0x0A, 0x19, 0x83 }; 53// change network settings to yours 54IPAddress ip( 0, 0, 0, 0); 55IPAddress gateway( 0, 0, 0, 0 ); 56IPAddress subnet( 0, 0, 0, 0 ); 57 58// Set the server POP3 address, the port, the user and password. 59// The POP3 mail server is something like this: 60// mail.yourdomain.com, pop.yourdomain.com, pop3.yourdomain.com 61// Using PROGMEM for these causes a fail when trying to connect and log in. 62const char pop_server[] = <pop mail server>; 63const int pop_port = 110; 64const char pop_user[] = <email address>; 65const char pop_pass[] = <email password>; 66 67 68// The number of milliseconds timeout for parseInt() and find(). 69// The response time for the Server can still be 10 seconds. 70#define POP_TIMEOUT 10000 71#define POP_TIMEOUT_DEFAULT 10000 72 73WiFiClient client; 74 75void CheckEmail(); 76int getEmail( char *pBuf, int nBufSize); 77boolean readOk( boolean readAll); 78void readRemaining(); 79void popFail(); 80 81void setup() { 82 83 ARDUINO_IF.begin(9600); 84 ARDUINO_IF.flush(); 85 86 USE_SERIAL.begin(9600); 87 USE_SERIAL.flush(); 88 // USE_SERIAL.setDebugOutput(true); 89 90 USE_SERIAL.println(); 91 USE_SERIAL.println(); 92 USE_SERIAL.println(); 93 94 for(uint8_t t = 4; t > 0; t--) 95 { 96 USE_SERIAL.printf("[SETUP] WAIT %d...\ 97", t); 98 USE_SERIAL.flush(); 99 delay(1000); 100 } 101 102 // Connect to the specified AccessPoint 103 WiFiMulti.addAP(<SSID>, <password>); 104 105 delay(3000); 106 107 if((WiFiMulti.run() == WL_CONNECTED)) 108 { 109 // Get current connection parameters. 110 ip = WiFi.localIP(); 111 gateway = WiFi.gatewayIP(); 112 subnet = WiFi.subnetMask(); 113 114 // print your local IP address. 115 USE_SERIAL.println(F( "WiFi started.")); 116 USE_SERIAL.print(F( "Local IP = ")); 117 USE_SERIAL.println(ip); 118 USE_SERIAL.print(F( "Gateway IP = ")); 119 USE_SERIAL.println(gateway); 120 USE_SERIAL.print(F( "Subnet Mask = ")); 121 USE_SERIAL.println(subnet); 122 } 123} 124 125void loop() 126{ 127 128 if (!sent) 129 { 130 // wait for WiFi connection 131 if((WiFiMulti.run() == WL_CONNECTED)) 132 { 133 HTTPClient http; 134 135 USE_SERIAL.print("[HTTP] begin...\ 136"); 137 // configure traged server and url 138 //http.begin("192.168.1.12", 443, "/test.html", true, "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS 139 140 http.begin("https://maker.ifttt.com/trigger/lytwifi_remote_control_on/with/key/<maker channel key>"); //HTTP 141 142 143 USE_SERIAL.print("[HTTP] GET...\ 144"); 145 // start connection and send HTTP header 146 int httpCode = http.GET(); 147 if(httpCode) 148 { 149 // HTTP header has been send and Server response header has been handled 150 USE_SERIAL.printf("[HTTP] GET... code = %d\ 151", httpCode); 152 153 // file found at server 154 if(httpCode == 200) 155 { 156 String payload = http.getString(); 157 USE_SERIAL.println(payload); 158 159 sent = true; 160 } 161 } 162 else 163 { 164 USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\ 165"); 166 } 167 } 168 else USE_SERIAL.print("No connection...\ 169"); 170 } 171 172 delay(5000); 173 174 CheckEmail(); 175 176} 177 178void CheckEmail() 179{ 180 // Create a buffer to receive the commands in (that is the Subject of the mail). 181 char buffer[32]; 182 183 // The getEmail gets the text of the mail Subject into the buffer. 184 // The valid number of received characters are returned. 185 // If the return value is < 0, it is an error. 186 int n = getEmail( buffer, sizeof(buffer)); 187 188 if( n<0) 189 { 190 USE_SERIAL.print(F("Email POP3 failed, error = ")); 191 USE_SERIAL.println( n); 192 } 193 else 194 { 195 if( n == 0) 196 { 197 USE_SERIAL.println(F("Ready, nothing to do.")); 198 } 199 else 200 { 201 // 'n' is > 0, a command received. 202 //Serial.print(F("Email checked, Command = \\"")); 203 //Serial.print( buffer); 204 //Serial.println(F("\\"")); 205 206 //Serial.print(F("Email checked, Command: \ \ 207")); 208 char temp[32] = ":"; 209 strcat(temp,buffer); 210 USE_SERIAL.println(temp); 211 212 ARDUINO_IF.println(temp); 213 214 } 215 } 216} 217 218// getEmail 219// -------- 220// Find an email on a mail server, using POP3. 221// The Subject should start with "ARDUINO " and the text 222// after that is copied into pBuf. 223// 224// The data in pBuf is only valid if the return value is not an error 225// (an error is return value less than zero). 226// 227int getEmail( char *pBuf, int nBufSize) 228{ 229 // nBytes is the number of bytes that is returned by getEmail. 230 int nBytes = 0; 231 232 // Connect to server 233 // client.connect returns '1' if okay, or negative number if error. 234 // SUCCESS 1 235 // 0 (error, unknown timeout, perhaps an error in the library) 236 // TIMED_OUT -1 237 // INVALID_SERVER -2 238 // TRUNCATED -3 239 // INVALID_RESPONSE -4 240 // -5 (there is no mail server at that IP address and port) 241 // The string for the server must be a normal string in sram, no PROGMEM allowed. 242 int nError = client.connect( pop_server, pop_port); 243 244 // During testing, a value of zero was sometimes returned. 245 // This is not according to the documentation and it is an error. 246 // Therefor the non-error value '0' is turned into a negative number to 247 // indicate an error. 248 if( nError == 0) 249 return( -200); 250 251 // Only a value of 1 is okay. 252 if( nError != 1) 253 return( nError); 254 255#ifdef DEBUG_POP 256 USE_SERIAL.println(F("connected")); 257#endif 258 259 // The server should respond with "+OK" and maybe more text after that. 260 // Check if "+OK" can be read. 261 // The parameter 'true' is to read also everything after the "+OK". 262 if(!readOk( true)) 263 return -102; 264 265#ifdef DEBUG_POP 266 USE_SERIAL.println(F("command USER")); 267#endif 268 client.print(F( "USER ")); 269 client.println( pop_user); 270 if(!readOk( true)) 271 return -103; 272 273#ifdef DEBUG_POP 274 USE_SERIAL.println(F("command PASS")); 275#endif 276 client.print(F( "PASS ")); 277 client.println( pop_pass); 278 if(!readOk( true)) 279 return -104; 280 281#ifdef DEBUG_POP 282 USE_SERIAL.println(F("command STAT")); 283#endif 284 client.println(F( "STAT")); 285 if(!readOk( false)) 286 return -105; 287 288 // The whole line was like this: "+OK 3 15343" 289 // It means that 3 emails are waiting with a total size of 15343. 290 // At this moment, the "+OK" is read, but nothing else. 291 // Check if there is a space after "+OK". 292 char c = client.read(); 293 if( c != ' ') 294 return -106; 295 296 client.setTimeout( POP_TIMEOUT); // set timeout lower for parseInt 297 // Read the number of emails that are on the server. 298 int nMails = client.parseInt(); 299 client.setTimeout( POP_TIMEOUT_DEFAULT); // restore timeout to 1 second 300 301 // Read the remaining of the response to STAT. 302 readRemaining(); 303 304#ifdef DEBUG_POP 305 USE_SERIAL.print(F( "Number of emails=")); 306 USE_SERIAL.println( nMails); 307#endif 308 309 // Test if there are emails waiting. 310 if( nMails == 0) 311 { 312 // No emails, but no error. Set buffer to empty string. 313 nBytes = 0; // the returned value 314 pBuf[0] = '\\0'; // set empty string 315 } 316 else if( nMails > 0) 317 { 318 // emails are waiting. 319 // Scan the emails until the first is found with the keyword "ARDUINO " at the 320 // beginning of the "Subject: ". 321 322 boolean found_and_ready = false; 323 for( int nMailNumber = 1; nMailNumber <= nMails && !found_and_ready; nMailNumber++) 324 { 325 // The command RETR <x> gets the whole mail. 326 // The command TOP <x> <size> gets the header plus 'size' of the body. 327 328#ifdef DEBUG_POP 329 USE_SERIAL.print(F( "command TOP ")); 330 USE_SERIAL.print( nMailNumber); 331 USE_SERIAL.println(F( " 0")); 332#endif 333 client.print(F( "TOP ")); 334 client.print( nMailNumber); 335 client.println(F( " 0")); 336 337 // Use readOk with parameter 'false' to stop reading after "+OK". 338 if(!readOk( false)) 339 return -107; 340 // The header of the email is waiting to be read, use the Stream.find() to look for the Subject. 341 // The text "Subject: " should be at the beginning of a line, but that is not tested. 342 // The first found text "Subject: " is assumed to be the real subject. 343 // I have checked many years of emails, and the text is always "Subject: ", and never "SUBJECT: ". 344 // At the moment, it is not possible to use the F() macro for Stream.find 345 // Only the email that starts with "ARDUINO " at the start of the Subject is used. 346 347 client.setTimeout( POP_TIMEOUT); // set short timeout for find(). 348 // find() returns true if found and false if not. 349 boolean foundsubject = client.find( "Subject: ARDUINO "); 350 client.setTimeout( POP_TIMEOUT_DEFAULT); // restore timeout to 1 second 351 352 if( foundsubject) 353 { 354#ifdef DEBUG_POP 355 USE_SERIAL.println(F("Found an email for me")); 356#endif 357 358 // Read the remaining subject (that is the command for the Arduino) into a buffer. 359 // Every line from the mail server should end with CR + LF, 360 // but to be sure, both CR and LF are checked. 361 // Alternative: 362 // client.readBytesUntil('\r', pBuf, nBufSize); 363 364 // The last position in the buffer is reserved for the zero terminator. 365 // So read data until (nBufSize-1). 366 int i; 367 for( i = 0; i < (nBufSize-1) && client.available(); i++) 368 { 369 char c = client.read(); 370 if (c == '\r' || c == '\n') 371 break; 372 pBuf[i] = c; 373 } 374 // Add zero terminator 375 pBuf[i] = '\\0'; 376 nBytes = i; // the number of received bytes is returned by the getEmail() function. 377 378 // More text of the header could be following the Subject. 379 // That is read and disregarded. 380 readRemaining(); 381 382#ifdef DEBUG_POP 383 USE_SERIAL.print(F( "Subject = \\"ARDUINO ")); 384 USE_SERIAL.print( pBuf); 385 USE_SERIAL.println(F( "\\"")); 386#endif 387 388#ifdef ENABLE_DELETE_POP 389 // Delete the just read message. 390#ifdef DEBUG_POP 391 USE_SERIAL.print(F( "command DELE ")); 392 USE_SERIAL.println( nMailNumber); 393#endif 394 client.print(F( "DELE ")); 395 client.println( nMailNumber); 396 if(!readOk( true)) 397 return -108; 398#endif 399 400 // Everything is okay, the mail is read and deleted. 401 // Ready for now, don't process the remaining emails. 402 found_and_ready = true; 403 } 404 else 405 { 406#ifdef DEBUG_POP 407 USE_SERIAL.println(F("No ARDUINO keyword in subject")); 408#endif 409 // This email has no "Subject: ARDUINO ". 410 // But the remaining text has still to be read and disregarded. 411 readRemaining(); 412 } 413 } 414 } 415 416#ifdef DEBUG_POP 417 USE_SERIAL.println(F( "Sending QUIT")); 418#endif 419 client.println(F( "QUIT")); 420 421 // After "QUIT", the server still respons with "+OK", 422 // but after that, the connection might get lost. 423 // So don't read everything after "+OK" (use parameter 'false' for readOk). 424 if(!readOk( false)) 425 return -109; 426 427 client.stop(); 428 429#ifdef DEBUG_POP 430 USE_SERIAL.println(F("normally disconnected")); 431#endif 432 433 return( nBytes); 434} 435 436 437// Read the response from the mail server. 438// That is "+OK" if everything is okay. 439// Parameter 'readAll' is to read every character after the "+OK". 440boolean readOk( boolean readAll) 441{ 442 // Check the response "+OK" from the mail server 443 // In most cases that is followed by a space and more text, but not always. 444 // In case of an error the text "-ERR" is received. 445 446 int loopCount = 0; 447 char bufOk[4]; 448 449 // Wait for response of mail server, with a timout. 450 while(!client.available()) 451 { 452 delay(1); 453 loopCount++; 454 455 // if nothing received for 10 seconds, timeout 456 if(loopCount > 20000) 457 { 458 client.stop(); 459#ifdef DEBUG_POP 460 USE_SERIAL.println(F("\ 461Timeout")); 462#endif 463 return false; 464 } 465 } 466 467 // Read the first three bytes. 468 client.readBytes(bufOk, 3); 469 470#ifdef DEBUG_POP 471 USE_SERIAL.write(bufOk, 3); 472 USE_SERIAL.println(); 473#endif 474 475 // Is it "+OK" ? 476 if( strncmp( bufOk, "+OK", 3) != 0) 477 { 478 popFail(); 479 return false; 480 } 481 482 // When the text after "+OK" is not needed, everything 483 // else can be read and disregarded 484 // (or shown in the serial monitor during debugging). 485 if( readAll) 486 readRemaining(); 487 488 return true; 489} 490 491 492void readRemaining() 493{ 494 // This function is called after checking the "+OK". 495 // It reads everything from the server, until no more text is 496 // available. 497 498 while(client.available()) 499 { 500 char c = client.read(); 501#ifdef DEBUG_POP_EXTRA 502 USE_SERIAL.print( c); 503#endif 504 } 505 return; 506} 507 508 509void popFail() 510{ 511 int loopCount = 0; 512 513#ifdef DEBUG_POP 514 USE_SERIAL.println(F("popFail")); 515#endif 516 517 while(!client.available()) 518 { 519 delay(1); 520 loopCount++; 521 522 // if nothing received for 10 seconds, timeout 523 if(loopCount > 10000) 524 { 525 client.stop(); 526#ifdef DEBUG_POP 527 USE_SERIAL.println(F("\ 528Timeout")); 529#endif 530 return; 531 } 532 } 533 534 client.stop(); 535 536#ifdef DEBUG_POP 537 USE_SERIAL.println(F("disconnected due to fail")); 538#endif 539} 540 541
demo_firmware_esp8266_ifttt_thermometer
c_cpp
The code to be uploaded to ESP8266 module of the LYTWiFi arduino shield for using of LYT led bulb as a thermometer.
1/* 2 * This program is free software; you can redistribute it and/or modify 3 * it under the terms of the GNU General Public License as published by 4 * the Free Software Foundation; either version 2 of the License, or 5 * (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 15 * MA 02110-1301, USA. 16 */ 17 18/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * 19 Code by AUTHOMETION S.r.l. 20 Version: 0.00 21 Date: 01.08.2016 22 * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 23 24// Using EmailPop3 sketch from Arduino playground as guideline: 25 26#include <Arduino.h> 27 28#include <ESP8266WiFi.h> 29#include <ESP8266WiFiMulti.h> 30 31#include <ESP8266HTTPClient.h> 32#include <SoftwareSerial.h> 33 34//#define USE_SERIAL Serial 35#define USE_SERIAL srvTerm 36#define ARDUINO_IF Serial 37 38SoftwareSerial srvTerm(14,13); 39 40 41// Use comments to enable or disable this define for debug messages 42//#define DEBUG_POP 43 44// Use comments to enable or disable the deleting of the mail 45#define ENABLE_DELETE_POP 46 47ESP8266WiFiMulti WiFiMulti; 48bool sent(false); 49 50 51// The mac address must be an unique number 52byte mac[] = { 0xDE, 0x36, 0x5F, 0x0A, 0x19, 0x83 }; 53// change network settings to yours 54IPAddress ip( 0, 0, 0, 0); 55IPAddress gateway( 0, 0, 0, 0 ); 56IPAddress subnet( 0, 0, 0, 0 ); 57 58// Set the server POP3 address, the port, the user and password. 59// The POP3 mail server is something like this: 60// mail.yourdomain.com, pop.yourdomain.com, pop3.yourdomain.com 61// Using PROGMEM for these causes a fail when trying to connect and log in. 62const char pop_server[] = <pop server name>; 63const int pop_port = 110; 64const char pop_user[] = <email address>; 65const char pop_pass[] = <email password>; 66 67 68// The number of milliseconds timeout for parseInt() and find(). 69// The response time for the Server can still be 10 seconds. 70#define POP_TIMEOUT 10000 71#define POP_TIMEOUT_DEFAULT 10000 72 73WiFiClient client; 74 75void CheckEmail(); 76int getEmail( char *pBuf, int nBufSize); 77boolean readOk( boolean readAll); 78void readRemaining(); 79void popFail(); 80 81void setup() { 82 83 ARDUINO_IF.begin(9600); 84 ARDUINO_IF.flush(); 85 86 USE_SERIAL.begin(9600); 87 USE_SERIAL.flush(); 88 // USE_SERIAL.setDebugOutput(true); 89 90 USE_SERIAL.println(); 91 USE_SERIAL.println(); 92 USE_SERIAL.println(); 93 94 for(uint8_t t = 4; t > 0; t--) 95 { 96 USE_SERIAL.printf("[SETUP] WAIT %d...\ 97", t); 98 USE_SERIAL.flush(); 99 delay(1000); 100 } 101 102 // Connect to the specified AccessPoint 103 WiFiMulti.addAP(<SSID>, <password>); 104 105 delay(3000); 106 107 if((WiFiMulti.run() == WL_CONNECTED)) 108 { 109 // Get current connection parameters. 110 ip = WiFi.localIP(); 111 gateway = WiFi.gatewayIP(); 112 subnet = WiFi.subnetMask(); 113 114 // print your local IP address. 115 USE_SERIAL.println(F( "WiFi started.")); 116 USE_SERIAL.print(F( "Local IP = ")); 117 USE_SERIAL.println(ip); 118 USE_SERIAL.print(F( "Gateway IP = ")); 119 USE_SERIAL.println(gateway); 120 USE_SERIAL.print(F( "Subnet Mask = ")); 121 USE_SERIAL.println(subnet); 122 } 123} 124 125void loop() 126{ 127 128 if (!sent) 129 { 130 // wait for WiFi connection 131 if((WiFiMulti.run() == WL_CONNECTED)) 132 { 133 HTTPClient http; 134 135 USE_SERIAL.print("[HTTP] begin...\ 136"); 137 138 http.begin("https://maker.ifttt.com/trigger/lytwifi_thermometer_on/with/key/<maker channel key>"); //HTTP 139 140 141 USE_SERIAL.print("[HTTP] GET...\ 142"); 143 // start connection and send HTTP header 144 int httpCode = http.GET(); 145 if(httpCode) 146 { 147 // HTTP header has been send and Server response header has been handled 148 USE_SERIAL.printf("[HTTP] GET... code = %d\ 149", httpCode); 150 151 // file found at server 152 if(httpCode == 200) 153 { 154 String payload = http.getString(); 155 USE_SERIAL.println(payload); 156 157 sent = true; 158 } 159 } 160 else 161 { 162 USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\ 163"); 164 } 165 } 166 else USE_SERIAL.print("No connection...\ 167"); 168 } 169 170 delay(5000); 171 172 CheckEmail(); 173 174} 175 176void CheckEmail() 177{ 178 // Create a buffer to receive the commands in (that is the Subject of the mail). 179 char buffer[32]; 180 181 // The getEmail gets the text of the mail Subject into the buffer. 182 // The valid number of received characters are returned. 183 // If the return value is < 0, it is an error. 184 int n = getEmail( buffer, sizeof(buffer)); 185 186 if( n<0) 187 { 188 USE_SERIAL.print(F("Email POP3 failed, error = ")); 189 USE_SERIAL.println( n); 190 } 191 else 192 { 193 if( n == 0) 194 { 195 USE_SERIAL.println(F("Ready, nothing to do.")); 196 } 197 else 198 { 199 // 'n' is > 0, a command received. 200 //Serial.print(F("Email checked, Command = \\"")); 201 //Serial.print( buffer); 202 //Serial.println(F("\\"")); 203 204 //Serial.print(F("Email checked, Command: \ \ 205")); 206 char cmd[32] = ":"; 207 int temp = atoi(buffer); 208 if (temp < 18) 209 strcat(cmd,"RGB,0,0,0,0,255,1"); 210 else if ((temp>=18)&&(temp<25)) 211 strcat(cmd,"RGB,0,0,0,255,0,1"); 212 else if ((temp>=25)&&(temp<30)) 213 strcat(cmd,"RGB,0,0,127,127,0,1"); 214 else if (temp>=30) 215 strcat(cmd,"RGB,0,0,255,0,0,1"); 216 217 218 USE_SERIAL.println(cmd); 219 ARDUINO_IF.println(cmd); 220 221 } 222 } 223} 224 225// getEmail 226// -------- 227// Find an email on a mail server, using POP3. 228// The Subject should start with "ARDUINO " and the text 229// after that is copied into pBuf. 230// 231// The data in pBuf is only valid if the return value is not an error 232// (an error is return value less than zero). 233// 234int getEmail( char *pBuf, int nBufSize) 235{ 236 // nBytes is the number of bytes that is returned by getEmail. 237 int nBytes = 0; 238 239 // Connect to server 240 // client.connect returns '1' if okay, or negative number if error. 241 // SUCCESS 1 242 // 0 (error, unknown timeout, perhaps an error in the library) 243 // TIMED_OUT -1 244 // INVALID_SERVER -2 245 // TRUNCATED -3 246 // INVALID_RESPONSE -4 247 // -5 (there is no mail server at that IP address and port) 248 // The string for the server must be a normal string in sram, no PROGMEM allowed. 249 int nError = client.connect( pop_server, pop_port); 250 251 // During testing, a value of zero was sometimes returned. 252 // This is not according to the documentation and it is an error. 253 // Therefor the non-error value '0' is turned into a negative number to 254 // indicate an error. 255 if( nError == 0) 256 return( -200); 257 258 // Only a value of 1 is okay. 259 if( nError != 1) 260 return( nError); 261 262#ifdef DEBUG_POP 263 USE_SERIAL.println(F("connected")); 264#endif 265 266 // The server should respond with "+OK" and maybe more text after that. 267 // Check if "+OK" can be read. 268 // The parameter 'true' is to read also everything after the "+OK". 269 if(!readOk( true)) 270 return -102; 271 272#ifdef DEBUG_POP 273 USE_SERIAL.println(F("command USER")); 274#endif 275 client.print(F( "USER ")); 276 client.println( pop_user); 277 if(!readOk( true)) 278 return -103; 279 280#ifdef DEBUG_POP 281 USE_SERIAL.println(F("command PASS")); 282#endif 283 client.print(F( "PASS ")); 284 client.println( pop_pass); 285 if(!readOk( true)) 286 return -104; 287 288#ifdef DEBUG_POP 289 USE_SERIAL.println(F("command STAT")); 290#endif 291 client.println(F( "STAT")); 292 if(!readOk( false)) 293 return -105; 294 295 // The whole line was like this: "+OK 3 15343" 296 // It means that 3 emails are waiting with a total size of 15343. 297 // At this moment, the "+OK" is read, but nothing else. 298 // Check if there is a space after "+OK". 299 char c = client.read(); 300 if( c != ' ') 301 return -106; 302 303 client.setTimeout( POP_TIMEOUT); // set timeout lower for parseInt 304 // Read the number of emails that are on the server. 305 int nMails = client.parseInt(); 306 client.setTimeout( POP_TIMEOUT_DEFAULT); // restore timeout to 1 second 307 308 // Read the remaining of the response to STAT. 309 readRemaining(); 310 311#ifdef DEBUG_POP 312 USE_SERIAL.print(F( "Number of emails=")); 313 USE_SERIAL.println( nMails); 314#endif 315 316 // Test if there are emails waiting. 317 if( nMails == 0) 318 { 319 // No emails, but no error. Set buffer to empty string. 320 nBytes = 0; // the returned value 321 pBuf[0] = '\\0'; // set empty string 322 } 323 else if( nMails > 0) 324 { 325 // emails are waiting. 326 // Scan the emails until the first is found with the keyword "ARDUINO " at the 327 // beginning of the "Subject: ". 328 329 boolean found_and_ready = false; 330 for( int nMailNumber = 1; nMailNumber <= nMails && !found_and_ready; nMailNumber++) 331 { 332 // The command RETR <x> gets the whole mail. 333 // The command TOP <x> <size> gets the header plus 'size' of the body. 334 335#ifdef DEBUG_POP 336 USE_SERIAL.print(F( "command TOP ")); 337 USE_SERIAL.print( nMailNumber); 338 USE_SERIAL.println(F( " 0")); 339#endif 340 client.print(F( "TOP ")); 341 client.print( nMailNumber); 342 client.println(F( " 0")); 343 344 // Use readOk with parameter 'false' to stop reading after "+OK". 345 if(!readOk( false)) 346 return -107; 347 // The header of the email is waiting to be read, use the Stream.find() to look for the Subject. 348 // The text "Subject: " should be at the beginning of a line, but that is not tested. 349 // The first found text "Subject: " is assumed to be the real subject. 350 // I have checked many years of emails, and the text is always "Subject: ", and never "SUBJECT: ". 351 // At the moment, it is not possible to use the F() macro for Stream.find 352 // Only the email that starts with "ARDUINO " at the start of the Subject is used. 353 354 client.setTimeout( POP_TIMEOUT); // set short timeout for find(). 355 // find() returns true if found and false if not. 356 boolean foundsubject = client.find( "Subject: ARDUINO "); 357 client.setTimeout( POP_TIMEOUT_DEFAULT); // restore timeout to 1 second 358 359 if( foundsubject) 360 { 361#ifdef DEBUG_POP 362 USE_SERIAL.println(F("Found an email for me")); 363#endif 364 365 // Read the remaining subject (that is the command for the Arduino) into a buffer. 366 // Every line from the mail server should end with CR + LF, 367 // but to be sure, both CR and LF are checked. 368 // Alternative: 369 // client.readBytesUntil('\r', pBuf, nBufSize); 370 371 // The last position in the buffer is reserved for the zero terminator. 372 // So read data until (nBufSize-1). 373 int i; 374 for( i = 0; i < (nBufSize-1) && client.available(); i++) 375 { 376 char c = client.read(); 377 if (c == '\r' || c == '\n') 378 break; 379 pBuf[i] = c; 380 } 381 // Add zero terminator 382 pBuf[i] = '\\0'; 383 nBytes = i; // the number of received bytes is returned by the getEmail() function. 384 385 // More text of the header could be following the Subject. 386 // That is read and disregarded. 387 readRemaining(); 388 389#ifdef DEBUG_POP 390 USE_SERIAL.print(F( "Subject = \\"ARDUINO ")); 391 USE_SERIAL.print( pBuf); 392 USE_SERIAL.println(F( "\\"")); 393#endif 394 395#ifdef ENABLE_DELETE_POP 396 // Delete the just read message. 397#ifdef DEBUG_POP 398 USE_SERIAL.print(F( "command DELE ")); 399 USE_SERIAL.println( nMailNumber); 400#endif 401 client.print(F( "DELE ")); 402 client.println( nMailNumber); 403 if(!readOk( true)) 404 return -108; 405#endif 406 407 // Everything is okay, the mail is read and deleted. 408 // Ready for now, don't process the remaining emails. 409 found_and_ready = true; 410 } 411 else 412 { 413#ifdef DEBUG_POP 414 USE_SERIAL.println(F("No ARDUINO keyword in subject")); 415#endif 416 // This email has no "Subject: ARDUINO ". 417 // But the remaining text has still to be read and disregarded. 418 readRemaining(); 419 } 420 } 421 } 422 423#ifdef DEBUG_POP 424 USE_SERIAL.println(F( "Sending QUIT")); 425#endif 426 client.println(F( "QUIT")); 427 428 // After "QUIT", the server still respons with "+OK", 429 // but after that, the connection might get lost. 430 // So don't read everything after "+OK" (use parameter 'false' for readOk). 431 if(!readOk( false)) 432 return -109; 433 434 client.stop(); 435 436#ifdef DEBUG_POP 437 USE_SERIAL.println(F("normally disconnected")); 438#endif 439 440 return( nBytes); 441} 442 443 444// Read the response from the mail server. 445// That is "+OK" if everything is okay. 446// Parameter 'readAll' is to read every character after the "+OK". 447boolean readOk( boolean readAll) 448{ 449 // Check the response "+OK" from the mail server 450 // In most cases that is followed by a space and more text, but not always. 451 // In case of an error the text "-ERR" is received. 452 453 int loopCount = 0; 454 char bufOk[4]; 455 456 // Wait for response of mail server, with a timout. 457 while(!client.available()) 458 { 459 delay(1); 460 loopCount++; 461 462 // if nothing received for 10 seconds, timeout 463 if(loopCount > 20000) 464 { 465 client.stop(); 466#ifdef DEBUG_POP 467 USE_SERIAL.println(F("\ 468Timeout")); 469#endif 470 return false; 471 } 472 } 473 474 // Read the first three bytes. 475 client.readBytes(bufOk, 3); 476 477#ifdef DEBUG_POP 478 USE_SERIAL.write(bufOk, 3); 479 USE_SERIAL.println(); 480#endif 481 482 // Is it "+OK" ? 483 if( strncmp( bufOk, "+OK", 3) != 0) 484 { 485 popFail(); 486 return false; 487 } 488 489 // When the text after "+OK" is not needed, everything 490 // else can be read and disregarded 491 // (or shown in the serial monitor during debugging). 492 if( readAll) 493 readRemaining(); 494 495 return true; 496} 497 498 499void readRemaining() 500{ 501 // This function is called after checking the "+OK". 502 // It reads everything from the server, until no more text is 503 // available. 504 505 while(client.available()) 506 { 507 char c = client.read(); 508#ifdef DEBUG_POP_EXTRA 509 USE_SERIAL.print( c); 510#endif 511 } 512 return; 513} 514 515 516void popFail() 517{ 518 int loopCount = 0; 519 520#ifdef DEBUG_POP 521 USE_SERIAL.println(F("popFail")); 522#endif 523 524 while(!client.available()) 525 { 526 delay(1); 527 loopCount++; 528 529 // if nothing received for 10 seconds, timeout 530 if(loopCount > 10000) 531 { 532 client.stop(); 533#ifdef DEBUG_POP 534 USE_SERIAL.println(F("\ 535Timeout")); 536#endif 537 return; 538 } 539 } 540 541 client.stop(); 542 543#ifdef DEBUG_POP 544 USE_SERIAL.println(F("disconnected due to fail")); 545#endif 546} 547 548
demo_firmware_lytwifi_2
c_cpp
The code to be uploaded to Arduino UNO board. Minor modifications to the official Authometion example code for LYT bulb control.
1/* 2 * This program is free software; you can redistribute it and/or modify 3 * it under the terms of the GNU General Public License as published by 4 * the Free Software Foundation; either version 2 of the License, or 5 * (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 15 * MA 02110-1301, USA. 16 */ 17 18/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * 19 Code by AUTHOMETION S.r.l. 20 Version: 1.02 21 Date: 14.10.2015 22 * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 23 24/**************************************************************************************** 25 * * 26 * !!!! INPORTANT NOTICE !!! * 27 * * 28 **************************************************************************************** 29 * * 30 * Please change the _SS_MAX_RX_BUFF value to 128 to use this sketch * 31 * You can find _SS_MAX_RX_BUFF value in the following header file * 32 * <Program dir>\\Arduino\\hardware\\arduino\\avr\\libraries\\SoftwareSerial\\SoftwareSerial.h * 33 * * 34 **************************************************************************************** 35 * * 36 * To use LYT Shield with Arduino MEGA 2560 please connect following pins: * 37 * * 38 * Shield MEGA 2560 * 39 * 5 A8 * 40 * 10 53 * 41 * 11 51 * 42 * 12 50 * 43 * 13 52 * 44 * * 45 * In JP1, JP2 and JP3 insert jumpers to left side * 46 * * 47 ****************************************************************************************/ 48 49#include <SPI.h> 50#include <EEPROM.h> 51#include <PL1167.h> 52#include <LYTWiFi.h> 53#include <Messenger.h> 54#include <string.h> 55#include "SoftwareSerial.h" 56#include <WiFiInterrupt.h> 57#include <stdlib.h> 58 59#define PL1167_CS_PIN 10 // 10 for Arduino Uno, 53 for Arduino MEGA 2560 60#define ESP8266_TCP_MUX 0 61#define ESP8266_TCP_SEVER_PORT 5001 62#define BUFFER_LENGTH 16 63 64SoftwareSerial mySerial(5, 6); // RX (5 for Arduino Uno / 62(A8) for Arduino Mega 2560), TX (6) 65LYTWiFi myNetWork(mySerial); 66//LYTWiFi myNetWork; 67 68//Redirect serial chars from Virt to WiFi serial port and vice versa 69uint8_t ui8BridgeOn=0; 70 71//Where commands are coming from 72uint8_t ui8WhoIs=0; 73 74// Instantiate Messenger object with the message function and the default separator (the comma character) 75Messenger SerWiFi=Messenger(','); 76Messenger SerHW=Messenger(','); 77 78char cBuf; 79uint8_t ui8Counter; 80 81// Define messenger function 82void messageWiFi() 83{ 84 char cBuffer[64]; 85 86 if(String(SerWiFi.buffer).indexOf(":")!=-1) 87 { 88 memset(cBuffer, '\\0', MESSENGERBUFFERSIZE); 89 strcpy(cBuffer, (String(SerWiFi.buffer).substring(String(SerWiFi.buffer).indexOf(":")+1, String(SerWiFi.buffer).indexOf("\ \ 90"))).c_str()); 91 strcpy(SerWiFi.buffer, cBuffer); 92 ui8WhoIs=1; 93 SerHW.setBufferString(SerWiFi.readBufferString()); 94 95 } 96} 97 98// Define messenger function 99void messageArduino() 100{ 101 uint8_t ui8Counter1, ui8Counter2, ui8Total; 102 uint8_t ui8MODE =0; 103 uint8_t ui8ADDR_A =0; 104 uint8_t ui8ADDR_B =0; 105 uint8_t ui8ADDR_A1=0; 106 uint8_t ui8ADDR_B1=0; 107 uint8_t ui8PAR1 =0; 108 uint8_t ui8PAR2 =0; 109 uint8_t ui8PAR3 =0; 110 uint8_t ui8DIM =0; 111 char cBuffer1[BUFFER_LENGTH]; 112 char cBuffer2[BUFFER_LENGTH]; 113 String sBuffer1; 114 String sBuffer2; 115 long lBaud; 116 117 // POWER ON LYT: PON,ADDR_A,ADDR_B,MODE 118 if (SerHW.checkString("PON")) 119 { 120 ui8ADDR_A=SerHW.readInt(); 121 ui8ADDR_B=SerHW.readInt(); 122 ui8MODE=SerHW.readInt(); 123 ui8PAR1=SerHW.readInt(); 124 memset(cBuffer2, '\\0', BUFFER_LENGTH); 125 if(myNetWork.ui8fSwitchOnAndCheck(ui8ADDR_A, ui8ADDR_B, ui8MODE, ui8PAR1)==C_ACK) 126 memcpy(cBuffer2, F(" OK!"), 4); 127 else 128 memcpy(cBuffer2, F(" KO!"), 4); 129 memset(cBuffer1, '\\0', BUFFER_LENGTH); 130 memcpy(cBuffer1, F("PON:"), 4); 131 strcat(cBuffer1, cBuffer2); 132 if(ui8WhoIs==1) 133 { 134 Serial.println(cBuffer1); 135 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 136 } 137 else 138 Serial.println(cBuffer1); 139 } 140 // POWER OFF LYT: POF,ADDR_A,ADDR_B,MODE 141 else if(SerHW.checkString("POF")) 142 { 143 ui8ADDR_A=SerHW.readInt(); 144 ui8ADDR_B=SerHW.readInt(); 145 ui8MODE=SerHW.readInt(); 146 ui8PAR1=SerHW.readInt(); 147 memset(cBuffer2, '\\0', BUFFER_LENGTH); 148 if(myNetWork.ui8fSwitchOffAndCheck(ui8ADDR_A, ui8ADDR_B, ui8MODE, ui8PAR1)==C_ACK) 149 memcpy(cBuffer2, F(" OK!"), 4); 150 else 151 memcpy(cBuffer2, F(" KO!"), 4); 152 memset(cBuffer1, '\\0', BUFFER_LENGTH); 153 memcpy(cBuffer1, F("POF:"), 4); 154 strcat(cBuffer1, cBuffer2); 155 if(ui8WhoIs==1) 156 { 157 Serial.println(cBuffer1); 158 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 159 } 160 else 161 Serial.println(cBuffer1); 162 } 163 // SET RGB VALUES: RGB,ADDR_A,ADDR_B,PAR1,PAR2,PAR3,MODE 164 else if(SerHW.checkString("RGB")) 165 { 166 ui8ADDR_A=SerHW.readInt(); 167 ui8ADDR_B=SerHW.readInt(); 168 ui8PAR1=SerHW.readInt(); 169 ui8PAR2=SerHW.readInt(); 170 ui8PAR3=SerHW.readInt(); 171 ui8MODE=SerHW.readInt(); 172 ui8DIM=SerHW.readInt(); 173 memset(cBuffer2, '\\0', BUFFER_LENGTH); 174 if(myNetWork.ui8fSetRGBValuesAndCheck(ui8ADDR_A, ui8ADDR_B, ui8PAR1, ui8PAR2, ui8PAR3, ui8MODE, ui8DIM)==C_ACK) 175 memcpy(cBuffer2, F(" OK!"), 4); 176 else 177 memcpy(cBuffer2, F(" KO!"), 4); 178 memset(cBuffer1, '\\0', BUFFER_LENGTH); 179 memcpy(cBuffer1, F("RGB:"), 4); 180 strcat(cBuffer1, cBuffer2); 181 if(ui8WhoIs==1) 182 { 183 Serial.println(cBuffer1); 184 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 185 } 186 else 187 Serial.println(cBuffer1); 188 } 189 // SET BRIGHTNESS VALUE: SBR,ADDA,ADDB,PAR1,MODE,DIM 190 else if(SerHW.checkString("SBR")) 191 { 192 ui8ADDR_A=SerHW.readInt(); 193 ui8ADDR_B=SerHW.readInt(); 194 ui8PAR2=SerHW.readInt(); 195 ui8MODE=SerHW.readInt(); 196 ui8DIM=SerHW.readInt(); 197 memset(cBuffer2, '\\0', BUFFER_LENGTH); 198 if(myNetWork.ui8fSetBrightnessValueAndCheck(ui8ADDR_A, ui8ADDR_B, ui8PAR2, ui8MODE, ui8DIM)==C_ACK) 199 memcpy(cBuffer2, F(" OK!"), 4); 200 else 201 memcpy(cBuffer2, F(" KO!"), 4); 202 memset(cBuffer1, '\\0', BUFFER_LENGTH); 203 memcpy(cBuffer1, F("SBR:"), 4); 204 strcat(cBuffer1, cBuffer2); 205 if(ui8WhoIs==1) 206 { 207 Serial.println(cBuffer1); 208 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 209 } 210 else 211 Serial.println(cBuffer1); 212 } 213 // SET NEW ADDRESS: SEA,ADDR_A,ADDR_B,ADDR_A1,ADDR_B1 214 else if(SerHW.checkString("SEA")) 215 { 216 ui8ADDR_A=SerHW.readInt(); 217 ui8ADDR_B=SerHW.readInt(); 218 ui8ADDR_A1=SerHW.readInt(); 219 ui8ADDR_B1=SerHW.readInt(); 220 memset(cBuffer2, '\\0', BUFFER_LENGTH); 221 if(myNetWork.ui8fAddLampAddressAndCheck(ui8ADDR_A, ui8ADDR_B, ui8ADDR_A1, ui8ADDR_B1)==C_ACK) 222 memcpy(cBuffer2, F(" OK!"), 4); 223 else 224 memcpy(cBuffer2, F(" KO!"), 4); 225 memset(cBuffer1, '\\0', BUFFER_LENGTH); 226 memcpy(cBuffer1, F("SEA: "), 4); 227 strcat(cBuffer1, cBuffer2); 228 if(ui8WhoIs==1) 229 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 230 else 231 Serial.println(cBuffer1); 232 } 233 // DELETE ADDRESS: DEA,ADDR_A,ADDR_B,ADDR_A1,ADDR_B1 234 else if(SerHW.checkString("DEA")) 235 { 236 ui8ADDR_A=SerHW.readInt(); 237 ui8ADDR_B=SerHW.readInt(); 238 ui8ADDR_A1=SerHW.readInt(); 239 ui8ADDR_B1=SerHW.readInt(); 240 memset(cBuffer2, '\\0', BUFFER_LENGTH); 241 if(myNetWork.ui8fDeleteLampAddressAndCheck(ui8ADDR_A, ui8ADDR_B, ui8ADDR_A1, ui8ADDR_B1)==C_ACK) 242 memcpy(cBuffer2, F(" OK!"), 4); 243 else 244 memcpy(cBuffer2, F(" KO!"), 4); 245 memset(cBuffer1, '\\0', BUFFER_LENGTH); 246 memcpy(cBuffer1, F("DEA:"), 4); 247 strcat(cBuffer1, cBuffer2); 248 if(ui8WhoIs==1) 249 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 250 else 251 Serial.println(cBuffer1); 252 } 253 // RESET BULB: RES 254 else if(SerHW.checkString("RES")) 255 { 256 memset(cBuffer2, '\\0', BUFFER_LENGTH); 257 if(myNetWork.ui8fResetLampAddressAndCheck()==C_ACK) 258 memcpy(cBuffer2, F(" OK!"), 4); 259 else 260 memcpy(cBuffer2, F(" KO!"), 4); 261 memset(cBuffer1, '\\0', BUFFER_LENGTH); 262 memcpy(cBuffer1, F("RES:"), 4); 263 strcat(cBuffer1, cBuffer2); 264 if(ui8WhoIs==1) 265 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 266 else 267 Serial.println(cBuffer1); 268 } 269 // READ ADDRESS TABLE: RAT,ADDR_A,ADDR_B 270 else if(SerHW.checkString("RAT")) 271 { 272 ui8ADDR_A=SerHW.readInt(); 273 ui8ADDR_B=SerHW.readInt(); 274 memset(cBuffer2, '\\0', BUFFER_LENGTH); 275 if(myNetWork.ui8fReadLampAddressTableAndCheck(ui8ADDR_A, ui8ADDR_B)==C_ACK) 276 memcpy(cBuffer2, F(" OK!"), 4); 277 else 278 memcpy(cBuffer2, F(" KO!"), 4); 279 if(ui8WhoIs==1) 280 { 281 for(ui8Counter1=0; ui8Counter1<PROTOCOL_ADDRESS_TABLE_POSITIONS; ui8Counter1++) 282 { 283 memset(cBuffer1, '\\0', BUFFER_LENGTH); 284 sprintf(cBuffer1, "(%2d,%2d)", myNetWork.LampAddressTable.ui8AddressTable[ui8Counter1][0], myNetWork.LampAddressTable.ui8AddressTable[ui8Counter1][1]); 285 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 286 delay(100); 287 } 288 memset(cBuffer1, '\\0', BUFFER_LENGTH); 289 memcpy(cBuffer1, F("RAT:"), 4); 290 strcat(cBuffer1, cBuffer2); 291 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 292 } 293 else 294 { 295 for(ui8Counter1=0; ui8Counter1<PROTOCOL_ADDRESS_TABLE_POSITIONS; ui8Counter1++) 296 { 297 Serial.print(F("(")); 298 Serial.print(myNetWork.LampAddressTable.ui8AddressTable[ui8Counter1][0], DEC); 299 Serial.print(F(", ")); 300 Serial.print(myNetWork.LampAddressTable.ui8AddressTable[ui8Counter1][1], DEC); 301 Serial.print(F(") ")); 302 } 303 Serial.println(); 304 memset(cBuffer1, '\\0', BUFFER_LENGTH); 305 memcpy(cBuffer1, F("RAT:"), 4); 306 strcat(cBuffer1, cBuffer2); 307 Serial.println(cBuffer1); 308 } 309 } 310 // SAVE CURRENT STATE OF BULB: SAV,ADDR_A,ADDR_B, 311 else if(SerHW.checkString("SAV")) 312 { 313 ui8ADDR_A=SerHW.readInt(); 314 ui8ADDR_B=SerHW.readInt(); 315 memset(cBuffer2, '\\0', BUFFER_LENGTH); 316 if(myNetWork.ui8fSaveLampSettingsAndCheck(ui8ADDR_A, ui8ADDR_B)==C_ACK) 317 memcpy(cBuffer2, F(" OK!"), 4); 318 else 319 memcpy(cBuffer2, F(" KO!"), 4); 320 memset(cBuffer1, '\\0', BUFFER_LENGTH); 321 memcpy(cBuffer1, F("SAV:"), 4); 322 strcat(cBuffer1, cBuffer2); 323 if(ui8WhoIs==1) 324 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 325 else 326 Serial.println(cBuffer1); 327 } 328 // READ INFO STATUS: RIS,ADDR_A,ADDR_B 329 else if(SerHW.checkString("RIS")) 330 { 331 ui8ADDR_A=SerHW.readInt(); 332 ui8ADDR_B=SerHW.readInt(); 333 memset(cBuffer2, '\\0', BUFFER_LENGTH); 334 if(myNetWork.ui8fAskLampInfoStatusAndCheck(ui8ADDR_A, ui8ADDR_B)==C_ACK) 335 memcpy(cBuffer2, F(" OK!"), 4); 336 else 337 memcpy(cBuffer2, F(" KO!"), 4); 338 memset(cBuffer1, '\\0', BUFFER_LENGTH); 339 memcpy(cBuffer1, F("RIS:"), 4); 340 strcat(cBuffer1, cBuffer2); 341 if(ui8WhoIs==1) 342 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 343 else 344 { 345 Serial.print(F("Answers: ")); 346 for(ui8Counter1=0; ui8Counter1<PROTOCOL_ANSWER_LENGHT; ui8Counter1++) 347 { 348 Serial.print(myNetWork.ReceivedAnswer.AnswerStruct.ui8Answer[ui8Counter1], DEC); 349 Serial.print(F(" ")); 350 } 351 Serial.println(); 352 Serial.println(cBuffer1); 353 } 354 } 355 // SET SYNC WORD: SSW,ADDR_A,ADDR_B,ADDR_A1,ADDR_B1 356 else if(SerHW.checkString("SSW")) 357 { 358 ui8ADDR_A=SerHW.readInt(); 359 ui8ADDR_B=SerHW.readInt(); 360 ui8ADDR_A1=SerHW.readInt(); 361 ui8ADDR_B1=SerHW.readInt(); 362 memset(cBuffer2, '\\0', BUFFER_LENGTH); 363 if(myNetWork.ui8fSetSyncWordAndCheck(ui8ADDR_A, ui8ADDR_B, ui8ADDR_A1, ui8ADDR_B1)==C_ACK) 364 memcpy(cBuffer2, F(" OK!"), 4); 365 else 366 memcpy(cBuffer2, F(" KO!"), 4); 367 memset(cBuffer1, '\\0', BUFFER_LENGTH); 368 memcpy(cBuffer1, F("SSW:"), 4); 369 strcat(cBuffer1, cBuffer2); 370 if(ui8WhoIs==1) 371 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 372 else 373 Serial.println(cBuffer1); 374 } 375 // SET LOCAL SYNC WORD: SLSW,ADDR_A,ADDR_B,MODE 376 else if(SerHW.checkString("SLSW")) 377 { 378 ui8ADDR_A=SerHW.readInt(); 379 ui8ADDR_B=SerHW.readInt(); 380 ui8MODE=SerHW.readInt(); 381 myNetWork.vfSetLocalSyncWord(ui8ADDR_A, ui8ADDR_B, ui8MODE); 382 if(ui8WhoIs==1) 383 { 384 memset(cBuffer1, '\\0', BUFFER_LENGTH); 385 memcpy(cBuffer1, F("SLSW!"), 5); 386 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, 5); 387 } 388 else 389 Serial.println(F("SLSW!")); 390 } 391 // SET RGB VALUE IN FAST MODE: FRGB,ADDR_A,ADDR_B,PAR1,PAR2,PAR3 392 else if(SerHW.checkString("FRGB")) 393 { 394 ui8ADDR_A=SerHW.readInt(); 395 ui8ADDR_B=SerHW.readInt(); 396 ui8PAR1=SerHW.readInt(); 397 ui8PAR2=SerHW.readInt(); 398 ui8PAR3=SerHW.readInt(); 399 while(Serial.available()) 400 Serial.read(); 401 while(Serial.available()==0) 402 { 403 myNetWork.ui8fSetRGBValuesAndCheck(ui8ADDR_A, ui8ADDR_B, ui8PAR1, 0, 0, C_MULTICAST); 404 delay(100); 405 myNetWork.ui8fSetRGBValuesAndCheck(ui8ADDR_A, ui8ADDR_B, 0, ui8PAR2, 0, C_MULTICAST); 406 delay(100); 407 myNetWork.ui8fSetRGBValuesAndCheck(ui8ADDR_A, ui8ADDR_B, 0, 0, ui8PAR3, C_MULTICAST); 408 delay(100); 409 } 410 if(ui8WhoIs==1) 411 { 412 memset(cBuffer1, '\\0', BUFFER_LENGTH); 413 memcpy(cBuffer1, F("FRGB!"), 5); 414 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, 5); 415 } 416 else 417 Serial.println(F("FRGB!")); 418 } 419 // SET LOCAL RADIO CHANNEL: SLRC,CHAN,MODE 420 else if(SerHW.checkString("SLRC")) 421 { 422 ui8PAR1=SerHW.readInt(); 423 ui8MODE=SerHW.readInt(); 424 myNetWork.vfSetLocalChannel(ui8PAR1, ui8MODE); 425 if(ui8WhoIs==1) 426 { 427 memset(cBuffer1, '\\0', BUFFER_LENGTH); 428 memcpy(cBuffer1, F("SLRC!"), 5); 429 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, 5); 430 } 431 else 432 Serial.println(F("SLRC!")); 433 } 434 // SET RADIO CHANNEL OF LYT SHIELD: SRC,ADDR_A,ADDR_B,CHAN 435 else if(SerHW.checkString("SRC")) 436 { 437 ui8ADDR_A=SerHW.readInt(); 438 ui8ADDR_B=SerHW.readInt(); 439 ui8PAR1=SerHW.readInt(); 440 memset(cBuffer2, '\\0', BUFFER_LENGTH); 441 if(myNetWork.ui8fSetChannelAndCheck(ui8ADDR_A, ui8ADDR_B, ui8PAR1)==C_ACK) 442 memcpy(cBuffer2, F(" OK!"), 4); 443 else 444 memcpy(cBuffer2, F(" KO!"), 4); 445 memset(cBuffer1, '\\0', BUFFER_LENGTH); 446 memcpy(cBuffer1, F("SRC:"), 4); 447 strcat(cBuffer1, cBuffer2); 448 if(ui8WhoIs==1) 449 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, strlen(cBuffer1)); 450 else 451 Serial.println(cBuffer1); 452 } 453 // TEST RADIO CHANNEL EFFICENCY: TEST,ADDR_A,ADDR_B,CHAN 454 else if(SerHW.checkString("TEST")) 455 { 456 ui8ADDR_A=SerHW.readInt(); 457 ui8ADDR_B=SerHW.readInt(); 458 ui8Counter1=SerHW.readInt(); 459 while(Serial.available()) 460 Serial.read(); 461 while((Serial.available()==0)&&(ui8Counter1<=127)) 462 { 463 ui8Counter2=0; 464 while((myNetWork.ui8fSetChannelAndCheck(ui8ADDR_A, ui8ADDR_B, ui8Counter1)!=C_ACK)&&(ui8Counter2<3)) 465 { 466 delay(1000); 467 ui8Counter2++; 468 } 469 if(ui8Counter2<3) 470 { 471 ui8Total=0; 472 for(ui8Counter2=0; ui8Counter2<20; ui8Counter2++) 473 { 474 myNetWork.ui8fSetRGBValuesAndCheck(ui8ADDR_A, ui8ADDR_B, 10*ui8Counter2, 0, 0, C_UNICAST); 475 ui8Total+=myNetWork.ui8SentCommandCounterMem; 476 delay(500); 477 if(Serial.available()!=0) 478 break; 479 } 480 Serial.print("Channel: "); 481 Serial.print(ui8Counter1); 482 Serial.print(", Total number of sent commands: "); 483 Serial.println(ui8Total); 484 ui8Counter1++; 485 } 486 else 487 break; 488 } 489 if(ui8WhoIs==1) 490 { 491 memset(cBuffer1, '\\0', BUFFER_LENGTH); 492 memcpy(cBuffer1, F("TEST!"), 5); 493 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, 5); 494 } 495 else 496 Serial.println(F("TEST!")); 497 } 498 // ACTVATE BRIDGE between Serial and Virtual serial: BRIDGE 499 else if(SerHW.checkString("BRIDGE")) 500 { 501 ui8BridgeOn=1; 502 if(ui8WhoIs==1) 503 { 504 memset(cBuffer1, '\\0', BUFFER_LENGTH); 505 memcpy(cBuffer1, F("BRIDGE!"), 7); 506 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, 7); 507 } 508 else 509 Serial.println(F("BRIDGE!")); 510 } 511 // Discover all available WiFi routers: LAP 512 else if(SerHW.checkString("LAP")) 513 { 514 if(ui8WhoIs==0) 515 { 516 Serial.println(myNetWork.sfGetAPList_ESP8266()); 517 Serial.println(F("LAP!")); 518 } 519 } 520 // Join ESP8266: JOI,SSID,PWD 521 else if(SerHW.checkString("JOI")) 522 { 523 if(ui8WhoIs==0) 524 { 525 if(myNetWork.bfSetOprToStationSoftAP_ESP8266()) 526 { 527 myNetWork.bfRestart_ESP8266(); 528 SerHW.copyString(cBuffer1, BUFFER_LENGTH); 529 SerHW.copyString(cBuffer2, BUFFER_LENGTH); 530 if(myNetWork.bfJoinAP_ESP8266(String(cBuffer1), String(cBuffer2))) 531 { 532 Serial.println(F("Join: OK")); 533 delay(4000); 534 myNetWork.vfGetLocalIP_ESP8266(sBuffer1, sBuffer2); 535 Serial.print(F("Local IP Address :")); 536 Serial.println(sBuffer1); 537 Serial.print(F("Local Mac Address:")); 538 Serial.println(sBuffer2); 539 delay(2000); 540 if(myNetWork.bfEnableMUX_ESP8266()) 541 { 542 delay(2000); 543 if(myNetWork.bfStartTCPServer_ESP8266(ESP8266_TCP_SEVER_PORT)) 544 Serial.println(F("TCP Server: OK")); 545 else 546 Serial.println(F("TCP Server: FAIL")); 547 } 548 else 549 Serial.println(F("TCP Server: FAIL")); 550 } 551 else 552 Serial.println(F("Join: FAIL")); 553 } 554 else 555 Serial.println(F("FAIL: ESP8266 not configured")); 556 Serial.println(F("JOI!")); 557 } 558 } 559 // Start a ESP8266 TCP server: STCP 560 else if(SerHW.checkString("STCP")) 561 { 562 if(ui8WhoIs==0) 563 { 564 delay(2000); 565 myNetWork.vfGetLocalIP_ESP8266(sBuffer1, sBuffer2); 566 Serial.print(F("Local IP Address :")); 567 Serial.println(sBuffer1); 568 Serial.print(F("Local Mac Address:")); 569 Serial.println(sBuffer2); 570 delay(2000); 571 if(atoi(sBuffer1.c_str())>0) 572 { 573 if(myNetWork.bfEnableMUX_ESP8266()) 574 { 575 delay(2000); 576 if(myNetWork.bfStartTCPServer_ESP8266(ESP8266_TCP_SEVER_PORT)) 577 Serial.println(F("TCP Server: OK")); 578 else 579 Serial.println(F("TCP Server: FAIL")); 580 } 581 else 582 Serial.println(F("TCP Server: FAIL")); 583 } 584 else 585 Serial.println(F("FAIL: ESP8266 not connected to network")); 586 Serial.println(F("STCP!")); 587 } 588 } 589 // Read IP and Mac Addresses of ESP8266: RIP 590 else if(SerHW.checkString("RIP")) 591 { 592 if(ui8WhoIs==0) 593 { 594 myNetWork.vfGetLocalIP_ESP8266(sBuffer1, sBuffer2); 595 Serial.print(F("Local IP Address :")); 596 Serial.println(sBuffer1); 597 Serial.print(F("Local Mac Address:")); 598 Serial.println(sBuffer2); 599 Serial.println(F("RIP!")); 600 } 601 } 602 // Change Serial port baud rate (***SE ARRIVA DA WIFI CHE SUCCEDE***) 603 else if(SerHW.checkString("UART")) 604 { 605 lBaud=SerHW.readLong(); 606 Serial.end(); 607 delay(200); 608 Serial.begin(lBaud); 609 } 610 else if(SerHW.checkString("WDG")) 611 { 612 613 } 614 else 615 { 616 if(ui8WhoIs==1) 617 { 618 Serial.print(F("UNK FROM ESP! ")); 619 Serial.println(SerHW.readBufferString()); 620 621 memset(cBuffer1, '\\0', BUFFER_LENGTH); 622 memcpy(cBuffer1, F("UNK!"), 4); 623 myNetWork.bfSend_ESP8266(ESP8266_TCP_MUX, (const unsigned char*)cBuffer1, 4); 624 625 } 626 else 627 Serial.println(F("UNK!")); 628 } 629 ui8WhoIs=0; 630} 631 632void setup() 633{ 634 // Initialize Serial Communications 635 mySerial.begin(9600); 636 Serial.begin(9600, SERIAL_8N1); 637 638 myNetWork.vfInitialize(PL1167_CS_PIN); 639// vfISRInit(&myNetWork); 640 vfISRInit(); 641 642 SerWiFi.attach(messageWiFi); 643 SerHW.attach(messageArduino); 644 delay(500); 645} 646 647void loop() 648{ 649 while (Serial.available()) 650 { 651 if(ui8BridgeOn==0) 652 SerHW.process(Serial.read()); 653 else 654 { 655 cBuf=Serial.read(); 656 if(cBuf=='!') 657 ui8BridgeOn=0; 658 else 659 mySerial.print(cBuf); 660 } 661 } 662 while (mySerial.available()) 663 { 664 if(ui8BridgeOn==0) 665 SerWiFi.process(mySerial.read()); 666 else 667 { 668 cBuf=mySerial.read(); 669 if(cBuf=='!') 670 ui8BridgeOn=0; 671 else 672 Serial.print(cBuf); 673 } 674 } 675} 676
Comments
Only logged in users can leave comments
enardone74
0 Followers
•0 Projects
Table of contents
Intro
1
0