返回列表 發帖

更新版程式碼 淹水警報器-成品

本帖最後由 蕭澧邦 於 2019-3-27 13:19 編輯

更新資訊:
1.回傳值中文化
2.接收端蜂鳴器間隔音

提醒! 上傳完畢後請重新啟動Arduino板,因為試驗時發現若未重啟開發板,斷網時會當機!

偵測端 Detector
  1. //請於下方提示處,分別填入您的WiFi名稱與密碼,完成後重新上傳至開發版。
  2. #define wifiName "TonysASUS"      //your wifi name
  3. #define wifiPass "myhotspot"      //your wifi password

  4. //#define wifiName "KITA"      //your wifi name
  5. //#define wifiPass "t3893527"  //your wifi password

  6. #include <Esp.h>
  7. #include <ESP8266WiFi.h>
  8. #include <FirebaseArduino.h>
  9. #define firebaseURL "floodingalarm.firebaseio.com"
  10. #define authCode "2gfMkb8d6BZw0iLmj7AdLqB40UtIzFZjEDxRjvLv"

  11. int connectionStatusPin=D9;
  12. int waterPin=A0;
  13. int sensorValue;

  14. void setupFirebase()
  15. {
  16.   Firebase.begin(firebaseURL,authCode);  
  17. }

  18. void setupWiFi()
  19. {
  20.   WiFi.begin(wifiName,wifiPass);
  21.   Serial.println();
  22.   Serial.println();  
  23.   Serial.println("Connecting to WiFi");

  24.   while(WiFi.status()!=WL_CONNECTED)
  25.   {
  26.     Serial.print(".");
  27.     delay(500);
  28.   }
  29.   Serial.println();
  30.   Serial.print("Connected to ");
  31.   Serial.println(wifiName);
  32.   Serial.print("IP: ");
  33.   Serial.println(WiFi.localIP());
  34.   digitalWrite(connectionStatusPin,1);
  35. }

  36. void setup() {
  37.   Serial.begin(115200);
  38.   pinMode(connectionStatusPin,OUTPUT);
  39.   setupWiFi();
  40.   setupFirebase();
  41. }

  42. void loop() {
  43.   sensorValue = analogRead(waterPin);
  44.   Serial.println(sensorValue);
  45.   Firebase.setInt("number", sensorValue);

  46.   //當與 Firebase 通訊失敗時,將所有數值歸零,接著重啟開發版
  47.   if (Firebase.failed()) {
  48.       Serial.print("setting data failed!");
  49.       digitalWrite(connectionStatusPin,0);
  50.       ESP.restart();
  51.   }
  52.   
  53.   delay(100);
  54. }
複製代碼

