Передача данных с датчиков с ESP32 в NODE-RED (mqtt-broker:Mosquitto)
This commit is contained in:
@@ -1,12 +1,22 @@
|
|||||||
#include "esp_camera.h"
|
#include "esp_camera.h"
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include "esp_http_server.h"
|
#include "esp_http_server.h"
|
||||||
#include <ESP32Servo.h>
|
|
||||||
|
#include <PubSubClient.h>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <BH1750.h>
|
#include <BH1750.h>
|
||||||
#include <Adafruit_GFX.h>
|
|
||||||
#include <Adafruit_SSD1306.h>
|
BH1750 lightMeter;
|
||||||
#include <Adafruit_NeoPixel.h>
|
|
||||||
|
#define AHT20_ADDR 0x38
|
||||||
|
|
||||||
|
float temperature = 0;
|
||||||
|
float humidity = 0;
|
||||||
|
|
||||||
|
WiFiClient espClient;
|
||||||
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
|
const char* mqtt_server = "192.168.31.78";
|
||||||
|
|
||||||
// WIFI
|
// WIFI
|
||||||
const char* ssid = "Cathouse";
|
const char* ssid = "Cathouse";
|
||||||
@@ -48,11 +58,9 @@ static esp_err_t stream_handler(httpd_req_t *req)
|
|||||||
camera_fb_t *fb = NULL;
|
camera_fb_t *fb = NULL;
|
||||||
|
|
||||||
static const char* boundary = "--frame\r\n";
|
static const char* boundary = "--frame\r\n";
|
||||||
static const char* header =
|
static const char* header = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
|
||||||
"Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
|
|
||||||
|
|
||||||
httpd_resp_set_type(req,
|
httpd_resp_set_type(req, "multipart/x-mixed-replace; boundary=frame");
|
||||||
"multipart/x-mixed-replace; boundary=frame");
|
|
||||||
|
|
||||||
uint32_t start = millis();
|
uint32_t start = millis();
|
||||||
|
|
||||||
@@ -158,8 +166,8 @@ bool initCamera()
|
|||||||
config.xclk_freq_hz = 20000000;
|
config.xclk_freq_hz = 20000000;
|
||||||
config.pixel_format = PIXFORMAT_JPEG;
|
config.pixel_format = PIXFORMAT_JPEG;
|
||||||
|
|
||||||
config.frame_size = FRAMESIZE_QVGA;
|
config.frame_size = FRAMESIZE_QQVGA;
|
||||||
config.jpeg_quality = 16;
|
config.jpeg_quality = 20;
|
||||||
config.grab_mode = CAMERA_GRAB_LATEST;
|
config.grab_mode = CAMERA_GRAB_LATEST;
|
||||||
config.fb_count = 1;
|
config.fb_count = 1;
|
||||||
config.fb_location = CAMERA_FB_IN_DRAM;
|
config.fb_location = CAMERA_FB_IN_DRAM;
|
||||||
@@ -186,6 +194,45 @@ bool initCamera()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool readAHT20(float &temp, float &hum)
|
||||||
|
{
|
||||||
|
uint8_t cmd[3] = {0xAC, 0x33, 0x00};
|
||||||
|
|
||||||
|
Wire.beginTransmission(AHT20_ADDR);
|
||||||
|
Wire.write(cmd, 3);
|
||||||
|
|
||||||
|
if (Wire.endTransmission() != 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
delay(80);
|
||||||
|
|
||||||
|
Wire.requestFrom(AHT20_ADDR, 6);
|
||||||
|
|
||||||
|
if (Wire.available() < 6)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
uint8_t data[6];
|
||||||
|
|
||||||
|
for (int i = 0; i < 6; i++)
|
||||||
|
data[i] = Wire.read();
|
||||||
|
|
||||||
|
uint32_t h =
|
||||||
|
((data[1] << 12) |
|
||||||
|
(data[2] << 4) |
|
||||||
|
(data[3] >> 4));
|
||||||
|
|
||||||
|
uint32_t t =
|
||||||
|
((data[3] & 0x0F) << 16) |
|
||||||
|
(data[4] << 8) |
|
||||||
|
data[5];
|
||||||
|
|
||||||
|
hum = (h * 100.0) / 1048576.0;
|
||||||
|
temp = (t * 200.0 / 1048576.0) - 50;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@@ -210,6 +257,13 @@ void setup()
|
|||||||
Serial.println("\nWIFI OK");
|
Serial.println("\nWIFI OK");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
Wire.begin(1, 2);
|
||||||
|
Wire.setClock(100000);
|
||||||
|
|
||||||
|
lightMeter.begin();
|
||||||
|
|
||||||
|
client.setServer(mqtt_server, 1883);
|
||||||
|
|
||||||
startServer();
|
startServer();
|
||||||
|
|
||||||
Serial.println("STREAM READY:");
|
Serial.println("STREAM READY:");
|
||||||
@@ -217,7 +271,59 @@ void setup()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void connectMQTT() {
|
||||||
|
while (!client.connected()) {
|
||||||
|
String id = "ESP32CAM-" + String(WiFi.macAddress());
|
||||||
|
|
||||||
|
if (client.connect(id.c_str())) {
|
||||||
|
Serial.println("MQTT OK");
|
||||||
|
} else {
|
||||||
|
Serial.println("MQTT FAIL");
|
||||||
|
delay(2000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
if (!client.connected())
|
||||||
|
connectMQTT();
|
||||||
|
|
||||||
|
client.loop();
|
||||||
|
|
||||||
|
static unsigned long lastMQTT = 0;
|
||||||
|
|
||||||
|
if (millis() - lastMQTT > 5000)
|
||||||
|
{
|
||||||
|
lastMQTT = millis();
|
||||||
|
|
||||||
|
float lux = lightMeter.readLightLevel();
|
||||||
|
|
||||||
|
readAHT20(temperature, humidity);
|
||||||
|
|
||||||
|
char buf[32];
|
||||||
|
|
||||||
|
snprintf(buf, sizeof(buf), "%.2f", lux);
|
||||||
|
client.publish("esp32/sensors/light", buf);
|
||||||
|
|
||||||
|
snprintf(buf, sizeof(buf), "%.2f", temperature);
|
||||||
|
client.publish("esp32/sensors/temp", buf);
|
||||||
|
|
||||||
|
snprintf(buf, sizeof(buf), "%.2f", humidity);
|
||||||
|
client.publish("esp32/sensors/humidity", buf);
|
||||||
|
|
||||||
|
Serial.println("MQTT SEND");
|
||||||
|
|
||||||
|
Serial.print("Lux: ");
|
||||||
|
Serial.println(lux);
|
||||||
|
|
||||||
|
Serial.print("Temp: ");
|
||||||
|
Serial.println(temperature);
|
||||||
|
|
||||||
|
Serial.print("Hum: ");
|
||||||
|
Serial.println(humidity);
|
||||||
|
}
|
||||||
|
|
||||||
vTaskDelay(2);
|
vTaskDelay(2);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user