Skip to content

Instantly share code, notes, and snippets.

@shikajiro
Created December 3, 2019 16:40
Show Gist options
  • Save shikajiro/6f3f21e03128b2f4847280adcd5fd3cd to your computer and use it in GitHub Desktop.
Save shikajiro/6f3f21e03128b2f4847280adcd5fd3cd to your computer and use it in GitHub Desktop.
RaspberryPi から画像をサーバーに送るサンプル実装
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