Sei sulla pagina 1di 29

UNIT 10 Wi-Fi Wireless Networking

When you have completed this unit, you will be able to explain the operation of WiFi
module and create a data logger channel in cloud to log the measured humidity and
temperature data.

Wi-Fi is the most widely used type of medium-range wireless communication
technology. Medium-range in this case means that typical Wi-Fi signals can carry
about 100 meters. Wi-Fi operates in frequencies of 2.4 GHz and 5 GHz.

Wi-Fi is used to create wireless networks. This can range from a Personal Area
Network (PAN) within a single home to a Wide Area Network that covers a large
region. The wireless network is created using wireless Access Points (APs),
sometimes referred also to as Wireless Access Points (WAPs). Access points
consist of small hardware devices with a built-in network adapter, antenna and radio

ThingSpeak is an open data platform for Internet of Things (IoT). ThingSpeak

channels store all the data that a ThingSpeak application collects. Once you collect
data in a channel, you can use ThingSpeak apps to analyze and visualize it.

In this unit, you’ll create a ThingSpeak channel called Temperature & Humidity Data
Logger that collects temperature and humidity data using the DHT11
Humidity/Temperature Sensor on MTS-100 Tutor for Arduino, and plots the
measured data on the charts of your own webpage for analysis.

1. Personal Computer
2. MTS-100 Tutor for Arduino
3. USB Cable
4. Jumper Wires
5. AC Power Cord

Exercise 10-1 Data Logger in Cloud


After completing this exercise, you should be able to:

1. State the operating principle of Wi-Fi module.
2. Create a Temperature & Humidity Data Logger channel in cloud.
3. Write sketches to measure and store ambient temperature and humidity in your
Data Logger channel using Wi-Fi.


The Internet of Things (IoT) is the network of physical objects -- devices, vehicles,
buildings and other items which are embedded with electronics, software, sensors,
and network connectivity, which enables these objects to collect and exchange data.
The IoT allows objects to be sensed and controlled remotely across existing network

Wi-Fi Module

In recent years, Wi-Fi is the popular wireless networking technology. It is an

important key to enter the Internet of Things (IoT). Wi-Fi technology is inexpensive
and easy to use with various sensors and devices such as temperature sensor,
humidity sensor, CCD, near field communication (NFC), and radio frequency
identification (RFID) for measurements. The measured data is sent through the Wi-Fi
module to the wireless network’s base station, such as Access Point (AP) and
Wireless Access Point (WAP), and then connected to the wired Internet, and finally
processed and stored in cloud. For this purpose, you can set up your own website in
cloud or create a free data logger webpage such as, as shown in
Fig. 10-1. Once you have created a website in cloud, you can send commands and
data from cloud website to the terminal microcontroller via the same path, in
opposite direction.

Fig. 10-1 Wi-Fi module in computer networking

In this exercise, an interface between Arduino boards (or other microcontrollers) and
wireless network’s base station (AP), the Wi-Fi module will be introduced. The core
of most popular Wi-Fi modules is the ESP8266 microcontroller, which is an Espressif
Systems’ 32-bit microcontroller. The ESP8266 is a System-on-Chip (SoC) controller
with powerful functions (including Wi-Fi function) and inexpensive.

Fig. 10-2 ESP8266 microcontroller

As shown in Fig. 10-2, ESP8266 is a 32-pin microcontroller, which features:
1. Integrated low power 32-bit microcontroller
2. Integrated 10-bit ADC
3. Integrated TCP/IP protocol stack
4. 802.11 b/g/n
5. Integrated TR switch, balun, LNA, power amplifier and matching network
6. Integrated PLL, regulators, and power management units
7. Supports antenna diversity
8. WiFi 2.4 GHz, support WPA/WPA2
9. Support STA/AP/STA+AP operation modes
10. Support Smart Link Function for both Android and iOS devices
11. SDIO 2.0, (H) SPI, UART, I2C, I2S, IR Remote Control, PWM, GPIO
12. STBC, 1x1 MIMO, 2x1 MIMO
13. A-MPDU & A-MSDU aggregation & 0.4s guard interval
14. Deep sleep power <10uA, Power down leakage current < 5uA
15. Wake up and transmit packets in < 2ms
16. Standby power consumption of < 1.0mW (DTIM3)
17. +20 dBm output power in 802.11b mode
18. Operating temperature range -40˚C ~ 125˚C
19. FCC, CE, TELEC, WiFi Alliance, and SRRC certified

