├── .gitattributes ├── .gitignore └── OTA.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /OTA.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "Adafruit_MQTT.h" 6 | #include "Adafruit_MQTT_Client.h" 7 | 8 | const char* ssid = "SSID Name"; 9 | const char* password = "Password"; 10 | int led = 16; 11 | int flash = 0; 12 | 13 | 14 | /************************* Adafruit.io Setup *********************************/ 15 | 16 | #define AIO_SERVER "io.adafruit.com" 17 | #define AIO_SERVERPORT 1883 // use 8883 for SSL 18 | #define AIO_USERNAME "username" 19 | #define AIO_KEY "KEY" 20 | 21 | 22 | bool flag =0; // For selecting Mode 23 | 24 | WiFiClient client; 25 | 26 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 27 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 28 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 29 | 30 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 31 | Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD); 32 | 33 | 34 | 35 | const char Relay2[] PROGMEM = AIO_USERNAME "/feeds/Mode"; 36 | Adafruit_MQTT_Subscribe Mode = Adafruit_MQTT_Subscribe(&mqtt, Relay2); 37 | /*************************** Sketch Code ************************************/ 38 | 39 | // Bug workaround for Arduino 1.6.6, it seems to need a function declaration 40 | // for some reason (only affects ESP8266, likely an arduino-builder bug). 41 | void MQTT_connect(); 42 | 43 | 44 | void setup() { 45 | pinMode(led,OUTPUT); 46 | pinMode(flash,INPUT); 47 | 48 | pinMode(5,OUTPUT); 49 | Serial.begin(115200); 50 | Serial.println("Booting"); 51 | WiFi.mode(WIFI_STA); 52 | WiFi.begin(ssid, password); 53 | while (WiFi.waitForConnectResult() != WL_CONNECTED) { 54 | Serial.println("Connection Failed! Rebooting..."); 55 | delay(5000); 56 | ESP.restart(); 57 | } 58 | 59 | // Port defaults to 8266 60 | // ArduinoOTA.setPort(8266); 61 | 62 | // Hostname defaults to esp8266-[ChipID] 63 | // ArduinoOTA.setHostname("My ESP"); 64 | 65 | // No authentication by default 66 | // ArduinoOTA.setPassword((const char *)"123"); // password 67 | 68 | ArduinoOTA.onStart([]() { 69 | digitalWrite(led,HIGH); 70 | Serial.println("Start"); 71 | }); 72 | ArduinoOTA.onEnd([]() { 73 | digitalWrite(led,LOW); 74 | Serial.println("\nEnd"); 75 | }); 76 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { 77 | Serial.printf("Progress: %u%%\r", (progress / (total / 100))); 78 | }); 79 | ArduinoOTA.onError([](ota_error_t error) { 80 | Serial.printf("Error[%u]: ", error); 81 | if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); 82 | else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); 83 | else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); 84 | else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); 85 | else if (error == OTA_END_ERROR) Serial.println("End Failed"); 86 | }); 87 | ArduinoOTA.begin(); 88 | Serial.println("Ready"); 89 | Serial.print("IP address: "); 90 | Serial.println(WiFi.localIP()); 91 | 92 | mqtt.subscribe(&Mode); 93 | } 94 | 95 | void loop() { 96 | 97 | MQTT_connect(); 98 | 99 | Adafruit_MQTT_Subscribe *subscription; 100 | while ((subscription = mqtt.readSubscription(10))) { 101 | 102 | if (subscription == &Mode) { 103 | Serial.print(F("Got_Mode: ")); 104 | Serial.println((char *)Mode.lastread); 105 | uint16_t num = atoi((char *)Mode.lastread); 106 | flag= num; 107 | } 108 | } 109 | 110 | 111 | if(flag==1) // OTA mode 112 | ArduinoOTA.handle(); 113 | 114 | else // normal mode 115 | { 116 | digitalWrite(led,LOW); 117 | delay(50); 118 | digitalWrite(led,HIGH); 119 | delay(30); 120 | 121 | } 122 | 123 | 124 | } 125 | 126 | 127 | 128 | void MQTT_connect() { 129 | int8_t ret; 130 | 131 | // Stop if already connected. 132 | if (mqtt.connected()) { 133 | return; 134 | } 135 | 136 | Serial.print("Connecting to MQTT... "); 137 | 138 | uint8_t retries = 3; 139 | while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 140 | Serial.println(mqtt.connectErrorString(ret)); 141 | Serial.println("Retrying MQTT connection in 5 seconds..."); 142 | mqtt.disconnect(); 143 | delay(5000); // wait 5 seconds 144 | retries--; 145 | if (retries == 0) { 146 | // basically die and wait for WDT to reset me 147 | while (1); 148 | } 149 | } 150 | Serial.println("MQTT Connected!"); 151 | } 152 | --------------------------------------------------------------------------------