Open-source weather station for astronomy
1#!/usr/bin/env python3
2
3from urllib import request
4from bs4 import BeautifulSoup as BS
5from influxdb import InfluxDBClient
6import datetime
7import numpy as np
8import matplotlib.pyplot as plt
9
10# Get data from HTML
11html = request.urlopen("http://10.42.0.126/index.hmtl") # Change this as necessary
12
13# Get current time
14time = datetime.datetime.utcnow()
15str_time = time.strftime('%Y-%m-%dT%H-%M-%S')
16
17# Parse data in dictionary
18soup = BS(html, 'html.parser')
19body = soup.body
20data = {}
21for elem in body:
22 string = str(elem)
23 try: #Field with data
24 str_data = string.split('=')
25 key = str_data[0][1:]
26 value = str_data[1][:-1]
27 if not (key in ['timestamp']):
28 value = float(value)
29 data[key] = value
30 except:
31 continue
32
33# Parse IR image
34try:
35 ir_image = soup.ir_image
36 ir_image = ir_image.contents[0][22:-3].split(', ')
37 ir_image = np.array(ir_image).astype(float).reshape((24,32))
38 # Save IR data
39 with open(f'/home/aurel/Astro/log/log_ir.log', 'a') as f:
40 f.write(f'{str_time}, ' + ', '.join([f'{v:.3f}' for v in ir_image.flatten()]) + '\n')
41 # Save IR image
42 fig, ax = plt.subplots(figsize=(8,6))
43 im = ax.imshow(ir_image, cmap='seismic', vmin=-20, vmax=20, interpolation='bicubic')
44 fig.colorbar(im)
45 fig.tight_layout()
46 fig.savefig(f'/home/aurel/Astro/log/ir_image_{str_time}.png', dpi=100)
47 plt.close()
48except:
49 pass
50
51# Send data to InfluxDB
52host = '127.0.0.1' # Change this as necessary
53port = 8086
54username = 'grafana' # Change this as necessary
55password = 'grafana' # Change this as necessary
56db = 'weatherstation' # Change this as necessary
57client = InfluxDBClient(host, port, username, password, db)
58data.pop('timestamp') # Remove timestamp from data logged to InfluxDB
59print(data)
60payload = [{"measurement": "astroweatherstation",
61 "time": time,
62 "fields": data
63 }]
64client.write_points(payload)