├── .gitattributes └── AWS_Home_Auto ├── secrets.h └── AWS_Home_Auto.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /AWS_Home_Auto/secrets.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SECRET 4 | #define THINGNAME "YOUR_THING_NAME" //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[] = "IOT_ENDPOINT"; //change this 9 | 10 | // Amazon Root CA 1 11 | static const char AWS_CERT_CA[] PROGMEM = R"EOF( 12 | -----BEGIN CERTIFICATE----- 13 | 14 | 15 | -----END CERTIFICATE----- 16 | )EOF"; 17 | 18 | // Device Certificate //change this 19 | static const char AWS_CERT_CRT[] PROGMEM = R"KEY( 20 | -----BEGIN CERTIFICATE----- 21 | 22 | -----END CERTIFICATE----- 23 | 24 | 25 | 26 | 27 | )KEY"; 28 | 29 | // Device Private Key //change this 30 | static const char AWS_CERT_PRIVATE[] PROGMEM = R"KEY( 31 | -----BEGIN RSA PRIVATE KEY----- 32 | 33 | -----END RSA PRIVATE KEY----- 34 | 35 | 36 | )KEY"; 37 | -------------------------------------------------------------------------------- /AWS_Home_Auto/AWS_Home_Auto.ino: -------------------------------------------------------------------------------- 1 | #include "secrets.h" // Contains all the credentials 2 | 3 | // Important Libraries 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | //Included just to use it's timer function(Blynk cloud platfrom is not used in this code) 10 | #include 11 | 12 | // Relay Pins 13 | #define Relay1 15 14 | #define Relay2 2 15 | #define Relay3 4 16 | #define Relay4 22 17 | 18 | //Counter Initialisation 19 | int c = 0; 20 | 21 | // Topics of MQTT 22 | #define AWS_IOT_PUBLISH_TOPIC "esp32/counter" 23 | 24 | #define AWS_IOT_SUBSCRIBE_TOPIC1 "esp32/relay1" 25 | #define AWS_IOT_SUBSCRIBE_TOPIC2 "esp32/relay2" 26 | #define AWS_IOT_SUBSCRIBE_TOPIC3 "esp32/relay3" 27 | #define AWS_IOT_SUBSCRIBE_TOPIC4 "esp32/relay4" 28 | 29 | 30 | BlynkTimer timer; 31 | WiFiClientSecure net = WiFiClientSecure(); 32 | PubSubClient client(net); 33 | 34 | // Timer Callback function 35 | void myTimerEvent() 36 | { 37 | 38 | StaticJsonDocument<200> doc; 39 | doc["message"] = "Hello from ESP32"; 40 | doc["Counter"] = c; 41 | char jsonBuffer[512]; 42 | serializeJson(doc, jsonBuffer); // print to client 43 | client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer); 44 | Serial.println("Message Published"); 45 | c++; 46 | } 47 | 48 | 49 | 50 | void connectAWS() 51 | { 52 | WiFi.mode(WIFI_STA); 53 | WiFi.begin(WIFI_SSID, WIFI_PASSWORD); 54 | 55 | Serial.println("Connecting to Wi-Fi"); 56 | 57 | while (WiFi.status() != WL_CONNECTED) { 58 | delay(500); 59 | Serial.print("."); 60 | } 61 | 62 | // Configure WiFiClientSecure to use the AWS IoT device credentials 63 | net.setCACert(AWS_CERT_CA); 64 | net.setCertificate(AWS_CERT_CRT); 65 | net.setPrivateKey(AWS_CERT_PRIVATE); 66 | 67 | // Connect to the MQTT broker on the AWS endpoint we defined earlier 68 | client.setServer(AWS_IOT_ENDPOINT, 8883); 69 | 70 | // Create a message handler 71 | client.setCallback(messageHandler); 72 | 73 | Serial.print("Connecting to AWS IOT"); 74 | 75 | while (!client.connect(THINGNAME)) { 76 | Serial.print("."); 77 | delay(100); 78 | } 79 | 80 | if (!client.connected()) { 81 | Serial.println("AWS IoT Timeout!"); 82 | return; 83 | } 84 | 85 | // Subscribe to a topic 86 | client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC1); 87 | client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC2); 88 | client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC3); 89 | client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC4); 90 | 91 | Serial.println("AWS IoT Connected!"); 92 | } 93 | 94 | void messageHandler(char* topic, byte* payload, unsigned int length) 95 | { 96 | Serial.print("incoming: "); 97 | Serial.println(topic); 98 | 99 | if ( strstr(topic, "esp32/relay1") ) 100 | { 101 | StaticJsonDocument<200> doc; 102 | deserializeJson(doc, payload); 103 | String Relay_data = doc["status"]; 104 | int r = Relay_data.toInt(); 105 | digitalWrite(Relay1, !r); 106 | Serial.print("Relay1 - "); Serial.println(Relay_data); 107 | } 108 | 109 | if ( strstr(topic, "esp32/relay2") ) 110 | { 111 | StaticJsonDocument<200> doc; 112 | deserializeJson(doc, payload); 113 | String Relay_data = doc["status"]; 114 | int r = Relay_data.toInt(); 115 | digitalWrite(Relay2, !r); 116 | Serial.print("Relay2 - "); Serial.println(Relay_data); 117 | } 118 | 119 | if ( strstr(topic, "esp32/relay3") ) 120 | { 121 | StaticJsonDocument<200> doc; 122 | deserializeJson(doc, payload); 123 | String Relay_data = doc["status"]; 124 | int r = Relay_data.toInt(); 125 | digitalWrite(Relay3, !r); 126 | Serial.print("Relay3 - "); Serial.println(Relay_data); 127 | } 128 | 129 | if ( strstr(topic, "esp32/relay4") ) 130 | { 131 | StaticJsonDocument<200> doc; 132 | deserializeJson(doc, payload); 133 | String Relay_data = doc["status"]; 134 | int r = Relay_data.toInt(); 135 | digitalWrite(Relay4, !r); 136 | Serial.print("Relay4 - "); Serial.println(Relay_data); 137 | } 138 | 139 | 140 | } 141 | 142 | 143 | void setup() 144 | { 145 | Serial.begin(115200); 146 | 147 | pinMode(Relay1, OUTPUT); 148 | pinMode(Relay2, OUTPUT); 149 | pinMode(Relay3, OUTPUT); 150 | pinMode(Relay4, OUTPUT); 151 | 152 | timer.setInterval(5000L, myTimerEvent); 153 | 154 | connectAWS(); 155 | } 156 | 157 | void loop() 158 | { 159 | timer.run(); // Initiates BlynkTimer 160 | client.loop(); 161 | } 162 | --------------------------------------------------------------------------------