0% found this document useful (0 votes)
3 views18 pages

Arduino Weather Station with ThingSpeak

The document outlines a lecture on creating a weather station using ThingSpeak and Arduino, detailing the necessary hardware and software setup. It provides step-by-step instructions for connecting components, setting up ThingSpeak accounts, and coding for data collection and transmission. The final goal is to successfully upload temperature and humidity data to ThingSpeak, completing an IoT project.

Uploaded by

arzan12hasan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Arduino Weather Station with ThingSpeak

The document outlines a lecture on creating a weather station using ThingSpeak and Arduino, detailing the necessary hardware and software setup. It provides step-by-step instructions for connecting components, setting up ThingSpeak accounts, and coding for data collection and transmission. The final goal is to successfully upload temperature and humidity data to ThingSpeak, completing an IoT project.

Uploaded by

arzan12hasan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CSE423: Embedded System

Summer-2020
ThingSpeak Arduino Weather Station
Todays Lecture
 Get familiar with ThningSpeak
 Creating account in ThingSpeak
 API Keys
 Integrating Arduino + ESP-01
 Collecting Data

CSE423@DIU, Summer 2020


What is ThingSpeak
 ThingSpeak is a Web Service that lets you collect
and store sensor data in the cloud and develop
Internet of Things (IOT) applications.

 It works with Arduino, Raspberry Pi and MATLAB


(premade libraries and APIs exists).

 Link: [Link]

CSE423@DIU, Summer 2020


Hardware Requirements
 Arduino UNO
 ESP – 01
 DHT-11 sensor (Temperature and Humidity Sensor)
 Breadboard
 External Power Supply
 Connecting wires

CSE423@DIU, Summer 2020


Connection Diagram

CSE423@DIU, Summer 2020


CONNECTIONS
ESP -01 DHT - 11

1. VCC - 3.3V 1. VCC - 5V

2. GND - GND 2. OUT - A0

3. CH_PD - 3.3V 3. GND - GND

4. RESET - 3.3V

5. TX - 2 ( Arduino)

6. RX - 3 (Arduino)

CSE423@DIU, Summer 2020


SETTING UP THINGSPEAK
Go to the thingspeak home page- [Link]

CSE423@DIU, Summer 2020


SETTING UP THINGSPEAK
Sign up and create a New Channel with two fields Temperature and
Humidity. On creating a new channel, you'll get something called the
API Keys. We will use “Write API Keys” for the purpose.

CSE423@DIU, Summer 2020


SETTING UP THE ARDUINO IDE
1. The libraries you need to install on your own are the Adafruit Unified Sensor Library
and the DHT sensor library. Download links DHT Sensor by adafruit-
[Link] Unified Sensor Library-
[Link]
2. Download both and add them using the [Link] Library feature in the IDE

CSE423@DIU, Summer 2020


Code-1
String myAPIkey = "Your Write API key here"; // API from ThingSpeak

#include <SoftwareSerial.h>
#include <DHT.h>;
SoftwareSerial ESP8266(2, 3); // Rx, Tx

#define DHTTYPE DHT11


#define DHTPIN A0

DHT dht(DHTPIN, DHTTYPE,11);


float humidity, temp_f;
long writingTimer = 17;
long startTime = 0;
long waitTime = 0;

boolean relay1_st = false;


boolean relay2_st = false;
unsigned char check_connection=0;
unsigned char times_check=0;
boolean error;

CSE423@DIU, Summer 2020


Code-2
void setup()
{
[Link](9600);
[Link](9600);
[Link]();
startTime = millis();
[Link]("AT+RST");
delay(2000);
[Link]("Connecting to Wifi");
while(check_connection==0)
{
[Link](".");
[Link]("AT+CWJAP=\"Wifi Network's name\",\"PassWord\"\r\n");
[Link](5000);
if([Link]("WIFI CONNECTED\r\n")==1)
{
[Link]("WIFI CONNECTED");
break;
}

CSE423@DIU, Summer 2020


Code-3
times_check++;
if(times_check>3)
{
times_check=0;
[Link]("Trying to Reconnect..");
}
}
}
void loop()
{
waitTime = millis()-startTime;
if (waitTime > (writingTimer*1000))
{
readSensors();
writeThingSpeak();
startTime = millis();
}
}

CSE423@DIU, Summer 2020


Code-4
void readSensors(void)
{
temp_f = [Link]();
humidity = [Link]();
}

void writeThingSpeak(void)
{
startThingSpeakCmd();
// preparacao da string GET
String getStr = "GET /update?api_key=";
getStr += myAPIkey;
getStr +="&field1=";
getStr += String(temp_f);
getStr +="&field2=";
getStr += String(humidity);
getStr += "\r\n\r\n";
GetThingspeakcmd(getStr);
}

CSE423@DIU, Summer 2020


Code-5
void startThingSpeakCmd(void)
{
[Link]();
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "[Link]"; // [Link] IP address
cmd += "\",80";
[Link](cmd);
[Link]("Start Commands: ");
[Link](cmd);

if([Link]("Error"))
{
[Link]("AT+CIPSTART error");
return;
}
}

CSE423@DIU, Summer 2020


Code-6
String GetThingspeakcmd(String getStr) [Link]("MessageBody received: ");
{ [Link](messageBody);
String cmd = "AT+CIPSEND="; return messageBody;
cmd += String([Link]()); }
[Link](cmd); else
[Link](cmd); {
if([Link](">")) [Link]("AT+CIPCLOSE");
{ [Link]("AT+CIPCLOSE");
[Link](getStr); }
[Link](getStr); }
delay(500);
String messageBody = "";
while ([Link]())
{
String line = [Link]('\n');
if ([Link]() == 1)
{
messageBody= [Link]('\n');
}
}

CSE423@DIU, Summer 2020


Before you upload the code

There are a few things to do in the code before


you upload it.
 Paste your Write API key from Thingspeak
here:

 Enter your Wi-Fi SSID and Password here:

CSE423@DIU, Summer 2020


Final step

Upload the code. If all was done correctly,


your Thingspeak Channel should look like
this.

CSE423@DIU, Summer 2020


Congratulations,
you just
completed an
IOT based task.
CSE423@DIU, Summer 2020

You might also like