Jan 20, 2026
Log_sht31_remote_influx1.py
190 
2026-01-20 23:16 +0000
import serial
import time
import requests
# === InfluxDB v1 Settings ===
INFLUXDB_URL = "http://192.168.0.21:8086" # IP of InfluxDB Pi
INFLUXDB_DB = "emon03"
INFLUXDB_USER = "admin"
INFLUXDB_PASS = "bucket"
# === Serial Settings ===
SERIAL_PORT = "/dev/ttyACM0" # or /dev/ttyUSB0
BAUD_RATE = 9600
def parse_line(line):
"""Parse a line like: 'TEMP:25.27,HUM:55.90'"""
try:
parts = line.strip().split(",")
temperature = float(parts[0].split(":")[1])
humidity = float(parts[1].split(":")[1])
return temperature, humidity
except Exception as e:
print(f"⚠️ Parse error: {e} — Line: {line.strip()}")
return None, None
def send_to_influx(temperature, humidity):
"""Send data to InfluxDB v1 using Line Protocol"""
line = f"sht31_data,sensor_id=sht31_1 temperature={temperature},humidity={humidity}"
try:
response = requests.post(
f"{INFLUXDB_URL}/write",
params={
"db": INFLUXDB_DB,
"u": INFLUXDB_USER,
"p": INFLUXDB_PASS
},
data=line,
timeout=5
)
if response.status_code == 204:
print("✅ Data written to InfluxDB")
else:
print(f"❌ InfluxDB write failed: {response.status_code} - {response.text}")
except Exception as e:
print(f"❌ Connection error: {e}")
def main():
print(f"🔌 Opening serial port {SERIAL_PORT}...")
with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=10) as ser:
while True:
try:
raw = ser.readline().decode("utf-8").strip()
if raw:
print(f"📡 Received: {raw}")
temp, hum = parse_line(raw)
if temp is not None and hum is not None:
send_to_influx(temp, hum)
time.sleep(2)
except Exception as e:
print(f"⚠️ Error reading from serial: {e}")
time.sleep(60)
if __name__ == "__main__":
main()