【arduino学习】:Arduino编译ESP8266的任务(温湿度测量、超声波距离测量、声音检测模块、api接口调试、读取解析Json数据)

                    


项目场景:

        最近花了一天玩了一下ESP8266,正好和同学写的网页前端&数据库进行了对接,亲测数据传输可靠准确,这里很感谢磊磊的前端页面和数据库支持,还有硬件

        具体情况:

        1.做了一个基于Arduino的ESP32开发板的集成项目。

        2.温湿度采集

        3.超声波距离采集

        4.声音检测模块

        5.WiFi链接&api接口对接前端页面数据实时更新


基于Arduino的ESP32开发板的集成项目

硬件设施(虽然没那么高级好看,确实很丐版,但是确实好用):

效果图:

 

视频展示:

基于Arduino的ESP8266项目


温湿度采集:

        温湿度采集用的是元器件  DHT11  学生档位基本就是这个了,很好用!

部分代码提供:

#include <DHT.h>

#define DHTPIN 14    //定义DHT11针脚
//定义类型,DHT11或者其它
#define DHTTYPE DHT11
//进行初始设置
DHT dht(DHTPIN, DHTTYPE);

//变量定义
double temp = 0.0;  //存放温度
int hum = 0;        //存放湿度

//温湿度测量
void DHT11_Task() {
  // 两次检测之间,要等几秒钟,这个传感器有点慢。
  delay(2000);
  // 读温度或湿度要用250毫秒
  hum = dht.readHumidity();      //读湿度
  temp = dht.readTemperature();  //读温度,默认为摄氏度
  Serial.print("Humidity: ");    //湿度
  Serial.println(hum);
  Serial.print("Temperature: ");  //温度
  Serial.print(temp);
  Serial.println(" ℃ ");
}


超声波距离采集:

        超声波距离采集使用的是  市面常有的 HC-SR04,注意这个部分我发现只有使用特殊的引脚会能测距,不知为何,并未深究这个问题。

        我使用的是     GPIO4(发射脚)GPIO5(接收脚)

部分代码提供:

#define trigPin 4    //发射脚
#define echoPin 5    //接收脚

int distance = 0;   //存放距离

//超声波测距
void Ultrasonic_Task(void) {
  int duration;

  // 发送10ms的超声波脉冲
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // 计算超声波的回波时间
  duration = pulseIn(echoPin, HIGH);

  // 将回波时间转换为距离
  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
}

声音检测模块:

 使用的就是市面常有的模块,这种有声音Out脚就会有高低电平的变化,采集引脚的变化即可!

部分代码提供:

#define sensorPin 0  //定义声音针脚

int value = 0;  //设置value为0


value = digitalRead(sensorPin); //将value设置为读取到的A0的数值
Serial.print("sound: ");//湿度
Serial.println(value); //显示value数值,并自动换行
delay(500);

WiFi链接&api接口调试:

这方面主要用上了三个库:

  1. #include <ESP8266WiFi.h>
  2. #include <ArduinoHttpClient.h>
  3. #include <ArduinoJson.h>

提供部分代码:

const char *ssid = "ESP32";          //这里写入网络的ssid
const char *password = "123456789";  //wifi密码

char serverAddress[] = "192.168.137.1";  // server address
int port = 8080;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);

int status = WL_IDLE_STATUS;


IPAddress ip;
String WG_ip;

  //连接wifi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi正在连接中......");
    delay(1000);
  }

  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

void getServerIP(){

  WiFiClient wifi;

  String serverIp = ".255";
  WG_ip = WiFi.gatewayIP().toString();
  Serial.println("网关IP:"+ WG_ip);
  int a = WG_ip.lastIndexOf(".");
  String WG_ip1 = WG_ip.substring(0,a);
  WG_ip1.concat(serverIp);
  Serial.println(WG_ip1);

  HttpClient client = HttpClient(wifi, WG_ip1, port);
  client.get("/sensor/getServerIP");

  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  client.stop();

  DynamicJsonDocument doc(response.length() * 2);  //解析的JSON数据大小
  // 重点3:反序列化数据
  deserializeJson(doc, response);
  // 重点4:获取解析后的数据信息
  String ip = doc["data"]["serverip"].as<String>();
  Serial.println("格式化后的:"+ip);
}


