前提・実現したいこと
ラズベリーパイ3 ModelB を温度・湿度・気圧センサと接続し,取得データをpythonでherokuに送信し,ブラウザ上でデータを見れるようにしたいです.
・送信側:python
・受信側:flask
以下のサイトを参考にプログラムを作りました.送信側プログラムはcronで定期的に動かそうと考えています.
【Linebot #3】家の温度をLineで通知するアプリ (BME280使用)
第39回「ラズベリーパイで温度・湿度・気圧をまとめて取得!AE-BME280でIC2通信」
[Python] 軽量WebフレームワークのFlaskに入門(準備、起動、HTML、静的ファイル、GET、POSTなど)
pythonを始めて約2週間なので,イマイチ分かってないです…
発生している問題・エラーメッセージ
herokuにデプロイし,heroku open で見てみると以下のメッセージが…
Method Not Allowed the method is not allowed for the requested URL.
該当のソースコード
・送信側 app.py
python
1#coding: utf-8 2import bme280_custom #データ取得プログラム 3import urllib.request, urllib.parse 4 5csv = bme280_custom.readData() 6list = csv.split(",") 7press = list[0] 8temp = list[1] 9hum = list[2] 10 11def post_message(t, p, h): 12 print("post") 13 data = {} 14 data["temp"] = t 15 data["pres"] = p 16 data["hum"] = h 17 server_addr = "https://xxxx.herokuapp.com/" 18 try: 19 data = urllib.parse.urlencode(data).encode("utf-8") 20 with urllib.request.urlopen(server_addr, data=data) as res: 21 res = res.read().decode("utf-8") 22 print(res) 23 except: 24 print('error') 25 26post_message(temp, press, hum)
・受信側 server.py
python
1# coding: utf-8 2from flask import Flask, request, render_template 3app = Flask(__name__) 4 5@app.route("/",methods=['POST']) 6def info(): 7 t = float(request.form['temp']) 8 p = float(request.form['pres']) 9 h = float(request.form['hum']) 10 return render_template('index.html',t=t,h=h,p=p) 11 12if __name__ == "__main__": 13 app.run()
・index.html
HTML
1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1"> 7 <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 8 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 9 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> 10 </head> 11 <body> 12 <div class="container"> 13 <div class="header"> 14 <form method="post"> 15 <h3 class="text-muted">温度:{{t}}℃,湿度:{{h}}%,気圧:{{p}}hPa</h3> 16 </form> 17 </div> 18 </div> 19 </body> 20</html>
・Procfile
web: gunicorn app:app
試したこと
アプリケーション?自体は開いてるのでflask側のプログラムがおかしいと思い調べるも解決せず…
私は以下のようになると考えています.
1.取得したデータを指定したURLに送る
2.デプロイされたプログラムをopenするとデータが見れる
この考えは違うのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/06/22 17:24
2018/06/22 17:42
2018/06/22 17:58 編集
退会済みユーザー
2018/06/23 01:54
2018/06/23 02:11 編集
2018/06/23 02:10
退会済みユーザー
2018/06/23 02:33
2018/06/23 03:01 編集
退会済みユーザー
2018/06/23 03:29
2018/06/23 03:48
退会済みユーザー
2018/06/24 04:19
2018/06/24 07:33
退会済みユーザー
2018/06/24 14:23