接收端 Alarm
  1. //請於下方提示處,分別填入您的WiFi名稱與密碼,完成後重新上傳至開發版。
  2. #define wifiName "TonysASUS"    //your wifi name
  3. #define wifiPass "myhotspot"    //your wifi password

  4. //#define wifiName "KITA"      //your wifi name
  5. //#define wifiPass "t3893527"  //your wifi password

  6. #include <Esp.h>
  7. #include <ESP8266WiFi.h>
  8. #include <FirebaseArduino.h>
  9. #define firebaseURL "floodingalarm.firebaseio.com"
  10. #define authCode "2gfMkb8d6BZw0iLmj7AdLqB40UtIzFZjEDxRjvLv"

  11. int numberLED[]={D2,D3,D4,D5,D13,D8};
  12. int btnInput=D6;
  13. int connectionStatusPin=D9;
  14. int bigLEDPin=D10;
  15. int buzzerPin=D11;
  16. int buzzerStartTime;
  17. int currentLevel, lastLevel;

  18. int numberGot;
  19. int alertValue1 = 50;  //一級警報
  20. int alertValue2 = 600;  //二級警報
  21. int alertValue3 = 900;  //三級警報

  22. void showNumber(int n)
  23. {
  24.   for(int i=0; i<sizeof(numberLED)/sizeof(int); i++)
  25.     digitalWrite(numberLED[i],0);
  26.   if(n==1)
  27.   {
  28.     digitalWrite(D4,1);
  29.     digitalWrite(D8,1);
  30.   }
  31.   if(n==2)
  32.   {
  33.     digitalWrite(D2,1);
  34.     digitalWrite(D3,1);
  35.     digitalWrite(D5,1);
  36.     digitalWrite(D13,1);
  37.     digitalWrite(D8,1);
  38.   }
  39.   if(n==3)
  40.   {
  41.     digitalWrite(D3,1);
  42.     digitalWrite(D4,1);
  43.     digitalWrite(D5,1);
  44.     digitalWrite(D13,1);
  45.     digitalWrite(D8,1);  
  46.   }
  47. }

  48. void setupFirebase()
  49. {
  50.   Firebase.begin(firebaseURL,authCode);  
  51. }

  52. void setupWiFi()
  53. {
  54.   WiFi.begin(wifiName,wifiPass);
  55.   Serial.println();
  56.   Serial.println();  
  57.   Serial.println("Connecting to WiFi");

  58.   while(WiFi.status()!=WL_CONNECTED)
  59.   {
  60.     Serial.print(".");
  61.     delay(500);
  62.   }
  63.   Serial.println();
  64.   Serial.print("Connected to ");
  65.   Serial.println(wifiName);
  66.   Serial.print("IP: ");
  67.   Serial.println(WiFi.localIP());
  68.   digitalWrite(connectionStatusPin,1);  
  69. }

  70. void setup() {
  71.   Serial.begin(115200);
  72.   pinMode(connectionStatusPin,OUTPUT);
  73.   pinMode(bigLEDPin,OUTPUT);
  74.   pinMode(buzzerPin,OUTPUT);
  75.   
  76.   for(int i=0; i<sizeof(numberLED)/sizeof(int); i++)
  77.   {
  78.     pinMode(numberLED[i],OUTPUT);
  79.   }
  80.   
  81.   pinMode(btnInput,INPUT);
  82.      
  83.   setupWiFi();
  84.   setupFirebase();
  85. }

  86. void loop() {

  87.   numberGot=Firebase.getInt("number");
  88.   Serial.print("number: ");
  89.   Serial.println(numberGot);

  90.   //當與 Firebase 通訊失敗時,將所有數值歸零,接著重啟開發版
  91.   if (Firebase.failed()) {
  92.       Serial.print("getting data failed!");
  93.       digitalWrite(connectionStatusPin,0);
  94.       numberGot==0;
  95.       digitalWrite(buzzerPin,0);
  96.       digitalWrite(bigLEDPin,0);
  97.       showNumber(0);
  98.       ESP.restart();
  99.   }
  100.    
  101.   if(numberGot >= alertValue3)
  102.   {
  103.     currentLevel=3;
  104.     Serial.println("三級警報 - 淹水了!");
  105.     showNumber(3);
  106.     if(currentLevel!=lastLevel)
  107.         buzzerStartTime=millis();
  108.     if(millis()-buzzerStartTime<=30000)
  109.         digitalWrite(buzzerPin,1);
  110.     else
  111.         digitalWrite(buzzerPin,0);
  112.     digitalWrite(bigLEDPin,1);
  113.     lastLevel=3;
  114.   }else if(numberGot >= alertValue2)
  115.   {
  116.     currentLevel=2;
  117.     Serial.println("二級警報 - 淹水了!");
  118.     showNumber(2);
  119.     if(currentLevel!=lastLevel)
  120.         buzzerStartTime=millis();
  121.     if(millis()-buzzerStartTime<=30000)
  122.         digitalWrite(buzzerPin,1);
  123.     else
  124.         digitalWrite(buzzerPin,0);
  125.     digitalWrite(bigLEDPin,1);
  126.     lastLevel=2;
  127.   }else if(numberGot >= alertValue1)
  128.   {
  129.     currentLevel=1;
  130.     Serial.println("一級警報 - 淹水了!");
  131.     showNumber(1);
  132.     if(currentLevel!=lastLevel)
  133.         buzzerStartTime=millis();
  134.     if(millis()-buzzerStartTime<=30000)
  135.         digitalWrite(buzzerPin,1);
  136.     else
  137.         digitalWrite(buzzerPin,0);
  138.     digitalWrite(bigLEDPin,1);
  139.     lastLevel=1;
  140.   }else
  141.   {
  142.     buzzerStartTime=millis();
  143.     showNumber(0);
  144.     digitalWrite(buzzerPin,0);
  145.     digitalWrite(bigLEDPin,0);
  146.   }
  147.   if(digitalRead(btnInput)==1)
  148.       buzzerStartTime-=30000;
  149.   delay(100);
  150. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

返回列表