Advertisement
seanho12345

AI light 2.0 Code

Sep 11th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SoftwareSerial.h>
  2. #include "Timer.h"
  3. #include "DHT.h"
  4.  
  5. Timer t;
  6. Timer dataupload;
  7. Timer quakeblink;
  8. SoftwareSerial debug(2,3); //ESP8266連接Pin位
  9.  
  10.  
  11. #define DHTPIN 4
  12. #define DHTTYPE DHT11
  13. #define IP "184.106.153.149"
  14. #define lightsensor 0       //亮度感測器Pin位
  15. #define quakeled 13         //紅色LED Pin位
  16. #define SSID "NHSH_zyxel_3" //Wifi SSID
  17. #define PASS "27977035"     //Wifi 密碼
  18.  
  19. DHT dht(DHTPIN, DHTTYPE);
  20. int datasent = 0;
  21. int brightness,lightswitch;
  22. int inittext = 1;
  23. int quakemode = 0;
  24. float dew;
  25. int dustPin=2;
  26. float dustVal=0;
  27. int ledPower=7;
  28. int delayTime=280;
  29. int delayTime2=40;
  30. float offTime=9680;
  31.  
  32.  
  33. void setup(){
  34.   Serial.begin(9600);
  35.   debug.begin(9600); // ESP8266 baud rate
  36.   pinMode(10, OUTPUT);  //LED PIN位設定
  37.   digitalWrite(10, LOW);
  38.   pinMode(11,OUTPUT);
  39.   digitalWrite(11,LOW);
  40.   pinMode(12,OUTPUT);
  41.   digitalWrite(12,LOW);
  42.   pinMode(13,OUTPUT);
  43.   digitalWrite(13,LOW);
  44.   pinMode(ledPower,OUTPUT);
  45.   pinMode(dustPin, INPUT);
  46.   t.every(500, Wifi_main);
  47.   dataupload.every(60000,sensorread);
  48.   quakeblink.every(2500,quakelight);
  49.   dht.begin();
  50.  
  51. }
  52.  
  53. void loop(){
  54.   if(datasent == 1){ //Enough time for it to sent out data
  55.     delay(1500);
  56.     datasent=0;
  57.   }
  58.   if(quakemode == 1){
  59.     quakeblink.update();
  60.   }else
  61.     digitalWrite(quakeled, LOW);
  62.   t.update();
  63.   dataupload.update();
  64.   motiontime.update();
  65. }
  66.  
  67. int Wifi_State,delaytime;
  68. String cmd;
  69. String GET = "GET /update?key=NIZSH6KT0F9JW782";//api THINGSPEAK_KEY
  70.  
  71.  
  72. void Wifi_main(){
  73.   switch (Wifi_State) {
  74.   case 0://--------------------------------------------------測試ESP8266
  75.       if(inittext == 1)
  76.         Serial.println(F("------Initializaition Start------"));
  77.       sendDebug("AT",0);
  78.       if (Loding(F("Sent AT"))){
  79.         Wifi_State++;
  80.       }
  81.     break;
  82.   case 1://------------------------------------------------開啟Wifi功能
  83.       sendDebug("AT+CWMODE=3",0);
  84.       if (Loding("AT+CWMODE=3")){
  85.         Wifi_State++;
  86.       }
  87.     break;
  88.   case 2://--------------------------------------------------連接Wifi
  89.  
  90.       cmd="AT+CWJAP=\"";
  91.       cmd+=SSID;
  92.       cmd+="\",\"";
  93.       cmd+=PASS;
  94.       cmd+="\"";
  95.       sendDebug(cmd,0);
  96.       Wifi_State++;
  97.       if (Loding("Wifi connect")){
  98.         Wifi_State++; //next setting wifi mode
  99.         break;
  100.       }
  101.       else{Wifi_State--;}
  102.     break;
  103.   case 3://--------------------------------------------------確認連接網路
  104.       sendDebug("AT+CIFSR",0);
  105.       if (Loding("Get ip address")){ //delay 1 sec
  106.         Wifi_State++; //next setting wifi mode
  107.       }
  108.       else{Wifi_State--;}
  109.       break;
  110.   case 4://--------------------------------------------------多重連接啟動
  111.         sendDebug("AT+CIPMUX=1",0);
  112.         if (Loding("Set CIPMUX")){
  113.             Wifi_State++; //next setting wifi mode
  114.         }
  115.         else{Wifi_State--;}
  116.       break;
  117.   case 5://--------------------------------------------------TCP伺服器啟動Port:8888
  118.         sendDebug("AT+CIPSERVER=1,8888",0);
  119.         Wifi_State++; //next setting wifi mode
  120.  
  121.       break;
  122.   case 6:// 偵測訊息及開關燈
  123.         if (inittext == 1){
  124.           Serial.println(F("------Initializaition Successful------"));
  125.           inittext = 0;
  126.         }
  127.         brightness=analogRead(lightsensor);
  128.         float h = dht.readHumidity();
  129.         float t = dht.readTemperature();
  130.             dew = dewtmp(t,h);
  131.         if(debug.available()) {      //訊息偵測
  132.           if(debug.find("+IPD,")){
  133.             Serial.println(F("Signal Detected!"));
  134.             Wifi_State++;
  135.             }
  136.         }else if(brightness<=180 && lightswitch != 2){    //二級亮度
  137.           lightswitch = 2;
  138.           digitalWrite(11, HIGH);
  139.           digitalWrite(12, HIGH);
  140.           Serial.println(F("Light On 2!"));
  141.         }else if (brightness <= 300 && lightswitch != 1){ //一級亮度
  142.           lightswitch = 1;
  143.           digitalWrite(11, HIGH);
  144.           digitalWrite(12, LOW);
  145.           Serial.println(F("Light On 1!"));
  146.         }else if (brightness > 300  && lightswitch != 0){ //關燈
  147.           lightswitch = 0;
  148.           digitalWrite(11, LOW);
  149.           digitalWrite(12, LOW);
  150.           Serial.println(F("Light Off!"));
  151.         }else if(t<=dew && lightswitch != 2){
  152.           lightswitch = 2;
  153.           digitalWrite(11, HIGH);
  154.           digitalWrite(12, HIGH);
  155.           Serial.println(F("Light On 2!(DEW)"));
  156.       }
  157.  
  158.       break;
  159.       case 7:
  160.           delay(10);
  161.           int connectionId = debug.read()-48; //讀取連線ID
  162.  
  163.           debug.find("mode=");
  164.           int modeNumber = (debug.read()-48);
  165.           Serial.print("Mode=");
  166.           Serial.println(modeNumber);
  167.           if(modeNumber == 1){      //地震模式
  168.             if(quakemode == 1){
  169.               quakemode = 0;
  170.               Serial.println(F("Quakemode off"));
  171.             }else if(quakemode == 0){
  172.               quakemode = 1;
  173.               Serial.println(F("Quakemode on"));
  174.             }
  175.           }else if(modeNumber == 2){   //手動資料上傳
  176.             Serial.println(F("Manual Upload Start"));
  177.             sensorread();
  178.             break;
  179.           }else if(modeNumber == 3){
  180.             digitalWrite(11, !digitalRead(11));
  181.             if(digitalRead(11)==HIGH)
  182.               lightswitch = 1;
  183.             else if(digitalRead(11)==LOW)
  184.               lightswitch = 0;
  185.           }
  186.           String closeCommand = "AT+CIPCLOSE=";
  187.           closeCommand+=connectionId;
  188.           closeCommand+="\r\n";
  189.           sendDebug(closeCommand,0); // 關閉連結
  190.  
  191.  
  192.           Wifi_State--;
  193.        break;
  194.     }
  195.   }
  196.   String get_response() {
  197.     String response="";
  198.     char c;
  199.     while (debug.available()) {
  200.       c=debug.read();
  201.       response.concat(c);
  202.       delay(10);
  203.       }
  204.     response.trim();
  205.     return response;
  206.   }
  207.   boolean Loding(String state){              //測試指令是否正常執行
  208.     String response=get_response();
  209.     for (int timeout=0 ; timeout<30 ; timeout++)
  210.     {
  211.       if(response.indexOf("OK") != -1 || response.indexOf("no change") != -1)
  212.       {
  213.           Serial.print(F(" OK ! "));
  214.           if(timeout>=2){
  215.             Serial.print(F("This command waste "));
  216.             Serial.print(timeout/2);
  217.             Serial.println(F(" sec."));
  218.           }
  219.           else Serial.println(F(""));
  220.           return 1;
  221.           break;
  222.       }
  223.       else if(timeout==29){ //15秒Timeout
  224.         Serial.print(state);
  225.         Serial.println(F(" fail...\nExit2"));
  226.         return 0;
  227.       }
  228.       else{
  229.         response=get_response();
  230.         if(timeout==0)
  231.           Serial.print(F("Wifi Loading."));
  232.         else
  233.           Serial.print(F("."));
  234.         delay(500);
  235.       }
  236.     }
  237.   }
  238.   void sensorread(){   //讀取各感測器數值
  239.     brightness=analogRead(lightsensor);
  240.     float h = dht.readHumidity();
  241.     float t = dht.readTemperature();
  242.     float f = dht.readTemperature(true);
  243.     dew = dewtmp(t,h);
  244.     digitalWrite(ledPower,LOW);
  245.     delayMicroseconds(delayTime);
  246.     dustVal=analogRead(dustPin);
  247.     delayMicroseconds(delayTime2);
  248.     digitalWrite(ledPower,HIGH);
  249.     delayMicroseconds(offTime);
  250.  
  251.     delay(1000);
  252.     if (dustVal>36.455)
  253.       dustVal=(float(dustVal/1024)-0.0356)*120000*0.035;
  254.       //灰塵數值計算
  255.  
  256.  
  257.   if (isnan(h) || isnan(t) || isnan(f)) {
  258.       Serial.println(F("Failed to read from DHT sensor!"));
  259.  
  260.   }else{
  261.     Serial.print(F("Brightness: "));
  262.     Serial.print(brightness, DEC);
  263.     Serial.print(F(" Humidity: "));
  264.     Serial.print(h);
  265.     Serial.print(F("%\n"));
  266.     Serial.print(F("Dew Temperature: "));
  267.     Serial.print(dew);
  268.     Serial.print(F("\n"));
  269.     Serial.print(F("Temperature: "));
  270.     Serial.print(t);
  271.     Serial.print(F("*C\t"));
  272.     Serial.print(f);
  273.     Serial.print(F("*F\n"));
  274.     Serial.print(F("Dust: "));
  275.     Serial.println(dustVal);
  276.  
  277.  
  278.     SentOnCloud( String(h), String(t) ,String(brightness) ,String(dew),String(lightswitch),String(dustVal));
  279.  
  280.   }
  281.  
  282. }
  283. void SentOnCloud( String T, String H ,String B ,String D ,String L,String P)    //ThingSpeak 資料上傳
  284. {
  285.     // 設定 ESP8266 作為 Client 端
  286.     sendDebug("AT+CIPSERVER=0",0);
  287.     sendDebug("AT+CIPMUX=0",0);
  288.     datasent = 1;
  289.     Wifi_State = 4;
  290.     String cmd = "AT+CIPSTART=\"TCP\",\"";
  291.     cmd += IP;
  292.     cmd += "\",80";
  293.     sendDebug(cmd,0);
  294.     if( debug.find( "Error" ) )
  295.     {
  296.         Serial.print( F("RECEIVED: Error\nExit1") );
  297.         return;
  298.     }
  299.     cmd = GET + "&field1=" + T + "&field2=" + H + "&field3=" + B + "&field4=" + D + "&field5=" + L + "&field6=" + P + "\r\n";
  300.     //使用GET指令傳送資料
  301.     debug.print( "AT+CIPSEND=" );
  302.     debug.println( cmd.length() );
  303.     if(debug.find( ">" ) )
  304.     {
  305.         Serial.print(">");
  306.         Serial.print(cmd);
  307.         debug.print(cmd);
  308.     }
  309.     else
  310.     {
  311.         debug.print( "AT+CIPCLOSE" );
  312.     }
  313.     Serial.write(debug.read());
  314.     if( debug.find("OK") )
  315.     {
  316.         Serial.println( F("RECEIVED: OK") );
  317.     }
  318.     else
  319.     {
  320.         Serial.println( F("RECEIVED: Error\nExit2") );
  321.     }
  322. }
  323. void quakelight(){
  324.   digitalWrite(quakeled,!digitalRead(quakeled));
  325. }
  326. float dewtmp(float t,float h){  //計算露點溫度
  327.   const float b=17.67;
  328.   const float c=243.5;
  329.   float gamma=log(h/100)+b*t/(c+t);
  330.   float out = c*gamma/(b-gamma);
  331.   return out;
  332. }
  333. void sendDebug(String sent_cmd,int cmdoutput)
  334. {
  335.  
  336.     Serial.print("SEND: ");
  337.     Serial.println(sent_cmd);
  338.     debug.println(sent_cmd);
  339.  
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement