[PYTHON] Scraping the rainfall data of the Japan Meteorological Agency and displaying it on M5Stack

Overview

IMG_9811.JPG

What to prepare

Environment

(1) Install Arduino IDE https://www.arduino.cc/en/main/software (2) Install M5Stack related libraries https://mag.switch-science.com/2018/02/28/getting-started-with-m5stack/ (3) Install Apache (http server) on Raspberri Pi

Preparation

Example) Naka-ku, Hiroshima City * I am working remotely from Hiroshima City スクリーンショット 2020-09-23 9.57.37.png

programming

Main program If you change Hiroshima to another area, you can get the rainfall of that land.

/var/www/html/rain.php


<?php

mb_language("Japanese");//Processing language setting
mb_internal_encoding("UTF-8");//Character code specification
date_default_timezone_set('Asia/Tokyo');//set timezone

$url = "http://www.data.jma.go.jp/obd/stats/data/mdrr/pre_rct/alltable/pre1h00.h
tml";
 
//Loading a web page
$contents= file_get_contents($url);
 
//Character code conversion
$contents_utf = mb_convert_encoding($contents,"UTF-8","auto");
 
//Extracts the part enclosed by the specified character string.
$startString = '<td style="white-space:nowrap">Hiroshima (Hiroshima)*</td><td style="
text-align:right;white-space:nowrap;">';
$endString = '</td>';
 
$startPoint = mb_strpos($contents_utf,$startString) ;//$Find out where startString appears
$startPoint = $startPoint + mb_strlen($startString);//Starting position
//echo $startPoint;
//echo"";
 
$endPoint = mb_strpos($contents_utf,$endString,$startPoint );//$endString is next
Find out where it appears
//echo $endPoint;
//echo"";
 
$length = $endPoint - $startPoint;//Find the length of the string.
$rainfallString = mb_substr($contents_utf,$startPoint,$length)." mm"."\n\n"."GET
 Time"."\n".date("H:i:s")."\n";
echo $rainfallString;
file_put_contents("/var/www/html/index.html", $rainfallString);
?>

Shell script for PHP execution

/var/www/html/cron.php


#!/bin/sh
/usr/bin/php /var/www/html/rain.php

PHP execution result

/var/www/html/index.html


0.0 mm

GET Time
09:30:02

Make sure you can see it from your browser. スクリーンショット 2020-09-23 9.37.08.png

Auto-start setting (in this example, get precipitation every hour) */60 * * * * root sudo sh /var/www/html/cron.sh

Programming on the M5Stack side

rain.ino


#include <M5Stack.h>
#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid       = "XXXXXXXXXXXXX";  // Wifi SSID
const char* password   = "XXXXXXXXXXXXX";  // Wifi Password

const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;


void setup()
{
  M5.begin();
  M5.Lcd.setBrightness(10);
  M5.Lcd.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      M5.Lcd.printf(".");
  }

  M5.Lcd.println(" CONNECTED");
  M5.Lcd.fillScreen(BLACK);
}

void getrain()
{
  M5.Lcd.fillScreen(BLACK);
  HTTPClient http;

  http.begin("http://xxx.xxx.xxx.xxx/index.html");  //IP address of Raspberry Pi
  int httpCode = http.GET();

  String result = http.getString();
  M5.Lcd.setTextSize(5);
  M5.Lcd.setCursor(0,0);
  M5.Lcd.print(result);
  
  http.end();

}

void loop()
{
  getrain();
  delay(xxxxxxx);  //From raspberry Pi`index.html`Interval to get (milliseconds)
}

important point

Try using

Recommended Posts

Scraping the rainfall data of the Japan Meteorological Agency and displaying it on M5Stack
Download the wind data of the Japan Meteorological Agency
Scraping and tabulating weather warnings and warnings nationwide from the Japan Meteorological Agency
A story about getting the Atom field (XML telegram) of the Japan Meteorological Agency with Raspberry Pi and tweeting it
Analyzing data on the number of corona patients in Japan
Read the GRIB2 file of the Japan Meteorological Agency with pygrib
I analyzed the rank battle data of Pokemon sword shield and visualized it on Tableau
Scraping the schedule of Hinatazaka46 and reflecting it in Google Calendar
Get information from the Japan Meteorological Agency and notify Slack of weather warnings in the 23 wards of Tokyo
[Personal memo] Get data on the Web and make it a DataFrame
Upload data to s3 of aws with a command and update it, and delete the used data (on the way)
[Python] Precautions when retrieving data by scraping and putting it in the list
I tried to rescue the data of the laptop by booting it on Ubuntu
Scraping the winning data of Numbers using Docker
[Python3] Take a screenshot of a web page on the server and crop it further
Data Langling PDF on the outbreak of influenza by the Ministry of Health, Labor and Welfare
Read the csv file with jupyter notebook and write the graph on top of it
IDWR bulletin data scraping the number of reports per fixed point of influenza and by prefecture
How to get the current weather data and display it on the GUI while updating it automatically
Return the image data with Flask of Python and draw it to the canvas element of HTML
Try scraping the data of COVID-19 in Tokyo with Python
Web scraping of comedy program information and notification on LINE
Data analysis based on the election results of the Tokyo Governor's election (2020)
The process of making Python code object-oriented and improving it
Fixed-point observation of specific data on the Web by automatically executing a Web browser on the server (Ubuntu16.04) (2) -Web scraping-
Scraping the list of Go To EAT member stores in Fukuoka prefecture and converting it to CSV
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
Scraping the list of Go To EAT member stores in Niigata prefecture and converting it to CSV
How to return the data contained in django model in json format and map it on leaflet