Components and supplies
NodeMCU ESP8266 Breakout Board
5 mm LED: Red
Jumper wires (generic)
LED Strip, 1 m
Relay Module (Generic)
Tools and machines
Tape, Clear
CardBoard
Hot glue gun (generic)
Apps and platforms
Adafruit.io
Maker service
Arduino IDE
Project description
Code
Code
arduino
1 // https://www.youtube.com/channel/UCaXI2PcsTlH5g0et67kdD6g // 2// Jaarvis via Google Assistant // 3// By MOHD SOHAIL // 4 5 6#include <ESP8266WiFi.h> 7#include "Adafruit_MQTT.h" 8#include "Adafruit_MQTT_Client.h" 9 10#define ironman D5 11 12#define WLAN_SSID "honor 9" // Your SSID 13#define WLAN_PASS "87654321" // Your password 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 "your username" // Replace it with your username 20#define AIO_KEY "your private key" // Replace with your Project Auth Key 21 22/************ Global State (you don't need to change this!) ******************/ 23 24// Create an ESP8266 WiFiClient class to connect to the MQTT server. 25WiFiClient client; 26// or... use WiFiFlientSecure for SSL 27//WiFiClientSecure client; 28 29// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 30Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 31 32/****************************** Feeds ***************************************/ 33 34 35// Setup a feed called 'onoff' for subscribing to changes. 36Adafruit_MQTT_Subscribe Light1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/ironman"); 37 38void MQTT_connect(); 39 40void setup() { 41 Serial.begin(115200); 42 43 pinMode(ironman, OUTPUT); 44 45 // Connect to WiFi access point. 46 Serial.println(); Serial.println(); 47 Serial.print("Connecting to "); 48 Serial.println(WLAN_SSID); 49 50 WiFi.begin(WLAN_SSID, WLAN_PASS); 51 while (WiFi.status() != WL_CONNECTED) { 52 delay(500); 53 Serial.print("."); 54 } 55 Serial.println(); 56 57 Serial.println("WiFi connected"); 58 Serial.println("IP address: "); 59 Serial.println(WiFi.localIP()); 60 61 62 // Setup MQTT subscription for onoff feed. 63 mqtt.subscribe(&Light1); 64} 65 66void loop() { 67 68 MQTT_connect(); 69 70 71 Adafruit_MQTT_Subscribe *subscription; 72 while ((subscription = mqtt.readSubscription(20000))) { 73 if (subscription == &Light1) { 74 Serial.print(F("Got: ")); 75 Serial.println((char *)Light1.lastread); 76 int Light1_State = atoi((char *)Light1.lastread); 77 digitalWrite(ironman, Light1_State); 78 } 79 } 80 } 81 82 83void MQTT_connect() { 84 int8_t ret; 85 86 // Stop if already connected. 87 if (mqtt.connected()) { 88 return; 89 } 90 91 Serial.print("Connecting to MQTT... "); 92 93 uint8_t retries = 3; 94 95 while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 96 Serial.println(mqtt.connectErrorString(ret)); 97 Serial.println("Retrying MQTT connection in 5 seconds..."); 98 mqtt.disconnect(); 99 delay(5000); // wait 5 seconds 100 retries--; 101 if (retries == 0) { 102 // basically die and wait for WDT to reset me 103 while (1); 104 } 105 } 106 Serial.println("MQTT Connected!"); 107 108} 109
Code
arduino
1 // https://www.youtube.com/channel/UCaXI2PcsTlH5g0et67kdD6g // 2// Jaarvis via Google Assistant // 3// By MOHD SOHAIL // 4 5 6#include <ESP8266WiFi.h> 7#include "Adafruit_MQTT.h" 8#include "Adafruit_MQTT_Client.h" 9 10#define ironman D5 11 12#define WLAN_SSID "honor 9" // Your SSID 13#define WLAN_PASS "87654321" // Your password 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 "your username" // Replace it with your username 20#define AIO_KEY "your private key" // Replace with your Project Auth Key 21 22/************ Global State (you don't need to change this!) ******************/ 23 24// Create an ESP8266 WiFiClient class to connect to the MQTT server. 25WiFiClient client; 26// or... use WiFiFlientSecure for SSL 27//WiFiClientSecure client; 28 29// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 30Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 31 32/****************************** Feeds ***************************************/ 33 34 35// Setup a feed called 'onoff' for subscribing to changes. 36Adafruit_MQTT_Subscribe Light1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/ironman"); 37 38void MQTT_connect(); 39 40void setup() { 41 Serial.begin(115200); 42 43 pinMode(ironman, OUTPUT); 44 45 // Connect to WiFi access point. 46 Serial.println(); Serial.println(); 47 Serial.print("Connecting to "); 48 Serial.println(WLAN_SSID); 49 50 WiFi.begin(WLAN_SSID, WLAN_PASS); 51 while (WiFi.status() != WL_CONNECTED) { 52 delay(500); 53 Serial.print("."); 54 } 55 Serial.println(); 56 57 Serial.println("WiFi connected"); 58 Serial.println("IP address: "); 59 Serial.println(WiFi.localIP()); 60 61 62 // Setup MQTT subscription for onoff feed. 63 mqtt.subscribe(&Light1); 64} 65 66void loop() { 67 68 MQTT_connect(); 69 70 71 Adafruit_MQTT_Subscribe *subscription; 72 while ((subscription = mqtt.readSubscription(20000))) { 73 if (subscription == &Light1) { 74 Serial.print(F("Got: ")); 75 Serial.println((char *)Light1.lastread); 76 int Light1_State = atoi((char *)Light1.lastread); 77 digitalWrite(ironman, Light1_State); 78 } 79 } 80 } 81 82 83void MQTT_connect() { 84 int8_t ret; 85 86 // Stop if already connected. 87 if (mqtt.connected()) { 88 return; 89 } 90 91 Serial.print("Connecting to MQTT... "); 92 93 uint8_t retries = 3; 94 95 while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 96 Serial.println(mqtt.connectErrorString(ret)); 97 Serial.println("Retrying MQTT connection in 5 seconds..."); 98 mqtt.disconnect(); 99 delay(5000); // wait 5 seconds 100 retries--; 101 if (retries == 0) { 102 // basically die and wait for WDT to reset me 103 while (1); 104 } 105 } 106 Serial.println("MQTT Connected!"); 107 108} 109
Downloadable files
Circuit Diagram
Circuit Diagram
Comments
Only logged in users can leave comments