2017年11月30日 星期四

Arduino微笑偵測器


Arduino

臉部 API 
這份 API 金鑰目前使用中 
尚餘 30
偵測、識別、分析、組織和標記相片中的臉孔
30,000 筆交易,每分鐘 20 筆。
端點: https://westcentralus.api.cognitive.microsoft.com/face/v1.0
金鑰 1: 69def20442884f59afa6f7f9009f99fd

金鑰 2: e457f2ac2447475cacf9bddbbc9cb22e





Maxde-MBP:downloads max$ vi faceapi_arduino.py

    a = parsed[0]["faceAttributes"]["smile"]   #顯示微笑程度
    conn.close()

except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))

print "smile value is"
print a                                #顯示微笑程度
if a > 0.75:                         #以下分成三個區間來控制LED,您可以自行修改
    board.digital[9].write(1)
    board.digital[10].write(1)
    board.digital[11].write(1)
    print 111
elif a < 0.25:
    board.digital[9].write(1)
    board.digital[10].write(0)
    board.digital[11].write(0)
    print 100
else:
    board.digital[9].write(1)
    board.digital[10].write(1)
    board.digital[11].write(0)
    print 110



2017年11月21日 星期二

Opendate PM 2.5


Government Open Date Platform
https://data.gov.tw

Open Data EPA
https://opendata.epa.gov.tw

Open data file typ: csv, txt, JSON, XML


Two view of JSON file:



http://opendata.epa.gov.tw/ws/Data/POP00049/?$orderby=M_Time%20desc&$skip=0&$top=1000&format=json
<Please open this url by FireFox or JSON Editor Online website>


{"Epb":"嘉義縣","CNO":"Q7600375","Abbr":"南亞塑膠工業股份有限公司嘉義廠","PolNo":"P001","ItemDesc":"氮氧化物監測設施十五分鐘數據紀錄值","Item":"923 ","M_Time":"2017-11-21 18:45:00","M_Val":"83.75","Unit":"ppm    ","Code2":"10","Code2Desc":"正常排放量測值","Std":"250","Std_s":"電力設施空氣污染物排放標準"}



可以看到兩種結構組成:
物件(object)用大括號 { },分號隔開
例如:{"subject":"Math","score":80}
陣列(array)用中括號 [ ] ,分號:隔開
例如:[0,4,5,2,7,8,3]






JSON Editor Online
http://www.jsoneditoronline.org

open this url 
https://opendata.epa.gov.tw/webapi/api/rest/datastore/355000000I-000207?offset=0&limit=1000



Maxde-MacBook-Pro:taipei_PM2.5 max$ cat taipei_PM2.5_ver1.py 
#!/usr/bin/python
#-*- coding: UTF-8 -*-

import urllib, json

url = "http://opendata2.epa.gov.tw/AQI.json"
response = urllib.urlopen(url)
data_list = json.loads(response.read())

print  "城市:"
print   data_list[10]['County']
print  "地區:"
print   data_list[10]['SiteName']
print  "PM2.5指數:"
print   data_list[10]['PM2.5']
print  "狀態:"
print   data_list[10]['Status']


======

接RGB LED 進行判斷式 空氣品質->亮紅藍綠燈...
佈線圖在ppt

#!/usr/bin/python
#-*- coding: UTF-8 -*-
import urllib, json, pyfirmata
from time import sleep

#ser = serial.Serial("COM4",9600)

port = '/dev/cu.usbmodem1411'
pin1 = 11 #R
pin2 = 10 #G
pin3 = 8  #B

board = pyfirmata.Arduino(port) 
sleep(5)

url = "http://opendata2.epa.gov.tw/AQI.json"
response = urllib.urlopen(url)
data_list = json.loads(response.read())

print  "城市:"
print   data_list[3]['County']
print  "地區:"
print   data_list[3]['SiteName']
print  "PM2.5指數:"
print   data_list[3]['PM2.5']
print  "狀態:"
print   data_list[3]['Status']

    
a = int(data_list[3]['PM2.5'])
print a

#PM2.5介於0~5亮綠燈
if  a>0 and a<5:
    board.digital[pin2].write(1)
    print 'good'
    
#PM2.5介於5~10亮藍燈 
elif a>=5 and a<=10:
    board.digital[pin3].write(1)
    print 'bad'
    
#PM2.5大於10亮紅燈   
elif a>10:
    board.digital[pin1].write(1)
    print 'warning'


else:
    board.digital[pin1].write(0)
    board.digital[pin2].write(0)
    board.digital[pin3].write(0)

成功!!

Differences between the A, CNAME, ALIAS and URL records

Differences between the A, CNAME, ALIAS and URL records

    The A record maps a name to one or more IP addresses, when the IP are known and stable. IP已知且穩定時,A記錄將名稱映射到一個或多個IP地址
    The CNAME record maps a name to another name. It should only be used when there are no other records on that name. CNAME記錄將名稱映射到另一個名稱。 只有在沒有其他記錄的情況下才能使用。
    The ALIAS record maps a name to another name, but in turns it can coexist with other records on that name. ALIAS記錄將名稱映射到另一個名稱,但是它可以與該名稱上的其他記錄共存。
    The URL record redirects the name to the target name using the HTTP 301 status code.

2017年11月19日 星期日

About AI

