├── .gitattributes ├── .gitignore └── Adafruit_MQTT_One_Feed.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 | -------------------------------------------------------------------------------- /Adafruit_MQTT_One_Feed.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library ESP8266 Example 3 | 4 | Must use ESP8266 Arduino from: 5 | https://github.com/esp8266/Arduino 6 | 7 | Works great with Adafruit's Huzzah ESP board & Feather 8 | ----> https://www.adafruit.com/product/2471 9 | ----> https://www.adafruit.com/products/2821 10 | 11 | Adafruit invests time and resources providing this open source code, 12 | please support Adafruit and open-source hardware by purchasing 13 | products from Adafruit! 14 | 15 | Written by Tony DiCola for Adafruit Industries. 16 | MIT license, all text above must be included in any redistribution 17 | 18 | 19 | Watch tutorial video on my YouTube channel 20 | search for techiesms on YouTube. 21 | 22 | Visit my Website for more such projects on IoT 23 | http://www.techiesms.com 24 | ****************************************************/ 25 | #include 26 | #include "Adafruit_MQTT.h" 27 | #include "Adafruit_MQTT_Client.h" 28 | #define l1 D0 29 | #define l2 D1 30 | #define l3 D2 31 | #define l4 D3 32 | #define l5 D4 33 | #define l6 D5 34 | #define l7 D6 35 | #define l8 D7 36 | #define l9 D8 37 | 38 | /************************* WiFi Access Point *********************************/ 39 | 40 | #define WLAN_SSID "SSID" 41 | #define WLAN_PASS "Password" 42 | 43 | /************************* Adafruit.io Setup *********************************/ 44 | 45 | #define AIO_SERVER "io.adafruit.com" 46 | #define AIO_SERVERPORT 1883 // use 8883 for SSL 47 | #define AIO_USERNAME "USername" 48 | #define AIO_KEY "AIO KEY" 49 | 50 | /************ Global State (you don't need to change this!) ******************/ 51 | 52 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. 53 | WiFiClient client; 54 | // or... use WiFiFlientSecure for SSL 55 | //WiFiClientSecure client; 56 | 57 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 58 | Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 59 | 60 | /****************************** Feeds ***************************************/ 61 | 62 | // Setup a feed called 'photocell' for publishing. 63 | // Notice MQTT paths for AIO follow the form: /feeds/ 64 | //Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test"); 65 | 66 | // Setup a feed called 'onoff' for subscribing to changes. 67 | Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/test"); 68 | 69 | /*************************** Sketch Code ************************************/ 70 | 71 | // Bug workaround for Arduino 1.6.6, it seems to need a function declaration 72 | // for some reason (only affects ESP8266, likely an arduino-builder bug). 73 | void MQTT_connect(); 74 | 75 | void setup() { 76 | Serial.begin(115200); 77 | delay(10); 78 | 79 | Serial.println(F("Adafruit MQTT demo")); 80 | 81 | // Connect to WiFi access point. 82 | Serial.println(); Serial.println(); 83 | Serial.print("Connecting to "); 84 | Serial.println(WLAN_SSID); 85 | 86 | WiFi.begin(WLAN_SSID, WLAN_PASS); 87 | while (WiFi.status() != WL_CONNECTED) { 88 | delay(500); 89 | Serial.print("."); 90 | } 91 | Serial.println(); 92 | pinMode(l1,OUTPUT); 93 | pinMode(l2,OUTPUT); 94 | pinMode(l3,OUTPUT); 95 | pinMode(l4,OUTPUT); 96 | pinMode(l5,OUTPUT); 97 | pinMode(l6,OUTPUT); 98 | pinMode(l7,OUTPUT); 99 | pinMode(l8,OUTPUT); 100 | pinMode(l9,OUTPUT); 101 | Serial.println("WiFi connected"); 102 | Serial.println("IP address: "); Serial.println(WiFi.localIP()); 103 | 104 | // Setup MQTT subscription for onoff feed. 105 | mqtt.subscribe(&onoffbutton); 106 | } 107 | 108 | uint32_t x=0; 109 | 110 | void loop() { 111 | // Ensure the connection to the MQTT server is alive (this will make the first 112 | // connection and automatically reconnect when disconnected). See the MQTT_connect 113 | // function definition further below. 114 | MQTT_connect(); 115 | 116 | // this is our 'wait for incoming subscription packets' busy subloop 117 | // try to spend your time here 118 | 119 | Adafruit_MQTT_Subscribe *subscription; 120 | while ((subscription = mqtt.readSubscription())) { 121 | if (subscription == &onoffbutton) { 122 | Serial.print(F("Got: ")); 123 | Serial.println((char *)onoffbutton.lastread); 124 | String RAW = (char *)onoffbutton.lastread; 125 | int i=0; 126 | while(RAW.charAt(i) != '_') 127 | i++; 128 | String pin = RAW.substring(0,i); 129 | //Serial.println(pin); 130 | int pin_number = pin.toInt(); 131 | Serial.println(pin_number); 132 | 133 | int j=i+1; 134 | while(RAW.charAt(j) != '_') 135 | j++; 136 | // Serial.println(i); 137 | //Serial.println(j); 138 | String state = RAW.substring(i+1,j); 139 | //Serial.println(state); 140 | int state_number = state.toInt(); 141 | Serial.println(state_number); 142 | 143 | if(pin_number == 1) 144 | { 145 | digitalWrite(l1,state_number); 146 | } 147 | else if(pin_number == 2) 148 | { 149 | digitalWrite(l2,state_number); 150 | } 151 | else if(pin_number == 3) 152 | { 153 | digitalWrite(l3,state_number); 154 | } 155 | else if(pin_number == 4) 156 | { 157 | digitalWrite(l4,state_number); 158 | } 159 | else if(pin_number == 5) 160 | { 161 | digitalWrite(l5,state_number); 162 | } 163 | else if(pin_number == 6) 164 | { 165 | digitalWrite(l6,state_number); 166 | } 167 | else if(pin_number == 7) 168 | { 169 | digitalWrite(l7,state_number); 170 | } 171 | else if(pin_number == 8) 172 | { 173 | digitalWrite(l8,state_number); 174 | } 175 | else if(pin_number == 9) 176 | { 177 | digitalWrite(l9,state_number); 178 | } 179 | 180 | 181 | 182 | } 183 | } 184 | 185 | 186 | 187 | // ping the server to keep the mqtt connection alive 188 | // NOT required if you are publishing once every KEEPALIVE seconds 189 | /* 190 | if(! mqtt.ping()) { 191 | mqtt.disconnect(); 192 | } 193 | */ 194 | } 195 | 196 | // Function to connect and reconnect as necessary to the MQTT server. 197 | // Should be called in the loop function and it will take care if connecting. 198 | void MQTT_connect() { 199 | int8_t ret; 200 | 201 | // Stop if already connected. 202 | if (mqtt.connected()) { 203 | return; 204 | } 205 | 206 | Serial.print("Connecting to MQTT... "); 207 | 208 | uint8_t retries = 3; 209 | while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 210 | Serial.println(mqtt.connectErrorString(ret)); 211 | Serial.println("Retrying MQTT connection in 5 seconds..."); 212 | mqtt.disconnect(); 213 | delay(5000); // wait 5 seconds 214 | retries--; 215 | if (retries == 0) { 216 | // basically die and wait for WDT to reset me 217 | while (1); 218 | } 219 | } 220 | Serial.println("MQTT Connected!"); 221 | } 222 | --------------------------------------------------------------------------------