最近研究养植物, 又赶上冬天,又加上阳台采光不好。遂购买水族加热棒(安全)+植物补光灯。 但是作为入门科技宅,当然不能盲操一切。esp32走起~~
设备 esp32板子1 光照传感器1 dht11 温湿度传感器1
esp32牛就牛在有wifi 通过http将数据发送到树莓派搭的服务器
代码到处乱抄的 晚点有空format下
#include <Wire.h> //IIC库
#include <math.h>
#include "SSD1306.h" //显示屏
#include "DHT.h" //温湿传感器
#include <WiFi.h>
#define DHTPIN 17 // 引脚定义
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
int BH1750address = 0x23;//芯片地址为16位23
byte buff[2];
//
#define SDA 21
#define SCL 22
SSD1306 display(0x3c, SDA, SCL);
int BH1750_Read(int address)
{
int i = 0;
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
while (Wire.available()) //
{
buff[i] = Wire.read(); // read one byte
i++;
}
Wire.endTransmission();
return i;
}
void BH1750_Init(int address)
{
Wire.beginTransmission(address);
Wire.write(0x10);//1lx reolution 120ms
Wire.endTransmission();
}
const char* ssid = "firework";
const char* password = "wifi密码";
const char* host = "192.168.1.50";
const char* streamId = "....................";
const char* privateKey = "....................";
void setup()
{
Wire.begin();
Serial.begin(9600);
display.init();
dht.begin();
// wifi
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//wifi
}
int value = 0;
void loop()
{
delay(2000); // 设置间隔时间为2000毫秒
int i;
uint16_t val = 0;
//
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("没有从DHT传感器上获取数据!");
return;
}
// 计算华氏温度 (默认)
// float hif = dht.computeHeatIndex(f, h);
// 计算摄氏温度 (Fahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
BH1750_Init(BH1750address);
delay(1000);
if (2 == BH1750_Read(BH1750address))
{
val = ((buff[0] << 8) | buff[1]) / 1.2;
Serial.print(val, DEC);
Serial.println("[lx]");
display.clear();
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, "T:" + (String)hic + "C");
display.drawString(0, 20, "H:" + (String)h + "%");
display.drawString(0, 40, (String)val + "lx");
display.display();
//http
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 8080;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = ( "192.168.1.50:8080/sensor/in");
Serial.print("Requesting URL: ");
Serial.println("GET " + url + " HTTP/1.0");
client.println("GET http://192.168.1.50:8080/sensor/in?inData="+(String)hic+","+(String)h+","+(String)val+" HTTP/1.0");
client.println("Host: 192.168.1.50:8080");
client.println("Connection: close");
client.println();
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
//http
}
delay(150);
}