Robotics and Iot Project: By: Konda Sai Nithin
Robotics and Iot Project: By: Konda Sai Nithin
I C S A ND I O
ROBOT
By:
Konda Sai Nithin
IoT controlled smart home
• Set up a google assistant applet from ifttt to
trigger a web request which will update a field
in thingspeak cloud.then read data from
thingspeak cloud using arduino to control
appliances in your home from anywhere in the
world.
Components Required
• Arduino uno with cable
• Esp2866
• Relay
• 230 volt bulb
• Wires
• Jumper cables
Circuit Diagram
CODE
• #include <ESP8266WiFi.h> • //WiFiClientSecure client;
• #include "Adafruit_MQTT.h" • // Setup the MQTT client class by passing in the
• #include "Adafruit_MQTT_Client.h" WiFi client and MQTT server and login details.
• #define in1 5 //D1 • Adafruit_MQTT_Client mqtt(&client, AIO_SERVER,
• #define in2 4 //D2 AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
• /********* WiFi Access Point ***********/
• /********** Feeds *************/
• #define WLAN_SSID "Ravi"
•
• // Setup a feed called 'photocell' for publishing.
#define WLAN_PASS "ravi3399"
• /********* Adafruit.io Setup ***********/ • // Notice MQTT paths for AIO follow the form:
• #define AIO_SERVER "io.adafruit.com" <username>/feeds/<feedname>
• #define AIO_SERVERPORT 1883 // use 8883 for • //Adafruit_MQTT_Publish photocell =
SSL Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
• #define AIO_USERNAME "ravi3399" "/feeds/test");
• #define AIO_KEY •
"77287ece71c84ec690ed49bfdd747ab7" • // Setup a feed called 'onoff' for subscribing to
• /**** Global State (you don't need to change this!) changes.
******/
•
• Adafruit_MQTT_Subscribe switchbutton =
// Create an ESP8266 WiFiClient class to connect to the
MQTT server. Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME
• WiFiClient client; "/feeds/switch");
• // or... use WiFiFlientSecure for SSL
CODE
• /********* Sketch Code ************/ • // Setup MQTT subscription for onoff feed.
• // Bug workaround for Arduino 1.6.6, it seems to need a function • mqtt.subscribe(&switchbutton);
declaration
•
• // for some reason (only affects ESP8266, likely an arduino-builder bug).
• void MQTT_connect(); • }
• void setup() { • uint32_t x=0;
• Serial.begin(115200); • void loop() {
• delay(10); • // Ensure the connection to the MQTT server is alive (this
• Serial.println(F("Adafruit MQTT demo")); will make the first
• // Connect to WiFi access point. • // connection and automatically reconnect when
• Serial.println(); Serial.println(); disconnected). See the MQTT_connect
• Serial.print("Connecting to "); • // function definition further below.
• Serial.println(WLAN_SSID);
• MQTT_connect();
•
• // this is our 'wait for incoming subscription packets' busy
• WiFi.begin(WLAN_SSID, WLAN_PASS);
subloop
• while (WiFi.status() != WL_CONNECTED) {
• delay(500); • // try to spend your time here
• Serial.print("."); • Adafruit_MQTT_Subscribe *subscription;
• } • while ((subscription = mqtt.readSubscription())) {
• Serial.println(); • if(subscription == &switch)
• • {
• Serial.println("WiFi connected");
• Serial.print(F("Got_switch: "));
• Serial.println("IP address: "); Serial.println(WiFi.localIP());
• Serial.println((char *)switchbutton.lastread);
• pinMode(in1,OUTPUT);
• • int switch_state = atoi((char *)switchbutton.lastread);
• digitalWrite(in1,LOW); •
CODE
• Serial.print(F("Got_switch: ")); • }
• Serial.println((char *)switchbutton.lastread); • }
• String RAW = (char *)switchbutton.lastread; • // ping the server to keep the mqtt connection
• int i=0; alive
• if(RAW.charAt(i) != '_') • // NOT required if you are publishing once
• i++; every KEEPALIVE seconds
• String pin = RAW.substring(0,i); • /* if(! mqtt.ping()) {
• //Serial.println(pin); • mqtt.disconnect();
• int pin_number = pin.toInt(); • }
• Serial.println(pin_number); • */
• if(pin_number == 1) • }
• {Serial.println("lights are on"); • // Function to connect and reconnect as
necessary to the MQTT server.
• digitalWrite(in1,HIGH);
• // Should be called in the loop function and it
• digitalWrite(in2,HIGH);
will take care if connecting.
• }
• void MQTT_connect() {
• if(pin_number == 0)
• int8_t ret;
• {Serial.println("lights are off");
• // Stop if already connected.
• digitalWrite(in1,LOW);
• if (mqtt.connected()) {
• }
• return;
•
• }
CODE
• Serial.print("Connecting to MQTT... ");
•
• uint8_t retries = 3;
• 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!");
• }