import network
import urequests
import time
import machine
import ssd1306
import random
# KONFIGURASI WIFI
SSID = "Wokwi-GUEST"
PASSWORD = ""
# KONFIGURASI UBIDOTS
TOKEN = "BBUS-0oOW64dLa4i4ityElQQJ2CfKmdw1Xg" # SENSORVERSE A
HEADERS = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}
DEVICE = "sensorverse-32"
UBIDOTS_URL = f"http://industrial.api.ubidots.com/api/v1.6/devices/{DEVICE}"
# KONFIGURASI OLED (I2C)
i2c = machine.SoftI2C(scl=machine.Pin(22), sda=machine.Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# KONFIGURASI LED BUILT-IN (Pin 2)
led = machine.Pin(2, machine.Pin.OUT)
# PIN AKTUATOR
servo = machine.PWM(machine.Pin(25), freq=50)
relay = machine.Pin(23, machine.Pin.OUT)
buzzer = machine.PWM(machine.Pin(26))
rgb_red = machine.PWM(machine.Pin(19))
rgb_green = machine.PWM(machine.Pin(18))
rgb_blue = machine.PWM(machine.Pin(5))
# KONEKSI WIFI
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
print("Menghubungkan ke WiFi...", end="")
while not wlan.isconnected():
print(".", end="")
time.sleep(1)
print("\nWiFi Terhubung:", wlan.ifconfig())
# 🔥 FUNGSI AMBIL DATA AKTUATOR UBIDOTS (Fix Float-to-Int)
def get_actuator_value(variable):
url = f"http://industrial.api.ubidots.com/api/v1.6/devices/{DEVICE}/{variable}"
try:
response = urequests.get(url, headers=HEADERS)
data = response.json()
response.close()
value = data.get("last_value", {}).get("value", None)
print(f"📥 {variable}: {value}")
return int(value) if value is not None else 0 # 🔥 FIX: Ubah ke integer
except Exception as e:
print(f"❌ ERROR mengambil {variable}: {e}")
return 0
# 🔥 UPDATE STATUS AKTUATOR
def update_actuators():
servo_angle = get_actuator_value("Servo-angle")
relay_status = get_actuator_value("Relay")
buzzer_status = get_actuator_value("Buzzer")
rgb_r = get_actuator_value("RGB-Red")
rgb_g = get_actuator_value("RGB-Green")
rgb_b = get_actuator_value("RGB-Blue")
# Servo PWM (0° - 180° → Duty 0 - 1023)
servo.duty(int((servo_angle / 180) * 1023))
# Relay
relay.value(1 if relay_status == 1 else 0)
# Buzzer (1000 Hz kalau ON, OFF kalau 0)
if buzzer_status == 1:
buzzer.freq(1000)
buzzer.duty(512)
else:
buzzer.duty(0)
# RGB LED (0 - 255 → Duty 0 - 1023)
rgb_red.duty(int((rgb_r / 255) * 1023))
rgb_green.duty(int((rgb_g / 255) * 1023))
rgb_blue.duty(int((rgb_b / 255) * 1023))
print(f"✅ Aktuator Diperbarui: Servo={servo_angle}, Relay={relay_status}, Buzzer={buzzer_status}, RGB=({rgb_r}, {rgb_g}, {rgb_b})")
# 🔥 KIRIM DATA SENSOR KE UBIDOTS
def send_to_ubidots():
led.off()
data = {
"DHT-1-Temp": {"value": round(random.uniform(25.0, 32.0), 1)},
"DHT-1-Humid": {"value": round(random.uniform(40, 70), 1)},
"DHT-2-Temp": {"value": round(random.uniform(25.0, 32.0), 1)},
"DHT-2-Humid": {"value": round(random.uniform(40, 70), 1)},
"Ultrasonic-Distance": {"value": random.randint(5, 150)},
"Pir-Motion": {"value": random.choice([0, 1])},
"LDR-Intensity": {"value": random.randint(200, 900)},
"RGB-Red": {"value": random.randint(0, 255)},
"RGB-Green": {"value": random.randint(0, 255)},
"RGB-Blue": {"value": random.randint(0, 255)},
}
success = False
try:
response = urequests.post(UBIDOTS_URL, json=data, headers=HEADERS)
if response.status_code in [200, 201]:
print("✅ SENSORVERSE A: Data berhasil dikirim!")
success = True
response.close()
except Exception as e:
print(f"❌ ERROR saat mengirim ke SENSORVERSE A: {e}")
update_actuators()
# LED Indikator
if success:
led.on()
else:
led.off()
# 🔥 CEK KONEKSI WIFI
def check_wifi():
wlan = network.WLAN(network.STA_IF)
if not wlan.isconnected():
print("⚠️ WiFi terputus! Reconnecting...")
connect_wifi()
# 🔥 MAIN PROGRAM
connect_wifi()
while True:
check_wifi()
send_to_ubidots()
time.sleep(5)