├── README.md ├── LICENSE └── aws-iot.ino /README.md: -------------------------------------------------------------------------------- 1 | # AWS-IoT-WLED 2 | 3 | ### [Detailed project information](https://t0nyz.com/projects/awsiot) 4 | 5 | The project demonstrates controlling a WLED device in near real-time using AWS IoT and an ESP32 microcontroller. It showcases how an MQTT message sent from a website can trigger a color change in a WLED light via AWS IoT, with the ESP32 acting as a bridge between the cloud and the physical device. 6 | 7 | ![image](https://github.com/user-attachments/assets/3475a147-0840-4e61-9963-7da4368fae64) 8 | 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Tony 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /aws-iot.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // Include the WiFi library 4 | #include 5 | 6 | const char* ca_cert = \ 7 | "-----BEGIN CERTIFICATE-----\n" \ 8 | "LONG RSA KEY INFO HERE \n" \ 9 | "-----END CERTIFICATE-----\n"; 10 | 11 | 12 | const char* device_cert = \ 13 | "-----BEGIN CERTIFICATE-----\n" \ 14 | "LONG RSA KEY INFO HERE \n" \ 15 | "-----END CERTIFICATE-----\n"; 16 | 17 | 18 | const char* private_key = \ 19 | "-----BEGIN RSA PRIVATE KEY-----\n" \ 20 | "LONG RSA KEY INFO HERE \n" \ 21 | "-----END RSA PRIVATE KEY-----\n"; 22 | 23 | 24 | WiFiClientSecure net; 25 | PubSubClient client(net); 26 | 27 | void sendHttpToWLED(String url, String payloadStr) { 28 | HTTPClient http; 29 | http.begin(url); 30 | http.addHeader("Content-Type", "application/json"); 31 | 32 | int httpResponseCode = http.POST(payloadStr); 33 | 34 | if (httpResponseCode > 0) { 35 | Serial.print("HTTP Response code: "); 36 | Serial.println(httpResponseCode); 37 | } else { 38 | Serial.print("Error on sending POST: "); 39 | Serial.println(httpResponseCode); 40 | } 41 | 42 | http.end(); 43 | } 44 | 45 | void messageReceived(char* topic, byte* payload, unsigned int length) { 46 | Serial.print("Message arrived ["); 47 | Serial.print(topic); 48 | Serial.print("] "); 49 | for (unsigned int i = 0; i < length; i++) { 50 | Serial.print((char)payload[i]); 51 | } 52 | Serial.println(); 53 | 54 | // Convert payload to String 55 | String payloadStr = String((char*)payload).substring(0, length); 56 | 57 | // Define the WLED HTTP API endpoint 58 | String url = "http://10.0.0.125/json/state"; // Replace with the actual IP of your WLED device 59 | 60 | // Send the received MQTT payload directly to the WLED device as an HTTP request 61 | sendHttpToWLED(url, payloadStr); 62 | } 63 | 64 | void setup() { 65 | Serial.begin(115200); 66 | 67 | WiFi.begin("--YOUR WIFI SSID--", "--YOUR WIFI PASSWORD--"); 68 | while (WiFi.status() != WL_CONNECTED) { 69 | delay(1000); 70 | Serial.println("Connecting to WiFi..."); 71 | } 72 | Serial.println("Connected to WiFi"); 73 | 74 | net.setCACert(ca_cert); 75 | net.setCertificate(device_cert); 76 | net.setPrivateKey(private_key); 77 | 78 | client.setServer("--YOUR AWS IOT ENDPOINT ADDRESS--", 8883); 79 | client.setCallback(messageReceived); 80 | 81 | if (client.connect("-- YOUR AWS IOT CLIENT ID --")) { 82 | Serial.println("Connected to AWS IoT"); 83 | client.subscribe("-- YOUR AWS IOT TOPIC --"); // Subscribe to the topic where commands will be sent 84 | } else { 85 | Serial.print("AWS IoT connection failed, state: "); 86 | Serial.println(client.state()); 87 | } 88 | } 89 | 90 | void loop() { 91 | if (!client.connected()) { 92 | Serial.println("Disconnected from AWS IoT. Attempting reconnection..."); 93 | while (!client.connect("-- YOUR AWS IOT CLIENT ID --")) { 94 | delay(1000); 95 | Serial.print("."); 96 | } 97 | Serial.println("Reconnected to AWS IoT"); 98 | client.subscribe("lights"); 99 | } 100 | 101 | client.loop(); // Handle AWS IoT MQTT messages 102 | } --------------------------------------------------------------------------------