Projets:Perso:2011:OpenShima
Le projet fonctionne avec python et les libraires suivantes :
pywebsocket : http://code.google.com/p/pywebsocket/
pyserial : http://pyserial.sourceforge.net/
Jquery min : http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js
Voila le code source de la partie python : def web_socket_transfer_data(request): value = "" while True: try : usbport = "/dev/ttyACM0" ser = serial.Serial(usbport, 9600, timeout=1) temp = "" line = "" try : while (temp != ";"): temp = ser.read() line += temp print line line = str(line[-4:-1]) print line ser.close() if line[0] == "0": line = "1%s"%line msgutil.send_message(request, line) value = line except Exception as e : print e print "value %s"%value msgutil.send_message(request,value) if line == _GOODBYE_MESSAGE: return except : msgutil.send_message(request,value)
Le code de la page html :
<html>
<head>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
var ws;
if ("WebSocket" in window) {
debug("Horray you have web sockets. Trying to connect...");
ws = new WebSocket("ws://localhost:9998/echo");
ws.onopen = function() {
// Web Socket is connected. You can send data by send() method.
debug("connected...");
ws.send("hello from the browser");
ws.send("more from browser");
};
run = function() {
var val=$("#i1").val(); // read the entry
$("#i1").val(""); // and clear it
ws.send(val); // tell erlang
return true; // must do this
};
ws.onmessage = function (evt)
{
//alert(evt.data);
var data = evt.data;
document.getElementById("Potar").innerHTML = "<p>"+data+"</p>";
};
ws.onclose = function()
{
debug(" socket closed");
};
} else {
alert("You have no web sockets");
};
function debug(str){
$("#debug").append("<p>" + str);
};
});
</script>
</head>
<body>
<h1>Interaction experiment</h1>
<h2>Debug</h2>
<div id="debug"></div>
<fieldset>
<legend>Potar</legend>
<div id="Potar">I am a clock</div>
</fieldset>
</body>
</html>
Le code uploadé sur le arduino :
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor
This example code is in the public domain.
*/
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println("<A0>");
Serial.print(sensorValue, DEC);
Serial.println("</A0>");
}
Le code de lancement : import os #test des differents ports serial #capsule jeu sucette : js: os.system("python mod_pywebsocket/standalone.py -p 9998 -w ../example/ &") os.system("/opt/google/chrome/google-chrome index.html &")
Diverses notes sur le projet : Il a fallut encapsuler les données du Open Fukushima vu que la connection est pourrie.