ESP-12 Wi-Fi module

The ESP-12 Wi-Fi module uses the core of ESP8266, so most pins of the ESP-12
are multiplexed with other functions. Fig. 10-3 shows the pinout of the ESP-12 Wi-Fi

Fig. 10-3 ESP-12 Wi-Fi module

In Fig. 10-3, VCC is the power supply (+3.3V) and GND is ground pin. The remaining
pins are as follows:

1. RESET: Reset pin.

2. ADC: Analog input to the integrated 10-bit ADC.

3. CH_PD: Trigger pin used to activate the channel to operate in power down

4. GPIO0 to GPIO5 and GPIO12 to GPIO16: These eleven general purpose I/O
pins can be controlled by the program written in certain programming languages
in the development system. These I/O pins will not be manipulated in this
exercise. In addition, the GPIO0 pin must be low to update the firmware of
ESP-12 module.

Besides these GPIO pins, the ESP-12 Wi-Fi module can communicate with the
peripherals using three different serial interfaces:

1. UART (Universal Asynchronous Receiver/Transmitter): When using UART

interface, GPIO1 is used as TxD pin (transmit), and GPIO2 as RxD pin (receive).
If hardware handshaking is used, GPIO15 is the RTS (Request to Send) pin, and
GPIO13 is the CTS (Clear to Send) pin.

2. SPI (Serial Peripheral Interface): When using the SPI interface, GPIO15 is used
as the CS or SS (Slave Select) pin, GPIO14 as the SCK (Serial Clock) pin,
GPIO12 as the MISO (Master In Slave Out) pin, and GPIO13 used as the MOSI
(Master Out Slave In) pin.

3. I2C (Inter-Integrated Circuit): When using I2C bus, GPIO5 is used as the SCL
(serial clock line), and GPIO4 as the SDA (serial data line).

Fig. 10-4 illustrates a basic application of the ESP-12 Wi-Fi module. It includes a
reset circuit, a UART and an external microcontroller.

Fig. 10-4 ESP-12 Wi-Fi module application (using a UART)

When using SPI interface, the connections between Arduino Uno and ESP-12 Wi-Fi
module include:

1. GPIO15 pin (CS) connected to Arduino pin 10.

2. GPIO14 pin (SCK) connected to Arduino pin 13.
3. GPIO12 pin (MISO) connected to Arduino pin 12.
4. GPIO13 pin (MOSI) connected to Arduino pin 11.

Fig. 10-5 shows the interface circuit of ESP-12 Wi-Fi module on the MTS-100 Tutor
for Arduino.

Fig. 10-5 ESP-12 Wi-Fi module interface circuit

ESP-12e Wi-Fi module

The ESP-12e Wi-Fi module shown in Fig. 10-6 is an enhanced version of ESP-12
Wi-Fi module. Compared with the ESP-12 Wi-Fi module, 6 extra pins (CS0, MISO,
GPIO9, GPIO10, MOSI, SCK) are found on the ESP-12e Wi-Fi module. Pins TxD0
and RxD0 of ESP-12e Wi-Fi module are equivalent to the pins TxD and RxD of
ESP-12 Wi-Fi module, respectively.

Fig. 10-6 ESP-12e Wi-Fi module

ESP8266 AT commands

Essentially ESP8266 is a 32-bit microcontroller, which can work alone and has the
dedicated development system. However, it is now employed to connect other
devices/servers through wireless Wi-Fi to Arduino Uno via a UART port. The whole
system is controlled by Arduino board.

Since a UART is used to connect to Arduino board, the baud rate of Arduino board
and Wi-Fi module must be the same. Unfortunately the default baud rates of ESP
series Wi-Fi modules vary from firmware version to firmware version. Indeed, the
firmware version of a Wi-Fi module can not be identified from appearance, but you
can use the AT command “AT+GMR” to query the firmware version of the Wi-Fi
module. The ESP8266 AT commands are listed in Table 10-1. The AT command
“AT” is usually used to test the connection.

