Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <WiFiClient.h>
- #define TRIGGER_PIN D1
- #define ECHO_PIN D2
- long duration, waterlevel;
- // INSERT WIFI CREDENTIALS
- const char* ssid = "converge-2.4g";
- const char* password = "Sierra911";
- // Your Domain name with URL path or IP address with path
- const char* serverName = "https://us-central1-floodmonitor-474e0.cloudfunctions.net/devices";
- void setup() {
- Serial.begin(9600);
- pinMode(TRIGGER_PIN, OUTPUT);
- pinMode(ECHO_PIN, INPUT);
- // CONNECTING TO WIFI
- int attempts = 0;
- Serial.print("Connecting to Wi-Fi...");
- while (WiFi.status() != WL_CONNECTED && attempts < 10) {
- attempts++;
- WiFi.begin(ssid, password);
- delay(1000);
- Serial.print(".");
- }
- }
- void loop() {
- //INSERT DEVICEID AND LATITUDE AND LONGITUDE (NO LAT AND LONG SINCE NO GPS MODULE IS USED)
- if (WiFi.status() == WL_CONNECTED) {
- Serial.println("Still Connected");
- } else {
- Serial.println("Reconnecting");
- int attempts = 0;
- Serial.print("Connecting to Wi-Fi...");
- while (WiFi.status() != WL_CONNECTED && attempts < 10) {
- attempts++;
- WiFi.begin(ssid, password);
- delay(1000);
- Serial.print(".");
- }
- }
- int deviceID = 1;
- String lat = "14.5784832";
- String longitude = "121.1367424";
- String flooded = "";
- // GETTING LEVEL OF FLOOD
- waterlevel = ultrasonic_distance();
- Serial.print("Flood Level: ");
- Serial.println(waterlevel);
- // CHANGE VALUE TO DETERMINE HOW HIGH IF ITS FLOODED OR NOT
- bool flood = (waterlevel >= 50);
- if (flood == 1) {
- Serial.println("The Area is Flooded.");
- flooded = "true";
- } else {
- Serial.println("The Area is not Flooded.");
- flooded = "false";
- }
- HTTPClient http;
- WiFiClientSecure client;
- client.setInsecure();
- // Your Domain name with URL path or IP address with path
- http.begin(client,serverName);
- // HTTP POST (SEND JSON TO DATABASE)
- http.addHeader("Content-Type", "application/json");
- String payload = "{\"deviceId\":\"" + String(deviceID) + "\",\"lat\":\"" + String(lat) + "\",\"long\":\"" + String(longitude) + "\",\"waterLevel\":\"" + String(waterlevel) + "\",\"flooded\":\"" + String(flooded) + "\"}"; ;
- int httpResponseCode = http.POST(payload);
- // IF RESPONSE CODE IS LESS THAN 0 THEN DATA IS NOT BEING SENT
- Serial.print("HTTP Response code: ");
- Serial.println(httpResponseCode);
- //Serial.print("Data Being Sent: ");
- // CREATE A STRING TO SEND TO THE ARDUINO
- String SMS = "Device ID: "+String(deviceID) +" Water Level : "+String(waterlevel)+ " Area: Dito Flooded: " + String(flooded);
- // PUT DATA IN SERIAL MONITOR TO BE SENT TO ARDUINO TO BE SENT IN AN SMS
- Serial.println(SMS);
- //STARTING TO SEND SMS
- Serial.print("\r");
- delay(10000);
- Serial.print("AT+CMGF=1\r");
- delay(1000);
- /*Replace XXXXXXXXXX to 10 digit mobile number & ZZ to 2 digit country code*/
- Serial.print("AT+CMGS=\"09171367340\"\r");
- //The text of the message to be sent.
- Serial.print(String(SMS));
- delay(1000);
- Serial.write(0x1A);
- delay(1000);
- // Free resources
- http.end();
- // Wait for 1 second
- delay(30000);
- }
- long ultrasonic_distance() {
- // Send a 10 microsecond pulse to the trigger pin
- // HC-SR04 VCC -> NodeMCU 5V
- // HC-SR04 GND -> NodeMCU GND
- // HC-SR04 TRIG -> NodeMCU D1
- // HC-SR04 ECHO -> NodeMCU D2
- digitalWrite(TRIGGER_PIN, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIGGER_PIN, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIGGER_PIN, LOW);
- // Measure the duration of the echo pulse
- duration = pulseIn(ECHO_PIN, HIGH);
- // Convert the duration to a distance
- waterlevel = duration * 0.034 / 2;
- // Return the distance measurement
- return waterlevel;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement