# [20190526 - Raspberry Pi](https://hackmd.io/s/SkgLJCCnV) ###### tags: `tcfst` ## Author - Yuan < e61983@gmail.com > - Wei < demon0303loki@gmail.com > - Jia-Hao,Hsu < jhs.8901.3737@gmail.com > ## License ![cc by-nc-sa](https://licensebuttons.net/l/by-nc-sa/3.0/88x31.png) ## Using Python connect MySQL - [Try It][online-python_sql] ### Install python-mysqldb ```shell= sudo apt install -y python-mysqldb ``` or ```shell= sudo easy_install pip sudo pip install pip --upgrade sudo apt-get build-dep python-mysqldb sudo pip install MySQL-python ``` ### Example #### Read ```python= #!/usr/bin/python # -*- coding: utf-8 -*- import MySQLdb db = MySQLdb.connect(host="localhost", user="db_user", passwd="db_pass", db="db_name") cursor = db.cursor() cursor.execute("SELECT * FROM db_table") # 取回所有查詢結果 results = cursor.fetchall() for record in results: col1 = record[0] col2 = record[1] print "%s, %s" % (col1, col2) db.close() ``` #### Insert ```python= db = MySQLdb.connect(host="localhost", user="db_user", passwd="db_pass", db="db_name") cursor = db.cursor() sql = 'INSERT INTO tempdat \ values(current_data(), now(), 'garage', 18.1)' try: cursor.execute(sql) db.commit() except: db.rollback() db.close() ``` ## Using C language connect MySQL ### Install ```shell= sudo apt install -y libmysqlclient-dev ``` ### Example ```c= #include <mysql.h> int main(int argc, char **argv) { MYSQL *con = mysql_init(NULL); if (con == NULL) { fprintf(stderr, "%s\n", mysql_error(con)); exit(1); } if (mysql_real_connect(con, "localhost", "root", "root_pswd", NULL, 0, NULL, 0) == NULL) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } if (mysql_query(con, "CREATE DATABASE testdb")) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } mysql_close(con); exit(0); } ``` ## Using PHP connect MySQL ```php= <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("連線失敗: " . $conn->connect_error); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "無结果"; } $conn->close(); ?> ``` ## Session & Cookie 負責紀錄在 web server 上的使用者訊息。Session 機制會在一個用戶完成身分認證後,存下所需的用戶資料,接著產生一組對應的 id,存入 cookie 後傳回用戶端。 ### PHP Setting Setting file : /etc/php/7.0/apache2/php.ini ```shell= sudo vim /etc/php/7.0/apache2/php.ini ``` ### Example index.html: ```html= <?php session_start(); if( isset( $_SESSION['counter'] ) ) { $_SESSION['counter'] += 1; }else { $_SESSION['counter'] = 1; } $msg = "You have visited this page ". $_SESSION['counter']; $msg .= "in this session."; ?> <html> <head> <title>Setting up a PHP session</title> </head> <body> <?php echo ( $msg ); ?> </body> </html> ``` ### Destroying a PHP Session ```php= <?php unset($_SESSION['counter']); ?> ``` or ```php= <?php session_destroy(); ?> ``` ### get & post method on PHP get: ```php= <?php  echo $_GET[Email]; ?> ``` post: ```php= <?php  echo $_POST["Email"]; ?> ``` ### 實作會員登入 #### Reference - [dreamtails-PHP會員登入機制][dreamtails-PHP會員登入機制] - [SQL語法教學][SQL語法教學] ### More Information - [PHP 教學][gitbook-PHP] ## I2C - Device - [datasheet][Datasheet-24LC02] ### 使用 I2C 工具測試 ```shell= # 印出裝置內的資料 sudo i2cdump -y 0x50 # 將第 18 個 byte 的值改寫為 6 sudo i2cset -y 1 0x50 0x12 6 # 讀取第 18 個 byte sudo i2cget -y 1 0x50 0x12 ``` ### 使用 Python 讀寫 Example 1 ```python= #!/usr/bin/python # -*- coding: utf-8 -*- import smbus import time i2c = smbus.SMBus(1) addr = 0x50 reg = 0x00 value = 100 i2c.write_byte_data(addr, reg, value) time.sleep(0.1) print i2c.read_byte_data(addr, reg) ``` Example 2: ```python= #!/usr/bin/python # -*- coding: utf-8 -*- import smbus import time addr = 0x50 reg = 0x00 vals=[1,2,3,4,5,6,7,8] i2c.write_i2c_block_data(addr, reg, vals) time.sleep(0.1) print i2c.read_block_data(addr, reg, len(vals)) ``` ## WebIOPi ### Install ```shell= wget http://webiopi.googlecode.com/files/WebIOPi-0.6.0.tar.gz tar xvzf WebIOPi-0.6.0.tar.gz cd WebIOPi-0.6.0 sudo ./setup.sh ``` ### Start WebIOPi Run as program ```shell= sudo webiopi -d -c /etc/webiopi/config ``` or Run as service ``` cd /etc/systemd/system/ sudo wget https://raw.githubusercontent.com/doublebind/raspi/master/webiopi.service sudo systemctl start webiopi sudo systemctl enable webiopi ``` ### Test Default url: http://localhost:8000/ Default user is "webiopi" and password is "raspberry" ### Change password ```shell= sudo webiopi-passwd ``` ### Reference - [Official-WebIOPi][Official-WebIOPi] ## RESTful API REST (Representational State Transfer) API 充分地使用了 HTTP protocol (GET/POST/PUT/DELETE). RESTful 的優點如下所列: - 瀏覽器即可以作為 client 端 - 可以更高效地利用 cache 來達到更快的回應速度 - 界面與資料分離 - 節省伺服器的計算資源 - 可重用! (web/android/ios 都可以用, 無痛轉換!) 可以參考: - [Instagram][instagram-api] ## Javascript ### Data Types ```javascript= let length = 16; // Number let lastName = "Johnson"; // String let x = {firstName:"John", lastName:"Doe"}; // Object ``` ### Function ```javascript= // Function is called, return value will end up in x var x = myFunction(4, 3); // Function returns the product of a and b function myFunction(a, b) { return a * b; } ``` ### More Information - [專為中學生寫的 JavaScript 程式書 ][gitbook-專為中學生寫的 JavaScript 程式書 ] ### CORS CORS (Cross-Origin Resource Sharing) 即跨域資源共享。他的原理就是使用自定義的 HTTP 頭部,讓服務器與瀏覽器進行溝通,主要是通過設置響應頭的 `Access-Control-Allow-Origin` 來達到目的的。這樣,XMLHttpRequest 就能跨網域了。 #### Reference - [MDN Web Developer - cors][mozilla-cors] ## Thunkable - [Official][Official-thunkable] - App - [Android][android-thunkable.live] - [iOS][ios-thunkable.live] Demo Video {%youtube BfFRmCjQXiQ %} ## MQTT MQTT(Message Queuing Telemetry Transport)是機器對機器(M2M)/“物聯網”連接協議。它被設計為一個非常輕量級的發布/訂閱消息傳輸。 - App - [Android][android-mymqtt] - [iOS][ios-mqtt-tool] ### Specification - [MQTT Specification 3.1.1][MQTT-SPEC] ### Using Javascript #### Example - Install MQTT.js ```shell= npm install mqtt --save ``` ```javascript= var mqtt = require('mqtt') var client = mqtt.connect('mqtt://test.mosquitto.org') client.on('connect', function () { client.subscribe('presence', function (err) { if (!err) { client.publish('presence', 'Hello mqtt') } }) }) client.on('message', function (topic, message) { // message is Buffer console.log(message.toString()) client.end() }) ``` ### Using C Language #### Install ```shell= sudo apt install libmosquitto-dev ``` #### Document - [Official Man page][doc-mosquitto] #### Example ```c= #include <stdio.h> #include <mosquitto.h> void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message) { if(message->payloadlen){ printf("%s %s\n", message->topic, message->payload); }else{ printf("%s (null)\n", message->topic); } fflush(stdout); } void my_connect_callback(struct mosquitto *mosq, void *userdata, int result) { int i; if(!result){ /* Subscribe to broker information topics on successful connect. */ mosquitto_subscribe(mosq, NULL, "$SYS/#", 2); }else{ fprintf(stderr, "Connect failed\n"); } } void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos) { int i; printf("Subscribed (mid: %d): %d", mid, granted_qos[0]); for(i=1; i<qos_count; i++){ printf(", %d", granted_qos[i]); } printf("\n"); } void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str) { /* Pring all log messages regardless of level. */ printf("%s\n", str); } int main(int argc, char *argv[]) { int i; char *host = "localhost"; int port = 1883; int keepalive = 60; bool clean_session = true; struct mosquitto *mosq = NULL; mosquitto_lib_init(); mosq = mosquitto_new(NULL, clean_session, NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } mosquitto_log_callback_set(mosq, my_log_callback); mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_message_callback_set(mosq, my_message_callback); mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); if(mosquitto_connect(mosq, host, port, keepalive)){ fprintf(stderr, "Unable to connect.\n"); return 1; } mosquitto_loop_forever(mosq, -1, 1); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; } ``` ## Python GUI Develop - Tkinter ### Install ```shell= sudo apt install python3-tk ``` ### Example - HelloWorld ```python= #!/usr/bin/python # -*- coding: utf-8 -*- import tkinter as tk # Python 3.x Version #import Tkinter as tk # Python 2.x Version if __name__ == '__main__': root = tk.Tk() # Create a text label label = tk.Label(root, text="Hello World!") # Pack it into the window label.pack(padx=20, pady=20) root.mainloop() ``` ### Example - Button ```python= #!/usr/bin/python # -*- coding: utf-8 -*- import tkinter as tk def button_click_handler(): label.configure(text="Hi") if __name__ == '__main__': root = tk.Tk() button = Button(root, text="show", command=button_click_handler) button.pack() root.mainloop() ``` ### Event and Binding ```python= #!/usr/bin/python # -*- coding: utf-8 -*- # Python 2.x from Tkinter import * import tkMessageBox def callback(): if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"): root.destroy() root = Tk() root.protocol("WM_DELETE_WINDOW", callback) root.mainloop() ``` #### Reference - [Event and Binding][effbot-even-and-binding] ### More Information - [Thinter 教學][tutorials-tkinter] ## 梗區 > 我什麼都沒有改啊~怎麼不會動 > [name=Jia-Hao] > 程式寫得好就不用註解 > [name=ChroXX] > 公司配的鍵盤Enter鍵是壞的 > [name=Jia-Hao] > >> Pchome 24h >> <img style="width:20%; height:auto;" src="https://i.imgur.com/y9wB0PQ.jpg" > >> [name=Yuan] > ~~原來今天有講了這麼多概念阿~~ > [name=Chun-Wei] [online-python_sql]: https://www.tutorialspoint.com/python_mysql_online.php [SQL語法教學]: https://www.1keydata.com/tw/sql/sql.html [dreamtails-PHP會員登入機制]: http://dreamtails.pixnet.net/blog/post/23583385-%5B%E6%95%99%E5%AD%B8%5Dphp%E6%9C%83%E5%93%A1%E7%99%BB%E5%85%A5%E6%A9%9F%E5%88%B6%EF%BC%8Csession%E7%9A%84%E4%BD%BF%E7%94%A8%EF%BC%8C%E7%B0%A1%E6%98%93%E5%9E%8B [Datasheet-24LC02]: https://www.holtek.com/documents/10179/116711/HT24LC02v190.pdf [Official-WebIOPi]: http://webiopi.trouch.com/ [instagram-api]: https://www.instagram.com/developer/ [mozilla-cors]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS [mozilla-javascript-tutorial]: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps [Official-thunkable]: http://app.thunkable.com/login/ [android-thunkable.live]: https://play.google.com/store/apps/details?id=com.thunkable.live [ios-thunkable.live]: https://itunes.apple.com/tw/app/thunkable-live/id1223262700?mt=8 [MQTT-SPEC]: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html [android-mymqtt]: https://play.google.com/store/apps/details?id=at.tripwire.mqtt.client [ios-mqtt-tool]: https://itunes.apple.com/us/app/mqttool/id1085976398?mt=8 [doc-mosquitto]: https://mosquitto.org/man/libmosquitto-3.html [effbot-even-and-binding]: https://effbot.org/tkinterbook/tkinter-events-and-bindings.htm [gitbook-專為中學生寫的 JavaScript 程式書 ]: https://ccckmit.gitbooks.io/javascript/content/ [gitbook-PHP]: https://wizardforcel.gitbooks.io/w3school-php/content/ [tutorials-tkinter]: https://morvanzhou.github.io/tutorials/python-basic/tkinter/