#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 start int measurePin = 0; //連接空氣品質傳感器 to Arduino A0 pin int ledPower = 2; //Connect 3 led driver pins of dust sensor to Arduino D2 int samplingTime = 280; int deltaTime = 40; int sleepTime = 9680; float voMeasured = 0; float calcVoltage = 0; float dustDensity = 0; //sharp end void setup() { Serial.begin(9600); //MONITOR dht.begin(); //DHT22 WiFiSerial.begin(9600); //ESP8266 sendtoWiFi("AT"); delay(1000); if(WiFiSerial.find("OK")) { Serial.println("RECEIVED: OK after AT Command\nData ready to sent!"); connectWiFi(); } pinMode(ledPower,OUTPUT); //sharp pinMode(RST,OUTPUT); pinMode(LED_G,OUTPUT); pinMode(LED_Y,OUTPUT); pinMode(LED_R,OUTPUT); pinMode(LED_B,OUTPUT); pinMode(Relay2,OUTPUT); pinMode(Buzzer,OUTPUT); digitalWrite(RST,HIGH); //CHANGE TO LOW IF HIGH ACTIVE digitalWrite(LED_G,LOW); digitalWrite(LED_Y,LOW); digitalWrite(LED_R,LOW); digitalWrite(LED_B,LOW); digitalWrite(Relay2,HIGH); } void loop() { digitalWrite(ledPower,LOW); // power on the LED delayMicroseconds(samplingTime); voMeasured = analogRead(measurePin); // read the dust value delayMicroseconds(deltaTime); digitalWrite(ledPower,HIGH); // turn the LED off delayMicroseconds(sleepTime); // 0 - 5V mapped to 0 - 1023 integer values // recover voltage calcVoltage = voMeasured * (5.0 / 1024.0); // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/ // Chris Nafis (c) 2012 if (calcVoltage < 0.583) { dustDensity = 0; } else{ dustDensity = 6 * calcVoltage / 35 - 0.1; } Serial.print("Raw Signal Value (0-1023): "); Serial.print(voMeasured); Serial.print(" - Voltage: "); Serial.print(calcVoltage); Serial.print(" - Dust Density: "); Serial.print(dustDensity * 1000); // 這裡將數值呈現改成較常用的單位( ug/m3 ) Serial.println(" ug/m3 "); float T=dustDensity * 1000 ; updateSensor(T); delay(updateInterval); // 設定傳送時間間隔 } 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; } } ```
進階搜尋