Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Vitaliy Rudnytskiy [SAP]
Published © Apache-2.0

Weather Station Data from RPi Sense HAT via HTTPS

Sending environmental data from Raspberry Pi's Sense HAT to SAP Cloud Platform IoT Service for Neo using HTTPS protocol.

BeginnerProtip30 minutes3,635
Weather Station Data from RPi Sense HAT via HTTPS

Things used in this project

Story

Read more

Code

scpiot_neo_rpisensehat_http.py

Python
python3 scpiot_neo_rpisensehat_http.py
from sense_hat import SenseHat
import requests # http://docs.python-requests.org/en/master/
import time
import sys, platform

accountOwner = '<your account id>'
deviceid = '<device id of your raspberrypi>'
authtoken = '<auth token for your raspberrypi virtual device>'
msgtypeid = '<message type id for the weatherStation>'

hostiotmms = 'iotmms'+accountOwner+'trial.hanatrial.ondemand.com'
apiiotmmsdata = '/com.sap.iotservices.mms/v1/api/http/data/'
url = "https://"+hostiotmms+apiiotmmsdata+deviceid
print("URL: ", url)

sense = SenseHat()
sense.clear()
sense.set_rotation(90)
red = (255, 0, 0)
green = (0, 255, 0)

def readsensors():
	d_tstamp = int(round(time.time()))

	temp = sense.get_temperature()
	d_temp = round(temp, 1)
	#print("Temperature C",temp)

	humidity = sense.get_humidity()
	d_humy = round(humidity, 1)
	#print("Humidity :",humidity)

	pressure = sense.get_pressure()
	d_pres = round(pressure, 1)
	#print("Pressure:",pressure)

	return (d_temp, d_humy, d_pres)

def postiotneo (d_temp, d_humy, d_pres):

	d_tstamp = int(round(time.time()))
	
	s_tstamp = str(d_tstamp)
	s_temp = str(d_temp)
	s_humy = str(d_humy)
	s_pres = str(d_pres)

	payload = "{\"mode\":\"sync\",\"messageType\":\""+msgtypeid+"\",\"messages\":[{\"temperature\":"+s_temp+",\"humidity\":"+s_humy+",\"timestamp\":"+s_tstamp+"}]}"
	headers = {
			'content-type': "application/json",
			'authorization': "Bearer "+authtoken,
			'cache-control': "no-cache"
			}

	print(payload)

	response = requests.request("POST", url, data=payload, headers=headers)

	if str(response.status_code)[0:1]=='2':
		sense.show_message("<-", text_colour=green)
	else:
		sense.show_message("O", text_colour=red)
		
	print(response.status_code, response.text)
	# print(response.headers)

	return

try:
	while(True):
		postiotneo(*readsensors())
		time.sleep(5)

except KeyboardInterrupt:
		time.sleep(1)
		print("Have a nice weather!")

Credits

Vitaliy Rudnytskiy [SAP]
1 project • 4 followers
SAP Developer Relations
Contact

Comments

Please log in or sign up to comment.