#8 Arduino Mega: Display Realtime Temperature graph on cloud (using LM35 and ESP01) | APDaga Tech

#8 Arduino Mega: Display Realtime Temperature graph on cloud (using LM35 and ESP01) | APDaga Tech

IoT Tutorial #8 [ Arduino Mega Tutorials #8 ]

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.


Physical Connections: 
Arduino Mega  ->  LM35 (Analog Tempt Sensor)
5v -> Vcc (Left Pin - Flat side facing towards you)
A0 -> Analog Out (Middle Pin) (10mV / degree Celcious)
Gnd -> Gnd (Right Pin - Flat side facing towards you)

Pin 13 -> LED+
Gnd -> LED-

PIN DIAGRAM OF ESP01: (This module works on 3.3v) (NOT 5v)
+---------------------------+
| |=| |=| |=|============== |
| | | | | | | ANTENA |
| | |=| |=| | Facing ur side|
| ========================= |
| |
| GND GPIO0 GPIO2 RxD |
| o o o o |
| |
| o o o o |
| TxD CH_PD RST Vcc |
+---------------------------+

CONNECTIONS FOR COMMUNICATION:
ESP01 -> Arduino

Vcc -> 3.3V
GND -> GND
TxD -> Rx1 (Pin 19)
RxD -> Tx1 (Pin 18)
CH_PD -> 3.3V

Serial Communication with PC through USB cable (Tx0 and Rx0)





Steps:
  1. Sign up at https://thingspeak.com
  2. Channels > New Channel > Update "Name" and "Field 1" field names > Save Channel
  3. Click On API keys > Copy "Write API key"
  4. Make all the Connections to Arduino Mega board mentioned Above.
  5. 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)
  6. Upload Program to Arduino Mega Board
  7. Open Arduino Serial Monitor on PC (Set Baud Rate to 9600 and set "Both NL & CR"
  8. 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

7 Comments

  1. Showing warnings like:

    C:\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"))

    ^

    ReplyDelete
  2. Hi sir, i need a help sir
    I am getting a connected to cloud statement in serial monitor but i am not getting my result on thingspeak graph,
    Plzz help me sir

    ReplyDelete
  3. i am getting this error while varifying program - Serial1 was not declare in this scope
    please help

    ReplyDelete
    Replies
    1. Are you sure, you are using Arduino Mega 2560 board ?
      If you are using Arduino Uno or similar small board, there is no Serial1 in Arduino Uno. (Uno has only one serial module : Serial)

      Delete
  4. 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

    ReplyDelete
    Replies
    1. I won't be able to debug your problem with this much information.
      Please try again by following each and every step I mentioned.
      Thanks

      Delete
Post a Comment
Previous Post Next Post