- #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 //數(shù)據(jù)總線
- OneWire oneWire(ONE_WIRE_BUS); //聲明
- DallasTemperature sensors(&oneWire); //聲明
- int x5 = A5;
- String outa=""; //讀取溫度值存入變量
- String outb=""; //讀取時(shí)間值存入變量
- 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); //設(shè)置為非阻塞模式
- }
- void loop()
- {
- yield();
- }
- defineTaskLoop(Task1)//水泵
- {
- digitalWrite(x5, HIGH); // 點(diǎn)亮
- sleep(3600000); // 等待
- digitalWrite(x5, LOW); // 通過將引腳電平拉低,關(guān)閉
- sleep(10000); // 等待
- }
- defineTaskLoop(Task2)//溫度
- {
- float tempC = sensors.getTempCByIndex(0); //獲取索引號(hào)0的傳感器攝氏溫度數(shù)據(jù)
- if (tempC != DEVICE_DISCONNECTED_C) //如果獲取到的溫度正常
- {
- Serial.print("\n當(dāng)前溫度是: ");
- Serial.print(tempC);
- Serial.println(" ℃");
- }
- //Serial.println("發(fā)起溫度轉(zhuǎn)換");
- sensors.requestTemperatures(); //發(fā)起新的溫度轉(zhuǎn)換
- //Serial.print(tempC); //打印當(dāng)前溫度值
- //outa=String(tempC)+"℃";
- outa=String(tempC)+"℃";
- sleep(1000);
- }
- /*
- defineTaskLoop(Task3)//時(shí)間
- {
- outb=String(rtc.getDateStr(FORMAT_LONG, FORMAT_LITTLEENDIAN, '-'))+"-"+String(rtc.getTimeStr());
- delay(1000);
- }
- */
- defineTaskLoop(Task4)//大氣壓
- {
- if (!bmp.begin())
- {
- Serial.println(F("找不到對(duì)應(yīng)的傳感器"));
- 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;
- }
- // 獲取測(cè)量數(shù)據(jù)
- 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;
- }
復(fù)制代碼
|