Esp32开发板的mqtt与webserver控制DHT11整合

两串代码写到一起
-#include "DHT.h"
#define DHTPIN 22
#define DHTTYPE DHT11
DHT dht (DHTPIN,DHTTYPE,4);
#include <ssl_client.h>
#include <WiFiClientSecure.h>
#include <ArduinoMqttClient.h>
#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
#include <WiFiNINA.h>
#elif defined(ARDUINO_SAMD_MKR1000)
#include <WiFi101.h>
#elif defined(ARDUINO_ESP8266_ESP12)
#include <ESP8266WiFi.h>
#endif
#include "arduino_secrets.h"
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "test.mosquitto.org";
int port = 1883;
const char topic[] = "SHD/SHN";
const long interval = 1000;
unsigned long previousMillis = 0;
int count = 0;
void setup() {
pinMode(DHTPIN,INPUT);
dht.begin();
Serial.println("DHT TESTING");
Serial.begin(9600);
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
Serial.print(".");
delay(5000);
}
Serial.println("You're connected to the network");
Serial.println();
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
}
void loop() {
delay(2000);
float count=dht.readTemperature();//读取摄氏度
Serial.print("Temperature");
Serial.print(count);
Serial.print("c");
mqttClient.poll();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
mqttClient.beginMessage(topic);
mqttClient.print("Temperature");
mqttClient.print(count);
mqttClient.print("c");
mqttClient.endMessage();
Serial.println();
count++;
}
第二个
//让开发板连接wifi,并通过网页来控制DHT11数据采集
#include <WiFi.h>
#include"DHT.h"
// 个人热点的名称和密码
const char
ssid = "liu";
const char
password = "Lht123456";
//设置服务端口号
WiFiServer server(80);
const int led = 15;
// 客户信息
char linebuf[80];
int charcount=0;

//DHT11的配置
#define DHTPIN 22
#define DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE,4);

void setup() {

pinMode(led, OUTPUT);
pinMode(DHTPIN, INPUT);
Serial.begin(115200);
//等待串口的配置
while(!Serial) { }
// 连接WiFi
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) //等待WiFi连接
{
delay(500); Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //打印开发板IP地址
//启动TCP server服务器
server.begin();
//初始化DHT
dht.begin();
}

void loop() {
// 判断是否有客户连接
WiFiClient client = server.available();
if (client)
{
Serial.println("New client");
memset(linebuf,0,sizeof(linebuf)); //内存中的内容全部设置为指定的值
charcount=0;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{char c = client.read();
Serial.write(c);
//read char by char HTTP request
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
if (c == '\n' && currentLineIsBlank)
{// 将响应体发送到客户端 若要使用中文则必须在其中添加<meta charset="utf-8">声明编码
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
// the connection will be closed after completion of the response
client.println();
client.println("");
client.println("<meta name="viewport" content="width=device-width, initial-scale=1"> ");
client.println("

DHT testing

");
client.println("<meta charset="utf-8">

刘鸿涛20181204490 曹阳20181200708

");
client.println("

DHT <a href="read_data_from_sensor"> <a href="refuse_data_from_sensor">

");
client.println(""); break;
}
if (c == '\n')
{
//检查发起的请求内容是否包含"GET /read_data_from_sensor"
if (strstr(linebuf,"GET /read_data_from_sensor") > 0)
{
float h=dht.readHumidity();
float f=dht.readTemperature();
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(f);
Serial.print(" *C ");
digitalWrite(led, HIGH);
}
//检查发起的请求内容是否包含"GET /refuse_data_from_sensor"
else if (strstr(linebuf,"GET /refuse_data_from_sensor") > 0)
{ Serial.print("State:");
Serial.println("no data from sensor");
digitalWrite(led, LOW);
}
// 将客户信息清零,准备下次客户请求
currentLineIsBlank = true;
memset(linebuf,0,sizeof(linebuf));
charcount=0; }
else if (c != '\r')
{// you've gotten a character on the current line
currentLineIsBlank = false;
}}}
// give the web browser time to receive the data
delayMicroseconds(100);
// close the connection:
client.stop();
Serial.println("client disconnected");

你好,我是有问必答小助手。为了技术专家团更好地为您解答问题,烦请您补充下(1)问题背景详情,(2)您想解决的具体问题,(3)问题相关代码图片或者报错信息。便于技术专家团更好地理解问题,并给出解决方案。

您可以点击问题下方的【编辑】,进行补充修改问题。

img