标题: ESP8266/WemosD1开发板联网教程 可联网酒精检测仪 Arduino程序 [打印本页]

作者: 2450059503    时间: 2022-4-1 01:11
标题: ESP8266/WemosD1开发板联网教程 可联网酒精检测仪 Arduino程序
ESP8266/WemosD1开发板联网教程 Arduino程序,相关物联网功能直接往标注的地方加内容就行了,拿来配置好环境即能用,保姆级注释

一 、实现功能:通过开发板检测mq3传感器数据,连接周围Wifi,向云服务器发送数据/请求数据。
二 、设备工具:
1、ESP8266/WemosD1开发板 (Arduino其他开发板连接esp8266模块也可以)
2、服务器接口

电路原理图如下:


三、代码:
  1. #include <Arduino.h>
  2. #include <ESP8266HTTPClient.h>
  3. #include <ESP8266WiFi.h>
  4. #include <ESP8266WiFiMulti.h>
  5. #define USE_SERIAL Serial

  6. //wifi实例化
  7. // 成功返回 接收数据
  8. ESP8266WiFiMulti WiFiMulti;
  9. //////////////////////// D1 连接wifi
  10. //wifi
  11. char ssid[] = "test";        //  路由器wifi名称(尽量不要用中文)
  12. char paswd[] = "123456789";       // 路由器wifi密码
  13. String myserver = "http://xxxxx:80/addtest";  //可以是网址可以是 ip地址,如果访问失败请注意接口网址最后是否需要加一个 /

  14. void setup() {
  15.   /*  wifi  初始化*/
  16.   USE_SERIAL.begin(115200);
  17.   USE_SERIAL.setDebugOutput(false);
  18.   USE_SERIAL.println();
  19.   USE_SERIAL.println();
  20.   USE_SERIAL.println();
  21.   for (uint8_t t = 4; t > 0; t--) {
  22.     USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
  23.     USE_SERIAL.flush();
  24.     delay(1000);
  25.   }
  26.   WiFi.mode(WIFI_STA);
  27.   WiFiMulti.addAP(ssid, paswd);
  28.   /*  mq3 传感器*/
  29.   pinMode(A0,INPUT);
  30. // 注意:arduinoUno等板子 使用 1 2 3(数字接口) A0 A1(模拟接口)
  31. // Esp8266/Wemos 使用 D1 D2 D3(数字接口) A0 (模拟接口只有A0)
  32. }
  33. void loop() {
  34. // 读取A0引脚数据
  35. int mq3Date = analogRead(A0);
  36. //此处可以写其他功能代码,比如显示屏、传感器检测、报警模块等,本例使用MQ3传感器作为数据来源
  37. // web
  38.   String urlDate = "?mq=" + String(mq3Date);
  39.   wifimodel(urlDate);
  40. }
  41. //封装Web功能
  42. void wifimodel(String urlDate){
  43.   // 等待wifi连接
  44.   if ((WiFiMulti.run() == WL_CONNECTED)) {
  45.     WiFiClient client;
  46.     HTTPClient http;
  47.     USE_SERIAL.print("[HTTP] begin...\n");
  48.     // 大量数据拼接示例:String urls = myserver + "?o2data1="  + String(o2data1) + "&o2data2="  + String(o2data2);
  49.    // Get方式请求接口,接口url拼接示例: www.baidu.com/my?o2data1=123&o2data2=2323
  50.     String urls = myserver+urlDate;
  51.     Serial.println("拼接url为:" + urls);
  52.     http.begin(client, urls); //HTTP
  53.     USE_SERIAL.print("[HTTP] GET...\n");
  54.     // start connection and send HTTP header
  55.     int httpCode = http.GET();
  56.     // httpCode will be negative on error
  57.     if (httpCode > 0) {
  58.       // HTTP header has been send and Server response header has been handled
  59.       USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
  60.       // file found at server
  61.       if (httpCode == HTTP_CODE_OK) {
  62.         //这里为接口访问成功后执行内容
  63.         String payload = http.getString();
  64.         Serial.println("返回数据:" + urls);
  65.         USE_SERIAL.println(payload);
  66.       }
  67.     } else {
  68.    
  69.       USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  70.     }
  71.     http.end();
  72.   }else{
  73. //这里可以写访问服务器失败后的逻辑 不需要则去掉
  74.       int ss = 1;
  75.     }
  76. }
复制代码






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1