- #include <SCoop.h>//引入头文件
- #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变量
- void setup() {
- mySCoop.start();
- pinMode(x5, OUTPUT);
- Serial.begin(9600);
- sensors.begin(); //初始化总线
- sensors.setWaitForConversion(false); //设置为非阻塞模式
- }
- void loop()
- {
- yield();
- }
- defineTaskLoop(Task1)//水泵
- {
- digitalWrite(x5, HIGH); // 点亮
- sleep(3600000); // 等待
- digitalWrite(x5, LOW); // 通过将引脚电平拉低,关闭
- sleep(10000); // 等待
- }
- defineTaskLoop(Task2)//温度
- {
- float tempC = sensors.getTempCByIndex(0); //获取索引号0的传感器摄氏温度数据
- if (tempC != DEVICE_DISCONNECTED_C) //如果获取到的温度正常
- {
- Serial.print("\n当前温度是: ");
- Serial.print(tempC);
- Serial.println(" ℃");
- }
- //Serial.println("发起温度转换");
- sensors.requestTemperatures(); //发起新的温度转换
- //Serial.print(tempC); //打印当前温度值
- //outa=String(tempC)+"℃";
- outa=String(tempC)+"℃";
- sleep(1000);
- }
- /*
- defineTaskLoop(Task3)//时间
- {
- outb=String(rtc.getDateStr(FORMAT_LONG, FORMAT_LITTLEENDIAN, '-'))+"-"+String(rtc.getTimeStr());
- delay(1000);
- }
- */
- defineTaskLoop(Task4)//大气压
- {
- 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);
- }
- defineTaskLoop(Task5)//湿度
- {
- 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);
- }
- defineTaskLoop(Task6)//上传
- {
- esp8266.begin(115200);
- sendCommand("AT", 5, "OK");
- sendCommand("AT+CWMODE=1", 5, "OK");
- sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
- 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;
- }
复制代码
|