Funpack第二季第三期任务一基于FireBeetle ESP32-E水质检测物联网系统
Funpack第二季第三期任务一使用FireBeetle ESP32-E开发板,和DS18S20温度传感器和Turbidity sensor浊度传感器模块,通过WIFI和阿里云的物联网平台实现了对水质的温度和浊度的实时监
标签
嵌入式系统
Arduino
Funpack活动
物联网
水质检测
wjhgbpqm
更新2023-01-04
杭州电子科技大学
480

Funpack第二季第三期任务一基于FireBeetle ESP32-E水质检测物联网系统

一 .FireBeetle ESP32-E硬件介绍

       FireBeetle是一个高性能且更mini的Arduino开源开发板系列,现在FireBeetle不仅完全兼容arduino的开发环境,并且拥有更丰富的软硬件资源,它将支持makecode、Mind+、pinpong、MicroPython的开发环境(即将完善),无论是图形化编程、C语言、python编程还是JS编程都可以随心所欲的操作硬件。

FireBeetle ESP32-E是一款基于ESP-WROOM-32E双核芯片的主控板,它专为IoT设计。

它的主要特性如下

  • DFRobot FireBeetle V2系列兼容,体积小巧仅25.4 × 60 mm
  • ESP32双核低功耗主控,WiFi+BT4.0二合一
  • GDI显示接口,单线连接显示屏
  • 板载充电电路以及PH2.0锂电池接口

详细参数如下

  • 工作电压:3.3V
  • 输入电压:3.3V~5.5V
  • 支持低功耗:10uA
  • 支持最大放电电流:600mA@3.3V LDO
  • 支持最大充电电流:500mA
  • 支持USB充电
  • 处理器:Tensilica LX6双核处理器(一核处理高速连接;一核独立应用开发)
  • 主频:240MHz
  • SRAM:520KB
  • Flash:4MB
  • Wi-Fi标准:FCC/CE/TELEC/KCC
  • Wi-Fi协议:802.11 b/g/n/d/e/i/k/r (802.11n,速度高达150 Mbps),A-MPDU和A-MSDU聚合,支持0.4us防护间隔
  • 频率范围:2.4~2.5 GHz
  • 蓝牙协议:符合蓝牙v4.2 BR/EDR和BLE标准
  • 蓝牙音频:CVSD和SBC音频低功耗:10uA
  • 工作电流:80mA(平均)
  • 频率范围:2.4~2.5GHz
  • 支持Arduino一键下载
  • 支持micropython
  • 片上时钟:40MHz晶振、32.768KHz晶振
  • 数字I/O x10(arduino默认)
  • 模拟输入 x5(arduino默认)
  • SPI x1(arduino默认)
  • I2C x1(arduino默认)
  • I2S x1(arduino默认)
  • RGB_LED:5/D8
  • 接口方式:FireBeetle V2系列兼容
  • 工作温度:-40℃~+85℃
  • 模块尺寸:25.4 × 60(mm)
  • 安装孔尺寸:M2安装孔 孔径2.0mm

外观指示图和引脚布局如图1所示。

Fp7xQefzeAOQV4aqWfcuA4eYrbJG

图.1

  • Type-C: USB接口:4.75v-5.5v
  • PH2.0锂电池接口:3.5-4.2v
  • 2/D9 LED灯:使用2/D9引脚控制的LED灯
  • 充电指示灯:指示充电方式的红色LED,通过三种方式指示充电状态:1、充满电或未充电时熄灭2、充电时常亮3、USB供电,未连接锂电池时高频闪烁
  • RST复位引脚:单击复位按钮,将程序复位
  • 5/D8 WS2812指示灯:使用5/D8引脚控制的WS2812RGB灯珠
  • 低功耗焊盘:此焊盘专为低功耗设计,默认为连接状态,使用小刀轻轻刮断中间的细线即可断开,断开后可降低500μA静态功耗,通过程序控制主控进入睡眠模式后可将功耗降低至13μA。注意:焊盘断开后仅USB方式供电可驱动RGB灯
  • GDI显示接口:DFRobot专用显示屏接口,详情后文GDI显示接口
  • ESP32模组:乐鑫公司推出的最新ESP32-E模组
  • 按钮:连接27/D4 的按钮

