我使用过温度、浊度和 pH 传感器。(使用 esp8266 和 arduino uno)
传感器的所有读数都显示在串行端口中。并非所有来自 Arduino 串口的数据都被 Thing speak 从传感器接收。这是代码
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <达拉斯温度.h>
#define ONE_WIRE_BUS 2
单线单线(ONE_WIRE_BUS);
达拉斯温度传感器(&oneWire);
#define SensorPin A1
unsigned long int avgValue;
浮动 b;
int buf[10],温度;
#定义 RX 3
#define TX 4
字符串 AP = "";
字符串 PASS = "";
字符串 API = "";
字符串主机 = "api.thingspeak.com";
字符串端口 = "80";
字符串字段1 =“字段1”;
字符串字段2 =“字段2”;
字符串字段3 =“字段3”;
int countTrueCommand;
整数计数时间命令;
布尔发现=假;
浮动 valSensor1;
浮动 valSensor2;
浮动 valSensor3;
SoftwareSerial esp8266(RX,TX);
无效设置()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
sensors.begin();
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
无效循环()
{
valSensor1 = getTemper();
valSensor2 = getTurbidity();
valSensor3 = getpH();
String getData1 = "GET /update?api_key="+ API +"&"+ field1 +"="+String(valSensor1);
String getData2 = "GET /update?api_key="+ API +"&"+ field2 +"="+String(valSensor2);
String getData3 = "GET /update?api_key="+ API +"&"+ field3 +"="+String(valSensor3);
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData1.length()+4),4,">");
esp8266.println(getData1);//delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData2.length()+4),4,">");
esp8266.println(getData2);//delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData3.length()+4),4,">");
esp8266.println(getData3);//delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}
浮动 getTemper()
{
sensors.requestTemperatures();
//print the temperature in Celsius
Serial.println();
Serial.print("Temperature: ");
int temperature=sensors.getTempCByIndex(0);
Serial.print(temperature);
//Serial.print((char)176);//shows degrees character
Serial.print("C | ");
//delay(500);
return temperature;
}
浮动获取浊度()
{
int sensorValue = analogRead(A0);// read the input on analog pin 0:
float voltt = sensorValue * (5.0 / 1024.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
Serial.print("Turbidity in volts: ");
Serial.print(voltt); // print out the value you read:
Serial.print(" | ");
//delay(500);
return voltt;
}
浮动 getpH()
{
for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
{
buf[i]=analogRead(SensorPin);
//delay(10);
}
for(int i=0;i<9;i++) //sort the analog from small to large
{
for(int j=i+1;j<10;j++)
{
if(buf[i]>buf[j])
{
temp=buf[i];
buf[i]=buf[j];
buf[j]=temp;
}
}
}
avgValue=0;
for(int i=2;i<8;i++) //take the average value of 6 center sample
avgValue+=buf[i];
float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
phValue=3.5*phValue; //convert the millivolt into pH value
Serial.print("pH:");
Serial.print(phValue,2);
Serial.println(" ");
digitalWrite(13, HIGH);
//delay(800);
digitalWrite(13, LOW);
return phValue;
}
void sendCommand(字符串命令,int maxTime,char readReplay[])
{
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}