import network
import urequests
import time
from machine import Pin, I2C
from mpu6050 import MPU6050
# WiFi (Wokwi default)
ssid = "Wokwi-GUEST"
password = ""
# IMPORTANT:
# Use HTTP (not HTTPS)
INFLUX_URL = "http://governessy-unchaffed-benton.ngrok-free.dev/write?db=factory"
# I2C setup
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
mpu = MPU6050(i2c)
# Connect WiFi
print("Connecting WiFi...")
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, password)
while not wifi.isconnected():
time.sleep(1)
print("WiFi Connected:", wifi.ifconfig())
print("Edge-based Machine Monitoring Started")
while True:
# Read acceleration
ax, ay, az = mpu.get_accel()
# Calculate vibration level
vibration = abs(ax) + abs(ay) + abs(az)
# Detect abnormal vibration
if vibration > 3:
alert = 1
print("ALERT: Abnormal vibration detected!")
else:
alert = 0
# Prepare InfluxDB data
data = "machine "
data += "vibration={},alert={}".format(vibration, alert)
print("Sending:", data)
# Send to InfluxDB
try:
response = urequests.post(INFLUX_URL, data=data)
print("HTTP status:", response.status_code)
response.close()
except Exception as e:
print("Send error:", e)
time.sleep(5)