二.开发环境搭建

  1. FireBeetle ESP32-E的开发环境主要有Arduino IDEMicropython我这里使用的是arduino 的开发环境,开发环境的搭建参考了官方文档。首先是驱动的安装,将设备连接至PC,打开设备管理器为设备安装 FTDI驱动 
  2. Arduino官网安装arduino。
  3. 库文件的安装,打开 Arduino IDE,选择 文件->首选项->设置如图2所示

FjyFnUOjbBakzNVKCGLQsklvtif8

图.2

4.将http://download.dfrobot.top/FireBeetle/package_DFRobot_index.json复制到 附加开发板管理器

5.选择 工具->开发板:->开发板管理器...在新弹出的对话框中,输入并搜索 FireBeetle ESP32-E,点击安装到此Arduino的开发环境配置完毕。

三.任务说明和软件编写

我这里选择的是任务1详细内容如下

任务一:水质监控系统
用FireBeetle ESP32-E开发板作为控制单元,观察养殖水质参数(温度/浊度/tds/pH值/电位/溶解氧中两项指标以上)的变化,并能把数据上传到IoT网站上,可实时动态显示监测水质的变化情况。

所需器件:

  • FireBeetle ESP32-E一套
  • 温度传感器DS18S20
  • 浊度传感器SEN0189

根据题目要求,我所实现的是FireBeetle ESP32-E开发板作为控制单元把水质的温度和浊度进行采集,通过WIFI把数据传输到阿里云的IOT的平台上,进行实时监控。

软件部分

使用Arduino进行编写程序,程序主要包括温度传感器模块的初始化、WIFI模块和阿里云物联网平台初始化。

温度传感器模块的初始化以及温度获取程序如下

/*DS18S20引脚*/
const int DS18S20_Pin = 3;//扩展版0,对应板子D3
OneWire ds(DS18S20_Pin);  // on digital pin 2
//温度获取函数
float getTemp(){
      //returns the temperature from one DS18S20 in DEG Celsius

      byte data[12];
      byte addr[8];

      if ( !ds.search(addr)) {
          //no more sensors on chain, reset search
          ds.reset_search();
          return -1000;
      }

      if ( OneWire::crc8( addr, 7) != addr[7]) {
          Serial.println("CRC is not valid!");
          return -1000;
      }

      if ( addr[0] != 0x10 && addr[0] != 0x28) {
          Serial.print("Device is not recognized");
          return -1000;
      }

      ds.reset();
      ds.select(addr);
      ds.write(0x44,1); // start conversion, with parasite power on at the end

      byte present = ds.reset();
      ds.select(addr);
      ds.write(0xBE); // Read Scratchpad


      for (int i = 0; i < 9; i++) { // we need 9 bytes
        data[i] = ds.read();
      }

      ds.reset_search();

      byte MSB = data[1];
      byte LSB = data[0];

      float tempRead = ((MSB << 8) | LSB); //using two's compliment
      float TemperatureSum = tempRead / 16;

      return TemperatureSum;

    }

WIFI模块和阿里云物联网平台初始化程序如下

*配置WIFI名和密码*/
const char* WIFI_SSID = "TP-LINK_900F";
const char* WIFI_PASSWORD = "cxd12345";

/*配置设备证书信息*/
String ProductKey = "hrg0EoTy4t4";//DS18S20设备ProductKey
String ClientId = "12345"; /*自定义ID*/
String DS18S20_DeviceName = "firebeetle";//温度传感器
String DS18S20_DeviceSecret = "1fd0df31a0946620d6f1b13faa89e351";

/*配置域名和端口号*/
String ALIYUN_SERVER = "iot-as-mqtt.cn-shanghai.aliyuncs.com";
uint16_t PORT = 1883;

/*需要操作的产品标识符*/
String TempIdentifier = "temperature";
String NTUIdentifier="zd_val";

/*需要上报TOPIC*/

const char* pubTopic = "/sys/hrg0EoTy4t4/firebeetle/thing/event/property/post";  //******post

DFRobot_Aliyun myAliyun;
WiFiClient espClient;
PubSubClient client(espClient);


void connectWiFi() {
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected");
  Serial.print("IP Adderss: ");
  Serial.println(WiFi.localIP());
}

/*灯的callback
void callback(char* topic, byte* payload, unsigned int len) {
  Serial.print("Recevice [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < len; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  StaticJsonBuffer<300> jsonBuffer;
  //arduino json5
  JsonObject& root = jsonBuffer.parseObject((const char*)payload);
 // DeserializationError root = deserializeJson(jsonBuffer, payload);
//arduino json5
  if (!root.success()) {
    Serial.println("parseObject() failed");
    return;
  }  
  const uint16_t LightStatus = root["params"][Identifier];
  //if (root)
 // {
  // / //  return;
  //} 
//const uint16_t LightStatus = root["params"][Identifier];
  if (LightStatus == 1) {
    openLight();
  } else {
    closeLight();
  }
  String tempMseg = "{\"id\":" + ClientId + ",\"params\":{\"" + Identifier + "\":" + (String)LightStatus + "},\"method\":\"thing.event.property.post\"}";
  char sendMseg[tempMseg.length()];
  strcpy(sendMseg, tempMseg.c_str());
  client.publish(pubTopic, sendMseg);
}
*/
//温度的callback
void callback(char * topic, byte * payload, unsigned int len){
  Serial.print("Recevice [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < len; i++){
    Serial.print((char)payload[i]);
  }
  Serial.println();
}
void ConnectAliyun() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    /*根据自动计算的用户名和密码连接到Alinyun的设备,不需要更改*/
    if (client.connect(myAliyun.client_id, myAliyun.username, myAliyun.password)) {
      Serial.println("connected");
     // client.subscribe(subTopic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}
void setup() {
  Serial.begin(115200);
  //pinMode(BEDROOD_LIGHT, OUTPUT);

  /*连接WIFI*/
  connectWiFi();

  /*初始化Alinyun的配置,可自动计算用户名和密码*/
  myAliyun.init(ALIYUN_SERVER, ProductKey, ClientId, DS18S20_DeviceName, DS18S20_DeviceSecret);

  client.setServer(myAliyun.mqtt_server, PORT);

  /*设置回调函数,当收到订阅信息时会执行回调函数*/
  client.setCallback(callback);

  /*连接到Aliyun*/
  ConnectAliyun();


}

 

最后是主循环,循环读取水的温度和浊度数据上传到阿里云平台上详细程序如下

void loop() {
  if (!client.connected()) {
    ConnectAliyun();
  }
  int sensorValue = analogRead(A0);// read the input on analog pin 0:
  float voltage = sensorValue * (5.0 / 2200.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float temp=0;
  float zd_val=0;
  //浊度换算
  if(voltage<2.5)
  {
      zd_val=3000;    
  }
  else
  {
    
    temp=-1120.4*voltage*voltage+5742.3*voltage-4352.9;
    zd_val=temp<0?0:temp;
  }

  float temp_val=getTemp();
  Serial.println(zd_val); // print out the zd_val
  Serial.println(temp_val); // print out the temp_val
  client.publish(pubTopic,("{\"id\":"+ClientId+",\"params\":{\""+TempIdentifier+"\":"+temp_val+",\""+NTUIdentifier+"\":"+zd_val+"},\"method\":\"thing.event.property.post\"}").c_str());
  delay(500);
  client.loop();
}

四.结果与总结

4.1结果

板子连接上电脑后,打开Arduino软件,打开串口监视器串口波特率设置为115200,打开阿里云IOT平台,可以观察到温度和浊度数据实时的显示如图3所示。详细的演示过程在视频中。

FkhrXBKTfNK5cRYSmJOBmu10mE15

图.3

4.2总结

很高兴参加硬禾学堂举办的Funpack第二季第三期的活动,学习了FireBeetle ESP32-E使用,温度和浊度传感器的使用,以及阿里云平台的使用,同时更加熟悉了arduino的使用。

 

附件下载
FireBeetle.zip
团队介绍
评论
0 / 100
查看更多
目录
硬禾服务号
关注最新动态
0512-67862536
info@eetree.cn
江苏省苏州市苏州工业园区新平街388号腾飞创新园A2幢815室
苏州硬禾信息科技有限公司
Copyright © 2023 苏州硬禾信息科技有限公司 All Rights Reserved 苏ICP备19040198号