add iot_esp32
This commit is contained in:
parent
08f089ea3a
commit
e623e599d5
78
iot_esp32/main.ino
Normal file
78
iot_esp32/main.ino
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <Adafruit_GFX.h>
|
||||||
|
#include <Adafruit_SSD1306.h>
|
||||||
|
#include <HTTPClient.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <DHT.h>
|
||||||
|
|
||||||
|
#define DHT22_PIN 23
|
||||||
|
#define SCREEN_WIDTH 128
|
||||||
|
#define SCREEN_HEIGHT 64
|
||||||
|
const char *ssid = "your_ssid";
|
||||||
|
const char *password = "your_password";
|
||||||
|
String serverAddress = "your_server_address";
|
||||||
|
|
||||||
|
DHT dht22(DHT22_PIN, DHT22);
|
||||||
|
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
Serial.begin(9600);
|
||||||
|
|
||||||
|
dht22.begin();
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
Serial.println("Connecting");
|
||||||
|
while (WiFi.status() != WL_CONNECTED)
|
||||||
|
{
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
Serial.println("");
|
||||||
|
Serial.print("Connected to WiFi network with IP Address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
float humi = dht22.readHumidity();
|
||||||
|
float tempC = dht22.readTemperature();
|
||||||
|
|
||||||
|
if (isnan(tempC) || isnan(tempF) || isnan(humi))
|
||||||
|
{
|
||||||
|
Serial.println("Failed to read from DHT22 sensor!");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print("Humidity: ");
|
||||||
|
Serial.print(humi);
|
||||||
|
Serial.print("% | Temperature: ");
|
||||||
|
Serial.print(tempC);
|
||||||
|
Serial.println("°C");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (WiFi.status() == WL_CONNECTED)
|
||||||
|
{
|
||||||
|
JsonDocument doc;
|
||||||
|
doc["temperature"] = tempC;
|
||||||
|
doc["humidity"] = humi;
|
||||||
|
char output[256];
|
||||||
|
serializeJson(doc, output);
|
||||||
|
WiFiClient client;
|
||||||
|
HTTPClient http;
|
||||||
|
|
||||||
|
http.begin(client, serverAddress);
|
||||||
|
http.addHeader("Content-Type", "application/json");
|
||||||
|
int httpResponseCode = http.POST(output);
|
||||||
|
Serial.print("HTTP Response code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
|
||||||
|
http.end();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.println("WiFi Disconnected");
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(2000);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user