Created
December 3, 2019 16:40
-
-
Save shikajiro/6f3f21e03128b2f4847280adcd5fd3cd to your computer and use it in GitHub Desktop.
RaspberryPi から画像をサーバーに送るサンプル実装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
import logging | |
import arrow | |
import random | |
logging.basicConfig(level=logging.DEBUG) | |
URL = "http://xxx.xxx.xxx.xxx:5000/" | |
while True: | |
now = arrow.utcnow().to('Asia/Tokyo').isoformat() | |
measure = { | |
"device_id": "shika_home_1", | |
"measured_at": now, | |
"sensor": { | |
"temperature": random.randrange(0, 40), | |
"humidity": random.randrange(0, 100), | |
"co2": random.randrange(100, 1000), | |
"illuminance": random.randrange(0, 1500), | |
"point": { | |
"x": random.randrange(0, 100), | |
"y": random.randrange(0, 100), | |
"z": random.randrange(0, 100) | |
} | |
} | |
} | |
try: | |
requests.post(URL + "measure", json=measure) | |
except Exception as e: | |
logging.error(e) | |
# 画像を送るのはここから | |
files = {'image': open('sample.jpg', 'rb')} | |
data = { | |
'timestamp': now, | |
'x': random.randrange(0, 100), | |
'y': random.randrange(0, 100) | |
} | |
try: | |
requests.post(URL + "image", files=files, data=data) | |
except Exception as e: | |
logging.error(e) | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment