2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Lan内に設置されたRaspberry piで収集したデータを公開する方法

Last updated at Posted at 2021-03-02

LAN内のRaspberrypiで収集したデータをGoogleSpreadSheetに書き込みます。
image.png

インターネットに接続されたRaspberry PIは、Google Driveを用いて簡単にWeb serviceとして公開できます。

Postで行う

sample.py
'''
ラズパイからGoogleSheetに書き込むサンプルプログラム
t:温度
h:湿度
p:気圧
'''
def ExportGoogleSheet(t,h,p):
    url='https://script.google.com/macros/s/○○○○○○○○○○○○○○○○/exec'
    data = '{"temp":t,"humid":h,"press":p}'
    r=requests.post(url, data=data)

post.js
function doPost(e) {
  // スプレッドシートオブジェクトを取得
  var ss = SpreadsheetApp.getActive()
  var sheet = ss.getActiveSheet();
  var d=JSON.parse(e.postData.contents);
  // 日付、postDataオブジェクトを追記
  sheet.appendRow([new Date(),d.temp,d.humid,d.press]);
}

image.png

image.png

getで行う方法

送り出し

def getData(d,t,h,b,m):
    url='https://script.google.com/macros/s/AKfycbykJNNMPwirzk〇〇〇〇〇〇〇〇〇B3S2H1ZtM/exec?d=%s&t=%f&h=%f&b=%d&m=%s'%(d,t,h,b,m)
    r=requests.get(url)

受け取り


//pythonからデータを受け取る
function doGet(e) {


        var d = e.parameter.d; //時刻データの受け取り
        var t = e.parameter.t; //温度データの受け取り
        var h = e.parameter.h; //湿度データの受け取り
        var b = e.parameter.b; //バッテリデータの受け取り
        //現状Activeになっているsheetを取得
        var sheet = SpreadsheetApp.getActiveSheet();

        //送信されたデータ値 を追記
        sheet.appendRow([d, t, h, b]);
        // response for debug    
        var ok = "OK " + d + " " + t + "" + h + "% " + b + "% Bat"
        var output = ContentService.createTextOutput(ok);
        output.setMimeType(ContentService.MimeType.TEXT);
        return output;
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?