-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_cam.py
128 lines (100 loc) · 3.74 KB
/
weather_cam.py
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
#!/usr/bin/python3
# -*- coding:utf-8 -*-
import pygame.camera
from astral import LocationInfo
from astral.sun import sun
import astral
from datetime import datetime
from time import sleep
import pytz
import paho.mqtt.publish as publish
import psutil
import string
import random
import requests
import json
# Captured image dimensions. It should be less than or equal to the maximum dimensions acceptable by the camera.
width = 320
height = 240
# Initializing PyGame and the camera.
pygame.init()
pygame.camera.init()
# Specifying the camera to be used for capturing images. If there is a single camera, then it has the index 0.
cam = pygame.camera.Camera("/dev/video0", (width, height))
# Preparing a resizable window of the specified size for displaying the captured images.
window = pygame.display.set_mode((width, height), pygame.RESIZABLE)
# Starting the camera for capturing images.
cam.start()
utc =pytz.UTC
api_key = "Your_key"
lat = "Your_latitude"
lon = "Your_longitude"
city = LocationInfo(lat, lon)
string.alphanum = '1234567890avcdefghijklmnopqrstuvwxyzxABCDEFGHIJKLMNOPQRSTUVWXYZ'
# The ThingSpeak Channel ID.
# Replace <YOUR-CHANNEL-ID> with your channel ID.
channelID = "<YOUR-CHANNEL-ID>"
# The write API key for the channel.
# Replace <YOUR-CHANNEL-WRITEAPIKEY> with your write API key.
writeAPIKey = "<YOUR-CHANNEL-WRITEAPIKEY> "
# The hostname of the ThingSpeak MQTT broker.
mqttHost = "mqtt.thingspeak.com"
# You can use any username.
mqttUsername = "TSMQTTRpiDemo"
# Your MQTT API key from Account > My Profile.
mqttAPIKey = "Your MQTT-API"
tTransport = "websockets"
tPort = 80
# Create the topic string.
topic = "channels/" + channelID + "/publish/" + writeAPIKey
while True:
s = sun(city.observer)
time_now = utc.localize(datetime.now())
sunrise =s["sunrise"]
print("Sunrise is",sunrise)
dusk = s["dusk"]
print("Sunset is ",dusk)
print("Time is", time_now)
if time_now > sunrise and time_now < dusk :
print("Daylight")
# Capturing an image.
image = cam.get_image()
# Displaying the image on the window starting from the top-left corner.
window.blit(image, (0, 0))
# Refreshing the window.
pygame.display.update()
# Saving the captured image.
created_at = datetime.now()
pygame.image.save(window, '/home/pi/Documents/python/capture/image_' + str(created_at) + '.jpg')
clientID = ''
# Create a random clientID.
for x in range(1,16):
clientID += random.choice(string.alphanum)
url = "https://api.openweathermap.org/data/2.5/onecall?lat=%s&lon=%s&appid=%s&units=metric" % (lat, lon, api_key)
response = requests.get(url)
data = json.loads(response.text)
#print(data)
current_temp = data["current"]["temp"]
current_humid = data["current"]["humidity"]
current_press = data["current"]["pressure"]
visibulity = data["current"]["visibility"]
clouds = data["current"]["clouds"]
print("Temp is",current_temp)
print("Humidity is",current_humid)
print("Pressure is",current_press)
print("Visibility is",visibulity )
print("Clouds are",clouds)
# build the payload string.
payload = "field1=" + str(current_temp) + "&field2=" + str(current_humid) + "&field3=" + str(current_press)+ "&field4=" + str(visibulity)+ "&field5=" + str(clouds)
# attempt to publish this data to the topic.
try:
publish.single(topic, payload, hostname=mqttHost, transport=tTransport, port=tPort,auth={'username':mqttUsername,'password':mqttAPIKey})
print (" Published ")
except:
print ("There was an error while publishing the data.")
sleep(60*10)
else:
print("Sleep for 10 minutes")
sleep(60*10)
# Stopping the camera.
cam.stop()