|
为啥红色部分是二进制数据!
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <DS1302.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <dht11.h>
#include <SoftwareSerial.h>
dht11 DHT11;
SoftwareSerial esp8266(5, 6); //(rx,tx),声明WIFI
#define DHT11PIN 4
#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10
Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
DS1302 rtc(9, 8, 7); // RST, DAT, CLK
#define ONE_WIRE_BUS A0 //数据总线
OneWire oneWire(ONE_WIRE_BUS); //声明
DallasTemperature sensors(&oneWire); //声明
int x5 = A5;
String outa=""; //读取温度值存入变量
String outb=""; //读取时间值存入变量
String outc=""; //读取气压值存入变量
String outd=""; //读取海拔值存入变量
String oute=""; //占用
String outf="空"; //空
String outh=""; //湿度
//WIFI变量
String AP = "padavan-2x";
String PASS = "12345678";
String Data;
int countTrueCommand;
int countTimeCommand;
boolean found = false;
//WIFI变量
boolean t1_state=1;
boolean t2_state=1;
boolean t3_state=1;
boolean t4_state=1;
boolean t5_state=1;
unsigned long changeTime;
unsigned long changeTime2;
unsigned long changeTime3;
unsigned long changeTime4;
unsigned long changeTime5;
unsigned long changeTime6;
void setup() {
changeTime = millis();
pinMode(x5, OUTPUT);
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT", 5, "OK");
sendCommand("AT+CWMODE=1", 5, "OK");
sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
sensors.begin(); //初始化总线
sensors.setWaitForConversion(false); //设置为非阻塞模式
}
void loop()
{
led1Update();
led2Update();
led3Update();
led4Update();
led5Update();
led6Update();
}
void led1Update()//水泵
{
if (millis() - changeTime > 3600000) {
changeTime = millis();
// 下面添加定时 3600 s 要执行的内容
if(t1_state==1)
{
t1_state=0;
digitalWrite(x5, HIGH); // 点亮
}
else
{
t1_state=1;
digitalWrite(x5, LOW); // 关闭
}
}
}
void led2Update()//温度
{
if (millis() - changeTime2 > 5000) {
changeTime2 = millis();
// 下面添加定时 5 s 要执行的内容
float tempC = sensors.getTempCByIndex(0); //获取索引号0的传感器摄氏温度数据
if (tempC != DEVICE_DISCONNECTED_C) //如果获取到的温度正常
{
Serial.print("\n当前温度是: ");
Serial.print(tempC);
Serial.println(" ℃");
}
//Serial.println("发起温度转换");
sensors.requestTemperatures(); //发起新的温度转换
outa=String(tempC)+"℃";
}
delay(1000);
}
void led3Update()//大气压
{
if (millis() - changeTime3 > 5000) {
changeTime3 = millis();
// 下面添加定时 5 s 要执行的内容
if (!bmp.begin())
{
Serial.println(F("找不到对应的传感器"));
while (1);
}
Serial.print(F("温度 = "));
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print(F("气压 = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print(F("海拔 = "));
Serial.print(bmp.readAltitude(1013.25));
Serial.println(" m");
oute=String(Serial.print(F("")));
outc=String(bmp.readPressure())+"Pa";
outd=String(bmp.readAltitude(1013.25))+"m";
delay(1000);
}
}
void led4Update()//湿度
{
if (millis() - changeTime4 > 5000) {
changeTime4 = millis();
// 下面添加定时 5 s 要执行的内容
int chk = DHT11.read(DHT11PIN);
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
// Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
// Serial.println("Time out error");
break;
default:
// Serial.println("Unknown error");
break;
}
// 获取测量数据
outh=String(((float)DHT11.humidity, 2));
delay(1000);
}
}
void led5Update()//时间
{
if (millis() - changeTime5 > 5000) {
changeTime5 = millis();
// 下面添加定时 5 s 要执行的内容
Serial.println(rtc.getTimeStr());
outb=String(rtc.getDateStr(FORMAT_LONG, FORMAT_LITTLEENDIAN, '-'))+"-"+String(rtc.getTimeStr());
delay(1000);
}
}
void led6Update()//上传
{
if (millis() - changeTime6 > 5000) {
changeTime6 = millis();
// 下面添加定时 5 s 要执行的内容
Data = "GET /crzz.php?aa="+outh+"&ab="+outd+"&ac="+outc+"&ad="+outb+"&ae="+outa;
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\"192.168.10.204\",80",4,"OK");
sendCommand("AT+CIPSEND=0," +String(Data.length()+4),2,">");
esp8266.println(Data);
delay(1000);
countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",2,"OK");
}
}
void sendCommand(String command, 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("Yes");
countTrueCommand++;
countTimeCommand = 0;
}
if (found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
|
|