In this program,
LED connected to Arduino Mega board is controlled (Toggled) using TalkBack Application from https://thingspeak.com server (cloud).
ESP01 WiFi Module is used to connect to the Internet and send data to https://thingspeak.com server
The output is shown using LED & Serial Monitor on PC.
Note:
TalkBack application is the feature in which,
we can store some commands on thinkspeak server and then we can call those commands using API.
When we trigger particular API, we receive some command as a response and then we can control the output device (Eg. LED) as per received response from the cloud.
Apart from that, we can also add, delete and Change the commands using APIs
Recommended Internet of Things (IOT) Courses:
- Udemy: Complete Guide to Build IOT Things from Scratch to Market
- LinkedIn: IoT Foundations: Fundamentals
- edX: Introduction to the Internet of Things (IoT)
- edureka: IoT Certification Training on Azure
- Coursera: An Introduction to Programming the Internet of Things (IOT) Specialization
- Eduonix: Internet Of Things (IOT) Bundle
Physical Connections:
Steps:
- Sign up at https://thingspeak.com
- Apps > TalkBalk > New TalkBack > Update "Name" > Click on "Add a new Command"
update position=1, command string=LED_ON > Click on "Add a new Command"
update position=2, command string=LED_OFF" > Save TalkBalk - Copy following things and replace those in the below program:
a. API_Key
b. TalkBack_ID
c. Command_ID (For LED_ON)
d. Command_ID (For LED_OFF) - Make all the Connections to Arduino Mega board mentioned Above.
- Change Following 6 fields in below written program.
a. ssid_name (Name of you WiFi)
b. password (Passord of your WiFi)
c. API_Key
d. TalkBack_ID
e. Command_ID
FORMAT:
https://api.thingspeak.com/talkbacks/<TalkBack_ID>/commands/<COMMAND_ID>?api_key=<API_Key>
Replace <TalkBack_ID>, <Command_ID> and <API_Key> by actual values
(you got from step 3.a, 3.b, 3.c or 3.d respectively) - Upload Program to Arduino Mega Board
- Open Arduino Serial Monitor on PC (Set Baud Rate to 9600 and set "Both NL & CR")
Program Outcome:
LED connected to Pin 13 should Blink with a specific interval.
Observe the connection to the thinkspeak server and received msgs using Serial monitor.
For Better understanding you can watch the demonstration video given below:
LED connected to Pin 13 should Blink with a specific interval.
Observe the connection to the thinkspeak server and received msgs using Serial monitor.
For Better understanding you can watch the demonstration video given below:
Downloads:
Download link is given in the Description of the YouTube video shown below.
Demonstration:
Program:
int led=13;
void setup()
{
Serial.begin(9600); // PC Arduino Serial Monitor
Serial1.begin(115200); // Arduino to ESP01 Communication // RXD - TX1, TXD - RX1
pinMode(led,OUTPUT); // Set ledPin as Output
digitalWrite(led,LOW); // LED OFF
delay(100); // wait for 100 mSec
connectWiFi(); // To connect to Wifi
}
void loop()
{
//////////////////////// START: LED_ON TALKBAK ////////////////////////////
Serial1.println("AT+CIPMUX=0\r\n"); // Set MUX=0
delay(1000); // wait for 1 Sec
// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection with https://thingspeak.com server
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80\r\n\r\n"; // Port No. = 80
Serial1.println(cmd); // Send above command to Rx1, Tx1
delay(1000); // wait for 1 Sec
if(Serial1.find("ERROR")) // If returns error in TCP Connection
{
Serial.println("AT+CIPSTART error"); // Display error msg to PC
return;
}
// prepare GET string
// Replace <TalkBack_ID>, <Command_ID> and <API_Key> by actual values here
String getStr = "GET /talkbacks/20941/commands/10327032?api_key=CLMIBI9XMK35YJWQ\r\n\r\n"; //LED_ON Get a TalkBack Command
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
cmd+="\r\n";
Serial1.println(cmd); // Send Data length command to Tx1, Rx1
delay(100); // wait for 0.1 Sec
if(Serial1.find(">")) // If prompt opens //verify connection with cloud
{
Serial.println("Request Sent"); // Display confirmation msg to PC
Serial1.print(getStr); // Send GET String to Rx1, Tx1
}
else
{
Serial1.println("AT+CIPCLOSE"); // Send Close Connection command to Rx1, Tx1
// alert user
Serial.println("Closing Connection"); // Display Connection closed command on PC
}
delay(5000); // wait for 5 Sec
String recd=""; // Received String
if(Serial1.available()>0) // Check Buffer
{
delay(1000); // wait for 1 Sec
while(Serial1.available()) // Till Buffer is not Empty
{
recd+=(char(Serial1.read())); // Read the Buffer
}
Serial.println("____________"); // Display msg to PC
Serial.println("Response: "); // Display msg to PC
Serial.print(recd); // Display Received msg to PC
}
if(find_text("LED_ON",recd)==1) // Check if LED_ON msg received
digitalWrite(led,HIGH); // LED ON
//////////////////////// END: LED_ON TALKBAK ////////////////////////////
//////////////////////// START: LED_OFF TALKBAK ////////////////////////////
Serial1.println("AT+CIPMUX=0\r\n"); // Set MUX=0
delay(1000); // wait for 1 Sec
// TCP connection
cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection with https://thingspeak.com server
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80\r\n\r\n"; // Port No. = 80
Serial1.println(cmd); // Send above command to Rx1, Tx1
delay(1000); // wait for 1 Sec
if(Serial1.find("ERROR")) // If returns error in TCP Connection
{
Serial.println("AT+CIPSTART error"); // Display error msg to PC
return;
}
// prepare GET string
// Replace <TalkBack_ID>, <Command_ID> and <API_Key> by actual values here
getStr = "GET /talkbacks/20941/commands/10327039?api_key=CLMIBI9XMK35YJWQ\r\n\r\n"; //LED_OFF Get a TalkBack Command
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
cmd+="\r\n";
Serial1.println(cmd); // Send Data length command to Tx1, Rx1
delay(100); // wait for 0.1 Sec
if(Serial1.find(">")) // If prompt opens //verify connection with cloud
{
Serial.println("Request Sent"); // Display confirmation msg to PC
Serial1.print(getStr); // Send GET String to Rx1, Tx1
}
else
{
Serial1.println("AT+CIPCLOSE"); // Send Close Connection command to Rx1, Tx1
// alert user
Serial.println("Closing Connection"); // Display Connection closed command on PC
}
delay(5000); // wait for 5 Sec
recd=""; // Received String
if(Serial1.available()>0) // Check Buffer
{
delay(1000); // wait for 1 Sec
while(Serial1.available()) // Till Buffer is not Empty
{
recd+=(char(Serial1.read())); // Read the Buffer
}
Serial.println("____________"); // Display msg to PC
Serial.println("Response: "); // Display msg to PC
Serial.print(recd); // Display Received msg to PC
}
if(find_text("LED_OFF",recd)==1) // Check if LED_OFF msg received
digitalWrite(led,LOW); // LED OFF
//////////////////////// END: LED_OFF TALKBAK ////////////////////////////
delay(5000);
}
int find_text(String sr, String recd) //function to serach a Substring in Received msg
{
int foundpos = -1;
for (int i = 0; (i < recd.length() - sr.length()); i++)
{
if (recd.substring(i,sr.length()+i) == sr)
{
foundpos = 1;
}
}
return foundpos;
}
boolean connectWiFi() { // Connect to Wifi Function
Serial1.println("AT+CWMODE=1\r\n"); // Setting Mode = 1
delay(100); // wait for 100 mSec
String cmd = "AT+CWJAP=\""; // Connect to WiFi
cmd += "SuiteUp"; // Replace ssid_name here
cmd += "\",\"";
cmd += "welcome@10"; // Replace password here
cmd += "\"\r\n";
Serial.println(cmd); // Display Connect Wifi Command on PC
Serial1.println(cmd); // send Connect WiFi command to Rx1, Tx1
delay(10000); // wait for 10 sec
Serial1.println("AT+CWJAP?"); // Verify Connected WiFi
if(Serial1.find("+CWJAP"))
{
Serial.println("OK, Connected to WiFi."); // Display Confirmation msg on PC
return true;
}
else
{
Serial.println("Can not connect to the WiFi."); // Display Error msg on PC
return false;
}
}
Click here to see more codes for Raspberry Pi 3 and similar Family.
&
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.
Feel free to ask doubts in the comment section. I will try my best to solve it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.
Thanks and Regards,
-Akshay P. Daga