Table 10-1 ESP8266 AT commands
Purpose AT Command Response
Restart AT+RST OK [System Ready,
View version AT+GMR AT+GMR 0018000902 OK
Lists available AT+CWLAP AT+CWLAP
APs +CWLAP:(4,"RochefortSurLac",-38,"7
Connect to AP AT+CWJAP? Query AT+CWJAP?
AT+CWJAP="SSID","Password" +CWJAP:"RochefortSurLac" OK
Disconnect AT+CWQAP=? Query
address OK
Set AT+ CWSAP? Query
configuration AT+ CWSAP= ssid, pwd
of ESP8266 <ssid>,<pwd>,<chl>, <ecn> chl = channel , ecn = encryption
WiFi mode AT+CWMODE? Query
Establish TCP AT+CIPSTART=? Query
connection or (CIPMUX=0) AT+CIPSTART = id = 0-4, type = TCP/UDP, addr = IP
register UDP <type>,<addr>,<port> address, port= port
<id><type>,<addr>, <port>
Set multiple AT+ CIPMUX? Query
connections AT+ CIPMUX=0 Single
mode AT+ CIPMUX=1 Multiple
Get station’s AT+CWLIF
IP which is
connected to
Send data (CIPMUX=0)
Close AT+CIPCLOSE=<id> or

Purpose AT Command Response
Configure as AT+ CIPSERVER= mode 0 to close server mode; mode 1
server <mode>[,<port>] to open; port = port
Set timeout AT+CIPSTO? Query
when AT+CIPSTO=<time> <time>0~28800 in seconds
ESP8266 runs
as TCP server
Set baudrate AT+CIOBAUD? Query AT+CIOBAUD?
Supported: 9600, 19200, 38400, +CIOBAUD:9600 OK
74880, 115200, 230400,
460800, 921600
address OK
Upgrade AT+CIUPDATE 1. +CIPUPDATE:1 found server
firmware 2. +CIPUPDATE:2 connect server
through 3. +CIPUPDATE:3 got edition
network (from
4. +CIPUPDATE:4 start update
Receive +IPD (CIPMUX=0): + IPD, <len>:
network data (CIPMUX=1): + IPD, <id>, <len>:
Enable AT+CSYSWDTENABLE Watchdog, auto restart when program
watchdog errors occur: enable
Disable AT+CSYSWDTDISABLE Watchdog, auto restart when program
watchdog errors occur: disable

ESP-12 Applications
A Wi-Fi module transmits/receives data through a wireless network, therefore
network is indispensable. At present, it is not difficult to set up a wireless network or
a wireless base station. You can easily set up a wireless network using a wireless
AP (or wireless router), and connect to a wired network such as Internet using Wi-Fi.
A wireless AP is inexpensive and easy to build by referring to the documentation
shipped with the hardware. When you have set up a wireless network, there are two
important codes:

1. Service Set Identifier (SSID)

2. Password (PWD)

These two codes are the keys which allow you to connect to a wireless network so
that you must remember surely. Once you connect to a wireless network, you will
use the wireless network as a channel for sending data and control commands, as
shown in Fig. 10-7, when the ESP-12 Wi-Fi module receives data or control
commands, it will drive/control the external devices such as relays and 7-segment
display through GPIO pins.

Fig. 10-7 External devices controlled by ESP-12 Wi-Fi module

In the wireless network based on Arduino Uno board, the ESP-12 Wi-Fi module acts
as a transceiver for transmitting/receiving Wi-Fi signals via a UART or SPI interface.
When the control commands are sent to the Arduino Uno, it will process the
commands and control other peripheral devices, as shown in Fig. 10-8.

Fig. 10-8 ESP-12 Wi-Fi module used in a wireless network

The Wi-Fi module in Fig. 10-8 plays a role similar to a Bluetooth module for wireless
communications. For advanced applications, a control website is required. To set up
a control website, you need to create your own webpage and get webspace enough
to run. This is a troublesome task for a beginner; fortunately you can create your
own webpage using the existing website such as ThingSpeak is an
open data platform for Internet of Things (IoT). ThingSpeak channels store all the
data that a ThingSpeak application collects. Each channel includes eight fields that
can hold any type of data, plus three fields for location data and one for status data.
Once you collect data in a channel, you can use ThingSpeak apps to analyze and
visualize it.

In this exercise, you’ll create a ThingSpeak channel called Temperature & Humidity
Data Logger that collects temperature and humidity data using the DHT11
Humidity/Temperature Sensor on MTS-100 Tutor for Arduino, and plots the
measured data on the charts of your own webpage for analysis. The entire system is
shown in Fig. 10-1. In addition to the DHT11 Humidity/Temperature Sensor, ESP-12
Wi-Fi module and I2C LCD are also required for this application. Fig. 10-17 shows
the circuit connections.


Creating a ThingSpeak Account

ThingSpeak is an open data platform for the Internet of Things (IoT). If you have an
email account, you can create a ThingSpeak account, and then create a
Temperature & Humidity Data Logger channel.

1. Go to as shown in Fig. 10-9.

Fig. 10-9 ThingSpeak homepage

2. Click the Sign Up in the upper right corner of the homepage. The sign up
window opens as shown in Fig. 10-10.

Fig. 10-10 Sign up window

3. Enter your user ID (account name) in the User ID field, your email address in the
Email field, and your time zone in the Time Zone field, your password in the
Password field and in the Password Confirmation field again. Check the “By
signing up, you agree to the Terms of Use and Privacy Policy” box, and then
click button to create a ThingSpeak account. Note: Write down
your User ID and Password for use in your sketch late.

Creating a New ThingSpeak Channel

4. Click Channels and select My Channels from the dropdown menu, as shown in
Fig. 10-11.

Fig. 10-11 Create a new ThingSpeak channel

5. Click button to open the New Channel window as shown in Fig.


Fig. 10-12 New Channel window

(1) Name: Enter a unique name for the ThingSpeak channel. This field is
(2) Description: Enter a description of the ThingSpeak channel.
(3) Field1~Field8: Check the box to enable the field, and enter a field name.
Each ThingSpeak channel can have up to 8 fields.
(4) Metadata: Enter information about channel data, including JSON, XML, or
CSV data. This field is optional.
(5) Tags: Enter keywords that identify the channel. Separate tags with commas.
This field is optional.
(6) Latitude: Specify the position of the sensor or thing that collects data in
decimal degrees. This field is optional.
(7) Longitude: Specify the position of the sensor or thing that collects data in
decimal degrees. This field is optional.
(8) Elevation: Specify the position of the sensor or thing that collects data in
meters. This field is optional.

(9) Make Public?: If you want to make the channel publicly available, check this
box. This field is optional.
(10) URL: If you have a website that contains information about your ThingSpeak
channel, specify the URL. This field is optional.
(11) Video ID: If you have a YouTube or Vimeo video that displays your channel
information, specify the full path of the video URL. This field is optional.

6. Enter “Temperature & Humidity Data Logger” in the Name field. Enter
“Records temperature and humidity from MTS-100 Tutor for Arduino” in the
Description field. Enter “Temperature (degC)” in the Field1 field. Check Field2
box and enter “Humidity (%)” in the Field2 field. Finally click button
to create the channel. The channel you created should look like Fig. 10-13.

Fig. 10-13 Created channel window – Private View tab

Editing Data Logger Channel

You can edit the feeds and rearrange charts on the logger channel for better

7. Originally these two charts are arranged in tile vertically, this arrangement is
inconvenient for comparison and observation. You can rearrange the charts in
tile horizontally using drag & drop method, as shown in Fig. 10-14.

Fig. 10-14 Charts rearranged

8. You can modify the title, X-axis and Y-axis name of a chart. Field 1 Chart for
example, click the button in the upper right corner of Field 1 Chart window,
the Chart Options dialog appears as shown in Fig. 10-15. You can enter a new
chart name in the Title field, a new X-axis name in the X-Axis field, and a new
Y-axis name in the Y-Axis field, and then click button. The charts
will respond to these changes.

Fig. 10-15 Chart Options dialog

9. On the API Key tab, you’ll see the Write and Read API Keys of your ThingSpeak
channel, as shown in Fig. 10-16. Write and Read API Keys are auto-generated
when you create a new ThingSpeak channel. Write API Key enables you to write
data to a channel or Read API Keys allow other people to view your private channel
feeds and charts. Note: Write down the Write API Key for use in your sketch late.

Fig. 10-16 API Key tab

Circuit Connections

Perform the following steps and refer to the wiring diagram in Fig. 10-17 to complete
the connections for Temperature & Humidity Data Logger:

1. Connect the Arduino Uno board to your computer using the USB cable.

2. Connect a correct AC voltage from a wall outlet to your MTS-100 using the AC
power cord, and then turn on the power.

3. Connect the Arduino pin 2 (TxD) to the header pin P29A1-RxD (Wi-Fi).

4. Connect the Arduino pin 3 (RxD) to the header pin P29A1-TxD (Wi-Fi).

5. Connect the Arduino pin A0 to the P27 header (HUMIDITY/TEMPERATURE


6. Insert LCD 1602 in the P13C header (LCD 16x2).

7. Connect the Arduino pin A4 to the header pin P13B-1 (SDA, LCD 16x2).

8. Connect the Arduino pin A5 to the header pin P13B-2 (SCL, LCD 16x2).

Fig. 10-17 Wiring diagram of Temperature & Humidity Data Logger

Program Design

9. Open the Arduino IDE window. In the text editor, write your sketch as shown
below and save it as “ex10-1-1.ino”. Note: You can omit the program comments
to save time for the actual study of the sketch.

Notes: Modify the string between quotation marks in the program lines:
(1) Line 13: Use the SSID of your WiFi wireless network instead of happyUno.
(2) Line 14: Use the password of your WiFi wireless network instead of
(3) Line 18: Use the Write API Key of your ThingSpeak channel instead of

// ******** ESP8266 application – Temperature & Humidity Data Logger ********

// =========== ex10-1-1.ino =============
//============ ESP8266 ==============
#include <SoftwareSerial.h> // include the SoftwareSerial library
#define PC_bps 9600
#define ESP_bps 115200
// The default baud rate of ESP8266 ESP12 is 9,600 bps or 115,200 bps.
// Set the baud rate of EPS12 to 115,200 bps.
#define ESP_TxD 3 // connect digital pin 3 to TxD of ESP8266 ESP12
#define ESP_RxD 2 // connect digital pin 2 to RxD of ESP8266 ESP12

SoftwareSerial ESP(ESP_RxD, ESP_TxD); // create a SoftwareSerial object named ESP

#define ssid "happyUno" // SSID of your wifi wireless network
#define pass "ac45cc042" // password of your wifi wireless network

#define ThingSpeak ""

// ThingSpeak IP Address:
String GET = "GET /update?key=C8A99S2P3W2S9L5I";
// Write API Key of your ThingSpeak channel

//============ DHT11 =================

#define DHT_p A0 // connect analog pin A0 to DHT11 data pin
byte MSG; // declare a byte variable MSG
byte T_H[5]; // declare a byte array T_H[] for storing DHT11 data
String Temp,Humi; // declare strings Temp and Humi

//============ I2C LCD1602 =================

#include <Wire.h> // include the Wire library

#include <LiquidCrystal_I2C.h> // include the LiquidCrystal_I2C library

LiquidCrystal_I2C myI2C(0x27,16,2); // create a LiquidCrystal_I2C object named myI2C

void setup()
{ //=== Connect to PC ==================
Serial.begin(PC_bps); // open the serial port at 9600 bps

//=== Connect to ESP8266 ==================

ESP.begin(ESP_bps); // initialize the ESP8266
sendCommand("AT+CWMODE=1"); // set Station (STA) mode
sendCommand("AT"); // test AT startup

// serial monitor displays: SEND:AT

if(ESP.find("OK")) // if ESP8266 has received “OK”,
{ Serial.println("RECEIVED: OK");
Serial.println("Data ready to send.");
// serial monitor displays:
// Data ready to send.
connectWiFi(); // connect ESP8266 to WiFi
// serial monitor displays:
// SEND: AT+CWJAP="happyUno"," ac45cc042"
{ Serial.println("No Response...");
// if ESP8266 has not received “OK”, serial monitor displays “No Response...”

//=== DHT ==================


//=== LCD ==================

myI2C.begin(); // initialize the LCD
myI2C.backlight(); // turn on the backlight

delay(2000); //a delay of 2 seconds


void loop() {
readDHT11(); // read temperature and humidity data
switch (MSG)
{ case 0: // valid
// ======== processing temperature and humidity data ==========
Humi = String(T_H[0])+'.'+String(T_H[1]);
Temp = String(T_H[2])+'.'+String(T_H[3]);

// ============== Update data in cloud ================

updateCloud(Temp, Humi);
// update the temperature & humidity data of logger channel via ESP8266

// ============== Serial Monitor ===============

Serial.print("Humidity: ");
Serial.print("Temperature: ");
Serial.println(" *C");
// ============== I2C LCD1602 ===============
myI2C.home(); // cursor home
myI2C.print("Humi: "); // display string
myI2C.print(Humi); // display humidity
myI2C.print(" "); // display spaces
myI2C.setCursor(0,1); // set cursor to the first position of line 1, (0,1)
myI2C.print("Temp: "); // display string
myI2C.print(Temp); // display temperature
myI2C.print(" "); // display spaces
case 1: // error 1
// ============== Serial Monitor ===============
Serial.println(); // next line
Serial.println(); // next line
Serial.println(); // next line
Serial.print("Error1: No Acknowledgement. ");
myI2C.home(); // cursor home
myI2C.print("Humi: "); // display string
// ============== I2C LCD1602 ===============
myI2C.home(); // cursor home
myI2C.print("== Error 1 =="); // display string
case 2: // error 2

// ============== Serial Monitor ===============
Serial.println(); // next line
Serial.println(); // next line
Serial.println(); // next line
Serial.print("Error2: Acknowledgement Error. ");
Serial.println(); // next line
Serial.println(); // next line
Serial.println(); // next line
// ============== I2C LCD1602 ===============
myI2C.home(); // cursor home
myI2C.print("== Error 2 =="); // display string
case 3: // error 3
// ============== Serial Monitor ===============
Serial.println(); // next line
Serial.println(); // next line
Serial.println(); // next line
Serial.print("Error3: Checksum error. ");
Serial.println(); // next line
Serial.println(); // next line
Serial.println(); // next line
// ============== I2C LCD1602 ==============
myI2C.home(); // cursor home
myI2C.print("== Error 3 =="); // display string
delay(10000); // pause 10 seconds

// ====== readDHT11() ===========
void readDHT11()
{ MSG=0; // valid by default
// a 20ms (at least 18ms) LOW, followed by a 20us~40us HIGH -- Start signal to DHT11
// microcontroller sends out a 20ms LOW, followed by a 40us HIGH,
// then waits for (40us) an ACK from DHT11
digitalWrite(DHT_p,0);delay(20); // a 20ms LOW
// a 40us HIGH, let DHT11 to pull LOW (ACK)

// ======= valid ACK signal is a 80us LOW ===================

pinMode(DHT_p,INPUT); // configure analog pin A0 as input
byte DHT_in=digitalRead(DHT_p); // read DHT11 (ACK)

// if a HIGH is read (error 1)
if(DHT_in) { MSG=1;return; } // No ACK

// ====== if a LOW is read, it should be a HIGH after 80us ======

DHT_in=digitalRead(DHT_p); // read DHT11

// if a LOW is read (error 2)

if(!DHT_in) { MSG=2;return; }
// if not a HIGH, ACK invalid

// ======= after 80us, ACK valid ========


// ======= continue to receive data =========

for(byte i=0; i<5; i++) T_H[i]=readDHT_byte(); // read DHT11 data (5 bytes)
pinMode(DHT_p,OUTPUT); // respond to DHT11 ACK
digitalWrite(DHT_p,1); // terminate data transmission

// Checksum
byte dht_check_sum = T_H[0]+T_H[1]+T_H[2]+T_H[3];
if(T_H[4]!= dht_check_sum) MSG=3; // Checksum Error(3)

// ========== readDHT_byte() ==================

byte readDHT_byte()
{ byte result=0;
// code 0: a 50us LOW, followed by a 26us~28us HIGH
// code 1: a 50us LOW, followed by a 70us HIGH
for(byte i=0;i<8;i++)
{ while(!digitalRead(DHT_p));
if (digitalRead(DHT_p)) result |= (1<<(7-i));
while (digitalRead(DHT_p)); // wait for a LOW
return result;

// ==== sendCommand() ======

void sendCommand(String cmd) // send command to ESP8266
{ Serial.print("SEND: "); // serial monitor displays “SEND:”
Serial.println(cmd); // serial monitor displays command
ESP.println(cmd); // send command to ESP8266

// ==== connectWiFi() ======
boolean connectWiFi() // connect ESP8266 to WiFi
{ ESP.println("AT+CWMODE=1"); // set Station (STA) mode
String cmd="AT+CWJAP=\""; // AT+CWJAP="
cmd+=ssid; // AT+CWJAP="ssid
cmd+="\",\""; // AT+CWJAP="ssid","
cmd+=pass; // AT+CWJAP="ssid","pass
cmd+="\""; // AT+CWJAP="ssid","pass"
// AT+CWJAP="ssid","pass"
sendCommand(cmd); // connect to WiFi
if(ESP.find("OK")) // if ok,
{ Serial.println("RECEIVED: OK");
return true;
else // if not ok,
{ Serial.println("RECEIVED: Error");
return false;

// set single connection mode

cmd = "AT+CIPMUX=0";
if(ESP.find("Error")) // if error occurs,
{ Serial.print("RECEIVED: Error");
return false;

//============ updateCloud() ====================

void updateCloud(String T, String H)
{ // set ESP8266 as Client, connected to ThingSpeak website
String cmd = "AT+CIPSTART=\"TCP\",\""; // AT+CIPSTART="TCP","
cmd += ThingSpeak; // AT+CIPSTART="TCP","ThingSpeak
cmd += "\",80"; // AT+CIPSTART="TCP","ThingSpeak",80
// AT command: AT+CIPSTART="TCP","ThingSpeak",80
if( ESP.find( "Error" ) ) // if ESP8266 connection error occurs,
{ Serial.print( "RECEIVED: Error\nExit1" );
// display error message in the serial monitor window

cmd = GET + "&field1=" + T + "&field2=" + H +"\r\n";
// webpage command: GET+field1 data+T+field2 data+H+(CR+NL)
// GET&field1=T&field2=H
ESP.print( "AT+CIPSEND=" ); // send AT+CIPSEND command to ESP8266
// AT+CIPSEND=length
ESP.println( cmd.length() ); // send command to ESP8266
if(ESP.find( ">" ) ) // if ESP8266 finds the prompt >,
{ Serial.print(">"); // serial monitor displays the prompt >
Serial.print(cmd); // serial monitor displays commands
// > GET&field1=T&field2=H
ESP.print(cmd); // send command to ESP8266
// If ESP8266 does not find the prompt >, close ESP8266
else ESP.print("AT+CIPCLOSE");

if(ESP.find("OK")) Serial.println("RECEIVED: OK");

else Serial.println("RECEIVED: Error\nExit2");

Compiling and Uploading

10. Once the editing is completed, press + keys to compile your sketch.
If “Error compiling” appears in the console, the message area will show the error
message with line number. If “Done compiling” appears in the console, the
message area will display memory usage for code and variables as follows:

Sketch uses 10,900 bytes (33%) of program storage space. Maximum is

32,256 bytes.

Global variables use 1,025 bytes (50%) of dynamic memory, leaving 1,023
bytes for local variables. Maximum is 2,048 bytes.

11. Press the + keys to upload the code to the Arduino board.

12. Click the Serial Monitor button on the toolbar or press + +

keys to open the Serial Monitor window.

13. The operating sequence and status shown in the Serial Monitor window should
look like Fig. 10-18.

Fig. 10-18 Serial Monitor window

14. If the message “No Response…” appears at the start, press the RESET button
on Arduino Uno to reset the microcontroller.

15. The LCD screen should display the current humidity and temperature value like
below. Blow on the DHT11 Humidity/Temperature Sensor to warm it. After a
while, Humi and Temp values displayed on LCD should increase.

Humi: 38. 0
Temp: 21. 0 *C

16. On the Thingspeak Homepage, click Sign In. The Sign In window opens as
shown in Fig. 10-19. Enter your User ID and Password, and then click on the
Sign In button.

Fig. 10-19 Sign In window

17. Go to your Temperature & Humidity Data Logger Channel at

The first set of measured temperature and humidity values should show on Field
1 Chart and Field 2 Chart, respectively, as shown in Fig. 10-20.

Fig. 10-20 Temperature & Humidity Data Logger Channel

18. The data logger receives the measured temperature/humidity data every 10
seconds and plots in Field 1 Chart (temperature) and Field 2 Chart (humidity).
After 30 minutes, the temperature chart (Field 1 Chart) and the humidity chart
(Field 2 Chart) should look like Fig. 10-21.

Fig. 10-21 30-minute temperature and humidity charts

19. If you need to query chart data, simply hover the mouse pointer over the curve,
the value and date of that point should be displayed as shown in Fig. 10-22.

Fig. 10-22 Query data

20. To export chart data, click button. The Data Export window opens
as shown in Fig. 10-23.

Fig. 10-23 Data Export window

21. There are three lines in the Data Export window. The first line represents
Temperature & Humidity Data Logger Channel Feed, the second line represents
Field 1 Data (temperature data), and the third line represents Field 2 Data
(humidity data). Each data includes three data formats JSON, XML, and CSV.
Select a format by clicking on the data format, the chart data in the selected
format is thus exported.