075582814553
Using ESP32 to Make an IoT Weather Station

FREE-SKY (HK) ELECTRONICS CO.,LIMITED / 06-11 08:44

ESP32 is a powerful microcontroller that is commonly used for IoT projects. In this article, we will show you how to build an IoT-based weather station using ESP32. This weather station will be able to measure temperature, humidity, and pressure, and then send the data to the cloud using Wi-Fi.


Introduction

ESP32 is a powerful microcontroller that is commonly used for IoT projects. In this article, we will show you how to build an IoT-based weather station using ESP32. This weather station will be able to measure temperature, humidity, and pressure, and then send the data to the cloud using Wi-Fi.

Materials Required

· ESP32 Development Board

· BME280 Sensor

· Breadboard

· Jumper Wires

· USB Cable

· Computer with Arduino IDE installed

Steps

Step 1: Set up the Arduino IDE

Firstly, you need to install the Arduino IDE on your computer. You can download it from the official website of Arduino. Once you have installed the Arduino IDE, you need to install the ESP32 board by following the instructions provided at this link: https://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/boards_manager.md.

Step 2: Connect the BME280 Sensor

Connect the BME280 Sensor to the ESP32 Development Board as shown in the following schematic:

Attachment1.

Step 3: Install necessary libraries

In order to communicate with the BME280 sensor, we need to install the necessary libraries. You can install them by opening the Arduino IDE, clicking on Sketch -> Include Library -> Manage Libraries, and then searching for the following libraries:

· Adafruit BME280 Library

· Adafruit Unified Sensor

Attachment2.

Step 4: Write the code

Next, open a new sketch in the Arduino IDE and copy and paste the following code:


#include <Wire.h>

#include <Adafruit_Sensor.h>

#include <Adafruit_BME280.h>

#include <WiFi.h>

#include <HTTPClient.h>

 

#define WIFI_SSID "your_wifi_ssid"

#define WIFI_PASSWORD "your_wifi_password"

 

Adafruit_BME280 bme;

 

void setup() {

  Serial.begin(115200);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);

    Serial.println("Connecting to WiFi...");

  }

  Serial.println("Connected to WiFi");

  bme.begin(0x76);

}

 

void loop() {

  float temperature = bme.readTemperature();

  float humidity = bme.readHumidity();

  float pressure = bme.readPressure() / 100.0F;

 

  Serial.print("Temperature: ");

  Serial.print(temperature);

  Serial.println(" *C");

 

  Serial.print("Humidity: ");

  Serial.print(humidity);

  Serial.println(" %");

 

  Serial.print("Pressure: ");

  Serial.print(pressure);

  Serial.println(" hPa");

 

  if (WiFi.status() == WL_CONNECTED) {

    HTTPClient http;

    String url = "http://yourserver.com/weather.php?temperature=" + String(temperature) + "&humidity=" + String(humidity) + "&pressure=" + String(pressure);

    Serial.print("Sending data to server: ");

    Serial.println(url);

    http.begin(url);

    int httpCode = http.GET();

    if (httpCode > 0) {

      String payload = http.getString();

      Serial.println(payload);

    }

    else {

      Serial.println("Error on HTTP request");

    }

    http.end();

  }

 

  delay(30000);

}


Make sure to replace "your_wifi_ssid" and "your_wifi_password" with your Wi-Fi network's SSID and password, respectively. Also, replace "http://yourserver.com/weather.php" with the URL of the server where you want to send the weather data.

 

This code uses the Adafruit BME280 library to read the temperature, humidity, and pressure data from the BME280 sensor, and then sends the data to the specified server using HTTP GET requests over Wi-Fi. The code also includes some debug statements to help you monitor the process in the serial monitor.

 

Weather.php PHP script that accepts the weather data sent by the ESP32-based IoT Weather Station:

 

Code:


<?php

  $temperature = $_GET['temperature'];

  $humidity = $_GET['humidity'];

  $pressure = $_GET['pressure'];

 

  // Insert the data into your database or do something else with it

  // For example, you could store the data in a MySQL database:

  $servername = "localhost";

  $username = "your_username";

  $password = "your_password";

  $dbname = "your_database";

 

  // Create a connection

  $conn = new mysqli($servername, $username, $password, $dbname);

 

  // Check connection

  if ($conn->connect_error) {

    die("Connection failed: " . $conn->connect_error);

  }

 

  // Prepare the SQL statement

  $sql = "INSERT INTO weather_data (temperature, humidity, pressure) VALUES ($temperature, $humidity, $pressure)";

 

  // Execute the SQL statement

  if ($conn->query($sql) === TRUE) {

    echo "Data inserted successfully";

  } else {

    echo "Error: ". $sql. "<br>" . $conn->error;

  }

 

  // Close the connection

  $conn->close();

?>

 Attachment3.

Make sure to replace "your_username", "your_password", "your_database" with your MySQL database's username, password, and database name, respectively. Also, replace "weather_data" with the name of the table where you want to store the weather data.

 

This PHP script retrieves the weather data from the GET parameters, inserts the data into a MySQL database, and returns a response indicating whether the data was inserted successfully or not. You can modify the script to perform any other action you want with the weather data, such as sending it to an MQTT broker or storing it in a different database.

 

If you wish to send data from your PHP MySQL database to AWS using an MQTT broker, you can utilize the AWS IoT service. We have provided a general outline of the necessary steps for doing so below. Please note that this guide is intended to provide an overview, and for more detailed implementation instructions, we recommend consulting other articles, as the scope of this guide is limited. Nonetheless, a general guide is provided below on using MQTT brokers and AWS for cloud connections.

1. Create an AWS account if you don't already have one.

2. Create an AWS IoT thing that will represent your IoT device.

3. Create an AWS IoT policy that will allow your IoT device to publish and subscribe to MQTT topics on the AWS IoT broker.

4. Download the AWS IoT Device SDK for PHP and follow the setup instructions to set up the SDK in your PHP environment.

5. Modify your PHP script to use the AWS IoT Device SDK to publish the weather data to an MQTT topic on the AWS IoT broker.

6. Create an AWS Lambda function that will be triggered by the MQTT topic and will store the weather data in an AWS database, such as Amazon DynamoDB.

As for the MQTT brokers available on AWS, here are some popular ones:

1. AWS IoT Core - This is the native MQTT broker provided by AWS and is a fully managed service. It allows you to securely connect your devices to the AWS cloud and provides features such as device shadowing and message routing.

2. Mosquito - This is an open source MQTT broker that can be deployed on AWS. It is lightweight and easy to use but does not provide the same level of integration with other AWS services as AWS IoT Core.

3. RabbitMQ - This is a message broker that supports multiple messaging protocols, including MQTT. It can be deployed on AWS and provides features such as message persistence and clustering.


To get started with AWS IoT and MQTT, you can refer to the AWS IoT Developer Guide, which provides detailed documentation and examples on how to use AWS IoT to connect your devices to the cloud and interact with other AWS services.


Processed in 0.120307 Second , 24 querys.