You are on page 1of 13

Aim: Write a program on

Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
.
Aim: Write a program on
Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.
Aim: Write a program on Arduino/Raspberry Pi to retrieve temperature and
humidity data from thingspeak cloud.
ThingSpeak Read: Read data stored in a ThingSpeak channel
 Description
You can retrieve data from the Internet of Things website by using ThingSpeak
to download the data to your target hardware. ThingSpeak is an application
platform that you can use for the Internet of Things. You can build an
application around data that is collected by sensors. ThingSpeak includes real-
time data collection, data processing, visualizations, apps, and plugins. You can
see the ThingSpeak server response to a data download request by selecting
the Print diagnostic messages option. This option has no effect when you use
External mode, Serial Transmit, or Serial Receive blocks that use port 0.
• Parameters
Channel ID
1. Get the Channel ID parameter from your channel on ThingSpeak.com.
2. After creating a ThingSpeak channel for your target hardware:
3. On the ThingSpeak.com website, select Channels > My Channels.
4. Under the channel that you created for this target hardware, click
Settings.
5. Click the Channel Settings tab and copy the Channel ID.
6. Open the ThingSpeak Read block in your model and paste the value into
the Channel ID parameter.
1.2. After creating a ThingSpeak channel for your target hardware:
2.3. On the ThingSpeak.com website, select Channels > My Channels.
3.4. Under the channel that you created for this target hardware,
4.click Settings.
5.5. Click the Channel Settings tab and copy the Channel ID.
6.6. Open the ThingSpeak Read block in your model and paste the value
into
7. the Channel ID parameter.
8. Channel Access
9. By default, the ThingSpeak channel access is private. To access the
channel,
10.you must have a read API key.
11.You can make the channel public to allow other users to use your feed
without a
12.Read API key.
Channel Access
By default, the ThingSpeak channel access is private. To access the channel,
you must have a read API key. You can make the channel public to allow other
users to use your feed without aRead API key.
Read API Key
1. On the ThingSpeak.com website, select Channels > My Channels.
2. Under the channel that you created for this target hardware, click Settings.
3. Click the API Keys tab.
4. Copy the key from Read API Key parameter.
5. Open the ThingSpeak Read block in your model and paste the copied API key
into the Read API key parameter.
Field Number
Specify a field that you want to read from the ThingSpeak channel.
Print diagnostic messages
Select this check box to get ThingSpeak server response data and send it to the
standard output device.
Program:
#include <WiFi.h>
#include "ThingSpeak.h"
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>

const char* ssid = "REPLACE_WITH_YOUR_SSID"; // your


network SSID (name)
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; //
your network password

WiFiClient client;

unsigned long myChannelNumber = X;


const char * myWriteAPIKey = "XXXXXXXXXXXXXXXX";

// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;

// Variable to hold temperature readings


float temperatureC;
//uncomment if you want to get temperature in Fahrenheit
//float temperatureF;

// Create a sensor object


Adafruit_BME280 bme; //BME280 connect to ESP32 I2C (GPIO 21
= SDA, GPIO 22 = SCL)

void initBME(){
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor,
check wiring!");
while (1);
}
}

void setup() {
Serial.begin(115200); //Initialize serial
initBME();

WiFi.mode(WIFI_STA);

ThingSpeak.begin(client); // Initialize ThingSpeak


}
void loop() {
if ((millis() - lastTime) > timerDelay) {

// Connect or reconnect to WiFi


if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nConnected.");
}

// Get a new temperature reading


temperatureC = bme.readTemperature();
Serial.print("Temperature (ºC): ");
Serial.println(temperatureC);

//uncomment if you want to get temperature in Fahrenheit


/*temperatureF = 1.8 * bme.readTemperature() + 32;
Serial.print("Temperature (ºC): ");
Serial.println(temperatureF);*/

// Write to ThingSpeak. There are up to 8 fields in a


channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write
to field 1.
int x = ThingSpeak.writeField(myChannelNumber, 1,
temperatureC, myWriteAPIKey);
//uncomment if you want to get temperature in Fahrenheit
//int x = ThingSpeak.writeField(myChannelNumber, 1,
temperatureF, myWriteAPIKey);

if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error
code " + String(x));
}
lastTime = millis();
}
}
Result

Sample time
Specify in seconds how often this block reads the ThingSpeak channel.
Outcomes:
Student will be able to understand how can retrieve data from the Internet of
Things website by using ThingSpeak to download the data to your target
hardware.
Applications:
Machine learning uses data and produces a program to perform a task.
Application is Cloud-based people counter by detecting faces with the
Computer Vision System. The raw people count is then sent to the ThingSpeak
IoT platform for data collection in the cloud and further data analysis.

Aim: Write a program on


Arduino/Raspberry Pi to
retrieve temperature and
humidity data from
thingspeak cloud.

You might also like