Home
About Us
Achievement
Home
Raspberry-Pi Projects
_Raspberry Pi 4 Complete Guide
Arduino Project
_All Projects
Esp8266 Project
_All Projects
Esp32 Project
_Automation-with-feedback
Controlling Smart Board Using Adafruit iO.
Just Do Electronics
May 01, 2020
Smart Board
Hey Friends In This Video I Will Show You How To Make Controlling Smart Board Using Adafruit.io IoT Platform.
Circuit Diagram :-
RELAY CONECTION :-
TWO WAY SWITCH CONECTIONS :-
Code :-
//Prateek //www.prateeks.in #include
#include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include "DHT.h" //Relays for switching appliances #define Relay1 D1 #define Relay2 D2 #define Relay3 D3 #define Relay4 D4 //DHT11 for reading temperature and humidity value #define DHTPIN D5 //buzzer to know the status of MQTT connections and can be used for any other purpose according to your project need. #define buzzer D0 #define WLAN_SSID "justDo" #define WLAN_PASS "pratik123" // Adafruit.io Setup #define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 #define AIO_USERNAME "justdoelectronics" #define AIO_KEY "aio_MbRZ81TiDcZaH6BsxlRkVBgIF6hE" WiFiClient client; // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); // Notice MQTT paths for AIO follow the form:
/feeds/
Adafruit_MQTT_Publish Humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity"); Adafruit_MQTT_Publish Temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature"); // Setup a feed called 'onoff' for subscribing to changes. Adafruit_MQTT_Subscribe Light1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay1"); Adafruit_MQTT_Subscribe Fan1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay2"); Adafruit_MQTT_Subscribe Light2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay3"); Adafruit_MQTT_Subscribe Fan2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay4"); #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); uint32_t delayMS; void MQTT_connect(); void setup() { Serial.begin(115200); pinMode(buzzer, OUTPUT); pinMode(Relay1, OUTPUT); pinMode(Relay2, OUTPUT); pinMode(Relay3, OUTPUT); pinMode(Relay4, OUTPUT); Serial.println(F("Adafruit MQTT demo")); // Connect to WiFi access point. Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(WLAN_SSID); WiFi.begin(WLAN_SSID, WLAN_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); //Setting up DHT sensor dht.begin(); // Setup MQTT subscription for onoff feed. mqtt.subscribe(&Light1); mqtt.subscribe(&Fan1); mqtt.subscribe(&Light2); mqtt.subscribe(&Fan2); } uint32_t x = 0; void loop() { MQTT_connect(); Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(20000))) { if (subscription == &Light1) { Serial.print(F("Got: ")); Serial.println((char *)Light1.lastread); int Light1_State = atoi((char *)Light1.lastread); digitalWrite(Relay1, Light1_State); } if (subscription == &Light2) { Serial.print(F("Got: ")); Serial.println((char *)Light2.lastread); int Light2_State = atoi((char *)Light2.lastread); digitalWrite(Relay2, Light2_State); } if (subscription == &Fan1) { Serial.print(F("Got: ")); Serial.println((char *)Fan1.lastread); int Fan1_State = atoi((char *)Fan1.lastread); digitalWrite(Relay3, Fan1_State); } if (subscription == &Fan2) { Serial.print(F("Got: ")); Serial.println((char *)Fan2.lastread); int Fan2_State = atoi((char *)Fan2.lastread); digitalWrite(Relay4, Fan2_State); } } // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; } if (! Humidity.publish(h)) { Serial.println(F("Failed")); } else { Serial.println(F("OK!")); } if (! Temperature.publish(t)) { Serial.println(F("Failed")); } else { Serial.println(F("OK!")); } } void MQTT_connect() { int8_t ret; // Stop if already connected. if (mqtt.connected()) { return; } Serial.print("Connecting to MQTT... "); uint8_t retries = 3; digitalWrite(buzzer, HIGH); delay(200); digitalWrite(buzzer, LOW); delay(200); digitalWrite(buzzer, HIGH); delay(200); digitalWrite(buzzer, LOW); delay(200); while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); mqtt.disconnect(); delay(5000); // wait 5 seconds retries--; if (retries == 0) { // basically die and wait for WDT to reset me while (1); } } Serial.println("MQTT Connected!"); digitalWrite(buzzer, HIGH); delay(2000); digitalWrite(buzzer, LOW); }
0 Comments
Newer
Older
ESP32 Interfacing With LDR Sensor
Build Your Own Gps Tracking System using Arduino
IoT Based Fingerprint Biometric Attendance System Using NodeMCU (Esp8266)
Ultrasonic Sensor as a Counter with Arduino
ESP32 Led Blink With Push Button
ESP32 Interfacing Potentiometer
Measure CO2 Level in Air Using Arduino
IoT Based Patient Health Monitoring System Using Blynk App
Vehicle Accident Alert System Using Accelerometer GPS And GSM
Play Audio Sound With Esp8266 With Blink App
YouTube
Like on Facebook
Follow on Twitter
Follow on Google+
Follow on Instagram
Subscribe on Youtube
Hi WelCome To My Website If you have a creative mind and want to learn Electronics even without studying it then FRIENDS this Website And Youtube Channel is for you, I am crazy creators who love making Electronics things. I have a bunch of more than 130 Tutorial Videos on My YouTube Channel and I upload a new Tutorial every Sunday.
Tags
Arduino Project
Esp32 Project
Esp8266 Project
0 Comments