本帖最後由 蕭澧邦 於 2019-3-27 13:19 編輯
更新資訊:
1.回傳值中文化
2.接收端蜂鳴器間隔音
提醒! 上傳完畢後請重新啟動Arduino板,因為試驗時發現若未重啟開發板,斷網時會當機!
偵測端 Detector- //請於下方提示處,分別填入您的WiFi名稱與密碼,完成後重新上傳至開發版。
- #define wifiName "TonysASUS" //your wifi name
- #define wifiPass "myhotspot" //your wifi password
- //#define wifiName "KITA" //your wifi name
- //#define wifiPass "t3893527" //your wifi password
- #include <Esp.h>
- #include <ESP8266WiFi.h>
- #include <FirebaseArduino.h>
- #define firebaseURL "floodingalarm.firebaseio.com"
- #define authCode "2gfMkb8d6BZw0iLmj7AdLqB40UtIzFZjEDxRjvLv"
- int connectionStatusPin=D9;
- int waterPin=A0;
- int sensorValue;
- void setupFirebase()
- {
- Firebase.begin(firebaseURL,authCode);
- }
- void setupWiFi()
- {
- WiFi.begin(wifiName,wifiPass);
- Serial.println();
- Serial.println();
- Serial.println("Connecting to WiFi");
- while(WiFi.status()!=WL_CONNECTED)
- {
- Serial.print(".");
- delay(500);
- }
- Serial.println();
- Serial.print("Connected to ");
- Serial.println(wifiName);
- Serial.print("IP: ");
- Serial.println(WiFi.localIP());
- digitalWrite(connectionStatusPin,1);
- }
- void setup() {
- Serial.begin(115200);
- pinMode(connectionStatusPin,OUTPUT);
- setupWiFi();
- setupFirebase();
- }
- void loop() {
- sensorValue = analogRead(waterPin);
- Serial.println(sensorValue);
- Firebase.setInt("number", sensorValue);
- //當與 Firebase 通訊失敗時,將所有數值歸零,接著重啟開發版
- if (Firebase.failed()) {
- Serial.print("setting data failed!");
- digitalWrite(connectionStatusPin,0);
- ESP.restart();
- }
-
- delay(100);
- }
複製代碼
接收端 Alarm- //請於下方提示處,分別填入您的WiFi名稱與密碼,完成後重新上傳至開發版。
- #define wifiName "TonysASUS" //your wifi name
- #define wifiPass "myhotspot" //your wifi password
- //#define wifiName "KITA" //your wifi name
- //#define wifiPass "t3893527" //your wifi password
- #include <Esp.h>
- #include <ESP8266WiFi.h>
- #include <FirebaseArduino.h>
- #define firebaseURL "floodingalarm.firebaseio.com"
- #define authCode "2gfMkb8d6BZw0iLmj7AdLqB40UtIzFZjEDxRjvLv"
- int numberLED[]={D2,D3,D4,D5,D13,D8};
- int btnInput=D6;
- int connectionStatusPin=D9;
- int bigLEDPin=D10;
- int buzzerPin=D11;
- int buzzerStartTime;
- int currentLevel, lastLevel;
- int numberGot;
- int alertValue1 = 50; //一級警報
- int alertValue2 = 600; //二級警報
- int alertValue3 = 900; //三級警報
- void showNumber(int n)
- {
- for(int i=0; i<sizeof(numberLED)/sizeof(int); i++)
- digitalWrite(numberLED[i],0);
- if(n==1)
- {
- digitalWrite(D4,1);
- digitalWrite(D8,1);
- }
- if(n==2)
- {
- digitalWrite(D2,1);
- digitalWrite(D3,1);
- digitalWrite(D5,1);
- digitalWrite(D13,1);
- digitalWrite(D8,1);
- }
- if(n==3)
- {
- digitalWrite(D3,1);
- digitalWrite(D4,1);
- digitalWrite(D5,1);
- digitalWrite(D13,1);
- digitalWrite(D8,1);
- }
- }
- void setupFirebase()
- {
- Firebase.begin(firebaseURL,authCode);
- }
- void setupWiFi()
- {
- WiFi.begin(wifiName,wifiPass);
- Serial.println();
- Serial.println();
- Serial.println("Connecting to WiFi");
- while(WiFi.status()!=WL_CONNECTED)
- {
- Serial.print(".");
- delay(500);
- }
- Serial.println();
- Serial.print("Connected to ");
- Serial.println(wifiName);
- Serial.print("IP: ");
- Serial.println(WiFi.localIP());
- digitalWrite(connectionStatusPin,1);
- }
- void setup() {
- Serial.begin(115200);
- pinMode(connectionStatusPin,OUTPUT);
- pinMode(bigLEDPin,OUTPUT);
- pinMode(buzzerPin,OUTPUT);
-
- for(int i=0; i<sizeof(numberLED)/sizeof(int); i++)
- {
- pinMode(numberLED[i],OUTPUT);
- }
-
- pinMode(btnInput,INPUT);
-
- setupWiFi();
- setupFirebase();
- }
- void loop() {
- numberGot=Firebase.getInt("number");
- Serial.print("number: ");
- Serial.println(numberGot);
- //當與 Firebase 通訊失敗時,將所有數值歸零,接著重啟開發版
- if (Firebase.failed()) {
- Serial.print("getting data failed!");
- digitalWrite(connectionStatusPin,0);
- numberGot==0;
- digitalWrite(buzzerPin,0);
- digitalWrite(bigLEDPin,0);
- showNumber(0);
- ESP.restart();
- }
-
- if(numberGot >= alertValue3)
- {
- currentLevel=3;
- Serial.println("三級警報 - 淹水了!");
- showNumber(3);
- if(currentLevel!=lastLevel)
- buzzerStartTime=millis();
- if(millis()-buzzerStartTime<=30000)
- digitalWrite(buzzerPin,1);
- else
- digitalWrite(buzzerPin,0);
- digitalWrite(bigLEDPin,1);
- lastLevel=3;
- }else if(numberGot >= alertValue2)
- {
- currentLevel=2;
- Serial.println("二級警報 - 淹水了!");
- showNumber(2);
- if(currentLevel!=lastLevel)
- buzzerStartTime=millis();
- if(millis()-buzzerStartTime<=30000)
- digitalWrite(buzzerPin,1);
- else
- digitalWrite(buzzerPin,0);
- digitalWrite(bigLEDPin,1);
- lastLevel=2;
- }else if(numberGot >= alertValue1)
- {
- currentLevel=1;
- Serial.println("一級警報 - 淹水了!");
- showNumber(1);
- if(currentLevel!=lastLevel)
- buzzerStartTime=millis();
- if(millis()-buzzerStartTime<=30000)
- digitalWrite(buzzerPin,1);
- else
- digitalWrite(buzzerPin,0);
- digitalWrite(bigLEDPin,1);
- lastLevel=1;
- }else
- {
- buzzerStartTime=millis();
- showNumber(0);
- digitalWrite(buzzerPin,0);
- digitalWrite(bigLEDPin,0);
- }
- if(digitalRead(btnInput)==1)
- buzzerStartTime-=30000;
- delay(100);
- }
複製代碼 |