Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include "Timer.h"
- #include "DHT.h"
- Timer t;
- Timer dataupload;
- Timer quakeblink;
- SoftwareSerial debug(2,3); //ESP8266連接Pin位
- #define DHTPIN 4
- #define DHTTYPE DHT11
- #define IP "184.106.153.149"
- #define lightsensor 0 //亮度感測器Pin位
- #define quakeled 13 //紅色LED Pin位
- #define SSID "NHSH_zyxel_3" //Wifi SSID
- #define PASS "27977035" //Wifi 密碼
- DHT dht(DHTPIN, DHTTYPE);
- int datasent = 0;
- int brightness,lightswitch;
- int inittext = 1;
- int quakemode = 0;
- float dew;
- int dustPin=2;
- float dustVal=0;
- int ledPower=7;
- int delayTime=280;
- int delayTime2=40;
- float offTime=9680;
- void setup(){
- Serial.begin(9600);
- debug.begin(9600); // ESP8266 baud rate
- pinMode(10, OUTPUT); //LED PIN位設定
- digitalWrite(10, LOW);
- pinMode(11,OUTPUT);
- digitalWrite(11,LOW);
- pinMode(12,OUTPUT);
- digitalWrite(12,LOW);
- pinMode(13,OUTPUT);
- digitalWrite(13,LOW);
- pinMode(ledPower,OUTPUT);
- pinMode(dustPin, INPUT);
- t.every(500, Wifi_main);
- dataupload.every(60000,sensorread);
- quakeblink.every(2500,quakelight);
- dht.begin();
- }
- void loop(){
- if(datasent == 1){ //Enough time for it to sent out data
- delay(1500);
- datasent=0;
- }
- if(quakemode == 1){
- quakeblink.update();
- }else
- digitalWrite(quakeled, LOW);
- t.update();
- dataupload.update();
- motiontime.update();
- }
- int Wifi_State,delaytime;
- String cmd;
- String GET = "GET /update?key=NIZSH6KT0F9JW782";//api THINGSPEAK_KEY
- void Wifi_main(){
- switch (Wifi_State) {
- case 0://--------------------------------------------------測試ESP8266
- if(inittext == 1)
- Serial.println(F("------Initializaition Start------"));
- sendDebug("AT",0);
- if (Loding(F("Sent AT"))){
- Wifi_State++;
- }
- break;
- case 1://------------------------------------------------開啟Wifi功能
- sendDebug("AT+CWMODE=3",0);
- if (Loding("AT+CWMODE=3")){
- Wifi_State++;
- }
- break;
- case 2://--------------------------------------------------連接Wifi
- cmd="AT+CWJAP=\"";
- cmd+=SSID;
- cmd+="\",\"";
- cmd+=PASS;
- cmd+="\"";
- sendDebug(cmd,0);
- Wifi_State++;
- if (Loding("Wifi connect")){
- Wifi_State++; //next setting wifi mode
- break;
- }
- else{Wifi_State--;}
- break;
- case 3://--------------------------------------------------確認連接網路
- sendDebug("AT+CIFSR",0);
- if (Loding("Get ip address")){ //delay 1 sec
- Wifi_State++; //next setting wifi mode
- }
- else{Wifi_State--;}
- break;
- case 4://--------------------------------------------------多重連接啟動
- sendDebug("AT+CIPMUX=1",0);
- if (Loding("Set CIPMUX")){
- Wifi_State++; //next setting wifi mode
- }
- else{Wifi_State--;}
- break;
- case 5://--------------------------------------------------TCP伺服器啟動Port:8888
- sendDebug("AT+CIPSERVER=1,8888",0);
- Wifi_State++; //next setting wifi mode
- break;
- case 6:// 偵測訊息及開關燈
- if (inittext == 1){
- Serial.println(F("------Initializaition Successful------"));
- inittext = 0;
- }
- brightness=analogRead(lightsensor);
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- dew = dewtmp(t,h);
- if(debug.available()) { //訊息偵測
- if(debug.find("+IPD,")){
- Serial.println(F("Signal Detected!"));
- Wifi_State++;
- }
- }else if(brightness<=180 && lightswitch != 2){ //二級亮度
- lightswitch = 2;
- digitalWrite(11, HIGH);
- digitalWrite(12, HIGH);
- Serial.println(F("Light On 2!"));
- }else if (brightness <= 300 && lightswitch != 1){ //一級亮度
- lightswitch = 1;
- digitalWrite(11, HIGH);
- digitalWrite(12, LOW);
- Serial.println(F("Light On 1!"));
- }else if (brightness > 300 && lightswitch != 0){ //關燈
- lightswitch = 0;
- digitalWrite(11, LOW);
- digitalWrite(12, LOW);
- Serial.println(F("Light Off!"));
- }else if(t<=dew && lightswitch != 2){
- lightswitch = 2;
- digitalWrite(11, HIGH);
- digitalWrite(12, HIGH);
- Serial.println(F("Light On 2!(DEW)"));
- }
- break;
- case 7:
- delay(10);
- int connectionId = debug.read()-48; //讀取連線ID
- debug.find("mode=");
- int modeNumber = (debug.read()-48);
- Serial.print("Mode=");
- Serial.println(modeNumber);
- if(modeNumber == 1){ //地震模式
- if(quakemode == 1){
- quakemode = 0;
- Serial.println(F("Quakemode off"));
- }else if(quakemode == 0){
- quakemode = 1;
- Serial.println(F("Quakemode on"));
- }
- }else if(modeNumber == 2){ //手動資料上傳
- Serial.println(F("Manual Upload Start"));
- sensorread();
- break;
- }else if(modeNumber == 3){
- digitalWrite(11, !digitalRead(11));
- if(digitalRead(11)==HIGH)
- lightswitch = 1;
- else if(digitalRead(11)==LOW)
- lightswitch = 0;
- }
- String closeCommand = "AT+CIPCLOSE=";
- closeCommand+=connectionId;
- closeCommand+="\r\n";
- sendDebug(closeCommand,0); // 關閉連結
- Wifi_State--;
- break;
- }
- }
- String get_response() {
- String response="";
- char c;
- while (debug.available()) {
- c=debug.read();
- response.concat(c);
- delay(10);
- }
- response.trim();
- return response;
- }
- boolean Loding(String state){ //測試指令是否正常執行
- String response=get_response();
- for (int timeout=0 ; timeout<30 ; timeout++)
- {
- if(response.indexOf("OK") != -1 || response.indexOf("no change") != -1)
- {
- Serial.print(F(" OK ! "));
- if(timeout>=2){
- Serial.print(F("This command waste "));
- Serial.print(timeout/2);
- Serial.println(F(" sec."));
- }
- else Serial.println(F(""));
- return 1;
- break;
- }
- else if(timeout==29){ //15秒Timeout
- Serial.print(state);
- Serial.println(F(" fail...\nExit2"));
- return 0;
- }
- else{
- response=get_response();
- if(timeout==0)
- Serial.print(F("Wifi Loading."));
- else
- Serial.print(F("."));
- delay(500);
- }
- }
- }
- void sensorread(){ //讀取各感測器數值
- brightness=analogRead(lightsensor);
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- float f = dht.readTemperature(true);
- dew = dewtmp(t,h);
- digitalWrite(ledPower,LOW);
- delayMicroseconds(delayTime);
- dustVal=analogRead(dustPin);
- delayMicroseconds(delayTime2);
- digitalWrite(ledPower,HIGH);
- delayMicroseconds(offTime);
- delay(1000);
- if (dustVal>36.455)
- dustVal=(float(dustVal/1024)-0.0356)*120000*0.035;
- //灰塵數值計算
- if (isnan(h) || isnan(t) || isnan(f)) {
- Serial.println(F("Failed to read from DHT sensor!"));
- }else{
- Serial.print(F("Brightness: "));
- Serial.print(brightness, DEC);
- Serial.print(F(" Humidity: "));
- Serial.print(h);
- Serial.print(F("%\n"));
- Serial.print(F("Dew Temperature: "));
- Serial.print(dew);
- Serial.print(F("\n"));
- Serial.print(F("Temperature: "));
- Serial.print(t);
- Serial.print(F("*C\t"));
- Serial.print(f);
- Serial.print(F("*F\n"));
- Serial.print(F("Dust: "));
- Serial.println(dustVal);
- SentOnCloud( String(h), String(t) ,String(brightness) ,String(dew),String(lightswitch),String(dustVal));
- }
- }
- void SentOnCloud( String T, String H ,String B ,String D ,String L,String P) //ThingSpeak 資料上傳
- {
- // 設定 ESP8266 作為 Client 端
- sendDebug("AT+CIPSERVER=0",0);
- sendDebug("AT+CIPMUX=0",0);
- datasent = 1;
- Wifi_State = 4;
- String cmd = "AT+CIPSTART=\"TCP\",\"";
- cmd += IP;
- cmd += "\",80";
- sendDebug(cmd,0);
- if( debug.find( "Error" ) )
- {
- Serial.print( F("RECEIVED: Error\nExit1") );
- return;
- }
- cmd = GET + "&field1=" + T + "&field2=" + H + "&field3=" + B + "&field4=" + D + "&field5=" + L + "&field6=" + P + "\r\n";
- //使用GET指令傳送資料
- debug.print( "AT+CIPSEND=" );
- debug.println( cmd.length() );
- if(debug.find( ">" ) )
- {
- Serial.print(">");
- Serial.print(cmd);
- debug.print(cmd);
- }
- else
- {
- debug.print( "AT+CIPCLOSE" );
- }
- Serial.write(debug.read());
- if( debug.find("OK") )
- {
- Serial.println( F("RECEIVED: OK") );
- }
- else
- {
- Serial.println( F("RECEIVED: Error\nExit2") );
- }
- }
- void quakelight(){
- digitalWrite(quakeled,!digitalRead(quakeled));
- }
- float dewtmp(float t,float h){ //計算露點溫度
- const float b=17.67;
- const float c=243.5;
- float gamma=log(h/100)+b*t/(c+t);
- float out = c*gamma/(b-gamma);
- return out;
- }
- void sendDebug(String sent_cmd,int cmdoutput)
- {
- Serial.print("SEND: ");
- Serial.println(sent_cmd);
- debug.println(sent_cmd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement