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:
- Line 1 gives me access to the commands used on lines 18 and 21 to connect the esp8266 to a WiFi network.
- Line 2 gives me access to the commands necessary to communicate with the google script
- Lines 3 & 4 give me access to the commands to control the temperature/humidity sensor and to debug programs
- Line 6 defines the temperature/humidity sensor.
- Line 8 defines the variable used to store the name of the WiFi network. You will need to change the name to match your network.
- Lines 8-13 are used by the HTTPSRedirect library to connect to a Google service. You will need to change line 10 to match your Google script's ID.
- Line 16 opens a port to the serial monitor so that I can see what the program is doing using Serial.println() commands.
- Line 17 provides a bunch of debugging information that is useful when troubleshooting.
- Line 19 is needed to start the DHT temperature/humidity sensor.
- Lines 18 and 21-23 are used to connect the esp8266 to the WiFi network (attempts are made every 500 milliseconds until successful).
- Line 27 reads temperature data from the temperature/humidity sensor and store them in variable t.
- Lines 28 and 29 create and configure an object named client that is later used on line 33 to send the data.
- Line 31 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 32 prints the url string to the serial monitor (to help with debugging).
- Line 33 is the command that sends the data to the Google script.
- Line 34 prints the Google script's response (useful when debugging).
- Line 35 deletes the object created on line 33.
- Line 37 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:
You need to put your spreadsheet Id where it's blurred.
- 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).