├── .gitattributes ├── .gitignore ├── Sonoff_with_Smart_Config └── Sonoff_with_Smart_Config.ino └── smart_config └── smart_config.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 thumbnail cache files 2 | Thumbs.db 3 | ehthumbs.db 4 | ehthumbs_vista.db 5 | 6 | # Folder config file 7 | Desktop.ini 8 | 9 | # Recycle Bin used on file shares 10 | $RECYCLE.BIN/ 11 | 12 | # Windows Installer files 13 | *.cab 14 | *.msi 15 | *.msm 16 | *.msp 17 | 18 | # Windows shortcuts 19 | *.lnk 20 | 21 | # ========================= 22 | # Operating System Files 23 | # ========================= 24 | -------------------------------------------------------------------------------- /Sonoff_with_Smart_Config/Sonoff_with_Smart_Config.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | This is the code for the project Sonoff with Smart config 3 | 4 | Original code is from 5 | DIY Sonoff using ESP8266 6 | 7 | Code edited by 8 | Sachin Soni 9 | http://www.techiesms.com 10 | ****************************************************/ 11 | #define relay 0 12 | #include 13 | #include "Adafruit_MQTT.h" 14 | #include "Adafruit_MQTT_Client.h" 15 | 16 | /************************* Adafruit.io Setup *********************************/ 17 | 18 | #define AIO_SERVER "io.adafruit.com" 19 | #define AIO_SERVERPORT 1883 // use 8883 for SSL 20 | #define AIO_USERNAME "AIO_USERNAME" 21 | #define AIO_KEY "AIO_KEY" 22 | 23 | /************ Global State (you don't need to change this!) ******************/ 24 | 25 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. 26 | WiFiClient client; 27 | // or... use WiFiFlientSecure for SSL 28 | //WiFiClientSecure client; 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, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 32 | 33 | /****************************** Feeds ***************************************/ 34 | 35 | // Setup a feed called 'photocell' for publishing. 36 | // Notice MQTT paths for AIO follow the form: /feeds/ 37 | //Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell"); 38 | 39 | // Setup a feed called 'onoff' for subscribing to changes. 40 | Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay1"); 41 | 42 | /*************************** Sketch Code ************************************/ 43 | 44 | // Bug workaround for Arduino 1.6.6, it seems to need a function declaration 45 | // for some reason (only affects ESP8266, likely an arduino-builder bug). 46 | void MQTT_connect(); 47 | 48 | void setup() { 49 | Serial.begin(115200); 50 | /* Set ESP32 to WiFi Station mode */ 51 | WiFi.mode(WIFI_STA); 52 | /* start SmartConfig */ 53 | WiFi.beginSmartConfig(); 54 | 55 | /* Wait for SmartConfig packet from mobile */ 56 | Serial.println("Waiting for SmartConfig."); 57 | while (!WiFi.smartConfigDone()) { 58 | delay(500); 59 | Serial.print("."); 60 | } 61 | Serial.println(""); 62 | Serial.println("SmartConfig done."); 63 | 64 | /* Wait for WiFi to connect to AP */ 65 | Serial.println("Waiting for WiFi"); 66 | while (WiFi.status() != WL_CONNECTED) { 67 | delay(500); 68 | Serial.print("."); 69 | } 70 | Serial.println("WiFi Connected."); 71 | Serial.print("IP Address: "); 72 | 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 | -------------------------------------------------------------------------------- /smart_config/smart_config.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void setup() { 4 | Serial.begin(115200); 5 | /* Set ESP8266 to WiFi Station mode */ 6 | WiFi.mode(WIFI_STA); 7 | /* start SmartConfig */ 8 | WiFi.beginSmartConfig(); 9 | 10 | /* Wait for SmartConfig packet from mobile */ 11 | Serial.println("Waiting for SmartConfig."); 12 | while (!WiFi.smartConfigDone()) { 13 | delay(500); 14 | Serial.print("."); 15 | } 16 | Serial.println(""); 17 | Serial.println("SmartConfig done."); 18 | 19 | /* Wait for WiFi to connect to AP */ 20 | Serial.println("Waiting for WiFi"); 21 | while (WiFi.status() != WL_CONNECTED) { 22 | delay(500); 23 | Serial.print("."); 24 | } 25 | Serial.println("WiFi Connected."); 26 | Serial.print("IP Address: "); 27 | Serial.println(WiFi.localIP()); 28 | } 29 | void loop() { 30 | } 31 | 32 | --------------------------------------------------------------------------------