Trouble running a motor via a cgi sctipt

I'm trying to power a brushless motor using a CGI script on a website, executed by python. My setup is as follows

Web server running on Raspberry Pi hosting a website with a button and a slider. This is connected to Arduino (DUE) via a USB to the programming port. When a button is pressed, or the slider is used, a python CGI script is run, the script is as follows:

import cgi
form = fieldStorage()

#!/usr/bin/env python
import cgi
form=cgi.FieldStorage()

import json

ser = serial.Serial('dev/ttyACM0', 9600)
ser.write("%s\n" % (form["value"]))
ser.close()

print "Content-type: application/json"
print
print(json.JSONEncoder().encode({"status":"ok"}))

What I expected to happen: Activate set the slider on the website to 50, the motor runs at 50%, set it to 20 runs at 20% etc.

What happens: When I do anything on the website, the motor activates and spins up in it's initial spin, then stops, no matter what value is entered.

Any ideas?

There many stages from your script to the motor. Have you checked these intermediate results?
In any case you need to give much more detail for anyone to help.

The below part of your code is probably causing the arduino to reset. You may need a 100 ohm resistor between the arduino +5v and reset pins to defeat the reset.

ser = serial.Serial('dev/ttyACM0', 9600)
ser.write("%s\n" % (form["value"]))
ser.close()

From testing a few things I definitely think you're right that it's the auto-restart causing the problem.

I've just tried your suggestion and it didn't seem to work.

Do you know of any programatic way of stopping the auto-reset? Either through Python or on the Raspberry Pi?

Others have used something like a 5mf capacitor between the arduino reset pin and ground to defeat the reset. You can open and close the serial monitor and watch the arduino LED to see if it looks like it is resetting.

I tested with a very basic sketch:

void setup() {                
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  delay(3000);  
}

void loop() {
  if (Serial.available() > 0) {
    byte incomingByte = Serial.read();
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }
  digitalWrite(13, HIGH);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(100);              // wait for a second
}

And your python script (bugfixed), executing it from the command line:

#!/usr/bin/env python
import cgi, serial, json
form=cgi.FieldStorage()

ser = serial.Serial('/dev/ttyUSB0', 9600)
ser.write("%s\n" % "ABC")
ser.close()

print "Content-type: application/json"
print
print(json.JSONEncoder().encode({"status":"ok"}))

The blinking does not indicate any type of reset of the Arduino. I'm running this on a Linux box -- not the Raspberry Pi.

I also checked the pySerial documentation and, by default, it does not use hardware reset.

Are you sure that your motor circuit itself is not causing the reset? Try putting a delay in your python script to see if it really is the short execution of the script that causes the behavior?

Chagrin:
I tested with a very basic sketch:

void setup() {                

Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  delay(3000); 
}

void loop() {
  if (Serial.available() > 0) {
    byte incomingByte = Serial.read();
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }
  digitalWrite(13, HIGH);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(100);              // wait for a second
}




And your python script (bugfixed), executing it from the command line:



#!/usr/bin/env python
import cgi, serial, json
form=cgi.FieldStorage()

ser = serial.Serial('/dev/ttyUSB0', 9600)
ser.write("%s\n" % "ABC")
ser.close()

print "Content-type: application/json"
print
print(json.JSONEncoder().encode({"status":"ok"}))




The blinking does not indicate any type of reset of the Arduino. I'm running this on a Linux box -- not the Raspberry Pi. 

I also checked the pySerial documentation and, by default, it does not use hardware reset.

Are you sure that your motor circuit itself is not causing the reset? Try putting a delay in your python script to see if it really is the short execution of the script that causes the behavior?

No luck I'm afraid. From what I'm seeing around the place it looks like it's related to the machine that's opening the serial port, so in this case the raspberry pi. Some computers you can turn it off, but not with the pi.

That's what it looks like anyway... well will just have to find a way around!

Cheers.