-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarch3rd.py
More file actions
167 lines (155 loc) · 4.95 KB
/
march3rd.py
File metadata and controls
167 lines (155 loc) · 4.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# This is my "main.py" micropython program, it needs to be so smooth that it will run and run for weeks at a time while the pico has power.
# It connects to the internet, collects data and I'd like it to connect to the adafruit IOT ckoud server but there is a bug in the micropython firmware that
# won't let it connect to a public network (and I'm smart enough to fix it - yet). So maybe mext week I'll convet this program into circuit python which should be
# compatible.
import time
from helper import GetTemperature, connect_to_wifi, WifiServer, to_json, collect_sensor_data, write_to_csv, calculate_mean
import usocket
import socket
from time import time, sleep
import network
import time
from umqtt.simple import MQTTClient
import machine
from picozero import pico_temp_sensor
from machine import Pin
import micropython
import ustruct as struct
import json
import random
import socket
import uos
import time
from machine import I2C, Pin
from I2C_LCD import I2CLcd
from collections import deque
from LCD_API import LcdApi
#(AIO_USER, AIO_SERVER, port=AIO_PORT, user=AIO_USER, password=AIO_KEY)
AIO_SERVER = b'io.adafruit.com'
AIO_PORT = 1883
AIO_USER = b'CuthbertB'
AIO_KEY = b'Secret_KEY!!!'
AIO_FEED = b'datalog'
dq = deque((), 100)
#bbutton = Pin(16, Pin.IN, Pin.PULL_UP)
Self_Name = 'PW_1'
ssidAP = 'CuthbertWifi' #Enter the router name
passwordAP = '999' # Enter the router password
local_IP = '192.168.1.1'
gateway = '192.168.1.1'
subnet = '255.255.255.0'
dns = '8.8.8.8'
sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)
adcpin = 4
sensor = machine.ADC(adcpin)
sock = usocket
SSID = 'BB'
PASSWORD = '6KH1jk1mn0s'
i2c = I2C(1, sda=Pin(14), scl=Pin(15), freq=400000)
devices = i2c.scan()
#lcd = I2CLcd(i2c, devices[0], 2, 16)
#lcd.display_off
USER = "Cuthbert"
wlan = network.WLAN(network.STA_IF)
TOPIC = "test_data"
#mqtt = MQTTClient
#sending = broker_address
#BROKER = "192.168.4.16"
#PORT = 1883 # Topic to publish to
#CLIENT_ID = "Cuthbert"
Temperature = GetTemperature()
timestamp = time.time()
csv_data = collect_sensor_data()
print(csv_data)
def send_data(value):
client = MQTTClient(AIO_USER, AIO_SERVER, port=AIO_PORT, user=AIO_USER, password=AIO_KEY)
client.connect()
client.publish('{}.feeds.{}'.format(AIO_USER, AIO_FEED), str(value))
client.disconnect()
def LCD_show(csv_data):
i2c = I2C(1, sda=Pin(14), scl=Pin(15), freq=400000)
devices = i2c.scan()
if devices != []:
Icd = I2CLcd(i2c, devices[0], 2, 16)
Icd.move_to(0, 0)
string_temp = str(csv_data)
Icd.putstr(csv_data)
# Icd.move_to(0, 1)
# Icd.putstr()
time.sleep(10)
Icd.display_off
else:
print("No address found")
def calculate_mean(dq):
calculate = sum(dq) / len(dq)
return calculate
start_time = time.time()
duration = 4000
"""
while True:
if not button.value():
LCD_show(str(Temperature), i2c, devices)
else:
pass
"""
try:
print("Connecting to MQTT broker...")
# client.connect()
dq.appendleft(Temperature)
print("Connected to MQTT broker.")
dq.appendleft(Temperature)
mean = calculate_mean(dq)
print(mean)
while (time.time() - start_time) < duration:
LCD_show(str(csv_data))
Temperature = GetTemperature()
timestamp = time.time()
# send_data(GetTemperature())
csv_data = collect_sensor_data()
write_to_csv(csv_data)
dq.appendleft(Temperature)
print(list(dq))
mean = calculate_mean(dq)
print(mean)
LCD_show(str(csv_data))
print(csv_data)
# send_data(csv_data)
# client.publish(TOPIC, json_result)
time.sleep(10) # Publish every 5 seconds
Temperature = GetTemperature()
csv_data = collect_sensor_data()
write_to_csv(csv_data)
timestamp = time.time()
# json_result = to_json(Self_Name, Temperature,timestamp)
dq.appendleft(Temperature)
print(list(dq))
print(csv_data)
# client.publish(TOPIC, json_result)
time.sleep(10)
Temperature = GetTemperature()
timestamp = time.time()
# json_result = to_json(Self_Name, Temperature, timestamp)
csv_data = collect_sensor_data()
write_to_csv(csv_data)
dq.appendleft(Temperature)
print(list(dq))
mean = calculate_mean(dq)
print(mean)
LCD_show(str(csv_data))
print(csv_data)
# client.publish(TOPIC, json_result)
Temperature = GetTemperature()
timestamp = time.time()
# json_result = to_json(Self_Name, Temperature, timestamp)
csv_data = collect_sensor_data()
write_to_csv(csv_data)
dq.appendleft(Temperature)
print(list(dq))
print(csv_data)
# client.publish(TOPIC, json_result)
# send_data(csv_data)
except Exception as e:
print("An error occurred:", e)
# client.disconnect()
# wlan.disconnect()