Forum

Using ChatGPT code ...
 
Notifications
Clear all

Using ChatGPT code to build a ESP8266 Motion and Temperature Sensor

3 Posts
2 Users
0 Likes
713 Views
CrackedConsole
(@crackedconsole)
Member Admin
Joined: 5 years ago
Posts: 368
Topic starter  

I used to have motion sensors in each room, but the wiring was a hassle as I wired them all down to the basement and used Arduino MEGA controllers to read their data.
That will all change today.

I've been using the ESP8266 for some time now in lighting and RGB projects, now I want to build new motion sensors to put in each room that utilize these.
I will also be including a DHT11 Temperature and Humidity sensor in each, very useful in areas such as the bathroom and will allow me to control the exhaust fans based on humidity.

This time, rather than writing the code myself, let's lean a bit on ChatGPT for the code.

 

Can you provide arduino code to use an ESP8266-01 to connect to wifi and then read its 2 available pins for data from the 2 following sensors HC-SR501 PIR motion Sensor DHT11 Humidity Temperature Sensor I want to be able to read these values by via http from the ESP8266-01

 

Sure! Here's some sample Arduino code that should do what you're looking for:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DHT.h>

// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// Create an ESP8266WebServer object on port 80
ESP8266WebServer server(80);

// Initialize the DHT sensor
#define DHTPIN 2          // DHT data pin is connected to GPIO2
#define DHTTYPE DHT11     // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// Define the PIR motion sensor input pin
#define PIR_PIN 0         // PIR motion sensor input pin is connected to GPIO0

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Connect to Wi-Fi network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Start the web server
  server.begin();
  Serial.println("Web server started");

  // Initialize the DHT sensor
  dht.begin();
}

void loop() {
  // Check for a new client connection
  server.handleClient();

  // Read the temperature and humidity from the DHT sensor
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  // Check if any reading failed and exit early
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor");
    return;
  }

  // Read the motion sensor input
  int motion = digitalRead(PIR_PIN);

  // Construct the response string
  String response = "Temperature: " + String(temperature) + " °C\n"
                    + "Humidity: " + String(humidity) + " %\n"
                    + "Motion: " + String(motion) + "\n";

  // Send the response to the client
  server.send(200, "text/plain", response);
}

This code should connect to your Wi-Fi network using the credentials you provide, and then read the temperature and humidity from a DHT11 sensor connected to GPIO2, as well as the motion sensor input connected to GPIO0. It then constructs a string containing the readings, and sends it back to the client when it receives an HTTP request. Note that the PIR motion sensor is initialized as a digital input using digitalRead() and its value is sent back as an integer.

You can access the data by opening a web browser and entering the IP address of the ESP8266-01 into the address bar. The data will be displayed in plain text.

 

Well, time to start working on a circuit, flash the code and see what we come up with!


   
Quote
CrackedConsole
(@crackedconsole)
Member Admin
Joined: 5 years ago
Posts: 368
Topic starter  

After some back and forth, I was able to get ChatGPT to provide the following

I have tested this code and it seems to work very well.

I am also working on a 3D printed case to hold the sensors and battery unit. Next posts will include a wiring diagram and 3D model info

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DHT.h>

// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "Password";

// Create an ESP8266WebServer object on port 80
ESP8266WebServer server(80);

// Initialize the DHT sensor
#define DHTPIN 2          // DHT data pin is connected to GPIO2
#define DHTTYPE DHT11     // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// Define the PIR motion sensor input pin
#define PIR_PIN 0         // PIR motion sensor input pin is connected to GPIO0

String response = "";

void setup() {
  // Initialize serial communication
  Serial.begin(115200);

  // Connect to Wi-Fi network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Start the web server
  server.begin();
  Serial.println("Web server started");

  // Initialize the DHT sensor
  dht.begin();
  server.on("/", handleRoot);
}

void loop() {
  // Check for a new client connection
  server.handleClient();

  // Read the temperature and humidity from the DHT sensor
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  // Check if any reading failed and exit early
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor");
    return;
  }

  // Convert temperature to Fahrenheit
  temperature = temperature * 1.8 + 32;

  // Read the motion sensor input
  int motion = digitalRead(PIR_PIN);

  // Construct the response string
  response = "Temperature: " + String(temperature, 2) + " F\n"
                    + "Humidity: " + String(humidity, 2) + " %\n"
                    + "Motion: " + String(motion) + "\n";

  delay(100);
}

void handleRoot() {

  // Send the response to the client
  server.send(200, "text/plain", response);
}

   
ReplyQuote
HYSTOU
(@hystou)
New Member
Joined: 8 hours ago
Posts: 1
 

It seems to be very useful. Getting chat to write the code is also something I use a lot haha! 🤣 


   
ReplyQuote
Share: