3. Control switches Over internet using voice commands
In this code, you have to import one more URL library in python code for open or read/write the data on API. This time I use third party API which is easy to use and access.
import pyttsx3
import speech_recognition as sr
import serial
import time
import urllib.request, urllib.parse, urllib.error
then you have to add the URL of writing API in code, the remaining code is the same as the previous code.
if __name__ == '__main__':
while True:
query = takecommand()
if query == "turn on light":
data = urllib.request.urlopen("https://api.thingspeak.com/update?api_key=3R9TIRNOQ&field1=" + str(1));
print("LED turned ON")
speak("LED turned ON")
time.sleep(1) if query == 'turn off light':
data = urllib.request.urlopen("https://api.thingspeak.com/update?api_key=3RRK249TINOQ&field1=" + str(0));
print("LED turned OFF")
speak("LED turned OFF")
time.sleep(1)
Hardware sketch
we have to access the internet so, we can use the Nodemcu 12-E developing board which can connect your hardware with the internet, and also it is programmed with the Arduino IDE.
So in this Arduino sketch, we have to import some libraries to access the internet and fetch data from API then set your password and SSID, initializing the digital pins, set your channel id, and API key.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>const char* ssid = "your SSID";
const char* password = "your password";int Relay = D1;
int LED = D2;WiFiClient client;unsigned long myChannelNumber = 1107673; //Your Channel Number (Without Brackets)
const char * myReadAPIKey = "YBBFTDBQ1R1JHNKT"; //Your Write API Key
Setup, this function gives you connected internet before starting the loop function so it shows your IP address, SSID name on the display, and connects you with the thingspeak server.
void setup() {
Serial.begin (115200);
delay (10);pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);pinMode(LED, OUTPUT);//CONNECT TO WIFI NETWORK
Serial.println();
Serial.println();
Serial.println("connecting to");
Serial.println(ssid);WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED){
delay(300);
digitalWrite(LED, HIGH);
Serial.print(".");
delay(100);
digitalWrite(LED, LOW);
}Serial.println("");
Serial.println("WiFi connected");
digitalWrite(LED, HIGH);//Print the IP address
Serial.println("Use this URL to connect:");
Serial.print("https://");
Serial.print(WiFi.localIP());
Serial.println("/");ThingSpeak.begin(client);
}
now moving onwards, codes the loop function, it reads the all data which you write on API from python code, and make sure your internet connection is stable otherwise you give late responses from the server.
This code works with reading API of thingspeak which is a third party application, if the status code is “200” then the code will work perfectly otherwise it will not work. 200 means your server is online and ready to use to get all requests(GET, POST, PUT…), and this function working in a loop until the interrupts.
void loop(){long temp = ThingSpeak.readLongField(myChannelNumber, 1, myReadAPIKey);
if(statusCode == 200){
int statusCode = ThingSpeak.getLastReadStatus();
if(temp == 1)
{
digitalWrite(Relay, HIGH);
Serial.println("HIGH");
Serial.println(temp);
}
if (temp == 0)
{
digitalWrite(Relay, LOW);
Serial.println("LOW");
Serial.println(temp);
}
}
else
{
Serial.println("Unable to read channel / No internet connection");
}
delay(100);
}
I made this video so you can easily understand how it works? if you want these codes, download from my GitHub.
If you learn something from my article, don’t forget to give me a clap👏🏻.
Thank you🧡