有限vs.無限 人的生命有限, 10的27次方也有限,但比我生命長,它對我來說是無限
BBS=> Blog => FB 社群 (Po文上去等人來砲) 本質一樣,換個包裝
VM的時代結束了
AI=玩數學,演算法,不是一般數學,高等數學
學習 錯誤 換演算法 ex SPAM mail rule
Computer只能做人類會做的事,只是它不會累,而且做得更好(but one-day programming )
IBM 華生醫生 望聞問切
切: IOT sensor收集data
資訊業=堆積木,Java programing高中生就能做,軟體工程師不用唸到大學
自駕車遇到事故,要保護誰? 撞到人誰賠? 車主自我犧牲誰要買自駕車.
如果一件事全公司只有你會:你很重要? 不會有成長!?
不要為了AI 而AI
最近紅,像Clouding一樣,紅一陣子?
Big Data vs. Big Fake Data: Line message 99% Fake
Facebook only transfer data but not verify data.

三部電影:
訂做積木: Microsof, Apple...etc.

2017年11月16日 星期四

Arduino, Python Firmata


$sudo pip install pySerial
$sudo pip install pyfirmata
$sudo pip install requests



upload firmata



>>> import pyfirmata
>>> pin = 13
>>> port = '/dev/cu.usbmodem1411'
>>> board = pyfirmata.Arduino(port)
>>> board.digital[pin].write(1)
>>> board.digital[pin].read()
1
>>> board.digital[pin].write(0)
>>> board.digital[pin].read()
0





1LED_Blink.py


#!/usr/bin/python



import pyfirmata
from time import sleep
pin = 13

port = '/dev/cu.usbmodem1411'
board = pyfirmata.Arduino(port) 
while True:
board.digital[pin].write(1) 
sleep(1)  
board.digital[pin].write(0) 
sleep(1)
board.exit()

Maxde-MacBook-Pro:downloads max$ python 1LED_Blink.py

跳出 Crtl+C
^CTraceback (most recent call last):
  File "1LED_Blink.py", line 11, in <module>
    sleep(1)  
KeyboardInterrupt
Maxde-MacBook-Pro:downloads max$

2console.py

#!/usr/bin/python

import pyfirmata
from time import sleep
pin = 13
port = '/dev/cu.usbmodem1411'
board = pyfirmata.Arduino(port) 
while True:
board.digital[pin].write(1) 
print(board.digital[pin].read()) 
sleep(1)  
board.digital[pin].write(0) 
print(board.digital[pin].read()) //Print out LED status
sleep(1)
board.exit()


^CTraceback (most recent call last):
  File "2console.py", line 14, in <module>
    sleep(1)
KeyboardInterrupt



4helloGUI.py  圖形化介面
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# This code is supporting material for the book
# Python Programming for Arduino
# by Pratik Desai
# published by PACKT Publishing

import Tkinter

# Initialize main windows with title and size
top = Tkinter.Tk()
top.title("Hello GUI")
top.minsize(200, 30)

# Label widget
helloLabel = Tkinter.Label(top,
                           text="Hello World! It is a course這是一個課程")
helloLabel.pack()

# Start and open the window

top.mainloop()

=====7csvWriter.py=====

#!/usr/bin/python
# This code is supporting material for the book
# Python Programming for Arduino
# by Pratik Desai
# published by PACKT Publishing

import csv

data = [[1, 2, 3],['a','b','c'],['Python','for','Arduino']]

with open('PythonforArduino.csv', 'wb') as f:
    w = csv.writer(f)
    for row in data:

        w.writerow(row)


=====7csvReader.py=====


import csv


f = open ('PythonforArduino.csv', 'r')
ll = f.read()
print ll

Run:


Maxde-MacBook-Pro:Downloads max$ python 7csvReader.py 

1,2,3
a,b,c
Python,for,Arduino


=====9plotLive.py=====
顯示電阻Status





2017年11月15日 星期三

RIDE Extract Keyword


RIDE核心部分:
Select steps -> Right click -> Extract Keyword



Create User Keyword



Keyword Arguments


Return variable...


Keyword Tags and  自建的Keyword 深監色








2017年11月14日 星期二

Windows Telnet Client


Could not execute Telnet



Install Tenlet Client by run as Adminstrator
dism /online /Enable-Feature /FeatureName:TelnetClient





tlntadmn [\\server] config port=PortNumber

tlntadmn [\\localhost] config port=139


tlntadmn config port=139



Check open TCP/IP port in Windows
netstat -an


Arduino the first time


1. Connect Arduino to MacBook by USB prot
2. Launch Arduino IDE


It will be auto-discovery 

Select connect com port



Pin A0-A5: Analog import
Digital: 0V - 5V?
TX/RX: 外部BT
L: LED pin 13
AT328P chip: 大腦


LED 長腳接+: Pin 13
Load Blink template:


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(200);                       // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second


Practice

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
  digitalWrite(12, LOW);  // turn the LED off by making the voltage LOW
  delay(500);             // wait for a second
  digitalWrite(13, LOW);
  digitalWrite(12, HIGH);
  delay(500);             // wait for a second


}

Pracetice: 呼吸燈


int led = 9;           // the PWM pin the LED is attached to Pin 9具備 PWM功能
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}

Notes: 0->255 then 255 -> 0 呼吸cycle


Practice:可變電阻



Practice: 感應式互動光源



2017年11月13日 星期一

RIDE 自定變數

幾個增加變數的方式

Right Click -> Extract Variable


Right Click -> New Scalar
New Scalar button




Import var_RF_Training.py as Variable 將變數寫在一個變數檔 .py
#--RF Training--
query1 = "Hello Variable"
msg_room1 = "C801" 

2017年11月11日 星期六

Mac OS Python version and pip install


Check system default python version

$python --version


Install pip and check pip version

$sudo easy_install pip



$pip --version





check_systemv1.1

 check_systemv1.1.bat 可用於電腦資產盤點 @echo off REM 後續命令使用的是:UTF-8編碼 chcp 65001 echo ***Thanks for your cooperation*** echo ***感謝你的合作*** timeout 1...