[PYTHON] Measure indoor and outdoor temperature and humidity and display on dashboard

Purpose

I want to check the indoor and outdoor conditions on the dashboard using a thermo-hygrometer.

Even when I bought the SwitchBot thermo-hygrometer, I felt that it wasn't used up, but I got noticed from this article and came up with the production. Thanks: bow: Acquire sensor data in the house with Raspberry Pi and realize a super IoT house

Overview

There are many articles that are processed using Raspberry Pi, but this time I will use the x86_64 server at home that is already running. To get the value from the sensor, use the Python program published by Wonder Labs. https://github.com/OpenWonderLabs/python-host The acquired value is recorded in Google Spreadsheet via Google Apps Script. Use this as a data source and visualize it in the Google Data Portal.

That's the flow. Untitled.png

Preparation

procedure

Installation of SwitchBot thermo-hygrometer

After purchasing, insert the battery and install it in the place to measure. When installing outdoors, take waterproof measures. You can enclose it in something, but if it is made of metal, it will interfere with communication. Install the SwitchBot app to find out the BLE MAC address you will need in the next step.

Creating a Google spreadsheet

Create a spreadsheet with any location and name. At this time, enter the first line as shown in the image below. (Before _t and _h is the MAC address of BLE) You will need the URL in the next step. logs-Google-スプレッドシート.png

Creating Google Apps Script

Create a Google Apps Script with any location and name. Please refer to other articles for how to create and prepare. https://qiita.com/tags/googleappsscript Rewrite the script with the contents below.

var SPREADSHEET_ID = "********"; //Here is the URL of the Spreadsheet

// Compiled using ts2gas 3.6.3 (TypeScript 3.9.7)
var MySpreadsheet = /** @class */ (function () {
    function MySpreadsheet(id, sheetId) {
        if (sheetId === void 0) { sheetId = 0; }
        if (id.match(/^https:\/\//)) {
            this.ss = SpreadsheetApp.openByUrl(id);
        }
        else {
            this.ss = SpreadsheetApp.openById(id);
        }
        this.sheet = this.ss.getSheets()[sheetId];
    }
    MySpreadsheet.prototype.headers = function () {
        var range = this.sheet.getRange(1, 1, 1, this.sheet.getMaxColumns());
        return range.getValues()[0];
    };
    MySpreadsheet.prototype.appendRow = function (row) {
        var insertRow = [];
        this.headers().forEach(function (name, index) {
            insertRow.push((name && name in row) ? row[name] : "");
        });
        this.ss.appendRow(insertRow);
    };
    MySpreadsheet.prototype.unshiftRow = function (row) {
        this.sheet.insertRowAfter(1);
        var range = this.sheet.getRange(2, 1, 1, this.sheet.getMaxColumns());
        this.headers().forEach(function (name, index) {
            if (name) {
                range.getCell(1, index + 1).setValue(row[name]);
            }
        });
    };
    return MySpreadsheet;
}());
var MyResponse = /** @class */ (function () {
    function MyResponse(status, message) {
        this.status = status;
        this.message = message;
    }
    MyResponse.prototype.toJSON = function () {
        return {
            status: this.status,
            message: this.message
        };
    };
    MyResponse.prototype.create = function () {
        var output = ContentService.createTextOutput();
        output.setMimeType(ContentService.MimeType.JSON);
        output.setContent(JSON.stringify(this));
        return output;
    };
    return MyResponse;
}());
function doPost(e) {
    var data = JSON.parse(e.postData.contents);
    var response = new MyResponse("success", "ok");
    try {
        var ss = new MySpreadsheet(SPREADSHEET_ID);
        ss.unshiftRow(data);
    }
    catch (err) {
        console.error(err);
        console.log(data);
        response.status = "error";
        response.message = err.message;
    }
    return response.create();
}

Publishing Google Apps Script

After saving the script, publish it. Menu> Publish> Introduced as a web application ... You will need the URL in the next step.

Installation of server program

Up to the point where the sensor value acquired by the Python program is POSTed to a specific URL is published in a Docker container. Please move here. https://hub.docker.com/repository/docker/kunikada/switchbot-meter-relay Specify the URL you published earlier in POST_URL.

Please refer to other articles for the process of running the Docker container in the first place. https://qiita.com/tags/docker

At this point, you should have data in your Google Sheets.

Google data portal settings

SwitchBot温湿度計-›-PC用レイアウト.png I'll omit the specific steps, but as a result of trial and error, something like an image was created. The temperature is displayed above, the humidity is displayed below, and indoor / outdoor data and differences are displayed in one. The left side is the history and the right side is the current value. Regarding the history, there are various parameters, but it can be set with one time series graph. The current value is displayed by overlaying two donut graphs and two scorecards. The data source also creates dedicated data in a separate sheet of the spreadsheet.

If you have any questions, please leave a comment.

Remarks

reference

Thermo-hygrometer API specifications https://github.com/OpenWonderLabs/python-host/wiki/Meter-BLE-open-API

Google Apps Script Reference https://developers.google.com/apps-script/reference

Docker Japanese Reference https://docs.docker.jp/index.html

Google Data Portal Help https://support.google.com/datastudio/?hl=ja

Recommended Posts

Measure indoor and outdoor temperature and humidity and display on dashboard
Raspberry + am2302 Measure temperature and humidity with temperature and humidity sensor
Record temperature and humidity with systemd on Raspberry Pi
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
Install matplotlib and display graph on Jupyter Notebook
DISPLAY error in wxPython (and traitsui) on vscode
Image display and video playback on Neovim Terminal
Display CPU temperature every 5 seconds on Raspberry Pi 4
Get temperature and humidity with DHT11 and Raspberry Pi