I have the following website:
<html>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#boton1').click(function(){
var dataString = $('#form1').serialize();
alert('Datos serializados: '+dataString);
$.ajax({
type: "POST",
url: "/cgi-bin/script.py",
data: dataString,
dataType: "json",
success: function(data) {
}
});
});
});
</script>
<body>
<form id="form1" data-ajax="false">
<label for="slider-fill">Input slider:</label>
<input type="range" name="slider-fill" id="slider-fill" value="90" min="0" max="180" data-highlight="true">
<input type="submit" value="Submit" id="boton1">
</form>
</body>
</html>
As you can see what it does is send information to /cgi-bin/script.py
and that information is serialized. The problem is that I don't know how to collect it in the script to later send it to an Arduino. This is what I have written in Python:
import cgi, cgitb
import serial
cgitb.enable()
ser=serial.Serial('/dev/ttyACM0',9600)
form = cgi.FieldStorage()
searchterm = form.getvalue('form1')
ser.write(str(searchterm) + "1")
It doesn't work for me and I don't really know what to do.
It probably won't work for you because you are confusing the parameters sent in the form.
Finally you must give execution permissions to the script with a
chmod a+x script.py
.In your HTML you have fields
<input>
that are referenced by their attributename
, not the form identifier,form1
.As I told you in the comments, it is important that the user
www-data
is in the groupdialup
. To add it do:In HTML you must modify the javascript to prevent the form submission using
event.preventDefault()
:If you wish, you do not need to serialize the entire form, you could also set up your own request with the data that best suits you as follows:
If we don't want to wait for the arduino to reset every time we open the port, we can create this perl script to disable the use of DTR (the signal that tells the arduino that we just opened the port) :
To install
Device::SerialPort
you must run:The script would have to be executed only once each time the Arduino is connected to the USB port.
PS: As we discussed in the chat, the latter does not work on Raspberry Pi.