Pages: [1]   Go Down
Author Topic: Arduino to Python or PHP via USB  (Read 1239 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am working on trying to setup a garage door monitor/opener. The Arduino will be connected to my home server via USB. Running Centos 6.4 within virtualbox running in Windows 7
I have the sketch written and it responds properly from the serial monitor in the Arduino software, but I have not been able to capture the data via PHP nor Python.
I am using Pyserial and the php_serial.class which are both able to send to the Arduino without a problem. If I cat the serial port while running the PHP script I see the output but nothing from PHP.
With python cat will close right away but if I use tail -f python will show the results.


The sketch:
Code:
const int garPin = 13; //set pin to trigger relay
const int buttonPin = 2; //set pin for button input
int garageInput; //set variable to open/close door
int buttonState; //set variable for button status
int reqState; //variable to request status
void setup()
{
  Serial.begin(9600); //opens serial port and sets baud rate
  pinMode(garPin, OUTPUT); //set relay trigger as output
  pinMode(buttonPin, INPUT); //set button as input
}

void loop()
{
  //open or close door
  if (Serial.available() > 0)
  {
    garageInput = Serial.read();
    switch (garageInput)
    {
      //open/close door
    case 49:
      digitalWrite(garPin, HIGH); //short wires to open/close
      delay(2000); //wait 2 seconds
      digitalWrite(garPin, LOW); //un shorts wires
      delay(20000); //wait for door to open/close 20 seconds
      Serial.read(); //clear the serial buffer
      Serial.read();
      Serial.read();
      Serial.println("done");
      break;
      //check status
    case 83:
      buttonState = digitalRead(buttonPin);
      if(buttonState == 1)
      {
        Serial.println("0");
      }
      if(buttonState == 0)
      {
        Serial.println("Closed");
      }
      break;
    }
  }
}


The PHP test code
Code:
<?php
//include PHP Serial Class
include "php_serial.class.php";
//Initialize the class
$serial = new phpSerial();
//set serial port
$serial->deviceset("COM1");
//setup serial port
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);




//find current status of door
$serial->deviceOpen();
//sleep(1);
$serial->sendMessage("S");
//sleep(1);
$status $serial->readPort();
//$serial->deviceClose();
echo $status;
print_r ($status);
$serial->deviceClose();
?>


And the Python code
Code:
#! /usr/bin/env python

import serial
import time
ser = serial.Serial('/dev/ttyS0', 9600, timeout=2)
ser.write('S')
print ser.readline()

Any help would be greatly appreciated.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The Arduino will be connected to my home server via USB. Running Centos 6.4 within virtualbox running in Windows 7
Have you tested the functionality on a real box, first? Using a VM for initial testing doesn't make sense to me.

Code:
      Serial.read(); //clear the serial buffer
      Serial.read();
      Serial.read();
A load of crap. There may be nothing to read, in which case this is a waste of effort, of there may be 50 bytes, in which case this is equally useless.

Code:
    case 49:
?

Wouldn't
Code:
    case '1':
be easier to understand/remember what to send?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did try it on a full Linux pc as well, same results.

The multiple serial.Read() were for another problem I thought I had, I forgot to remove them.

As for using 49 vs 1 that is what I was receiving on the Arduiono...once I can read it's response I can go on to tidying up the rest of the code, but for now I know that works.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
As for using 49 vs 1 that is what I was receiving on the Arduiono
In response to sending what? I doubt that you pressed the key labeled 49.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When I would send a 1 to the Arduino. I'm sure it is either how I am sending the characters or how I am reading in that they are converted to their ASCII equivalent. However, for now, the Arduino understands what I am requesting and executes the appropriate code.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
However, for now, the Arduino understands what I am requesting and executes the appropriate code.
Sure it does. That's not the issue. The issue, and you are free to ignore it, is this. Will you remember, when you come back to this sketch in three months, just what to do to get the Arduino to receive a 49? Would you know what to do to get the Arduino to receive a '1'?

Quote
I'm sure it is either how I am sending the characters or how I am reading in that they are converted to their ASCII equivalent.
It is. It is because you are storing the value in an int, not a char. The Serial.print() method is overloaded, and handles ints and chars differently.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

While I appreciate your concern for me remembering that 83 is S I would like to keep this on topic.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
//find current status of door
$serial->deviceOpen();
//sleep(1);
$serial->sendMessage("S");
//sleep(1);
$status = $serial->readPort();
//$serial->deviceClose();
echo $status;
print_r ($status);
$serial->deviceClose();
Open the serial port, causing the Arduino to reset. While it's booting, send some data. Expect an immediate response. Then, close the serial port, resetting the Arduino again.

Expect something other than no reply.

Hey, I think I see the problem. It's that expectation that is wrong.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you again for your reply. I thought that it might be a problem with resetting, which is why the sleep(1) lines are in there, although commented out currently.
Also, when running 'cat /dev/ttyS0' while the PHP script is run I capture a response from the Arduino with the cat.

And since my original post I have added a resistor to prevent it from restarting.
Logged

Pages: [1]   Go Up
Jump to: