Login

Sending Data to Thingspeak

Now that you know what Thingspeak is and created your account on Thingspeak, let us have a look on how data is transmitted from your Arduino to the Thingspeak cloud platform. In this exercise, we will be sending some sample data to understand how the whole process works. Thingspeak also allows us to visualize the data we’ve transmitted in the form of graphs to better interpret our data.

To start, log in to your Thingspeak account and navigate to MyChannels. It will look something like this:

The screenshot above shows you a few examples of some ‘Channels’ to which your data will be sent. Click on ‘New Channel’ to create your first new channel to sent data to. Add a suitable title, description, and two fields as shown in the screenshot below. Each ‘Field’ will contain a specific kind of data. For example, to monitor temperature and humidity of a greenhouse, temperature data is logged to one field and humidity to another field. The number of fields corresponds to the number of different types of data you want to send to your channel. You may leave other fields empty for now. Once your channel is created, it will look something like this:

Now let’s actually send some data into the two fields we created. For this example, let us send a set of odd numbers to ‘Field 1’ and set of even numbers to ‘Field 2’.

Hardware connections: 

Connect your NodeMCU board to your computer, following the setup instructions as explained in the previous unit.

Code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>
const char* ssid = “Enter your Wifi name”;
const char* password = “Enter your Wifi password”;
WiFiClient client;
unsigned long myChannelNumber = 561042;
const char * myWriteAPIKey = “UY47OILGY9AP2AGU”;
int val1=1;
int val2=0;
int state = 1;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
// Print the IP address
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}

void loop() {
if (state==1) {
ThingSpeak.writeField(myChannelNumber, 1, val1, myWriteAPIKey);
state = state+1;
val1=val1+2; // val1 prints a set of odd numbers
Serial.println(state);
Serial.println(“Channel 1 transmitted”);
}

else {
ThingSpeak.writeField(myChannelNumber, 2, val2, myWriteAPIKey);
Serial.println(state);
Serial.println(“channel 2 transmitted”);
state = 1;
val2=val2+2; //val2 prints a set of even numbers
}
Serial.println(state);
delay(30000);
// put your main code here, to run repeatedly:

}

Copy the code above to your Arduino IDE, make the following changes to it:

  1. Enter the SSID ID and Password of your Wi-Fi connection in their corresponding fields: const char* ssid = “Enter your Wifi name”;
    const char* password = “Enter your Wifi password”;
  2. Enter your channel number in its corresponding field. It can be found under the title of your channel as shown below:
  3. Find your WriteAPI key under the ‘API Keys’ tab as shown below. Copy it and paste it in myWriteAPI variable: const char * myWriteAPIKey = “UY47OILGY9AP2AGU”. Your API key will be different and can be changed if you feel that the key may be compromised. It is unique to your channel and it is used to write data into it, as the name suggests.

Once you upload the code and your connection is established, you should be able to see your data being logged onto your channel and can be visualized on the plot. You’ve now sent your first set of data from your Arduino to the cloud!

Data will be sent using the ThingSpeak.writeField() function. ThingSpeak will only send data in intervals of 15 seconds. If you observe closely, there will be a 15 second time gap between two consecutive data entries. In the next section, we will send some useful data from a sensor, using the same framework as in the above code.

 

 

 

August 21, 2018

2 responses on "Sending Data to Thingspeak"

  1. tadalafil max dose: http://tadalafilonline20.com/ order tadalafil

  2. medications without a doctor’s prescription: https://genericwdp.com/ buy cheap prescription drugs on

Leave a Message

Your email address will not be published.

top

Knowledge and Content by Li2 Technologies | © 2021 NASSCOM Foundation | All rights reserved

X