顯示具有 Regexp 標籤的文章。 顯示所有文章
顯示具有 Regexp 標籤的文章。 顯示所有文章

2017年8月14日 星期一

Arduino smart home ac remote

想要玩arduino很久了,總算有時間來試一下。參考了網路上的資訊,從完全沒摸過到寫程式燒錄到arduino,還有網頁http request的研究,總算最後是完成了。花了三天的時間,很有成就感也很累,在炎炎夏日還蠻實用的。

幾個過程中的重點:

arduino:

  • 匯入程式庫後compile才成功,不能自己用貼上.h的方式
  • Could not connect to YunServer 146 Connection refused的問題在我去除while (!Serial);後有顯著的改善
  • 目前紅外線LED的遙控距離還是太短,考慮增加電阻
  • 使用readuntil到?時會中斷(應該是timeout),所以目前是拿掉,之後再看有沒有辦法解決
  • 網頁資料是使用serial,我改用rest的方式,所以在資料取得方式上有些不同


web:


  • header必須要用Status: 200而非HTTP/1.1 200 OK,後者會導致content type無法變成text/html
  • arduino rest api protection要關閉,是為了讓網頁執行時不會hit到CORS(cross-origin resource sharing)的問題。狀況是這樣,因為使用了authrization所以會變成非簡單要求,發出OPTION request後因為我們建置的並非正式webserver所以response為空沒辦法加入header(arduino寫的程式必須要先能夠碰到REST)因此被擋(無法回傳access-allow-origin: *),使用datatype: jasonp並沒有辦法解決問題。沒使用authrization前是401,使用後是400。



參考資料
自己
smart home ac
http://cc.beingo.net/archives/2234
https://m.mobile01.com/topicdetail.php?f=731&t=4135366&p=1
http://cms.35g.tw/coding/arduino-remote-daikin-3/
http://cms.35g.tw/coding/arduino-remote-daikin-2/
http://cms.35g.tw/coding/arduino-remote-daikin/

arduino yun
https://forum.arduino.cc/index.php?topic=191895.0
https://forum.arduino.cc/index.php?topic=227213.0
http://yehnan.blogspot.tw/2014/04/arduino-yunbridgeconsoleprocess.html
https://www.arduino.cc/en/Tutorial/Bridge
https://arduino.stackexchange.com/questions/10257/sending-values-to-an-arduino-yun-across-different-networks
http://yehnan.blogspot.tw/2014/04/arduino-yunbridgeyunserveryunclient.html
http://yehnan.blogspot.tw/2013/01/arduinods18b20.html
http://yehnan.blogspot.tw/2013/05/arduino.html
http://yehnan.blogspot.tw/2016/04/arduino-yunwi-fi.html
https://forum.arduino.cc/index.php?topic=192936.0

CORS
http://www.ruanyifeng.com/blog/2016/04/cors.html
https://blog.m157q.tw/posts/2016/09/07/cross-origin-resource-sharing/
https://developer.mozilla.org/zh-TW/docs/Web/HTTP/Access_control_CORS

ajax
http://blog.techbridge.cc/2017/05/20/api-ajax-cors-and-jsonp/
https://babaawesam.com/2014/11/06/control-input-output-of-arduino-yun-with-ajax/
http://www.cnblogs.com/Mainz/p/3506956.html
http://shiyousan.com/post/635433082130309661

phpmyadmin create sql
http://www.complete-concrete-concise.com/web-tools/creating-a-mysql-database-using-ampps

HTML
http://www.w3school.com.cn/tags/tag_doctype.asp

字元與字串
http://dhcp.tcgs.tc.edu.tw/c/p009.htm

Basic Auth
https://stackoverflow.com/questions/5507234/how-to-use-basic-auth-with-jquery-and-ajax
https://stackoverflow.com/questions/3571090/basic-authentication-with-jquery-ajax-request-and-jsonp
https://stackoverflow.com/questions/27904082/using-http-basic-authentication-with-ajax-call

php windows工作排程
http://mrbignose.blogspot.tw/2010/05/php-windows-php.html

2015年2月3日 星期二

Robot Framework

Regular Expression
在使用 Read Until Regexp 時,卡在$及 . 很久,研究的結果是,由於使用或 ( | ) 所以要轉義兩次,第一次轉義後由於仍在 ( | ) 內所以仍被當成regular expression看,故要再轉義一次。

