Arduinoからシリアル通信で送られてきた値をリアルタイムにグラフでプロットしていく
Processingでやれよ!!!!
三軸方位センサの値をプロットしています。
冷静に考えて、webとしてFlask+p5.jsとかでやったほうがきれいだったんじゃないか…。
ちなみに何回かやってわかったのですが、これやらないほうがいいです。Ctrl-cでも止まらなくなったりdelayが出たり、そもそも重くなったりするので。
#app.py import serial import re import arrange def main(): arra = arrange.Arrange() with serial.Serial('COM3',9600,timeout=1) as ser: while True: c = ser.readline() de = c.decode('utf-8') m = re.match("\-*[\w]+", str(de)) if(m != None): arra.fetch_three_numbers(m.group()) else: pass ser.close() if __name__ == "__main__": main()
#arrage.py import numpy as np from scipy import signal from pylab import * class Arrange: bwn_a = False ax = None plots_numbers = [] def __init__(self): plot(500, 1) #ここいらない気がする。 plot(500, 3) plot(500, 5) def plots(self, x, y, z): clf() axis([-500, 500, -0.5, 5.5]) subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.2, hspace=0.5) plot(x, 1, 'o') plot(y, 3, '*') plot(z, 5, 'd') pause(0.01) def fetch_three_numbers(self, matched_group): if matched_group == "a": self.bwn_a = True self.plots_numbers = [] elif self.bwn_a: self.plots_numbers.append(matched_group) if len(self.plots_numbers) == 3: pl = self.plots_numbers self.plots(pl[0], pl[1], pl[2]) self.bwn_a = False print(pl)
ちなみにArduinoのコード
#include <Wire.h> #include "skHMC5883L.h" #define SENSOR_ADRS 0x1E skHMC5883L Compass(SENSOR_ADRS); void setup() { int ans; Serial.begin(9600); Wire.begin(); ans = Compass.Begin() ; } void loop() { // put your main code here, to run repeatedly: int ans; float deg; ans = Compass.SingleRead(°,6.6); if(ans == 0){ Serial.println("a"); Serial.println(RowX); Serial.println(RowY); Serial.println(RowZ); }else Serial.println("NG"); delay(1000); }
mySQLで新たに作ったユーザでデータベースが作れなかった。
mysql -u root -p
でrootに入る。この状態で作成したユーザに権限を付与しないとエラーになる(なった)
grant all on *.* to 'user'@'localhost';
これするといい!
flask, python, html, form, inputで画像のアップロード
これでだいたいうまくいく。
が
UPLOAD_FOLDER = '/path/to/the/uploads'
の部分は
UPLOAD_FOLDER = 'path/to/the/uploads'
とする(初心者なのでここでつまずいた)。
また日本語が入るファイル名はアップできない
ArduinoとpythonのSerial通信
基本形。ほんとに単純化した者。
pythonのコードを記載しておく。Arduinoは方位センサを使っている。
pip install pyserial
して
import serial import re def main(): with serial.Serial('COM3',9600,timeout=1) as ser: while True: c = ser.readline() de = c.decode('utf-8') #バイトから文字として認識できる形に。 m = re.match("\-*\d+", str(de)) if(m != None): print(m.group()) else: pass #print(type(m)) ser.close() if __name__ == "__main__": main()
ちなみに方位センサのコードは
#include <Wire.h> #include "skHMC5883L.h" #define SENSOR_ADRS 0x1E skHMC5883L Compass(SENSOR_ADRS); void setup() { int ans; Serial.begin(9600); Wire.begin(); ans = Compass.Begin() ; if (ans == 0) Serial.println("Initialization normal") ; else { Serial.print("Initialization abnormal ans=") ; Serial.println(ans) ; } } void loop() { int ans; float deg; ans = Compass.SingleRead(°,6.6); if(ans == 0){ Serial.println(RowX); Serial.println(RowY); Serial.println(RowZ); }else Serial.println("NG"); delay(1000); }
別にサンプルコードのままだから特に何も。
pythonでLINEのreplyからuserIdを取得する あと timestampのdatetimeへの変更
単純なんだけどはまったので
event.source.userId
だと思っていたんだけど
event.source.user_id
なんだよね…。
あと現在時刻の取得。herokuでやっているから時差あるけど…。
import time from datetime import datetime # print(datetime.fromtimestamp(event.timestamp/1000.0)) #
何も考えずにスクレイピングをpythonで始めてみた。
やってみたかったので始めました。王道かなと
scrapy
を入れてみたのですが別に使いやすくないなーと思いました。
入れ方はこちら
Installation guide — Scrapy 1.1.3 documentation
pip install scrapy
で入らなかったので詰んだかなと思ったのですが、上記手順で普通に入った。
あとは適当にごにょごにょ。
でも直感的じゃない。例えばサンプルコードは
Scrapy | A Fast and Powerful Scraping and Web Crawling Framework
class BlogSpider(scrapy.Spider): name = 'blogspider' start_urls = [url] def parse(self, response): for title in response.css('h2.entry-title'): yield {'title': title.css('a ::text').extract_first()} next_page = response.css('div.prev-post > a ::attr(href)').extract_first() if next_page:
実行コマンドも
scrapy runspider myspider.py
みたいな感じ。
コンソールにやたら出るし初心者には直感的じゃないよねぇ
というわけでbeautifulSoup4
PythonとBeautiful Soupでスクレイピング - Qiita
超簡単!
リンクが直接は得られないようなので
url.requestを僕は使いました(何度もpip installしようとしたのですが、標準搭載の当たり前の機能でした)
書いたコードがこれ
from bs4 import BeautifulSoup import urllib.request html = urllib.request.urlopen(url) soup = BeautifulSoup(html) print(soup.find_all("a")[0].get("href"))
find_allして最初の配列を取るとかいう意味不明なことしてますが許して。
楽しい!