Building your own DIY IoT temperature sensor with an ESP8266 is a great way to get started with IoT projects. With just a few simple components, you can create a sensor that measures temperature and humidity, and automatically sends the data to Google Sheets for logging and analysis.
Overview of the Project
The main components we will use are:
-
ESP8266 - This is a low-cost WiFi microcontroller that will be the brains of our sensor. The ESP8266 can connect to WiFi and make HTTP requests to send data.
-
DHT22 - A digital humidity and temperature sensor that provides accurate readings. It connects easily to the ESP8266.
-
Google Sheets - We will send the sensor data to a Google Sheet using the Sheets API for centralized logging and analysis.
The basic steps are:
-
Set up the circuit with the ESP8266 and DHT22 sensor.
-
Write code for the ESP8266 to take readings from the sensor.
-
Send HTTP requests from the ESP8266 to Google Sheets to log the data.
-
Create a Google Sheet and connect it to the ESP8266 using the Sheets API.
Circuit Setup and Wiring
The circuit will consist of the ESP8266, the DHT22 sensor, and some basic components like a resistor and capacitor.
Here is how to connect the components:
-
Connect VCC and GND from the ESP8266 to the breadboard power rails.
-
Connect the DHT22 data pin to GPIO 5 on the ESP8266.
-
Add a 10K resistor from the data pin to the 3.3V power rail.
-
Add a 0.1uF capacitor from the data pin to GND.
This forms the basic circuit. The resistor and capacitor help smooth the DHT22 data signal for reliable readings.
Installing Libraries
We need to install the following libraries in the Arduino IDE to communicate with the components:
-
DHT Sensor Library - To interface with the DHT22 sensor.
-
ESP8266WiFi Library - For WiFi functions like connecting to a network.
-
HTTP Client Library - For making HTTP requests to Google Sheets.
To install a library, simply go to Sketch > Include Library > Manage Libraries in the Arduino IDE and search for the library name above. Click install to add them.
Programming the ESP8266
With the circuit complete and libraries installed, we can now program the ESP8266 using the Arduino IDE.
Here are the key steps in the code:
- Include the required libraries:
```cpp
include ;
include ;
include ;
```
- Set your WiFi credentials:
cpp
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
- Create a DHT sensor object:
cpp
DHT dht(D5, DHT22);
- In the setup(), connect to WiFi:
```cpp
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
```
- In the loop(), read the sensor and make an HTTP request:
```cpp
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (WiFi.status()== WL_CONNECTED) { //Check WiFi connection status
HTTPClient http;
// Build the HTTP request data
String url = "sheets.example.com/api/data";
String data = "&humidity=" + humidity + "&temperature=" + temperature;
http.begin(url);
http.POST(data);
http.end();
}
```
This will continuously read the DHT22 and send readings to the specified URL which we will setup next.
Logging Sensor Data to Google Sheets
To log the sensor data to Google Sheets, we need to:
-
Create a Google Sheet - Add columns for "temperature" and "humidity".
-
Publish the sheet for API access.
-
Get the sheet ID from the URL of the published sheet.
-
Set up a Google Apps Script that receives the HTTP request and logs it to the sheet.
Here is sample Apps Script code to log the request to the sheet:
```javascript
function doPost(e) {
var data = JSON.parse(e.postData.contents);
var sheet = SpreadsheetApp.openById('SHEET_ID');
var lastRow = sheet.getLastRow();
sheet.getRange(lastRow+1, 1).setValue(new Date());
sheet.getRange(lastRow+1, 2).setValue(data.humidity);
sheet.getRange(lastRow+1, 3).setValue(data.temperature);
return ContentService.createTextOutput();
}
```
This will automatically add the data received to new rows in the sheet!
Now whenever the ESP8266 makes a request, the data will be logged. We can view live charts and graphs in the Google Sheet.
Conclusion
Building an IoT temperature logger with ESP8266 and Google Sheets is easy and inexpensive. With just a few components and some simple code, we can get sensor data logging to the cloud automatically.
This project can be expanded by adding multiple sensors, data visualization, and connecting to cloud platforms like ThingSpeak. The ESP8266 is a versatile chip that enables limitless IoT applications.
Let me know in the comments if you end up building your own ESP8266 temperature logger! I'm always happy to help with any issues.