├── .gitattributes └── ESP32_MQTT └── ESP32_MQTT.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /ESP32_MQTT/ESP32_MQTT.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 | #include 19 | #include "Adafruit_MQTT.h" 20 | #include "Adafruit_MQTT_Client.h" 21 | 22 | #define LED 2 23 | 24 | /************************* WiFi Access Point *********************************/ 25 | 26 | #define WLAN_SSID "SmS_jiofi" 27 | #define WLAN_PASS "sms123458956" 28 | 29 | /************************* Adafruit.io Setup *********************************/ 30 | 31 | #define AIO_SERVER "io.adafruit.com" 32 | #define AIO_SERVERPORT 1883 // use 8883 for SSL 33 | #define AIO_USERNAME "techiesms" 34 | #define AIO_KEY "912b30c900574034a653f41e2b4df838" 35 | #define topic_name "light" 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 WiFiClientSecure 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 'onoff' for subscribing to changes. 50 | Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/"topic_name); 51 | 52 | /*************************** Sketch Code ************************************/ 53 | 54 | // Bug workaround for Arduino 1.6.6, it seems to need a function declaration 55 | // for some reason (only affects ESP8266, likely an arduino-builder bug). 56 | void MQTT_connect(); 57 | 58 | void setup() { 59 | Serial.begin(115200); 60 | delay(10); 61 | pinMode(LED,OUTPUT ); 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 onoff feed. 80 | mqtt.subscribe(&onoffbutton); 81 | } 82 | 83 | uint32_t x = 0; 84 | 85 | void loop() 86 | { 87 | // Ensure the connection to the MQTT server is alive (this will make the first 88 | // connection and automatically reconnect when disconnected). See the MQTT_connect 89 | // function definition further below. 90 | MQTT_connect(); 91 | 92 | // this is our 'wait for incoming subscription packets' busy subloop 93 | // try to spend your time here 94 | 95 | Adafruit_MQTT_Subscribe *subscription; 96 | while ((subscription = mqtt.readSubscription(5000))) { 97 | if (subscription == &onoffbutton) { 98 | Serial.print(F("Got: ")); 99 | Serial.println((char *)onoffbutton.lastread); 100 | int light_data = atoi((char *)onoffbutton.lastread); 101 | if ( light_data == 1) 102 | { 103 | digitalWrite(LED, HIGH); 104 | Serial.println("Light On"); 105 | } 106 | 107 | else 108 | { 109 | digitalWrite(LED, LOW); 110 | Serial.println("Light Off"); 111 | } 112 | } 113 | } 114 | } 115 | 116 | // Function to connect and reconnect as necessary to the MQTT server. 117 | // Should be called in the loop function and it will take care if connecting. 118 | void MQTT_connect() { 119 | int8_t ret; 120 | 121 | // Stop if already connected. 122 | if (mqtt.connected()) { 123 | return; 124 | } 125 | 126 | Serial.print("Connecting to MQTT... "); 127 | 128 | uint8_t retries = 3; 129 | while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 130 | Serial.println(mqtt.connectErrorString(ret)); 131 | Serial.println("Retrying MQTT connection in 5 seconds..."); 132 | mqtt.disconnect(); 133 | delay(5000); // wait 5 seconds 134 | retries--; 135 | if (retries == 0) { 136 | // basically die and wait for WDT to reset me 137 | while (1); 138 | } 139 | } 140 | Serial.println("MQTT Connected!"); 141 | } 142 | --------------------------------------------------------------------------------