#include #include #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秒鐘) 時間太短亦會發生錯誤 String IP = "pm25.tn.edu.tw"; //PM2.5網站網址 //sharp GP2Y1051 二代 start int incomeByte[7]; int data; int z=0; int sum, sount; unsigned long error, starttime; // Arduino夏普GP2Y1051AU0F的串口讀取_源代碼_Weesky維天監製 // 作者ID:Weesky ; ... // 2014年11月23日 // v1.0 基礎版本,成功讀取數值,並且算出PM2.5濃度。 // v1.1 改進了算法,這樣看起來數值高了些。對於愛好者操作更容易了。 //請不要在採樣週期裡面添加延遲delay這樣的。不然串口寄存器只有64Byte,很可能被塞滿。 // 修改者:ng344 // 增加溫濕度感測、2-way-relay、ESP8266、LED警示 void setup() { Serial.begin(2400); dht.begin(); WiFiSerial.begin(9600); starttime=millis(); //設定Sharp感測器開始的擷取時間 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 ) { /* Serial.print("Data OK! |"); for(int k=0;k<7;k++) { Serial.print(incomeByte[k]); //列印出接收到Sharp的資料 Serial.print("|"); } */ //Serial.print(" Vo="); float vo=(incomeByte[1]*256.0+incomeByte[2])/1024.0*5.00; //Serial.print(vo,3); //Serial.print("v | "); float T = vo*700; // 這裡我修改過咯,2014年11月23日,v1.1 // 普通Arduinio玩家也可以算出濃度啦! // 當然更精準的還需要自行標定哦:) //Serial.print(" PM2.5 = "); //Serial.print(T,2); //Serial.print("ug/m3 "); //Serial.println(); updateSensor(T); delay(updateInterval); // 設定傳送時間間隔 } else{ z=0; Serial.flush(); data='/0'; for(int m=0;m<7;m++){ incomeByte[m]=0; } } z=0; } } } void updateSensor( float T) { digitalWrite(LED_G,HIGH); digitalWrite(LED_Y,HIGH); digitalWrite(LED_R,HIGH); digitalWrite(LED_B,HIGH); delay(2000); digitalWrite(LED_G,LOW); digitalWrite(LED_Y,LOW); digitalWrite(LED_R,LOW); digitalWrite(LED_B,LOW); delay(1000); 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 if(T>=200) { digitalWrite(LED_B,HIGH); digitalWrite(Relay2,LOW); delay(1000); tone(Buzzer,500); delay(1000); tone(Buzzer,1000); delay(1000); noTone(Buzzer);} else {digitalWrite(Relay2,HIGH); delay(1000); } // noTone(Buzzer);} // 設定 ESP8266 作為 Client 端 String cmd = "AT+CIPSTART=\"TCP\",\"" + IP + "\",80"; sendtoWiFi(cmd); delay(1000); 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(5000); if(WiFiSerial.find( ">" ) ) { Serial.print(">"); WiFiSerial.print(cmd); //送出pm2.5資料 if( WiFiSerial.find("OK") ){ //檢查WiFi模組是否正確傳出資料 sendOK = true; } Serial.print(cmd); } 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(1000); digitalWrite(RST,HIGH); //need chang to LOW if Relay use HIGH active } } 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); int count =0; String cmd="AT+CWJAP=\"" + SSID + "\",\"" + PASS + "\""; sendtoWiFi(cmd); Serial.println("Waiting for 15 second to connect network ....."); delay(15000); /* if(WiFiSerial.find("OK")) { Serial.println("RECEIVED: OK"); return true; } else { Serial.println("RECEIVED: Unknown"); return false; } */ cmd = "AT+ CIPMUX=0"; sendtoWiFi( cmd ); if( WiFiSerial.find( "Error") ) { Serial.print( "RECEIVED: Error" ); return false; } } ```
進階搜尋