Page 1 of 1

Web control for leds and motors

Posted: Sat Apr 12, 2008 8:07 am
by Kevin
I'm trying to do a project to control leds and motors on the website. I plan to use XAMMP ( ) for Apache, PHP, and MySQL because I have worked with PHP and MySQL before.

I'm thinking about using struct for each object (led, motor) like this

Code: Select all

struct obj
    int id;  //0, 1 ,2
    char name[6]; // 'led', 'motor'
    char func1[8]; // 'on', 'off', 'flashing', 'speed'
    char func2; // 0 -255 <--- this is for motor's speed

And RS-232 (Serial) will be use to communicate with PIC uC, the basic diagram is shown below:

Code: Select all

led1, led2, motor1, motor2 <--> PIC <--> MAX232 <-> Serial Cable <--> Computer <---> Web server (Apache, PHP, MySQL) <--> Website

1 - Website is requested (open the linl)
2 - PHP accesses MySQL database to load the last settings (led1 = on, led2=off, motor1 = speed, motor1 = 255, motor2 = off)
3 - The users can change the settings and hit "Submit" -> settings are save in MySQL.
4 - Using $_POST to get all the settings and serialize the data to a bit stream.
5 - Invoke a local .exe file (I will probably do in C#) to send data to serial
6 - PIC receives the data, deserialize it and use interrupt to change all the objects states.

But I'm not sure how to serialize and deserialize the data to be transferred and received, also I don't know how to use interrupt in PIC either.

Any suggestions would be appreciated!
Thank you very much.

Posted: Sat Apr 12, 2008 5:15 pm
by ThePyroElectro
Hey Kevin,

That seems like a pretty cool project :D . To answer your questions...

But I'm not sure how to serialize and deserialize the data to be transferred and received, also I don't know how to use interrupt in PIC either.

You really won't have to do too much because serial functions are built into C/C++ libraries both for a standard windows/linux compiler & the c18 compiler. However you will need to add Max233A to translate the RS232 signals that come out of the PC so the PIC can understand it. Check out the autonomous drifter project I did to see an example of this. :)

For understanding how to use and make your own program on a PC for sending/receiving serial data take a look at this:

Regarding interrupts. You can enable interrupts to occcur when an 8-bit transmit or receive has completed.

:arrow: Interrupt for receiving
Flag bit RCIF will be set when reception is complete and an interrupt will be generated if enable bit RCIE was set.

:arrow: Interrupt for transmitting
.....TXREG register is empty and flag bit TXIF (PIR1<4>) is set. This interrupt can be enabled/disabled by setting/clearing enable bit TXIE ( PIE1<4>).

:idea: In my Wooden Menace project there is an example of how to invoke interrupts, although that one uses the timer interrupt, you will be able to get a feel for how they look and operate. :wink:

So since this is in the request-a-project section, did you want me to do something like this in a pyro project? Let me know if you have any other questions! :P

Posted: Sun Apr 13, 2008 6:24 pm
by Kevin
Hi Chris,

What I mean by serializing and (de|un)serializing is that the way to convert all the data members' values into a string and transmit it, kind of like serialize() function in PHP. For example:

Code: Select all

$obj = array('id' => 1, 'name' => 'led');
$str = serialize($obj);
echo $str.'<br>';

$obj2 = unserialize($str);
echo '<pre>';


Code: Select all


    [id] => 1
    [name] => led

Also exec() function in PHP can be used to execute an exe file with some argument:

Code: Select all

// $str is the serialized content
$command = "c:\serial.exe ".$str;

For Serial Communication from the PC (serial.exe) I plan to use the following code in C#:

Code: Select all

using System.IO.Ports;

private static void SendSampleData() {

    SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
    port.Write("Hello World");
    port.Write(new byte[] {0x01, 0x02, 0x03}, 0, 3);


Thanks for your guidance, I will definitely go look into how to use interrupts for PIC18F451.

If possible, can you do a project with just leds (on, off, flashing)? :P I'll try to do the rest :D


Posted: Sun Apr 13, 2008 8:04 pm
by Kevin
Actually someone made a serial class for PHP so I think I'm gonna use it instead of doing C#. ... &cid=8658A

Posted: Mon Apr 14, 2008 6:57 pm
by ThePyroElectro
Hey Kevin,

Dear God...someone made a PHP class to utilize the serial port on the server? :shock: Some people have way too much time on their hands. However this should help you greatly Kevin.

Let me know how it goes, I'll try to think of something nifty to do for a project that uses this sort of 'remote control'. Let me know if you have any specific ideas beyond blinking LEDs. :wink: