- //开发板为DOIT ESP32 DEVKIT V1//
- #include "DS18b20forESP.h"
- #include "ADS1115custom.h"
- #define DIRPIN 25
- #define PULPIN 26
- int x,i=0, n = 0,j=0,k,nn,N;
- float voltage, ecValue,ecValue_sum,ecValue_aver,ecValue_t[500], temperature = 25.0;
- float _kvalue = 0.779, rawEC, valueTemp, value;
- float celsius, fahrenheit;
- float Temp_Buffer = 0, vv = 0;
- float val = 0, EV = 0, strain = 0, pH = 0;
- void setup()
- { Serial.begin(9600);
- DS18b20Init();
- ADS1115init();
- pinMode(PULPIN, OUTPUT); // steps:脉冲个数
- pinMode(DIRPIN, OUTPUT); // dir:为方向控制
- }
- void loop()
- { if(Serial.read()=='1'){//串口接收到1时运行以下函数
- n = 0;//记录循环次数,
- for (i = 0; i < 1000000000000000; i++)//当不发送0时一直循环
- {n++;
- if(Serial.read()=='0'){break;}//串口接收到0时跳出循环
-
- ////////采集温度率数据/////////////////
- celsius=DS18b20GetTemp();
-
- ////////采集电导率数据/////////////////
- ecValue_sum=0;
- nn=100;
- for (k = 0; k < nn; k++){ECdata();ecValue_t[k]=ecValue;delay(10);}//间隔10ms采集一个数据
- sort();//数据排序
- N=0;
- for (k = 20; k < 80; k++){ecValue_sum=ecValue_sum+ecValue_t[k];N++;}//去掉前后20个数据
- ecValue_aver=ecValue_sum/N;//取剩下数据平均值
-
- ////////控制步进电机添加溶液/////////////////
- motor();//添加溶液
-
- ////////////////输出四个参数///////////////
- Serial.print(celsius); Serial.print(' '); //串口输出温度
- Serial.print(celsius); Serial.print(' ');
- Serial.print(ecValue_aver,4); Serial.print(' '); //口输出电导率
- Serial.println(n*0.01389*1.1429); //串口输出体积
- }}
- }
- //////////////////数据排序////////////////////////
- void sort()
- {int ii,jj;
- float temp1;
- for(ii=0;ii<nn-1;ii++)
- {for(jj=0;jj<nn-1-ii;jj++)
- {if(ecValue_t[jj]>ecValue_t[jj+1])
- { temp1=ecValue_t[jj];
- ecValue_t[jj]=ecValue_t[jj+1];
- ecValue_t[jj+1]=temp1;
- }
- }
- }
- }
- //控制42步进电机/////
- void motor() {
- digitalWrite(DIRPIN, HIGH); // Set Dir high
- for (x = 0; x < 200; x++) // 300个脉冲数
- { digitalWrite(PULPIN, HIGH); // Output high
- delayMicroseconds(700); // Wait 1/2 a ms
- digitalWrite(PULPIN, LOW); // Output low
- delayMicroseconds(700); // Wait 1/2 a ms
- }
- }
- void ECdata()
- {
- ADS1115GetAdcValue();
- voltage=adc_value[1]*0.125;//电压毫伏
- rawEC = 1000 * voltage / 820.0 / 196.0;//原始电导率,电压与电导率的线性比列关系,有先前实验得到
- value = rawEC * _kvalue;//校正机械误差后的电导率,_kvalueLow和_kvalueHigh由实际的电导率比上测试得到的电导率
- temperature = celsius; //dht.getTemperature();//校正后的电导率
- ecValue = value / (1.0 + 0.0185 * (temperature - 25.0));//修正后的电导率,修正完以后都是25摄氏度下的数据,因此用除
- }
复制代码
- #include <Wire.h>
- #include <Adafruit_ADS1X15.h>
- Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
- int adc_value[4];
- void ADS1115init(void)
- {
- ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
- ads.begin();
- }
- void ADS1115GetAdcValue(void)
- {
- adc_value[1] = ads.readADC_SingleEnded(1);
- }
- void ADS1115GetAdcValueWithFiller() //带平均值滤波的获取
- {
- unsigned long sum1 = 0;
- unsigned char i = 0;
- for (i = 0; i < 10; i++);
- {
- sum1 += ads.readADC_SingleEnded(1);
- delayMicroseconds(10);
- }
- adc_value[1] = sum1/10;
- }
复制代码
- #include <OneWire.h>
- #include <DallasTemperature.h>
- // 定义DS18B20数据口连接ESP32的4号IO上
- #define ONE_WIRE_BUS 4
- // 初始连接在单总线上的单总线设备
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- void DS18b20Init()
- {
- sensors.begin();
- }
- float DS18b20GetTemp()
- {
- float temp;
- sensors.requestTemperatures(); // 发送命令获取温度
- temp = sensors.getTempCByIndex(0);
- return temp;
- }
复制代码
|