0% found this document useful (0 votes)
4 views2 pages

NODEMCU

This document contains an Arduino sketch for an ESP8266 microcontroller that connects to a WiFi network and uses an ultrasonic sensor to measure distance. The measured distance is sent to a Flask server via HTTP GET requests. The code includes setup for WiFi connection and the ultrasonic sensor, as well as a loop that continuously measures distance and sends the data to the server every two seconds.

Uploaded by

mrvnd002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

NODEMCU

This document contains an Arduino sketch for an ESP8266 microcontroller that connects to a WiFi network and uses an ultrasonic sensor to measure distance. The measured distance is sent to a Flask server via HTTP GET requests. The code includes setup for WiFi connection and the ultrasonic sensor, as well as a loop that continuously measures distance and sends the data to the server every two seconds.

Uploaded by

mrvnd002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <ESP8266WiFi.

h>

#include <ESP8266HTTPClient.h>

// Konfigurasi WiFi

const char* ssid = "Nama_WiFi";

const char* password = "Password_WiFi";

// Konfigurasi Sensor Ultrasonik

#define TRIGGER_PIN D1

#define ECHO_PIN D2

void setup() {

Serial.begin(115200);

pinMode(TRIGGER_PIN, OUTPUT);

pinMode(ECHO_PIN, INPUT);

// Sambungkan ke WiFi

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);

Serial.println("Connecting to WiFi...");

Serial.println("Connected to WiFi");

void loop() {

long duration, distance;

// Kirim sinyal ke trigger


digitalWrite(TRIGGER_PIN, LOW);

delayMicroseconds(2);

digitalWrite(TRIGGER_PIN, HIGH);

delayMicroseconds(10);

digitalWrite(TRIGGER_PIN, LOW);

// Hitung waktu pantulan

duration = pulseIn(ECHO_PIN, HIGH);

distance = (duration * 0.034) / 2; // Konversi ke cm

// Kirim data ke server Flask

if (WiFi.status() == WL_CONNECTED) {

HTTPClient http;

String serverPath = "http://<IP_FLASK>:5000/monitoring"; // Ganti <IP_FLASK> dengan IP komputer


Flask

http.begin(serverPath + "?distance=" + String(distance));

int httpResponseCode = http.GET();

if (httpResponseCode > 0) {

Serial.println(http.getString());

http.end();

delay(2000);

You might also like