import network
import time
import ujson
import urequests
# ===== KONFIGURASI WI-FI =====
WIFI_SSID = "Andier"
WIFI_PASSWORD = "kucobaLG"
# Ganti dengan URL HTTPS Quick Tunnel milik Anda.
BASE_URL = "https://blocks-touch-controlling-trainers.trycloudflare.com/docs"
TELEMETRY_URL = BASE_URL + "/telemetry"
STATUS_URL = BASE_URL + "/status"
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print("Menghubungkan ke Wi-Fi...")
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
timeout = 20
while not wlan.isconnected() and timeout > 0:
time.sleep(1)
timeout -= 1
if wlan.isconnected():
print("Wi-Fi terhubung:", wlan.ifconfig()[0])
return True
print("Wi-Fi gagal terhubung")
return False
def send_telemetry(level):
response = None
try:
payload = {"level": level}
response = urequests.post(
TELEMETRY_URL,
data=ujson.dumps(payload),
headers={"Content-Type": "application/json"}
)
print("POST telemetry:", response.status_code)
print(response.text)
except Exception as e:
print("Gagal mengirim telemetry:", e)
finally:
if response is not None:
response.close()
def read_status():
response = None
try:
response = urequests.get(STATUS_URL)
print("GET status:", response.status_code)
print(response.text)
except Exception as e:
print("Gagal membaca status:", e)
finally:
if response is not None:
response.close()
if connect_wifi():
while True:
# Nilai simulasi; nantinya ganti dengan
# pembacaan sensor level tandon sebenarnya.
level_air = 20
send_telemetry(level_air)
read_status()
time.sleep(10)
print("Hello, ESP32!")
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4