jdz/TEMPMOSQUITTOGPS.py

118 lines
3.6 KiB
Python
Raw Permalink Normal View History

2024-07-05 15:05:28 +00:00
#!/usr/bin/python
# -*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import minimalmodbus
import serial
import time
import random
import paho.mqtt.client as mqtt
# Modbus Configuration
mb_address = 1
sensy_boi = minimalmodbus.Instrument('/dev/ttyUSB0', mb_address)
sensy_boi.serial.baudrate = 9600
sensy_boi.serial.bytesize = 8
sensy_boi.serial.parity = minimalmodbus.serial.PARITY_NONE
sensy_boi.serial.stopbits = 1
sensy_boi.serial.timeout = 0.5
sensy_boi.mode = minimalmodbus.MODE_RTU
# GPIO Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
# MQTT Configuration
broker_address = "65.108.199.212" # IP or hostname
port = 1883
topic = "iiot"
def on_publish(client, userdata, result):
print("Data published!")
def send_at(command, expected_response, timeout):
ser.write((command + '\r\n').encode())
time.sleep(timeout)
rec_buff = ser.read(ser.inWaiting()).decode()
if expected_response in rec_buff:
print(rec_buff)
return True
else:
print(f"{command} ERROR: {rec_buff}")
return False
def get_gps_position():
print("Initializing GPS...")
if send_at("AT+CGPS=1,1", "OK", 1):
while True:
if send_at("AT+CGPSINFO", "+CGPSINFO: ", 1):
if ",,,,,," in rec_buff:
print("GPS not acquired")
break
else:
print(f"Error getting GPS data: {rec_buff}")
send_at("AT+CGPS=0", "OK", 1)
break
time.sleep(1.5)
def power_on(power_key):
print('SIM7600X is starting...')
GPIO.setup(power_key, GPIO.OUT)
GPIO.output(power_key, GPIO.HIGH)
time.sleep(2)
GPIO.output(power_key, GPIO.LOW)
time.sleep(20)
ser.flushInput()
print('SIM7600X is ready')
def power_down(power_key):
print('SIM7600X is logging off...')
GPIO.output(power_key, GPIO.HIGH)
time.sleep(3)
GPIO.output(power_key, GPIO.LOW)
time.sleep(18)
print('Goodbye')
def write_to_influxdb(measurement, tags, fields):
point = influxdb_client.Point(measurement).tag(**tags).field(**fields)
write_api = client.write_api(write_options=SYNCHRONOUS)
write_api.write(bucket=bucket, record=point)
def read_sensor_data():
try:
client = mqtt.Client("iiot")
client.on_publish = on_publish
client.connect(broker_address, port)
while True:
if GPIO.input(17) == GPIO.HIGH:
data = sensy_boi.read_registers(0, 2, 3)
humidity = data[0] / 10
temperature = data[1] / 10
print("-------------------------------------")
print(f"Temperature: {temperature}°C")
print(f"Humidity: {humidity}%")
print("-------------------------------------")
payload = f"Temperature: {temperature}°C, Humidity: {humidity}%"
client.publish(topic, payload)
# Prepare data for InfluxDB
measurement_name = "sensor_data"
location_tag = "your_location" # Replace with actual location
fruit_tag = "your_fruit" # Replace with actual fruit
id_tag = "your_id" # Replace with actual ID
write_to_influxdb(
measurement=measurement_name,
tags={
"location": location_tag,
"fruit": fruit_tag,
"id": id_tag,
},
fields={
"temperature": temperature, # Corrected variable name
"humidity": humidity, # Add humidity field if needed
}
)