void Http_Send(String temp_name, double a, int c) {
  String contentType = "application/x-www-form-urlencoded";
  String postData = "sensorTag=aaa";
  String postData1 = "";
  int statusCode;
  String response;

  postData.replace("aaa", temp_name);
  sprintf(buff, "&sensorValue=%2.3lf", a);
  postData.concat(buff);
  client.post("/sensor/putSensorData", contentType, postData);
  Serial.println(postData);

  statusCode = client.responseStatusCode();
  response = client.responseBody();
  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  delay(c);
}

项目代码:

/*
auto:嵌入式up
Date:2023/3/7
*/
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>
#include <stdio.h>
#include <string.h>

#define trigPin 4    //发射脚
#define echoPin 5    //接收脚
#define DHTPIN 14    //定义DHT11针脚
#define sensorPin 0  //定义声音针脚

//定义类型,DHT11或者其它
#define DHTTYPE DHT11
//进行初始设置
DHT dht(DHTPIN, DHTTYPE);

int value = 0;  //设置value为0

const char *ssid = "ESP32";          //这里写入网络的ssid
const char *password = "123456789";  //wifi密码


char serverAddress[] = "192.168.137.1";  // server address
int port = 8080;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);

int status = WL_IDLE_STATUS;

//变量定义
double temp = 0.0;  //存放温度
int hum = 0;        //存放湿度
int distance = 0;   //存放距离
char buff[40];
char buff1[40];
IPAddress ip;
String WG_ip;



void setup() {
  Serial.begin(115200);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  dht.begin();  //DHT初始化

  pinMode(sensorPin, INPUT);  //声音模块

  //连接wifi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi正在连接中......");
    delay(1000);
  }

  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {
  getServerIP();
  delay(2000);

  // DHT11_Task();
  // Http_Send("temp",temp,500);
  // Http_Send("hum",hum,500);
  // Ultrasonic_Task();
  // Http_Send("csb",distance,500);

  // Ultrasonic_Task();

  // value = digitalRead(sensorPin); //将value设置为读取到的A0的数值
  // Serial.print("sound: ");//湿度
  // Serial.println(value); //显示value数值,并自动换行
  // delay(500);
}

void getServerIP(){

  WiFiClient wifi;

  String serverIp = ".255";
  WG_ip = WiFi.gatewayIP().toString();
  Serial.println("网关IP:"+ WG_ip);
  int a = WG_ip.lastIndexOf(".");
  String WG_ip1 = WG_ip.substring(0,a);
  WG_ip1.concat(serverIp);
  Serial.println(WG_ip1);

  HttpClient client = HttpClient(wifi, WG_ip1, port);
  client.get("/sensor/getServerIP");

  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  client.stop();

  DynamicJsonDocument doc(response.length() * 2);  //解析的JSON数据大小
  // 重点3:反序列化数据
  deserializeJson(doc, response);
  // 重点4:获取解析后的数据信息
  String ip = doc["data"]["serverip"].as<String>();
  Serial.println("格式化后的:"+ip);
}


void Http_Send(String temp_name, double a, int c) {
  String contentType = "application/x-www-form-urlencoded";
  String postData = "sensorTag=aaa";
  String postData1 = "";
  int statusCode;
  String response;

  postData.replace("aaa", temp_name);
  sprintf(buff, "&sensorValue=%2.3lf", a);
  postData.concat(buff);
  client.post("/sensor/putSensorData", contentType, postData);
  Serial.println(postData);

  statusCode = client.responseStatusCode();
  response = client.responseBody();
  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  delay(c);
}

//温湿度测量
void DHT11_Task() {
  // 两次检测之间,要等几秒钟,这个传感器有点慢。
  delay(2000);
  // 读温度或湿度要用250毫秒
  hum = dht.readHumidity();      //读湿度
  temp = dht.readTemperature();  //读温度,默认为摄氏度
  Serial.print("Humidity: ");    //湿度
  Serial.println(hum);
  Serial.print("Temperature: ");  //温度
  Serial.print(temp);
  Serial.println(" ℃ ");
}

//超声波测距
void Ultrasonic_Task(void) {
  int duration;

  // 发送10ms的超声波脉冲
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // 计算超声波的回波时间
  duration = pulseIn(echoPin, HIGH);

  // 将回波时间转换为距离
  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
}

联系我们:

        公众号:嵌入式up(码字不易,谢谢支持,如有能力打赏一元!)

        源码链接:基于Arduino的ESP8266项目


人生只有一次,它提醒我珍惜这易逝的时光。

猜你喜欢

转载自blog.csdn.net/oxygen23333/article/details/129434841