├── .gitattributes └── code_for_workshop_.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /code_for_workshop_.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | This is the code for the project 3 | "Google Assistant Controllled Appliances" 4 | using a ESP8266 12e board 5 | 6 | Original code is from 7 | Adafruit MQTT Library ESP8266 Example 8 | Code edited by 9 | Sachin Soni 10 | http://www.techiesms.com 11 | ****************************************************/ 12 | 13 | // Necessary Library Declarations 14 | #include 15 | #include "Adafruit_MQTT.h" 16 | #include "Adafruit_MQTT_Client.h" 17 | 18 | //What does a libray means? 19 | //Library contains the detailed code of the functions used in the code. 20 | 21 | // Defining Pin for our Relay 22 | #define relay D1 23 | 24 | /************************* WiFi Access Point *********************************/ 25 | 26 | #define WLAN_SSID "SmS" 27 | #define WLAN_PASS "sms123458956" 28 | /************************* Adafruit.io Setup *********************************/ 29 | //This Will remain as it is for all adafruit accounts 30 | #define AIO_SERVER "io.adafruit.com" 31 | #define AIO_SERVERPORT 1883 // use 8883 for SSL 32 | 33 | //Here you have to enter the details of your Adafruit IO account. 34 | #define AIO_USERNAME "techiesms" 35 | #define AIO_KEY "912b30c900574034a653f41e2b4df838" 36 | 37 | /************ Global State (you don't need to change this!) ******************/ 38 | 39 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. 40 | WiFiClient client; 41 | // or... use WiFiFlientSecure for SSL 42 | //WiFiClientSecure client; 43 | 44 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 45 | Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 46 | 47 | /****************************** Feeds ***************************************/ 48 | 49 | // Setup a feed called 'onoffbutton' for subscribing to changes. 50 | //Here you need to add the feed name of the button you have created in your adafruit account. 51 | Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay1"); 52 | 53 | /*************************** Sketch Code ************************************/ 54 | 55 | // Bug workaround for Arduino 1.6.6, it seems to need a function declaration 56 | // for some reason (only affects ESP8266, likely an arduino-builder bug). 57 | void MQTT_connect(); 58 | 59 | //This is a setup function in which the initial settings are decalred 60 | //and this function will run only once when the code starts running 61 | void setup() { 62 | Serial.begin(115200);//Turning ON the serial communication to view response in serial monitor 63 | delay(10); 64 | 65 | Serial.println(F("Adafruit MQTT demo")); 66 | 67 | // Connect to WiFi access point. 68 | Serial.println(); Serial.println(); 69 | Serial.print("Connecting to "); 70 | Serial.println(WLAN_SSID); 71 | 72 | WiFi.begin(WLAN_SSID, WLAN_PASS); 73 | while (WiFi.status() != WL_CONNECTED) { 74 | delay(500); 75 | Serial.print("."); 76 | } 77 | Serial.println(); 78 | 79 | Serial.println("WiFi connected"); 80 | Serial.println("IP address: "); Serial.println(WiFi.localIP()); 81 | 82 | //Defining our relay pin as an Output 83 | pinMode(relay, OUTPUT); 84 | 85 | // Setup MQTT subscription for onoff feed. 86 | mqtt.subscribe(&onoffbutton); 87 | } 88 | 89 | uint32_t x=0; 90 | 91 | void loop() { 92 | // Ensure the connection to the MQTT server is alive (this will make the first 93 | // connection and automatically reconnect when disconnected). See the MQTT_connect 94 | // function definition further below. 95 | MQTT_connect(); 96 | 97 | // this is our 'wait for incoming subscription packets' busy subloop 98 | // try to spend your time here 99 | 100 | Adafruit_MQTT_Subscribe *subscription; 101 | while ((subscription = mqtt.readSubscription(5000))) { 102 | if (subscription == &onoffbutton) { 103 | Serial.print(F("Got: ")); 104 | Serial.println((char *)onoffbutton.lastread); 105 | uint16_t state = atoi((char *)onoffbutton.lastread); 106 | digitalWrite(relay, state); 107 | } 108 | } 109 | 110 | 111 | // ping the server to keep the mqtt connection alive 112 | // NOT required if you are publishing once every KEEPALIVE seconds 113 | /* 114 | if(! mqtt.ping()) { 115 | mqtt.disconnect(); 116 | } 117 | */ 118 | } 119 | 120 | // No need to change anything inside this function 121 | // Function to connect and reconnect as necessary to the MQTT server. 122 | // Should be called in the loop function and it will take care if connecting. 123 | void MQTT_connect() { 124 | int8_t ret; 125 | 126 | // Stop if already connected. 127 | if (mqtt.connected()) { 128 | return; 129 | } 130 | 131 | Serial.print("Connecting to MQTT... "); 132 | 133 | uint8_t retries = 3; 134 | while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 135 | Serial.println(mqtt.connectErrorString(ret)); 136 | Serial.println("Retrying MQTT connection in 5 seconds..."); 137 | mqtt.disconnect(); 138 | delay(5000); // wait 5 seconds 139 | retries--; 140 | if (retries == 0) { 141 | // basically die and wait for WDT to reset me 142 | while (1); 143 | } 144 | } 145 | Serial.println("MQTT Connected!"); 146 | } 147 | --------------------------------------------------------------------------------