In this program,
Analog Temperature sensor (LM35) is connected to the Arduino Mega board and Its output will be sent to https://thingspeak.com server (Cloud) where we can see the visual graph of Temperature vs Time.
ESP01 WiFi Module is used to connect to the Internet and send data to https://thingspeak.com server
The output is shown using GRAPH (Temperature vs Time)on thinkspeak server.
Note:
Internal 10 bit ADC (A0) is used to read the Analog output of the Temperature Sensor.
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
- Channels > New Channel > Update "Name" and "Field 1" field names > Save Channel
- Click On API keys > Copy "Write API key"
- Make all the Connections to Arduino Mega board mentioned Above.
- Change Following 3 fields in below written program.
a. apiKey by above copied "Write API key" (in step 3)
b. ssid_name (Name of you WiFi)
c. password (Passord of your WiFi) - Upload Program to Arduino Mega Board
- Open Arduino Serial Monitor on PC (Set Baud Rate to 9600 and set "Both NL & CR"
- Go to https://thingspeak.com and login
Channels > My Channels > Click on Channel Name (created in step 2) > Private View
Here you can observe the Graph of Temperature Vs Day.
For Better understanding you can watch demonstration video given below:
Downloads:
Download link is given in the Description of the YouTube video shown below.
Demonstration:
Program:
int ledPin = 13; // LED+ //float j=0; int temp_sensor = A0; // LM35 Output String apiKey = "W4BW8JXBYH3NBUMS"; //Change this key to your "Write API key" void setup() { // put your setup code here, to run once: pinMode(ledPin, OUTPUT); // Set ledPin as Output Serial.begin(9600); // PC Arduino Serial Monitor Serial1.begin(115200); // Arduino to ESP01 Communication connectWiFi(); // To connect to Wifi } void loop() { // put your main code here, to run repeatedly: float analog_val = analogRead(temp_sensor); // Read Analog Temperature float digital_vout = (analog_val*5000.0)/1023; // Convert Analog Tempt. to Digital Value (10 bit ADC) // mV float tempc = digital_vout/10; // Storing value in Degree Celsius //j=tempc; digitalWrite(ledPin, HIGH); // LED ON delay(500); // wait for 500 mSec digitalWrite(ledPin, LOW); // LED OFF Serial1.println("AT+CIPMUX=0\r\n"); // To Set MUX = 0 delay(2000); // Wait for 2 sec // TCP connection String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection with https://thingspeak.com server cmd += "184.106.153.149"; // IP addr of api.thingspeak.com cmd += "\",80\r\n\r\n"; // Port No. = 80 Serial1.println(cmd); // Display above Command on PC Serial.println(cmd); // Send above command to Rx1, Tx1 delay(20000); // Wait for 20 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 String getStr = "GET /update?api_key="; getStr += apiKey; getStr +="&field1="; getStr += tempc; getStr += "\r\n\r\n"; Serial.println(getStr); // Display GET String on PC cmd = "AT+CIPSEND="; // send data length cmd += String(getStr.length()); cmd+="\r\n"; Serial.println(cmd); // Display Data length on PC Serial1.println(cmd); // Send Data length command to Tx1, Rx1 delay(20000); // wait for 20sec if(Serial1.find(">")) // If prompt opens //verify connection with cloud { Serial.println("connected to Cloud"); // Display confirmation msg to PC Serial1.print(getStr); // Send GET String to Rx1, Tx1 } else { Serial1.println("AT+CIPCLOSE\r\n"); // Send Close Connection command to Rx1, Tx1 Serial.println("AT+CIPCLOSE"); // Display Connection closed command on PC } // thingspeak free version needs 16 sec delay between updates delay(16000); // wait for 16sec } 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"; // ssid_name cmd += "\",\""; cmd += "welcome@10"; // password 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 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
Showing warnings like:
ReplyDeleteC:\Users\Divyanshu\Desktop\Temperature_Graph_WiFi-APDaga\Temperature_Graph_WiFi-APDaga.ino: In function 'void loop()':
C:\Users\Divyanshu\Desktop\Temperature_Graph_WiFi-APDaga\Temperature_Graph_WiFi-APDaga.ino:41:26: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
if(Serial1.find("ERROR")) // If returns error in TCP connection
^
C:\Users\Divyanshu\Desktop\Temperature_Graph_WiFi-APDaga\Temperature_Graph_WiFi-APDaga.ino:65:22: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
if(Serial1.find(">")) // If prompt opens //verify connection with cloud
^
C:\Users\Divyanshu\Desktop\Temperature_Graph_WiFi-APDaga\Temperature_Graph_WiFi-APDaga.ino: In function 'boolean connectWiFi()':
C:\Users\Divyanshu\Desktop\Temperature_Graph_WiFi-APDaga\Temperature_Graph_WiFi-APDaga.ino:98:27: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
if(Serial1.find("+CWJAP"))
^
I need some help....
ReplyDeleteI would like to send sensor data to my LocalHost. so i changed the
String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection with https://thingspeak.com server
cmd += "my local host ip address"; // IP addr of api.thingspeak.com
cmd += "\",80\r\n\r\n"; // Port No. = 80
and
// prepare GET string
String getStr = "sensordata.php?api_key=";
But no success....I would appreciate your efforts
Hi sir, i need a help sir
ReplyDeleteI am getting a connected to cloud statement in serial monitor but i am not getting my result on thingspeak graph,
Plzz help me sir
What error you are getting?
Deletei am getting this error while varifying program - Serial1 was not declare in this scope
ReplyDeleteplease help
Are you sure, you are using Arduino Mega 2560 board ?
DeleteIf you are using Arduino Uno or similar small board, there is no Serial1 in Arduino Uno. (Uno has only one serial module : Serial)
i'm having a problem, the sketch doesn't upload to the board, every time it shows me this message:avrdude: stk500v2_getsync(): timeout communicating with programmer, what i'm i doing wrong? i have the same arduino, the same esp-01, i configured it with no problem (using the previous video), but now it won't upload
ReplyDeleteI won't be able to debug your problem with this much information.
DeletePlease try again by following each and every step I mentioned.
Thanks
This comment has been removed by the author.
ReplyDeleteMissed seeing CWJAP.......fine @apdhaga
Delete