Pages: [1]   Go Down
Author Topic: Processing Arduino Input/Output from C++/Python to PHP/Javascript  (Read 1211 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My basic idea is to read the input and output from the Arduino to the next in the transfer to the PHP / Javascript. I have tried using two methods.

1. By using C + + as a terminal to receive data from PHP. A later series of C + + is sent to the Arduino Board. It went really well. But can not run opposite because I struggle to find a way to run the Arduino Boards C + + to process the output from Arduino.

2. By using Python and PYserial
By using Python and PYSerial to do that, but I had difficulty in finding a way to receive the serial / data from Python to PHP.


Can anyone help or provide any other solution? I really appreciate your help.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50411
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My basic idea is to read the input and output from the Arduino to the next in the transfer to the PHP / Javascript.
Right from the beginning, this does not make sense.

The Arduino can read serial data and it can send serial data. It cares not a whit what is sending it data, or what it is sending data to, except that there can only be one thing on the other end of the serial port.

PHP and Javascript are completely separate things.

Start by describing what is sending serial data to the Arduino, and what you want the Arduino to do with that serial data.

A mention of which Arduino you are using, on what operating system, with what version of the IDE, running what sketch, would be useful, too.
Logged

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

Thanks for the reply.
The purpose of my idea to create a control lamp, which lights can be controlled with a switch panel or by computer (web server). In the computer can see the condition of the lights being lit or not, even though the lights turned on / off manually with a switch panel on the wall.

I am using Arduino IDE 1.0, UNO Arduino board, Windows PC

Arduino Code
Code:
char inData[20];
char inChar=-1;
byte index = 0;
int out[]={2,3,4,5,6,7,8,9,10,11,12};

void setup() {
     for(int i=0;i<=11;i++) { pinMode (out[i], OUTPUT); }
    Serial.begin(9600);
}

char Comp(char* This) {
    while (Serial.available() > 0) {
        if(index < 19)
        { inChar = Serial.read(); inData[index] = inChar; index++; inData[index] = '\0'; }
    }
    if (strcmp(inData,This)  == 0) {
        for (int i=0;i<19;i++) { inData[i]=0; }
        index=0;
        return(0);
    }
    else { return(1); }
}

void loop()
{
    if (Comp("1pin2")==0) { digitalWrite(2,HIGH); Serial.println("1pin2"); } if (Comp("0pin2")==0) { digitalWrite(2,LOW);Serial.println("0pin2"); }
    if (Comp("1pin3")==0) { digitalWrite(3,HIGH); Serial.println("1pin3"); } if (Comp("0pin3")==0) { digitalWrite(3,LOW);Serial.println("0pin3"); }
    if (Comp("1pin4")==0) { digitalWrite(4,HIGH); } if (Comp("0pin4")==0) { digitalWrite(4,LOW); }
    if (Comp("1pin5")==0) { digitalWrite(5,HIGH); } if (Comp("0pin5")==0) { digitalWrite(5,LOW); }
    if (Comp("1pin6")==0) { digitalWrite(6,HIGH); } if (Comp("0pin6")==0) { digitalWrite(6,LOW); }
    if (Comp("1pin7")==0) { digitalWrite(7,HIGH); } if (Comp("0pin7")==0) { digitalWrite(7,LOW); }
    if (Comp("1pin8")==0) { digitalWrite(8,HIGH); } if (Comp("0pin8")==0) { digitalWrite(8,LOW); }
    if (Comp("1pin9")==0) { digitalWrite(9,HIGH); } if (Comp("0pin9")==0) { digitalWrite(9,LOW); }
    if (Comp("1pin10")==0) { digitalWrite(10,HIGH); } if (Comp("0pin10")==0) { digitalWrite(10,LOW); }
    if (Comp("1pin11")==0) { digitalWrite(11,HIGH); } if (Comp("0pin11")==0) { digitalWrite(11,LOW); }
    if (Comp("1pin12")==0) { digitalWrite(12,HIGH); } if (Comp("0pin12")==0) { digitalWrite(12,LOW); }
    delay (1000);
   
  }

Python
Code:

import serial
import gtk
import MySQLdb
import datetime

ser=None

def sendSerial(widget, ch):
    global ser

    print("Sending "+ch)
    ser.write(ch)
    conn = MySQLdb.connect (host = "localhost", user="root", passwd="", db="arduino")
    cursor = conn.cursor()
   
    var1 = ser.readline()
    pin = str(var1[1:])
    pin2 = int(var1[4:])
    st = var1[:1]
    list = ['pin1','pin2','pin3','pin4','pin5','pin6','pin7','pin8','pin9','pin10','pin11','pin12','pin13']
    pin3 = list[pin2-1]

    timenow = datetime.datetime.now()
    timeform = timenow.strftime('%Y-%m-%d %H:%M:%S')
    cursor.execute ("""INSERT INTO data (value, date, status) VALUES (%s,%s,%s)""", (pin, timeform, st))
    cursor.execute ("""UPDATE pin SET status = %s WHERE pinname = %s""", (st, pin3))
    cursor.close ()
    conn.commit()
    conn.close ()

def main():
    global ser
    ser = serial.Serial('COM2', 9600)


    if (ser):
        print("Serial port " + ser.portstr + " opened.")

 
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.connect("destroy", gtk.main_quit)
 
    vbox=gtk.VBox()
    window.add(vbox)
    row1=gtk.HBox()
    off1 = gtk.Button("1 off")
    off1.connect("clicked", sendSerial, "0pin2")
    row1.pack_start(off1)
    vbox.pack_start(row1)
    on1 = gtk.Button("1 on")
    on1.connect("clicked", sendSerial, "1pin2")
    row1.pack_start(on1)
 
    row2=gtk.HBox()
    vbox.pack_start(row2) # vertical box starts at the bottom
    off2 = gtk.Button("2 off")
    off2.connect("clicked", sendSerial, "0pin3")
    row2.pack_start(off2)
    on2 = gtk.Button("2 on")
    on2.connect("clicked", sendSerial, "1pin3")
    row2.pack_start(on2)

    row3=gtk.HBox()
    vbox.pack_start(row3)
    off3 = gtk.Button("3 off")
    off3.connect("clicked", sendSerial, "0pin4")
    row3.pack_start(off3)
    on3 = gtk.Button("3 on")
    on3.connect("clicked", sendSerial, "1pin4")
    row3.pack_start(on3)

    row4=gtk.HBox()
    vbox.pack_start(row4)
    off4 = gtk.Button("4 off")
    off4.connect("clicked", sendSerial, "d")
    row4.pack_start(off4)
    on4 = gtk.Button("4 on")
    on4.connect("clicked", sendSerial, "4")
    row4.pack_start(on4)

    row5=gtk.HBox()
    vbox.pack_start(row5)
    off5 = gtk.Button("5 off")
    off5.connect("clicked", sendSerial, "e")
    row5.pack_start(off5)
    on5 = gtk.Button("5 on")
    on5.connect("clicked", sendSerial, "5")
    row5.pack_start(on5)
 
    window.show_all()
    gtk.main()
   
if __name__ == "__main__":
    main()

Is there a way to be able to send a series of other web applications like PHP to be read by Python and then proceed to the Arduino?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50411
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    if (Comp("1pin2")==0) { digitalWrite(2,HIGH); Serial.println("1pin2"); } if (Comp("0pin2")==0) { digitalWrite(2,LOW);Serial.println("0pin2"); }
The Comp function returns a char, whose value is 0 or 1. Why a char? Why not a more logical byte?

Code:
char Comp(char* This) {
The keyword this has very specific meaning in C++. A variable with the same name, except for a capital letter is confusing. In this case, it is also meaningless.

Code:
        for (int i=0;i<19;i++) { inData[i]=0; }
You don't understand C strings, do you? A string is NULL terminated, as you do in the while loop. It takes only one NULL to terminate the loop.

Code:
        return(0);
    }
    else { return(1); }
The parentheses are not needed.

Quote
Is there a way to be able to send a series of other web applications like PHP to be read by Python and then proceed to the Arduino?
PHP is not a web application. It is a scripting language that is particularly well suited to creating and manipulating text files that a web browser can consume. You can't "send a web application" anywhere.

You could, and probably should, get an ethernet shield for your Arduino. Connect the Arduino to the router, configure the router properly, and any web browser, PHP script, Python script, etc. can talk to the Arduino, sending it data, and getting data from it.
Logged

Pages: [1]   Go Up
Jump to: