Upload Sensor Data to ThingSpeak using NodeMCUSharath
ThingSpeak is an Open-Source IoT application and API to store and retrieve data from Hardware devices and Sensors. It uses HTTP Protocol over the Internet or LAN for its communication. The MATLAB analytics is included to analyze and visualize the data received from your Hardware or Sensor Devices.
We can create channels for each and every sensor data. These channels can be set as private channels or you can share the data publically through Public channels. The commercial features include additional features. But we will be using the free version as we doing it for educational purpose.
You can make projects like Weather Station, Tide Prediction, Counter and Many More.
- Collect data in private channels.
- Share Data with Public Channels
- REST API and MQTT APIS
- MATLAB® Analytics and Visualizations.
- Worldwide Community
In this tutorial, we will be using an LDR to plot its light Intensity level on ThingSpeak using NodeMCU. We will program the NodeMCU to read and store the LDR data into a variable and then upload it to ThingSpeak using its channel name and API key. The NodeMCU should be connected to the internet via Wi-Fi. We will see how to create ThingSpeak Channels and configure it on NodeMCU.
- LDR Module
- 10K Ohm Resistor
- Jumper Wires
- Breadboard (Optional)
Once the hardware is set up, We can go ahead and create our ThingSpeak Channel.
Step 1: Go to https://thingspeak.com/ and create your ThingSpeak Account if you don’t have. Login to Your Account.
Step 2: Create a Channel by clicking ’New Channel’.
Step 3: Enter the channel details.
Name: Any Name
Field 1: Light Intensity LDR – This will be displayed on the analytics graph. If you need more than 1 Channels you can create for additional Sensor Data.
Save this setting.
Step 4: Now you can see the channels. Click on the ‘API Keys’ tab. Here you will get the Channel ID and API Keys. Note this down.
Step 5: Open Arduino IDE and Install the ThingSpeak Library. To do this go to Sketch>Include Library>Manage Libraries. Search for ThingSpeak and install the library.
Step 6: Now we need to modify the program with your credentials.
In the below code you need to change your Network SSID, Password and your ThingSpeak Channel and API Keys.
Replace the following content in the code,
‘Your SSID Here’ – Your Wi-Fi Name
‘Your Password Here’ – Your Wi-Fi Password
‘YYYYYY’ – Your ThingSpeak Channel Number (without Quotes)
‘XXXXXXXXXXX’ – Your Thing Speak API Key.
const char* ssid = "Your SSID Here"; //Your Network SSID
const char* password = "Your Password Here"; //Your Network Password
int LDRpin = A0; //LDR Pin Connected at A0 Pin
unsigned long myChannelNumber = YYYYYY; //Your Channel Number (Without Brackets)
const char * myWriteAPIKey = "XXXXXXXXXXXXXXX"; //Your Write API Key
// Connect to WiFi network
val = analogRead(LDRpin); //Read Analog values and Store in val variable
Serial.print(val); //Print on Serial Monitor
ThingSpeak.writeField(myChannelNumber, 1,val, myWriteAPIKey); //Update in ThingSpeak
Upload the code. Once it is connected to Wi-Fi the data will start uploading to the ThingSpeak Channel. You can now open your Channel and see the data changes plotted on the ThingSpeak.