Flask by Raspi

現在、学寮の電動自転車貸出簿のシステム化に

取り組んでいる私であるが、Webサーバーの意味も

まともに分からなかった人間なので、とりあえず

形だけでもWebServerを作ってみようと

RaspberryPiをいじってみた。

 

Raspberry PiにFlaskをインストールする | LightHouseLab



上記サイトを参考に進めたものの中で初心者の

私が戸惑ったところだけ解説していくことにする。

 

①PythonCodeどこに何で作るん?

私の場合、/homeに新規フォルダを作成し、Flaskと名付けた。

そしてMenu→Programming→ThonnyPythonIDEの順に選択し

以下Codeを入手してFlaskFolderに保存した。

from flask import Flask 
app = Flask( __name__ )  
@app.route('/') def hello_world():    
   return "Hello World"       
if __name__ == '__main__':     
   app.run(host='0.0.0.0', port=5000, debug=True) 


②ブラウザのアドレスは?

結論:ラズパイのIPアドレスを入力する必要がある。

http://IP:5000

IPの部分はターミナルから

$ ip a

と入力すれば inet に続いてアドレスが表示されるので

最後の /×× より前の数字をIPの部分に入れよう。


Wi-fiはRaspberryPi,ブラウザ用デバイスともに

同じWi-fiルーターに繋ぐべし


f:id:massiro-myaon:20180505114326j:plain

RaspberryPi用3.5inLCD

ラズベリーパイ用ディスプレイ(12pin)として以下のものを購入した。

 

ja.aliexpress.com

 

なるべく安く手に入れるために初めてAliExpressを利用したが

購入手続きからSetupまでのほとんどが英語で大変だった。

 

raspberry piで3.5インチ(480x320)のタッチLCDを使う方法 : 試行錯誤な日々

 

Setup方法を探すのは大変だったが、上記サイトの通りに進めると

うまくいったので良かった。

 

f:id:massiro-myaon:20180504170424j:plain



 

※1 電源は5V1.8AのACアダプターであったが、問題なく動作した。

※2 バーのサイズをSmallにしておくとよい

解体作業

スマートフォン「SH-01F」を解体し、

MacBook Air の蓋を開けてみた。

f:id:massiro-myaon:20180320081229j:plain

f:id:massiro-myaon:20180320081420j:plain

f:id:massiro-myaon:20180320081447j:plain

マイナスドライバーでこじ開けて、

要所々々でドライバーを用いた。


使用ドライバー

基本は謎の黒テープで接着されていたので

思いきってマイナスドライバーでそれらを切り離していった。


比較的簡単なので、バッテリー持ちがわるいだけなら

バッテリーだけ買ってきて、自分で取り替えて

みるのも いいかも知れません。

f:id:massiro-myaon:20180320082126j:plain

こちらはただ開けただけだが、

ホコリだらけだった。

機械は定期的にメンテナンスしましょう。


ちなみにどちらも壊れた製品を貰ってやりました。

もともと壊れているから問題ない…

という覚悟で解体すると、

案外うまく行くのかも知れませんね。

高専カンファレンスin西京

 高専カンファレンスin西京で展示とLT(Lightning Talks)をしてきた。


  発表スライド
  Microsoft OneDrive - Access files anywhere. Create docs with free Office Online.


 興味深いことを沢山学べたし、多少なりとも発信もできたので良かった!

Wifi経由でArduino操作

 IoTっぽいものが作りたかったので、Arduinoスマホで操作してみました。

 実行映像
Microsoft OneDrive - Access files anywhere. Create docs with free Office Online.

 スマホ画面
f:id:massiro-myaon:20180316201047j:plain


 配線は以下の通り。(トラ技2017年11月号付録のWi-fi Arduinoを使用)

f:id:massiro-myaon:20180316190823j:plain

 コードもあげときます。

#define ECHOPIN 26  // エコー(受信側)ピン
#define CTM 10     // HIGHの時間(μ秒)

#define DX 12
int abc[] = {262, 294, 330, 349, 392, 440, 494, 523}; //ドレミの設定
//トーン設定(dx:ピン番号、hz:周波数、tm:ミリ時間)
void mtone(int dx, int hz, long tm) {
  long t = millis();
  long ns = 10000 / hz * 50;
  while (millis() - t < tm) {
    digitalWrite(dx, HIGH);
    delayMicroseconds(ns);
    digitalWrite(dx, LOW);
    delayMicroseconds(ns);
  }
}


