Author Topic: Writing to DigiUSB from PHP or Command Line?  (Read 12169 times)

digi_guy

  • Jr. Member
  • **
  • Posts: 87
Writing to DigiUSB from PHP or Command Line?
« on: November 29, 2013, 06:30:52 pm »

I have the Echo example set up, and I would like to be able to write to it from a PHP script, or alternatively from the Linux command line (PHP could then use echo...)  Anyone know how?

To give a bit of background.  Right now I have the Digispark connected to my Raspberry Pi.  I then use Python to send commands to turn pins on and off. 

The next step is to be able to do this via a server hosted on my RPi.  Ideally I'd like to avoid having to use PHP to talk to Python to talk to the Digispark.  So I was wondering if anyone knows what the command line script would be to send "hello." 

digi_guy

  • Jr. Member
  • **
  • Posts: 87
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #1 on: November 29, 2013, 08:55:28 pm »
After banging my head against the wall for the past few hours I've gotten close.

The Digispark is showing up as /dev/hiddev0, and each time I plug it in I need to set permission
$ sudo chmod a+rw /dev/hidraw0

If I then type $ echo "test" > /dev/hidraw0 I'm pretty sure it sends something and the spark gets something.  But I have no idea how to read.

I've also attempted to set up a php script based on  php_serial.class.php that looks something like this:

<!DOCTYPE html>
<html>
        <?php
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

        include "php_serial.class.php";
        $serial = new phpSerial;
        $serial->deviceSet("/dev/hidraw0");
        $serial->deviceOpen();
        $read = $serial->readPort();
        echo "message spark:   $read      ";

        $serial->sendMessage("1");
        echo "sent";

        $read = $serial->readPort();
        echo "$read";

        $serial->deviceClose();
        echo "closed";

        ?>
    </body>
</html>


But that doesn't work, I get an error that the "Specified serial port is not valid"

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #2 on: November 29, 2013, 11:54:40 pm »
I imagine php_serial class is trying to talk to it as a serial port - but it is a raw hid port, not a serial port - how to make that work is beyond my knowledge - but a work around might be to use send and receive executables provided with  the Digispark IDE and just use exec from php

digi_guy

  • Jr. Member
  • **
  • Posts: 87
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #3 on: November 30, 2013, 02:46:32 pm »
You were right!  I modified send.py a bit and use the command
exec("sudo python /var/www/send.py $msg");

The trick is that you have to go into /etc/sudoers and add www-data to the users
www-data ALL=(ALL) NOPASSWD: ALL

Thanks!


digi_guy

  • Jr. Member
  • **
  • Posts: 87
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #4 on: November 30, 2013, 03:14:10 pm »
Just to continue with my documentation. 

I managed to get TinyWireS.h working by copying Tiny85_I2C_Slave_Ex and making a few changes, the address is then set to 0x26

from php I can write the command
exec("sudo i2cset -y 1 0x26 0x00 0x00")

which I don't understand but the set up blinks to let me know something was received.

What i find interesting is that this method has a significantly faster response time from when the php is executed to when the LED blinks.  For some reason executing the python script is painfully slow.  I suspect that the send.py has a lot of libraries being loaded each time it gets called.


digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #5 on: December 01, 2013, 02:46:33 am »
try the straight C++ command line tools that are included with the Digispark IDE (if you have python ones with yours then you have an old copy) - they should run MUCH faster than the python

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #6 on: December 02, 2013, 06:59:13 am »
You might find the information in this thread useful:

http://digistump.com/board/index.php/topic,944.msg3623.html

Until I made those changes to DigiUSB, I wasn't able to properly read data from the Digispark (because it's overloading the feature report functionality in a non-standard way).

I haven't tried using the /dev/hidraw interface in Linux myself, so this is about all the advice I can offer, right now.

digi_guy

  • Jr. Member
  • **
  • Posts: 87
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #7 on: December 02, 2013, 05:25:19 pm »
I had looked through the discussion you linked but I'm not familiar with Java at all so most of it went over my head. 

At the moment I2C is working great so my spark is wired deep into my robot.  This weekend I'll probably pull it all apart and look at making USB work. 

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #8 on: December 03, 2013, 06:36:21 am »
You can ignore the Java-related bits. But the first code block in the first message of the thread is a change to be made in DigiUSB.cpp, and there are also notes about changing some defines in usbconfig.h. Making those changes helped me a lot when I was trying to use the nodejs HID library to communicate with my Digisparks.

Without those changes, you have to use lower-level libusb calls to read data from the Digispark over USB. With the changes, you can use the standard HID read and write calls.

digi_guy

  • Jr. Member
  • **
  • Posts: 87
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #9 on: December 09, 2013, 01:45:21 pm »
try the straight C++ command line tools that are included with the Digispark IDE (if you have python ones with yours then you have an old copy) - they should run MUCH faster than the python

I'm not sure which file your referring to, could you point me in the right direction?

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #10 on: December 10, 2013, 11:09:54 pm »
The download here: http://digistump.com/wiki/digix/tutorials/software
Within that go to DigiUSB Programs

digi_guy

  • Jr. Member
  • **
  • Posts: 87
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #11 on: December 11, 2013, 06:26:41 pm »
Got it up and running.  I didn't realize how many usb libraries had to be updated.  Then it was as easy as typing "make" in the send source folder. 

Thanks for your help.

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #12 on: December 11, 2013, 09:50:46 pm »
mind sharing the raspberry pi binaries for anyone else looking for them?

digi_guy

  • Jr. Member
  • **
  • Posts: 87
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #13 on: December 13, 2013, 10:40:20 am »
I'd be happy to but I have no idea how.

joshipoditunes

  • Newbie
  • *
  • Posts: 3
Re: Writing to DigiUSB from PHP or Command Line?
« Reply #14 on: December 14, 2013, 11:22:41 am »
I'd be happy to but I have no idea how.

It depends on which OS you have, another linux computer is the easiest.
http://raspberrypi.stackexchange.com/questions/311/how-do-i-backup-my-raspberry-pi