processing 用來送串列資料
Arduino IDE 用來寫程式
python 用來抓取網頁更新
processing 間隔5秒送出 ACSII 1
import processing.serial.*;
Serial ser;
void setup () {
size(400,400);
ser= new Serial(this, "COM7", 9600);
for(;; delay(5000)){
ser.write(49);
//delay(150);
//ser.write(76);
}
}
void draw () {
line (0,0,200,100);
}
Arduino 偵測收到 1 就搖搖鈴
#include <Servo.h>
Servo myservo;
const int RINGTIMES = 5;
byte b = 0;
void setup() {
Serial.begin(9600);
myservo.attach(14);
myservo.write(90);
}
void loop() {
while (Serial.available()) {
b = Serial.read();
delay(2);
}
if (b == 49) {
ring(RINGTIMES);
b = 0;
}
}
void ring(int times){
for(int i = 0; i < times; i++){
myservo.write(125);
delay(150);
myservo.write(90);
delay(150);
}
}
用spyder 撰寫 python程式,產生一個網頁計數器
from flask import Flask
app = Flask(__name__)
count = 0
@app.route('/')
def index():
with open('./counter.txt','r') as f:
count = int(f.readline())
count = count + 1
with open('./counter.txt','w') as f:
f.write(str(count))
return "<p>Hello visitor. There are "+str(count)+' visitors </p>'
if __name__ == '__main__':
app.run(debug=True)
用 processing 讀取 計數器的值 如有修改 就送出 "1" 到 串列埠
import processing.serial.*;
Serial ser;
String lines[];
int counter = 0;
void setup(){
ser = new Serial(this, "COM7",9600);
surface.setVisible( false );
for(;; delay(5000)){
lines = loadStrings("c:\\1\\counter.txt");
print(int(lines[0])," ");
if (int(lines[0]) != counter){
counter = int(lines[0]);
ser.write(49);
}
}
}
void draw(){
}
Arduino 接收串列埠,如果讀到 "1" 就 搖鈴
#include <Servo.h>
Servo myservo;
const int RINGTIMES = 5;
byte b = 0;
void setup() {
Serial.begin(9600);
myservo.attach(14);
myservo.write(90);
}
void loop() {
while (Serial.available()) {
b = Serial.read();
delay(2);
}
if (b == 49) {
ring(RINGTIMES);
b = 0;
}
}
void ring(int times){
for(int i = 0; i < times; i++){
myservo.write(125);
delay(150);
myservo.write(90);
delay(150);
}
}