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