#include <SoftwareSerial.h> #include <DHT.h> #define _rxpin 4 //接ESP8266 TX #define _txpin 5 //接ESP8266 RX #define RST 6 //接ESP8266 RST #define DHTPIN 7 //接DHT22資料傳輸 #define LED_G 8 //接綠燈 #define LED_Y 9 //接黃燈 #define LED_R 10 //接紅燈 #define LED_B 11 //接藍(紫)燈 #define Relay2 12 //Control Fan or Light #define Buzzer 13 //接蜂鳴器 #define DHTTYPE DHT22 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS DHT dht(DHTPIN, DHTTYPE); SoftwareSerial WiFiSerial( _rxpin, _txpin ); // ESP8266 RX, TX //*-- IoT Information String SSID = "TN-Computer-bridge"; //無線網路名稱 String PASS = "computer123"; //無線網路密碼 //String SSID = "修改自己的無線網路名稱"; //無線網路名稱 //String PASS = "修改自己的無線網路密碼"; //無線網路密碼 String schoolcode = "修改自己的學校代碼"; //學校代碼 int no = 0; //設備編號,可設定0~255,在監測網上可區別一校多個設備 String PMgps = "修改自己的學校經緯度"; //學校經緯度座標 //long updateInterval = 1800000; //傳送資料時間間隔,測試完請設定1800000(30分鐘) long updateInterval = 10000; //傳送資料時間間隔,測試用10000(10秒鐘) 時間太短亦會發生錯誤 long Relay2Interval = 1200000; //設定Relay2控制的設備運作時間長短,1200000(20分鐘) String IP = "pm25.tn.edu.tw"; //PM2.5網站網址 //sharp GP2Y1051 二代 start int incomeByte[7]; int data; int z=0; int sum, count; float average_pm2d5,sum_2s; unsigned long error, starttime, buzzerStart, updateStart, Relay2Start; void sendtoWiFi(String cmd) { Serial.print("SEND: "); Serial.println(cmd); WiFiSerial.println(cmd); } boolean connectWiFi() { WiFiSerial.println("AT+CWMODE=1"); Serial.println("Send AT+CWMODE=1"); delay(3000); String cmd="AT+CWJAP=\"" + SSID + "\",\"" + PASS + "\""; sendtoWiFi(cmd); Serial.println("Waiting for 15 second to connect network ....."); delay(15000); cmd = "AT+ CIPMUX=0"; sendtoWiFi( cmd ); if( WiFiSerial.find( "Error") ) { Serial.print( "RECEIVED: Error" ); return false; } } void updateSensor( float T) { digitalWrite(LED_G,LOW); digitalWrite(LED_Y,LOW); digitalWrite(LED_R,LOW); digitalWrite(LED_B,LOW); if(T>=0 && T<36) { digitalWrite(LED_G,HIGH); } else if(T>=36 && T<54) { digitalWrite(LED_Y,HIGH); } else if(T>=54 && T<71) { digitalWrite(LED_R,HIGH); } else if(T>=71 && T<200) { digitalWrite(LED_B,HIGH); } else { //T>=200 digitalWrite(LED_B,HIGH); digitalWrite(Relay2,LOW); digitalWrite(Buzzer,HIGH); Relay2Start=millis(); //設定Relay2運作開始時間 buzzerStart=millis(); //設定Buzzer告警開始時間 } if(abs(millis()-buzzerStart)>2000){ digitalWrite(Buzzer,LOW); } if(abs(millis()-Relay2Start)>Relay2Interval){ digitalWrite(Relay2,HIGH); } if(abs( millis() - updateStart) > updateInterval | updateStart == 0 ){ // 設定 ESP8266 作為 Client 端 String cmd = "AT+CIPSTART=\"TCP\",\"" + IP + "\",80"; sendtoWiFi(cmd); delay(500); if( WiFiSerial.find("Error") ) { Serial.print( "RECEIVED: Error\nExit1" ); return; } float h = dht.readHumidity(); float t = dht.readTemperature(); boolean sendOK = false; cmd = "GET /xoops/api.php?schoolcode=" + schoolcode + "&no=" + no + "&pm25=" + T + "&t=" + t + "&h=" + h + "&gps=" + PMgps + "\r\n"; sendtoWiFi("AT+CIPSEND=" + (String)cmd.length()); delay(500); if(WiFiSerial.find( ">" ) ) { Serial.print(">"); WiFiSerial.print(cmd); //送出pm2.5資料 if( WiFiSerial.find("OK") ){ //檢查WiFi模組是否正確傳出資料 sendOK = true; } Serial.print(cmd); updateStart = millis(); //重設上傳時間 } else { sendtoWiFi( "AT+CIPCLOSE" ); } if(sendOK) { Serial.println( "RECEIVED: OK" ); } else { Serial.println( "RECEIVED: Error\nExit2" ); // reset ESP8266 digitalWrite(RST,LOW); //need chang to HIGH if Relay use HIGH active delay(500); digitalWrite(RST,HIGH); //need chang to LOW if Relay use HIGH active } } } void setup() { Serial.begin(2400); dht.begin(); WiFiSerial.begin(9600); starttime = millis(); //設定Sharp感測器開始的擷取時間 updateStart = millis(); //設定第一次即上傳 sendtoWiFi("AT"); delay(1000); if(WiFiSerial.find("OK")) { Serial.println("RECEIVED: OK after AT Command\nData ready to sent!"); connectWiFi(); } else{ Serial.println("fale"); } pinMode(LED_G,OUTPUT); pinMode(LED_Y,OUTPUT); pinMode(LED_R,OUTPUT); pinMode(LED_B,OUTPUT); pinMode(RST,OUTPUT); pinMode(Relay2,OUTPUT); pinMode(Buzzer,OUTPUT); digitalWrite(LED_G,LOW); digitalWrite(LED_Y,LOW); digitalWrite(LED_R,LOW); digitalWrite(LED_B,LOW); digitalWrite(RST,HIGH); digitalWrite(Relay2,HIGH); } void loop(){ while (Serial.available()>0){ data=Serial.read(); if(data == 170){ z=0; incomeByte[z]=data; } else{ z++; incomeByte[z]=data; } if(z==6) { sum=incomeByte[1]+ incomeByte[2]+ incomeByte[3] + incomeByte[4]; if(incomeByte[5]==sum && incomeByte[6]==255 ) { float vo=(incomeByte[1]*256.0+incomeByte[2])/1024.0*5.00; float pm = vo*700; sum_2s=sum_2s + pm; count++; if(abs(millis()-starttime)>2000){ average_pm2d5=(sum_2s/count)*1.00; updateSensor(average_pm2d5); Serial.print("PM2.5_Average = "); Serial.println(average_pm2d5,2); starttime=millis(); sum_2s=0; count=0; } } else{ z=0; Serial.flush(); data='/0'; for(int m=0;m<7;m++){ incomeByte[m]=0; } } z=0; } } }