Lesson 1: Overview

Before you begin work on connecting your own thing to the Internet, let's start with the temperature/humidity sensor in my crawl space. This will give you a good overview of how the whole setup works. Don't worry about understanding every line of code right away. In later lessons we will develop something similar step by step.

Here is the Arduino program I loaded on the esp8266:

 1 | #include <ESP8266WiFi.h>
 2 | #include "DHT.h"
 3 | #include "HTTPSRedirect.h"
 4 | #include "DebugMacros.h"
 5 | 
 6 | const char* ssid = "Backus";
 7 | const char* password = "TopSecret";
 8 | 
 9 | #define DHTPIN 5
10 | DHT dht(DHTPIN, DHT22, 15);
11 | 
12 | const char* host = "script.google.com";
13 | const char* GScriptId = "MyGoogleScritID";
14 | const int httpsPort = 443;
15 | 
16 | HTTPSRedirect* client = nullptr;
17 | 
18 | void setup() {
19 |   Serial.begin(115200);
20 |   Serial.setDebugOutput(true);
21 |   dht.begin();
22 | 
23 |   WiFi.begin(ssid, password);
24 |   while (WiFi.status() != WL_CONNECTED) {
25 |     delay(500);
26 |   }
27 | }
28 | 
29 | void loop() {
30 |   float h = dht.readHumidity();
31 |   float t = dht.readTemperature();
32 | 
33 |   client = new HTTPSRedirect(httpsPort);
34 |   client->connect(host, httpsPort);
35 | 
36 |   String url = String("/macros/s/") + GScriptId + "/exec?t=" + t + "&h=" + h;
37 |   Serial.println(url);
38 |   client->GET(url, host);
39 |   Serial.println(client->getResponseBody());
40 |   delete client;
41 | 
42 |   Serial.println("Waiting...");
43 |   delay(300000);
44 | }

Code Breakdown
  • Line 1 gives me access to the commands used on lines 23 and 24 to connect the esp8266 to a WiFi network.
  • Line 2 gives me access to the commands use on lines 21, 30, and 31 to read temperature and humidity data from my DHT sensor.
  • Lines 3 & 4 give me access to the commands needed to communicate with a Google script.
  • Lines 6 and 7 define the variables used to store the name and password of the WiFi network. You will need to change these to match your network.
  • Line 9 defines which pin they DHT's signal wire is connected to.
  • Line 10 defines the type of DHT sensor I am using.
  • Lines 12-16 are used by the HTTPSRedirect library to connect to a Google service. You will need to change line 13 to match your Google script's ID.
  • Line 19 opens a port to the serial monitor so that I can see what the program is doing using Serial.println() commands.
  • Line 20 provides a bunch of debugging information that is useful when troubleshooting.
  • Line 21 is needed to start the DHT temperature/humidity sensor.
  • Lines 23-26 connect the esp8266 to the WiFi network (attempts are made every 500 milliseconds until successful).
  • Lines 31 and 32 read humidity and temperature data from the DHT sensor and store them in variables h and t respectively.
  • Lines 33 and 34 create and configure an object named client that is later used on line 38 to send the data.
  • Line 36 creates a string variable named url that includes the humidity and temperature data at the end in the form of a get request. For more information about how to form a get request, go to...
  • Line 37 prints the url string to the serial monitor (to help with debugging).
  • Line 38 is the command that sends the data to the Google script.
  • Line 39 prints the Google script's response (useful when debugging).
  • Line 40 deletes the object created on line 33.
  • Line 43 causes the esp8266 to wait before reading the sensor and sending data again. For testing purposes, I recommend 3000 milliseconds (anything faster than 1500 milliseconds has been reported to cause errors). Once you have tested your code, I suggest increasing the delay. I use 300000 milliseconds (5 minutes).

Here is the code for the app that is bound to my Google spreadsheet:
 1 | var ss = SpreadsheetApp.openById('1pHPL31BCjkA4erqY5wT6QIpiXxIIC4TCnJbMyM-42As');
 2 |
 3 | function doGet(e) {
 4 |  ss.getSheetByName('Log').insertRowAfter(1);
 5 |  ss.getSheetByName('Log').getRange(2, 1, 1, 3).setValues([[new Date(), e.parameter.t,e.parameter.h]]);
 6 |  return ContentService.createTextOutput('Data logged\n');
 7 | }

Code Breakdown
  • Line 1 creates an object named ss that allows me to manipulate my spreadsheet. You will need to change this to your spreadsheet's ID.
  • Lines 3-7 is a function that is called whenever the script's url is requested.
  • Line 4 inserts a blank row on the 2nd line of my only sheet which is named Log.
  • Line 5 pastes a date/time stamp into the 2nd row's first column and temperature and humidity data into columns 2 and 3.
  • Line 6 sends a response to whoever or whatever requested the script's url (useful for debugging).

Comments