├── .DS_Store ├── .gitattributes ├── Adafruit IO Codes ├── .DS_Store ├── ESP32_Adafruit_MQTT_Demo │ ├── .DS_Store │ └── ESP32_Adafruit_MQTT_Demo.ino └── ESP32_Adafruit_MQTTs_Demo.ino │ └── ESP32_Adafruit_MQTTs_Demo.ino.ino ├── LEDBlink └── LEDBlink.ino ├── LEDBlink_define_ └── LEDBlink_define_.ino ├── ESP32_Servo_ └── ESP32_Servo_.ino ├── AWS └── AWS_Demo_Code │ ├── secrets.h │ └── AWS_Demo_Code.ino ├── ESP32_OLED └── ESP32_OLED.ino ├── DHT11_code └── DHT11_code.ino └── CloudMQTT └── CloudMQTT_Demo_code └── CloudMQTT_Demo_code.ino /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techiesms/Codes-for-Course-/main/.DS_Store -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Adafruit IO Codes/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techiesms/Codes-for-Course-/main/Adafruit IO Codes/.DS_Store -------------------------------------------------------------------------------- /Adafruit IO Codes/ESP32_Adafruit_MQTT_Demo/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techiesms/Codes-for-Course-/main/Adafruit IO Codes/ESP32_Adafruit_MQTT_Demo/.DS_Store -------------------------------------------------------------------------------- /LEDBlink/LEDBlink.ino: -------------------------------------------------------------------------------- 1 | 2 | // the setup function runs once when you press reset or power the board 3 | void setup() 4 | { 5 | 6 | // To use any Pin as Input or Output, we first need to define that here in Setup part 7 | 8 | pinMode(2, OUTPUT); // initialize digital pin 2 as an output. 9 | } 10 | 11 | // the loop function runs over and over again forever 12 | 13 | void loop() 14 | { 15 | digitalWrite(2, HIGH); // turn the LED ON by making the pin HIGH ( HIHH -> 3.3V ) 16 | 17 | delay(1000); // wait for a second (1000 means 1000ms = 1s) 18 | 19 | digitalWrite(2, LOW); // turn the LED OFF by making the pin LOW ( LOW -> 0V ) 20 | 21 | delay(1000); // wait for a second (1000 means 1000ms = 1s) 22 | } 23 | -------------------------------------------------------------------------------- /LEDBlink_define_/LEDBlink_define_.ino: -------------------------------------------------------------------------------- 1 | 2 | #define LED 2 // #define will replace pin 2 to the name LED 3 | 4 | // the setup function runs once when you press reset or power the board 5 | void setup() 6 | { 7 | 8 | // To use any Pin as Input or Output, we first need to define that here in Setup part 9 | 10 | pinMode(LED, OUTPUT); // initialize LED as an output. 11 | } 12 | 13 | // the loop function runs over and over again forever 14 | 15 | void loop() 16 | { 17 | digitalWrite(LED, HIGH); // turn the LED ON by making the pin HIGH ( HIHH -> 3.3V ) 18 | 19 | delay(1000); // wait for a second (1000 means 1000ms = 1s) 20 | 21 | digitalWrite(LED, LOW); // turn the LED OFF by making the pin LOW ( LOW -> 0V ) 22 | 23 | delay(1000); // wait for a second (1000 means 1000ms = 1s) 24 | } 25 | -------------------------------------------------------------------------------- /ESP32_Servo_/ESP32_Servo_.ino: -------------------------------------------------------------------------------- 1 | #include // https://github.com/RoboticsBrno/ServoESP32 2 | 3 | Servo myservo; // create servo object to control a servo 4 | // twelve servo objects can be created on most boards 5 | 6 | int pos = 0; // Integer variable to store the servo position 7 | 8 | void setup() 9 | { 10 | myservo.attach(13); // attaches the servo on pin 13 to the servo object 11 | } 12 | 13 | void loop() 14 | { 15 | for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees 16 | // in steps of 1 degree 17 | myservo.write(pos); // tell servo to go to position in variable 'pos' 18 | delay(15); // waits 15ms for the servo to reach the position 19 | } 20 | for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees 21 | myservo.write(pos); // tell servo to go to position in variable 'pos' 22 | delay(15); // waits 15ms for the servo to reach the position 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /AWS/AWS_Demo_Code/secrets.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SECRET 4 | #define THINGNAME "THINGNAME" //change this 5 | 6 | const char WIFI_SSID[] = "SSID"; //change this 7 | const char WIFI_PASSWORD[] = "PASS"; //change this 8 | const char AWS_IOT_ENDPOINT[] = "AWSENDPOINT"; //change this 9 | 10 | // Amazon Root CA 1 11 | static const char AWS_CERT_CA[] PROGMEM = R"EOF( 12 | -----BEGIN CERTIFICATE----- 13 | 14 | -----END CERTIFICATE----- 15 | )EOF"; 16 | 17 | // Device Certificate //change this 18 | static const char AWS_CERT_CRT[] PROGMEM = R"KEY( 19 | -----BEGIN CERTIFICATE----- 20 | 21 | -----END CERTIFICATE----- 22 | 23 | 24 | 25 | 26 | )KEY"; 27 | 28 | // Device Private Key //change this 29 | static const char AWS_CERT_PRIVATE[] PROGMEM = R"KEY( 30 | -----BEGIN RSA PRIVATE KEY----- 31 | 32 | -----END RSA PRIVATE KEY----- 33 | 34 | 35 | )KEY"; 36 | -------------------------------------------------------------------------------- /ESP32_OLED/ESP32_OLED.ino: -------------------------------------------------------------------------------- 1 | /************************* Library Declaration *********************************/ 2 | #include 3 | #include 4 | #include 5 | 6 | /************************* Declaring Pixels *********************************/ 7 | 8 | #define SCREEN_WIDTH 128 // OLED display width, in pixels 9 | #define SCREEN_HEIGHT 64 // OLED display height, in pixels 10 | 11 | /************************* Making object for calss Adafruit_SSD1306 *********************************/ 12 | 13 | // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) 14 | Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire); 15 | 16 | void setup() 17 | { 18 | Serial.begin(115200); // Beginning Serial Communication with Computer 19 | 20 | if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 21 | Serial.println("SSD1306 allocation failed"); 22 | for(;;); 23 | } 24 | delay(2000); 25 | display.clearDisplay(); 26 | 27 | display.setTextSize(1); 28 | display.setTextColor(WHITE); 29 | display.setCursor(0, 10); 30 | // Display static text 31 | display.println("Hello, world!"); 32 | display.display(); 33 | } 34 | 35 | void loop() 36 | { 37 | 38 | } 39 | -------------------------------------------------------------------------------- /DHT11_code/DHT11_code.ino: -------------------------------------------------------------------------------- 1 | 2 | /************************* Declaring Library *********************************/ 3 | 4 | #include "DHT.h" // https://github.com/adafruit/DHT-sensor-library 5 | 6 | 7 | /************************* Declaring Pin *********************************/ 8 | 9 | #define DHTPIN 2 // Digital pin connected to the DHT sensor 10 | 11 | 12 | /************************* Declaring DHT Sensor Type *********************************/ 13 | #define DHTTYPE DHT11 // DHT 11 14 | 15 | 16 | /************************* Declaring Object for DHT class *********************************/ 17 | DHT dht(DHTPIN, DHTTYPE); 18 | 19 | // Link for understanding Classes & Object - https://www.w3schools.com/cpp/cpp_classes.asp 20 | 21 | void setup() 22 | { 23 | Serial.begin(115200); // Beginning Serial Communication with Computer 24 | 25 | dht.begin(); // beginning communication with Sensor 26 | } 27 | 28 | void loop() 29 | { 30 | 31 | // Read humidity 32 | float h = dht.readHumidity(); 33 | 34 | // Read temperature as Celsius (the default) 35 | float t = dht.readTemperature(); 36 | 37 | /************************* Printing Data on Serial Monitor *********************************/ 38 | Serial.print("Humidity: "); 39 | Serial.print(h); 40 | Serial.print("% Temperature: "); 41 | Serial.print(t); 42 | Serial.print("°C "); 43 | } 44 | -------------------------------------------------------------------------------- /AWS/AWS_Demo_Code/AWS_Demo_Code.ino: -------------------------------------------------------------------------------- 1 | #include "secrets.h" // Contains all the credentials 2 | 3 | 4 | /************************* Declaring Libraries *********************************/ 5 | // Important Libraries 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | /************************* Pin Declaration *********************************/ 13 | #define LED 2 14 | 15 | //Counter Initialisation 16 | int c = 0; 17 | 18 | 19 | /************************* Defining Topics *********************************/ 20 | // Topics of MQTT 21 | #define AWS_IOT_PUBLISH_TOPIC1 "esp32/sensor" 22 | 23 | #define AWS_IOT_SUBSCRIBE_TOPIC1 "esp32/light" 24 | 25 | 26 | SimpleTimer Timer; // Declaring Timer Object for Sending data at particular time 27 | 28 | 29 | // Declaring Oject for Wifi Secure Client 30 | WiFiClientSecure net = WiFiClientSecure(); 31 | PubSubClient client(net); 32 | 33 | /************************* This Function is responsible for WiFi and MQTT connection *********************************/ 34 | void connectAWS() 35 | { 36 | WiFi.mode(WIFI_STA); 37 | WiFi.begin(WIFI_SSID, WIFI_PASSWORD); 38 | 39 | Serial.println("Connecting to Wi-Fi"); 40 | 41 | while (WiFi.status() != WL_CONNECTED) { 42 | delay(500); 43 | Serial.print("."); 44 | } 45 | 46 | // Configure WiFiClientSecure to use the AWS IoT device credentials 47 | net.setCACert(AWS_CERT_CA); 48 | net.setCertificate(AWS_CERT_CRT); 49 | net.setPrivateKey(AWS_CERT_PRIVATE); 50 | 51 | // Connect to the MQTT broker on the AWS endpoint we defined earlier 52 | client.setServer(AWS_IOT_ENDPOINT, 8883); 53 | 54 | // Create a message handler 55 | client.setCallback(messageHandler); 56 | 57 | Serial.print("Connecting to AWS IOT"); 58 | 59 | while (!client.connect(THINGNAME)) { 60 | Serial.print("."); 61 | delay(100); 62 | } 63 | 64 | if (!client.connected()) { 65 | Serial.println("AWS IoT Timeout!"); 66 | return; 67 | } 68 | 69 | // Subscribe to a topic 70 | client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC1); 71 | 72 | Serial.println("AWS IoT Connected!"); 73 | } 74 | 75 | /************************* This Function is responsible for all the Incoming Data *********************************/ 76 | 77 | void messageHandler(char* topic, byte* payload, unsigned int length) 78 | { 79 | Serial.print("incoming: "); 80 | Serial.println(topic); 81 | 82 | if ( strstr(topic, "esp32/light") ) 83 | { 84 | StaticJsonDocument<200> doc; 85 | deserializeJson(doc, payload); 86 | String Relay_data = doc["status"]; 87 | int value = Relay_data.toInt(); 88 | digitalWrite(LED, value); 89 | Serial.print("LED - "); Serial.println(value); 90 | } 91 | 92 | } 93 | 94 | 95 | void setup() 96 | { 97 | Serial.begin(115200); 98 | 99 | pinMode(LED, OUTPUT); 100 | 101 | // Set an interval to 3 secs for the second timer 102 | Timer.setInterval(3000); 103 | 104 | connectAWS(); 105 | } 106 | 107 | void loop() 108 | { 109 | 110 | client.loop(); // Handles all the MQTT realted task in the backend 111 | 112 | 113 | if (Timer.isReady()) 114 | { // Check is ready a second timer 115 | Serial.println("Called every 3 sec"); 116 | 117 | StaticJsonDocument<200> doc; 118 | doc["message"] = "Hello from ESP32"; 119 | doc["Counter"] = c; 120 | char jsonBuffer[512]; 121 | serializeJson(doc, jsonBuffer); // print to client 122 | client.publish(AWS_IOT_PUBLISH_TOPIC1, jsonBuffer); 123 | Serial.println("Message Published"); 124 | c++; 125 | Timer.reset(); // Reset a second timer 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /CloudMQTT/CloudMQTT_Demo_code/CloudMQTT_Demo_code.ino: -------------------------------------------------------------------------------- 1 | 2 | /************************* Declaring Libraries *********************************/ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /************************* WiFi Access Point *********************************/ 9 | 10 | const char* ssid = "SSID"; 11 | const char* password = "PASS"; 12 | 13 | /************************* CloudMQTT Setup *********************************/ 14 | 15 | const char* mqtt_server = "SERVER ADDRESS"; 16 | const int port = PORT_NUMBER; 17 | const char* USERNAME = "USERNAME"; 18 | const char* PASSWORD = "PASSWORD"; 19 | 20 | 21 | /************************* Defining Relay Pins *********************************/ 22 | 23 | #define LED 2 24 | 25 | /************************* Topics to Subscribe *********************************/ 26 | 27 | #define SUB_TOPIC1 "esp32/light" 28 | 29 | #define PUB_TOPIC1 "esp32/sensor" 30 | 31 | /************************* WiFi Client Setup *********************************/ 32 | 33 | WiFiClient espClient; 34 | PubSubClient client(espClient); 35 | 36 | /************************* Timer Setup *********************************/ 37 | 38 | SimpleTimer Timer; 39 | 40 | int counter = 0; 41 | 42 | /************************* WiFi Setup *********************************/ 43 | 44 | void setup_wifi() 45 | { 46 | delay(10); 47 | // We start by connecting to a WiFi network 48 | Serial.println(); 49 | Serial.print("Connecting to "); 50 | Serial.println(ssid); 51 | 52 | WiFi.mode(WIFI_STA); 53 | WiFi.begin(ssid, password); 54 | 55 | while (WiFi.status() != WL_CONNECTED) 56 | { 57 | delay(500); 58 | Serial.print("."); 59 | } 60 | 61 | Serial.println(""); 62 | Serial.println("WiFi connected"); 63 | Serial.println("IP address: "); 64 | Serial.println(WiFi.localIP()); 65 | } 66 | 67 | 68 | /************************* Callback Function *********************************/ 69 | 70 | 71 | void callback(char* topic, byte* payload, unsigned int length) 72 | { 73 | 74 | 75 | Serial.print("Message arrived ["); 76 | Serial.print(topic); 77 | Serial.print("] "); 78 | for (int i = 0; i < length; i++) 79 | { 80 | Serial.print((char)payload[i]); 81 | } 82 | Serial.println(); 83 | 84 | 85 | // Comparing the topics 86 | if ( strstr(topic, SUB_TOPIC1) ) 87 | { 88 | if ((char)payload[0] == '1') 89 | { 90 | digitalWrite(LED, HIGH); // Turn the LED on (Note that LOW is the voltage level 91 | } 92 | if ((char)payload[0] == '0') 93 | { 94 | digitalWrite(LED, LOW); // Turn the LED off by making the voltage HIGH 95 | } 96 | } 97 | } 98 | 99 | /************************* Responsible for MQTT Connection *********************************/ 100 | void reconnect() 101 | { 102 | // Loop until we're reconnected 103 | while (!client.connected()) 104 | { 105 | Serial.print("Attempting MQTT connection..."); 106 | // Create a random client ID 107 | String clientId = "esp32client"; 108 | // Attempt to connect 109 | if (client.connect(clientId.c_str(), USERNAME, PASSWORD) ) { 110 | Serial.println("connected"); 111 | // Once connected, publish an announcement... 112 | // ... and resubscribe 113 | client.subscribe(SUB_TOPIC1); // Subscribing to the Topics 114 | } 115 | else 116 | { 117 | Serial.print("failed, rc="); 118 | Serial.print(client.state()); 119 | Serial.println(" try again in 5 seconds"); 120 | // Wait 5 seconds before retrying 121 | delay(5000); 122 | } 123 | } 124 | } 125 | 126 | void setup() 127 | { 128 | Serial.begin(115200); 129 | 130 | pinMode(LED, OUTPUT); 131 | 132 | setup_wifi(); 133 | 134 | client.setServer(mqtt_server, port); 135 | client.setCallback(callback); 136 | 137 | // Set an interval to 5 secs for the second timer 138 | Timer.setInterval(5000); 139 | } 140 | 141 | void loop() { 142 | 143 | // ReConnect to Cloud if disconnected 144 | if (!client.connected()) 145 | { 146 | reconnect(); 147 | } 148 | 149 | client.loop(); // Handles all the task in backend 150 | 151 | 152 | if (Timer.isReady()) 153 | { // Check is ready a second timer 154 | Serial.println("Called every 3 sec"); 155 | Serial.print("Sending Data - "); Serial.println(counter); 156 | 157 | 158 | String TEMPORARY = (String) counter; // Coverting Int into String 159 | char* value = &TEMPORARY[0]; // Coverting String into char* 160 | 161 | client.publish(PUB_TOPIC1, value); 162 | 163 | counter = counter + 1; 164 | Timer.reset(); // Reset a second timer 165 | } 166 | 167 | } 168 | -------------------------------------------------------------------------------- /Adafruit IO Codes/ESP32_Adafruit_MQTT_Demo/ESP32_Adafruit_MQTT_Demo.ino: -------------------------------------------------------------------------------- 1 | /************************* Library Declaration *********************************/ 2 | #include 3 | #include "Adafruit_MQTT.h" 4 | #include "Adafruit_MQTT_Client.h" 5 | 6 | 7 | /************************* Pin Declaration *********************************/ 8 | #define LED 2 9 | 10 | /************************* WiFi Access Point *********************************/ 11 | 12 | #define WLAN_SSID "SmS_jiofi" 13 | #define WLAN_PASS "sms123458956" 14 | 15 | /************************* Adafruit.io Setup *********************************/ 16 | 17 | #define AIO_SERVER "io.adafruit.com" 18 | #define AIO_SERVERPORT 1883 // use 8883 for SSL 19 | #define AIO_USERNAME "techiesms" 20 | #define AIO_KEY "912b30c900574034a653f41e2b4df838" 21 | 22 | /************ Global State (you don't need to change this!) ******************/ 23 | 24 | // Create an ESP32 WiFiClient class to connect to the MQTT server. 25 | WiFiClient client; 26 | 27 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 28 | Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 29 | 30 | 31 | /****************************** Feeds(Topics) ***************************************/ 32 | 33 | // Setup a feed called 'sensor' for publishing. 34 | // Notice MQTT paths for AIO follow the form: /feeds/ 35 | Adafruit_MQTT_Publish sensor = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/sensor"); 36 | 37 | // Setup a feed called 'onoff' for subscribing to changes. 38 | Adafruit_MQTT_Subscribe light = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/light"); 39 | 40 | /*************************** Sketch Code ************************************/ 41 | 42 | void setup() 43 | { 44 | Serial.begin(115200); // Beginning Serial Communication between ESP32 and Computer at 115200 baud rate 45 | 46 | pinMode(LED, OUTPUT); // Declaring LED as OUTPUT 47 | 48 | Serial.println("Adafruit MQTT demo"); // It will be printed on Serial Monitor 49 | 50 | 51 | /*************************** WiFi Connection Code ************************************/ 52 | // Connect to WiFi access point. 53 | Serial.println(); Serial.println(); 54 | Serial.print("Connecting to "); 55 | Serial.println(WLAN_SSID); 56 | 57 | WiFi.begin(WLAN_SSID, WLAN_PASS); 58 | while (WiFi.status() != WL_CONNECTED) 59 | { 60 | delay(500); 61 | Serial.print("."); 62 | } 63 | Serial.println(); 64 | 65 | Serial.println("WiFi connected"); 66 | Serial.println("IP address: "); Serial.println(WiFi.localIP()); 67 | 68 | 69 | 70 | 71 | // Setup MQTT subscription for onoff feed. 72 | mqtt.subscribe(&light); 73 | } 74 | 75 | uint32_t x = 0; 76 | 77 | void loop() 78 | { 79 | // Ensure the connection to the MQTT server is alive (this will make the first 80 | // connection and automatically reconnect when disconnected). See the MQTT_connect 81 | // function definition further below. 82 | MQTT_connect(); 83 | 84 | // this is our 'wait for incoming subscription packets' busy subloop 85 | // try to spend your time here 86 | 87 | Adafruit_MQTT_Subscribe *subscription; 88 | while ((subscription = mqtt.readSubscription(5000))) 89 | { 90 | if (subscription == &light) 91 | { 92 | // Storing the data 93 | char* Data = (char*) light.lastread; 94 | Serial.print("Got: "); 95 | Serial.println(Data); 96 | 97 | // comparing the data 98 | if (strstr(Data, "1")) 99 | { 100 | digitalWrite(LED, HIGH); 101 | } 102 | if (strstr(Data, "0")) 103 | { 104 | digitalWrite(LED, LOW); 105 | } 106 | } 107 | } 108 | 109 | // Now we can publish stuff! 110 | Serial.print("\nSending sensor val "); 111 | Serial.print(x); 112 | Serial.print("..."); 113 | sensor.publish(x); 114 | x++; 115 | 116 | } 117 | 118 | // Function to connect and reconnect as necessary to the MQTT server. 119 | // Should be called in the loop function and it will take care if connecting. 120 | void MQTT_connect() 121 | { 122 | int ret; 123 | 124 | // Stop if already connected. 125 | if (mqtt.connected()) 126 | { 127 | return; 128 | } 129 | 130 | Serial.print("Connecting to MQTT... "); 131 | 132 | int retries = 3; 133 | while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 134 | Serial.println(mqtt.connectErrorString(ret)); 135 | Serial.println("Retrying MQTT connection in 5 seconds..."); 136 | mqtt.disconnect(); 137 | delay(5000); // wait 5 seconds 138 | retries--; 139 | if (retries == 0) { 140 | // basically die and wait for WDT to reset me 141 | while (1); 142 | } 143 | } 144 | Serial.println("MQTT Connected!"); 145 | } 146 | -------------------------------------------------------------------------------- /Adafruit IO Codes/ESP32_Adafruit_MQTTs_Demo.ino/ESP32_Adafruit_MQTTs_Demo.ino.ino: -------------------------------------------------------------------------------- 1 | /************************* Library Declaration *********************************/ 2 | 3 | #include 4 | #include "WiFiClientSecure.h" 5 | #include "Adafruit_MQTT.h" 6 | #include "Adafruit_MQTT_Client.h" 7 | 8 | /************************* Pin Declaration *********************************/ 9 | #define LED 2 10 | 11 | /************************* WiFi Access Point *********************************/ 12 | 13 | #define WLAN_SSID "SmS_jiofi" 14 | #define WLAN_PASS "sms123458956" 15 | 16 | /************************* Adafruit.io Setup *********************************/ 17 | 18 | #define AIO_SERVER "io.adafruit.com" 19 | // Using port 8883 for MQTTS 20 | #define AIO_SERVERPORT 8883 21 | 22 | // Adafruit IO Account Configuration 23 | // (to obtain these values, visit https://io.adafruit.com and click on Active Key) 24 | #define AIO_USERNAME "techiesms" 25 | #define AIO_KEY "912b30c900574034a653f41e2b4df838" 26 | 27 | /************ Global State (you don't need to change this!) ******************/ 28 | 29 | // WiFiFlientSecure for SSL/TLS support 30 | WiFiClientSecure client; 31 | 32 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 33 | Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 34 | 35 | // io.adafruit.com root CA 36 | const char* adafruitio_root_ca = \ 37 | "-----BEGIN CERTIFICATE-----\n" \ 38 | "MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" \ 39 | "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" \ 40 | "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n" \ 41 | "QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n" \ 42 | "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n" \ 43 | "b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n" \ 44 | "9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n" \ 45 | "CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n" \ 46 | "nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n" \ 47 | "43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n" \ 48 | "T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n" \ 49 | "gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n" \ 50 | "BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n" \ 51 | "TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n" \ 52 | "DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n" \ 53 | "hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n" \ 54 | "06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n" \ 55 | "PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n" \ 56 | "YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" \ 57 | "CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \ 58 | "-----END CERTIFICATE-----\n"; 59 | 60 | /****************************** Feeds ***************************************/ 61 | 62 | // Setup a feed called 'test' for publishing. 63 | // Notice MQTT paths for AIO follow the form: /feeds/ 64 | Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/sensor"); 65 | 66 | // Setup a feed called 'onoff' for subscribing to changes. 67 | Adafruit_MQTT_Subscribe light = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/light"); 68 | 69 | /*************************** Sketch Code ************************************/ 70 | 71 | void setup() { 72 | Serial.begin(115200); 73 | 74 | pinMode(LED, OUTPUT); // Declaring LED as OUTPUT 75 | 76 | Serial.println(F("Adafruit IO MQTTS (SSL/TLS) Example")); 77 | 78 | /*************************** WiFi Connection Code ************************************/ 79 | // Connect to WiFi access point. 80 | Serial.println(); Serial.println(); 81 | Serial.print("Connecting to "); 82 | Serial.println(WLAN_SSID); 83 | 84 | delay(1000); 85 | 86 | WiFi.begin(WLAN_SSID, WLAN_PASS); 87 | delay(2000); 88 | 89 | while (WiFi.status() != WL_CONNECTED) { 90 | delay(500); 91 | Serial.print("."); 92 | } 93 | Serial.println(); 94 | 95 | Serial.println("WiFi connected"); 96 | Serial.println("IP address: "); Serial.println(WiFi.localIP()); 97 | 98 | // Set Adafruit IO's root CA 99 | client.setCACert(adafruitio_root_ca); 100 | 101 | // Setup MQTT subscription for onoff feed. 102 | mqtt.subscribe(&light); 103 | } 104 | 105 | int x = 0; 106 | 107 | void loop() { 108 | // Ensure the connection to the MQTT server is alive (this will make the first 109 | // connection and automatically reconnect when disconnected). See the MQTT_connect 110 | // function definition further below. 111 | MQTT_connect(); 112 | 113 | // this is our 'wait for incoming subscription packets' busy subloop 114 | // try to spend your time here 115 | 116 | Adafruit_MQTT_Subscribe *subscription; 117 | while ((subscription = mqtt.readSubscription(5000))) 118 | { 119 | if (subscription == &light) 120 | { 121 | // Storing the data 122 | char* Data = (char*) light.lastread; 123 | Serial.print("Got: "); 124 | Serial.println(Data); 125 | 126 | // comparing the data 127 | if (strstr(Data, "1")) 128 | { 129 | digitalWrite(LED, HIGH); 130 | } 131 | if (strstr(Data, "0")) 132 | { 133 | digitalWrite(LED, LOW); 134 | } 135 | } 136 | } 137 | 138 | 139 | // Now we can publish stuff! 140 | Serial.print("\nSending val "); 141 | Serial.print(x); 142 | Serial.print(" to sensor feed..."); 143 | test.publish(x); 144 | x = x + 1; 145 | 146 | // wait a couple seconds to avoid rate limit 147 | delay(2000); 148 | 149 | } 150 | 151 | // Function to connect and reconnect as necessary to the MQTT server. 152 | // Should be called in the loop function and it will take care if connecting. 153 | void MQTT_connect() { 154 | int8_t ret; 155 | 156 | // Stop if already connected. 157 | if (mqtt.connected()) { 158 | return; 159 | } 160 | 161 | Serial.print("Connecting to MQTT... "); 162 | 163 | uint8_t retries = 3; 164 | while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 165 | Serial.println(mqtt.connectErrorString(ret)); 166 | Serial.println("Retrying MQTT connection in 5 seconds..."); 167 | mqtt.disconnect(); 168 | delay(5000); // wait 5 seconds 169 | retries--; 170 | if (retries == 0) { 171 | // basically die and wait for WDT to reset me 172 | while (1); 173 | } 174 | } 175 | 176 | Serial.println("MQTT Connected!"); 177 | } 178 | --------------------------------------------------------------------------------