#include <WiFi.h>

const char* ssid     = "ID";
const char* password = "パス";

WiFiServer server(80);

void setup()
{

  Serial.begin(115200);
  pinMode(2, OUTPUT) ;    // デジタル出力に設定
  pinMode(23, OUTPUT) ;    // デジタル出力に設定
  pinMode(18, OUTPUT) ;    // デジタル出力に設定
  pinMode(19, OUTPUT) ;    // デジタル出力に設定
  pinMode(DX, OUTPUT);   // スピーカのデジタル出力宣言
  pinMode(TRIGPIN, OUTPUT);// トリガーピンのデジタル出力設定
  pinMode(ECHOPIN, INPUT); // エコーピンのデジタル入力設定

  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  server.begin();

}

int value = 0;

void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            client.println("<!DOCTYPE html>");
            client.println("<html>");
            client.println("<head>");
            client.println("<meta name='viewport' content='initial-scale=1.5'>");
            client.println("</head>");
            client.println("<body>");
            client.println("<form method='get'>");
            client.println("<font size='4'>ESP-WROOM-32<br>");
            client.println("Wi-Fi  LED  Switch</font><br>");
            client.println("<br>");
            client.println("<input type='submit' name=0 value='ON' style='background-color:#88ff88; color:red;'>");
            client.println("<input type='submit' name=1 value='OFF' style='background-color:black; color:white;'>");
            client.println("</form>");
            client.println("</body>");
            client.println("</html>");

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> to turn the Red LED on.<br>");
            client.print("Click <a href=\"/L\">here</a> to turn the Green LED on.<br>");
            client.print("Click <a href=\"/R\">here</a> to turn the Blue LED on.<br>");

            client.print("Click <a href=\"/A\">here</a> to turn the Red LED off.<br>");
            client.print("Click <a href=\"/B\">here</a> to turn the Green LED off.<br>");
            client.print("Click <a href=\"/C\">here</a> to turn the Blue LED off.<br>");

            client.print("Click <a href=\"/D\">here</a> to turn the speaker up.<br>");
            client.print("Click <a href=\"/E\">here</a> to turn the speaker down.<br>");

            client.print("Click <a href=\"/F\">here</a> to turn the IO2 LED on.<br>");
            client.print("Click <a href=\"/G\">here</a> to turn the IO2 LED off.<br>");

            int val = analogRead(A4);
            client.println("<br>");
            client.print ("Light = ");
            client.print (val);
            client.println("<br>");

            int temp = analogRead(A5);
            client.print ("Temp = ");
            client.print (temp);
            client.println("<br>");

            int dur;    // 時間差(μ秒)
            float dis;  // 距離(cm)
            digitalWrite(TRIGPIN, HIGH);
            delayMicroseconds(CTM);
            digitalWrite(TRIGPIN, LOW);
            dur = pulseIn(ECHOPIN, HIGH); // HIGHになる待ち時間の計測
            dis  = (float) dur * 0.017;   // 音速による距離計算
            client.print ("Distance = ");
            client.print(dis);
            client.println(" cm");
            client.println("<br>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
        // Check
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(23, HIGH);
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(19, HIGH);
        }
        if (currentLine.endsWith("GET /R")) {
          digitalWrite(18, HIGH);
        }

        if (currentLine.endsWith("GET /A")) {
          digitalWrite(23, LOW);
        }
        if (currentLine.endsWith("GET /B")) {
          digitalWrite(19, LOW);
        }
        if (currentLine.endsWith("GET /C")) {
          digitalWrite(18, LOW);
        }

        if (currentLine.endsWith("GET /F")) {
          digitalWrite(2, HIGH);
        }
        if (currentLine.endsWith("GET /G")) {
          digitalWrite(2, LOW);
        }

        if (currentLine.endsWith("GET /D")) {
          for (int i = 0; i < 8; i++) {
            mtone(DX, abc[i], 500);
            delay(50);
          }
        }
        if (currentLine.endsWith("GET /E")) {
          for (int i = 8; i > -1; i--) {
            mtone(DX, abc[i], 500);
            delay(50);
          }
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

 お試しあれ!

参考文献
http://deviceplus.jp/hobby/entry_007/