所以最後結果是 (\\$|\\.txt)

在使用SSHLibrary抓取terminal內字串時遇到換行符號,本來以為是\n,結果試了不是,又試了轉義以及\r都不是,最後靈機一動,居然是\r\n,記錄在這邊,希望能夠幫到人。

SSHLibrary Open Connection
與Telnet不同,ssh open connection 必須同時使用open connection及login兩個keyword,原因應該是因為ssh連接時是使用username@ip。robot因考慮Telnet使用同一組Keyword所以造成此限制。另外AttributeError: 'NoneType'錯誤則是因為沒有輸入帳號密碼,也就是上面所說沒有配合使用login造成。

unbalanced parenthesis則是括號沒有配合到,我遇到的狀況是空了兩格造成robot將右括號視作另一個argument了

SSHLibrary.Read until regexp 我有遇到狀況是會卡住,即使timeout時間到也不會FAIL。後來使用Read交叉測試的結果是因為output內有不同的編碼所以decode時卡住。Read結果的錯誤訊息為
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 318: invalid start byte
解決方法是close connection等到該段output被跳過之後再重新建立連線,換言之就是跳過該段output。另外也有一可能是整個編碼錯誤,如開始英文後轉為中文。此時可於open connection指定編碼encoding=

參考資料
自己測試結果
Read until regexp卡住
http://stackoverflow.com/questions/6180521/unicodedecodeerror-utf8-codec-cant-decode-bytes-in-position-3-6-invalid-dat
http://www.robotframework.net/question/68

google apps script

Google apps sript 使用for loop處理簡單的寫入時會很慢,不像excel運算的速度。因此需要簡化程式,以參考資料為例即為將每格寫入轉換成一次寫入,速度會提升很多。

另外矩陣變數則是要用"[["開頭,"]]"結尾,另外如果需要用迴圈加入單元,可以用.push(),記得變數要用var 變數 = [];。google apps script的for是小寫,我試過用大寫會無法辨識,需要注意。

在尋找字串裡的"("時,由於"("是特殊字元,必須escape,而search本身也是regular express所以要再escape一次,故應寫成search("\\(")。此時的錯誤訊息會是SyntaxError: unterminated parenthetical。

同理在取代字串的時候也會遇到regular express的問題,因為是regexp所以只會取代找到的第一個,解決方法是直接使用regexp寫法,如replace(/,/g)可取代字串中所有","。

function的部分就是直接呼叫,如HelloWorld();

replace在spreadsheet內使用的話要寫.toString().replace()才能用。還有length在計算array內字元我遇過會出錯,記得最後再加上[]。

Invalid assignment left-hand side. 我遇到的狀況是comparison的問題,就是code裡該使用"=="的地方(判斷式)用成"="(變數assign)就會出現這個error。而且這個error的debug會抓錯地方一直顯示第一行錯,所以第一次遇到會不知道發生甚麼事,連存檔都無法。

另外還有遇到一個狀況是在使用importrange公式,跑完script後會無法正常update數值,查了一下好像也有人遇到同樣情形,在google修好之前只好先用script頂著先。現象是都不會更新,將該格公式更動,或刪除復原就可以正常更新。

參考資料
基礎
https://developers.google.com/apps-script/guides/bound#creating_a_bound_script
簡化程式
http://stackoverflow.com/questions/19120211/google-script-performance-slow-down
加入公式
http://stackoverflow.com/questions/12036726/how-do-i-add-formulas-to-google-spreadsheet-using-google-apps-script
Matrix
http://stackoverflow.com/questions/15783169/convert-string-to-matrix-array
search "("
http://stackoverflow.com/questions/3977351/javascript-search-fails-to-find
replace all
http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript
javaScript運算子
http://taiwantc.com/js/js_tut_a4.htm
javaScript字串運算
http://miisoo.blogspot.tw/2008/01/javascript-string-operations.html
Invalid assignment left-hand side.
http://stackoverflow.com/questions/11888923/what-could-cause-invalid-assignment-left-hand-side-error-message-that-doesnt
importrange issue
http://stackoverflow.com/questions/25970185/importrange-function-not-refreshing-new-entries-in-linked-google-sheet
http://stackoverflow.com/questions/22587418/new-google-spreadsheet-formula-results-different-than-what-i-see-importrange-fu?rq=1