-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoolllectarray.py
More file actions
95 lines (82 loc) · 2.95 KB
/
coolllectarray.py
File metadata and controls
95 lines (82 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import paho.mqtt.client as mqtt
import json
import time
from sense_hat import SenseHat
from datetime import datetime
from collections import deque
# from array import *
import numpy as np
import pandas
import sqlite3
# from statistics import mean
sense = SenseHat()
# Initialize deque with 120 None values
data_window = deque([], maxlen=120)
data_week = deque([], maxlen=840)
data_month = deque([], maxlen=3360)
# MQTT broker details
broker_address = "localhost" # Use the address of your local MQTT broker
port = 1883 # Default MQTT port
temp1 = float()
# Create an MQTT client instance
client = mqtt.Client("csv_publisher")
conn = sqlite3.connect('sensor_data.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS sensor_data
(id INTEGER PRIMARY KEY AUTOINCREMENT,
temperature REAL,
humidity REAL,
pressure REAL,
timestamp TEXT)''')
# Connect to the MQTT broker
client.connect(broker_address, port)
cursor = conn.cursor()
# Define the topic to which you want to publish
topic = "data1"
start_time = time.time()
duration = 400
def calculate_rolling_averagemean(data):
total = sum(data)
num1 = len(data)
aveMean = total / num1
# ave1 = mean([data_window])
# print(ave)
return aveMean
# Run the while loop for the specified duration
while (time.time() - start_time) < duration:
data_window.appendleft(sense.get_temperature())
data_week.appendleft(sense.get_temperature())
data_month.appendleft(sense.get_temperature())
data1 = {"temp": str(sense.get_temperature()),
"Humidity": str(sense.get_humidity()),
"Date": str(datetime.now()),
"Ave1": str(calculate_rolling_averagemean(data_window)),
"AveWeek": str(calculate_rolling_averagemean(data_week)),
"AveMonth": str(calculate_rolling_averagemean(data_month)),
}
# Collect sensor data
temperature = sense.get_temperature()
humidity = sense.get_humidity()
pressure = sense.get_pressure()
timestamp = datetime.now().isoformat()
# Insert data into the table
cursor.execute('''INSERT INTO sensor_data (temperature, humidity, pressure, timestamp)
VALUES (?, ?, ?, ?)''', (temperature, humidity, pressure, timestamp))
# Commit changes to the database
conn.commit()
print("Saved sensor data:", temperature, humidity, pressure, timestamp)
# Convert dictionary to JSON string
message = json.dumps(data1)
ave2 = calculate_rolling_averagemean(data_week)
# print(ave2)
ave3 = calculate_rolling_averagemean(data_month)
# print (ave3)
client.publish(topic, message)
print(f"Published: {message} to topic: {topic}")
# Sleep for a short duration if needed
time.sleep(1)
rolling_avg = calculate_rolling_averagemean(data_window)
print(f"Calculted Rolling averages: {data1}")
# Disconnect from the MQTT broker
else:
client.disconnect()