├── .gitattributes ├── .gitignore └── Sonoff_using_ESP8266 └── Sonoff_using_ESP8266.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 | -------------------------------------------------------------------------------- /Sonoff_using_ESP8266/Sonoff_using_ESP8266.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | This is the code for the project DIY Sonoff using ESP8266 3 | 4 | Original code is from 5 | Adafruit MQTT Library ESP8266 Example 6 | 7 | Code edited by 8 | Sachin Soni 9 | http://www.techiesms.com 10 | ****************************************************/ 11 | #include 12 | #include "Adafruit_MQTT.h" 13 | #include "Adafruit_MQTT_Client.h" 14 | #define relay 2 15 | 16 | /************************* WiFi Access Point *********************************/ 17 | 18 | #define WLAN_SSID "...your SSID..." 19 | #define WLAN_PASS "...your password..." 20 | 21 | /************************* Adafruit.io Setup *********************************/ 22 | 23 | #define AIO_SERVER "io.adafruit.com" 24 | #define AIO_SERVERPORT 1883 // use 8883 for SSL 25 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 26 | #define AIO_KEY "...your AIO key..." 27 | 28 | /************ Global State (you don't need to change this!) ******************/ 29 | 30 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. 31 | WiFiClient client; 32 | // or... use WiFiFlientSecure for SSL 33 | //WiFiClientSecure client; 34 | 35 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 36 | Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 37 | 38 | /****************************** Feeds ***************************************/ 39 | 40 | // Setup a feed called 'photocell' for publishing. 41 | // Notice MQTT paths for AIO follow the form: /feeds/ 42 | //Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell"); 43 | 44 | // Setup a feed called 'onoff' for subscribing to changes. 45 | Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Sonoff_1"); 46 | 47 | /*************************** Sketch Code ************************************/ 48 | 49 | // Bug workaround for Arduino 1.6.6, it seems to need a function declaration 50 | // for some reason (only affects ESP8266, likely an arduino-builder bug). 51 | void MQTT_connect(); 52 | 53 | void setup() { 54 | Serial.begin(115200); 55 | delay(10); 56 | 57 | Serial.println(F("Adafruit MQTT demo")); 58 | 59 | // Connect to WiFi access point. 60 | Serial.println(); Serial.println(); 61 | Serial.print("Connecting to "); 62 | Serial.println(WLAN_SSID); 63 | 64 | WiFi.begin(WLAN_SSID, WLAN_PASS); 65 | while (WiFi.status() != WL_CONNECTED) { 66 | delay(500); 67 | Serial.print("."); 68 | } 69 | Serial.println(); 70 | 71 | Serial.println("WiFi connected"); 72 | Serial.println("IP address: "); Serial.println(WiFi.localIP()); 73 | 74 | pinMode(relay,OUTPUT); 75 | 76 | // Setup MQTT subscription for onoff feed. 77 | mqtt.subscribe(&onoffbutton); 78 | } 79 | 80 | uint32_t x=0; 81 | 82 | void loop() { 83 | // Ensure the connection to the MQTT server is alive (this will make the first 84 | // connection and automatically reconnect when disconnected). See the MQTT_connect 85 | // function definition further below. 86 | MQTT_connect(); 87 | 88 | // this is our 'wait for incoming subscription packets' busy subloop 89 | // try to spend your time here 90 | 91 | Adafruit_MQTT_Subscribe *subscription; 92 | while ((subscription = mqtt.readSubscription(1))) { 93 | if (subscription == &onoffbutton) { 94 | Serial.print(F("Got: ")); 95 | Serial.println((char *)onoffbutton.lastread); 96 | uint16_t state = atoi((char *)onoffbutton.lastread); 97 | digitalWrite(relay,state); 98 | } 99 | } 100 | 101 | 102 | // ping the server to keep the mqtt connection alive 103 | // NOT required if you are publishing once every KEEPALIVE seconds 104 | /* 105 | if(! mqtt.ping()) { 106 | mqtt.disconnect(); 107 | } 108 | */ 109 | } 110 | 111 | // Function to connect and reconnect as necessary to the MQTT server. 112 | // Should be called in the loop function and it will take care if connecting. 113 | void MQTT_connect() { 114 | int8_t ret; 115 | 116 | // Stop if already connected. 117 | if (mqtt.connected()) { 118 | return; 119 | } 120 | 121 | Serial.print("Connecting to MQTT... "); 122 | 123 | uint8_t retries = 3; 124 | while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 125 | Serial.println(mqtt.connectErrorString(ret)); 126 | Serial.println("Retrying MQTT connection in 5 seconds..."); 127 | mqtt.disconnect(); 128 | delay(5000); // wait 5 seconds 129 | retries--; 130 | if (retries == 0) { 131 | // basically die and wait for WDT to reset me 132 | while (1); 133 | } 134 | } 135 | Serial.println("MQTT Connected!"); 136 | } 137 | --------------------------------------------------------------------------------