IoT Tutorial #10 [ NodeMCU Tutorials #2 ]
In this program,
In this program,
Analog Temperature sensor (LM35) is connected to the NodeMCU ESP8266 board and Its output will be sent to https://thingspeak.com server (Cloud) where we can see the visual graph of Temperature vs Time.
ESP8266MOD NodeMCU 1.0 (ESP-12E Module) Board is used to connect to the Internet and send data to https://thingspeak.com server
Note:
Internal 10 bit ADC (A0) is used to read the Analog output of the Temperature Sensor (LM35).
you can visit the following link to check the step to configure Arduino IDE to connect to NodeMCU board and burn the first program in it:
http://www.apdaga.blogspot.com/2017/12/nodemcu-connect-to-arduino-ide-and-burn.html
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
Requirements:
- NodeMCU board - 1.0 ESP8266 (ESP-12E Module)
- Micro USB Cable
- LM35 (Analog Temperature Sensor)
- Arduino IDE installed on your PC.
- Active Internet Connection
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 the below-written program.
a. apiKey by above copied "Write API key" (in step 3)
b. ssid_name (Name of you WiFi)
c. password (Password of your WiFi) - Upload Program to NodeMCU 1.0 (ESP-12E Module)
- Open Arduino Serial Monitor on PC (Set Baud Rate to 115200 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
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:
#include <ESP8266WiFi.h> // Import NodeMCU Libraries
const char* host = "api.thingspeak.com"; // Your cloud domain
String ApiKey = "UUC6ZAZTL28P9SFU"; // Change this key to your "Write API key"
String path = "/update?key=" + ApiKey + "&field1="; // API Format
const char* ssid = "SuiteUp"; // Change this to your ssid_name (WiFi Name)
const char* pass = "welcome@10"; // Change this to your WiFi password
int sensor = A0; // Analog Input LM35 Output
float tempc;
float Digital_mVolt;
void setup(void){
Serial.begin(115200); // PC Arduino Serial Monitor
Serial.println("");
WiFi.begin(ssid, pass); // Func to Connect to WiFi
while (WiFi.status() != WL_CONNECTED) { // Wait for connection
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid); // Display confirmation msg to PC
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); // Display IP Address to PC
}
void loop() {
float analog_val = analogRead(sensor); // Read Analog Input
Digital_mVolt = (analog_val*3100.0)/1023; // Analog temperature to mVolt //Max Analog Input = 3.1V
tempc = Digital_mVolt/10; // Temperature from mVolt to degree Celsius
Serial.println(tempc); // Display Temperature to PC
WiFiClient client; // Create object of Class WiFiClient
const int httpPort = 80; // Port No. 80
if (!client.connect(host, httpPort)) { // If Couldn't connect
Serial.println("connection failed"); // Display error msg to PC
return;
}
client.print(String("GET ") + path + String(tempc) + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: keep-alive\r\n\r\n"); // Send Temperature data to Cloud
// thingspeak free version needs 16 sec delay between updates
delay(16000); // wait for 16sec
}
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