Advertisement
rsbelza

NODEMCU ULTRASONIC

Feb 13th, 2023
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.80 KB | Source Code | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266HTTPClient.h>
  3. #include <WiFiClient.h>
  4. #define TRIGGER_PIN D1
  5. #define ECHO_PIN D2
  6.  
  7.   long duration, waterlevel;
  8.  
  9.   // INSERT WIFI CREDENTIALS
  10.   const char* ssid = "WIFI USER";
  11.   const char* password = "WIFI PASS";
  12.   // Your Domain name with URL path or IP address with path
  13.   const char* serverName = "WEBSITE URL";
  14.  
  15. void setup() {
  16.   Serial.begin(115200);
  17.   pinMode(TRIGGER_PIN, OUTPUT);
  18.   pinMode(ECHO_PIN, INPUT);
  19.  
  20.   // CONNECTING TO WIFI
  21.   WiFi.begin(ssid, password);
  22.   Serial.println("Connecting");
  23.   while(WiFi.status() != WL_CONNECTED) {
  24.     delay(500);
  25.     Serial.print(".");
  26.   }
  27.   Serial.println("");
  28.   Serial.print("Connected to WiFi network with IP Address: ");
  29.   Serial.println(WiFi.localIP());
  30.  
  31.  
  32. }
  33.  
  34. void loop() {
  35.   //INSERT DEVICEID AND LATITUDE AND LONGITUDE (NO LAT AND LONG SINCE NO GPS MODULE IS USED)
  36.   int deviceID = 5;
  37.   String lat = "14.627656";
  38.   String longitude = "121.070773";
  39.   String flooded = "";
  40.  
  41.   // GETTING LEVEL OF FLOOD
  42.   waterlevel = ultrasonic_distance();
  43.   Serial.print("Flood Level: ");
  44.   Serial.println(waterlevel);
  45.  
  46.   //  CHANGE VALUE TO DETERMINE HOW HIGH IF ITS FLOODED OR NOT
  47.   bool flood = (waterlevel >= 50);
  48.   if (flood == 1) {
  49.   Serial.println("The Area is Flooded.");
  50.   flooded = "true";
  51.   } else {
  52.   Serial.println("The Area is not Flooded.");
  53.     flooded = "false";
  54.   }
  55.  
  56.  
  57.   HTTPClient http;
  58.   WiFiClientSecure client;
  59.   client.setInsecure();
  60.      
  61.   // Your Domain name with URL path or IP address with path
  62.   http.begin(client,serverName);
  63.  
  64.      
  65.   // HTTP POST (SEND JSON TO DATABASE)
  66.   http.addHeader("Content-Type", "application/json");
  67.   String payload = "{\"deviceId\":\"" + String(deviceID) + "\",\"lat\":\"" + String(lat) + "\",\"long\":\"" + String(longitude) + "\",\"waterLevel\":\"" + String(waterlevel) + "\",\"flooded\":\"" + String(flooded) + "\"}"; ;
  68.   int httpResponseCode = http.POST(payload);
  69.      
  70.   // IF RESPONSE CODE IS LESS THAN 0 THEN DATA IS NOT BEING SENT  
  71.   Serial.print("HTTP Response code: ");
  72.   Serial.println(httpResponseCode);
  73.  
  74.   Serial.print("Data Being Sent: ");
  75.   Serial.println(payload);
  76.        
  77.   // Free resources
  78.   http.end();
  79.  
  80.   delay(60000);
  81. }
  82.  
  83. long ultrasonic_distance() {
  84.   // Send a 10 microsecond pulse to the trigger pin
  85.   // HC-SR04 VCC -> NodeMCU 5V
  86.   // HC-SR04 GND -> NodeMCU GND
  87.   // HC-SR04 TRIG -> NodeMCU D1
  88.   // HC-SR04 ECHO -> NodeMCU D2
  89.   digitalWrite(TRIGGER_PIN, LOW);
  90.   delayMicroseconds(2);
  91.   digitalWrite(TRIGGER_PIN, HIGH);
  92.   delayMicroseconds(10);
  93.   digitalWrite(TRIGGER_PIN, LOW);
  94.  
  95.   // Measure the duration of the echo pulse
  96.   duration = pulseIn(ECHO_PIN, HIGH);
  97.  
  98.   // Convert the duration to a distance
  99.   waterlevel = duration * 0.034 / 2;
  100.  
  101.   // Return the distance measurement
  102.   return waterlevel;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement