-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprev_send.py
More file actions
48 lines (40 loc) · 1.46 KB
/
prev_send.py
File metadata and controls
48 lines (40 loc) · 1.46 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
import time
import zmq
from Adafruit_BME280 import *
"""
The reciever must already be running before the sender is started
"""
#setup the sensor
sensor = BME280(t_mode=BME280_OSAMPLE_8, p_mode=BME280_OSAMPLE_8, h_mode=BME280_OSAMPLE_8)
#identify the weather station
weather_id = "apple"
#the function that sends data
#must already have created the data dictionary before calling producer()
def producer():
context = zmq.Context()
zmq_socket = context.socket(zmq.PUSH)
zmq_socket.bind("tcp://129.118.107.227:5556")
zmq_socket.send_json(data)
print("Sending data at time {}:{}".format(timelist[4],timelist[5]))
#read the TPH data and save it in a time-stamped dictionary
while True:
#get the time as a time-tupple
#localtime[0] is year
#localtime[1] is month (1-12)
#localtime[2] is day (1-31)
#localtime[3] is hour (0-23)
#localtime[4] is minute (0-59)
#localtime[5] is second (0-59)
localtime = time.localtime(time.time())
#convert to a list so JSON can handle it
timelist = [localtime[0],localtime[1],localtime[2],localtime[3],localtime[4],localtime[5]]
#get the weather info
degrees = sensor.read_temperature()
pascals = sensor.read_pressure()
humidity = sensor.read_humidity()
#save to a dictionary
data = {'idnum':weather_id, 'timestamp':timelist, 'temp':degrees, 'pressure':pascals, 'humidity':humidity}
#send the data
producer()
#wait for 1 minute
time.sleep(60)