Show Posts
Pages: [1] 2 3 4
1  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem sending datas from Arduino to the computer. on: March 10, 2013, 05:58:46 pm
Quote
Why is the length bigger and what are the character that have been added?
Send me your computer so I can see the screen, and I'll tell you. Otherwise, you'll need to print each character in hex to find out. I'd guess that a carriage return and line feed were added somewhere.

Sending my computer to Seattle will takes too much time; time to go and time to go back to Spain, and I need the computer.
Probably you're right and the characters added are carriage return and line feed. I will try to convert the string to hex to see what character are.
Thank you for the idea. I am a bit lost sometimes.
2  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem sending datas from Arduino to the computer. on: March 10, 2013, 03:15:00 pm
Pointers and arrays are very closely related. You can think of an array as a collection of objects, but in reality the declaration itself is nothing more than the address of the first element of the array. Perhaps this makes it clear(er) why you can't simply use == to compare a string literal (stored in one location in memory) with an array (a pointer to another location in memory).

I undestand what you say, but I don't know how to solve it.
I have another doubt with Java.
When I execute this code I see that the length of contenido is two bytes bigger that I expected, so I can't compare it with the array that I am looking for.
Why is the length bigger and what are the character that have been added?
Code:
inString = puerto.readString();
  println(inString);
  tipo = inString.charAt(0);
   if (tipo == 'I')
    {
      contenido = inString.substring(2);
      println(contenido.length());
      println(tipo);
      println(contenido);
  }
3  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem sending datas from Arduino to the computer. on: March 10, 2013, 12:48:48 pm
Quote
Could you explain me why I can't use the first form?
recibido is a pointer to the start of a block of memory.

"mas" is a string literal. Is there any way that a pointer and string literal can possibly be equal?

Only in the same way that a tennis shoe and an elephant can be equal.

strcmp() returns -1 if the strings are in one order, 0 if they are equal, and +1 if they are in the opposite order. It does NOT return a boolean, so the boolean usage is wrong.


The use of a int as a boolean is not a good way in programming, but sometimes works. In this cases probably works, and it is easy to use. But I understand that is not correct. I'll change it.
I didn't know that 'recibido' was a pointer, I thought that 'recibido' was the contents of the array. Because of that I didn't understand what was the problem.
4  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem sending datas from Arduino to the computer. on: March 10, 2013, 12:10:35 pm
Quote
I have to save 'recibido' in a String because and array has the character '\0' at the end, so if I use 'recibido' in the statement of 'if' function, this doesn't works.
No, you don't. The NULL at the end of a char array makes it a string, and people dealt with strings for 40 years before there were Strings. The strcmp() function will tell you if two strings are equal, with wasting ANY resources.

I have make some changes in the program based in your advices, but I have found another "problem"; if I write 'recibido=="mas"' in the 'if' statement, the statement is never true. I solved it writting '!strcmp(recibido,"mas")'.
For example:
Code:
if (recibido == "mas")
is never true.
Code:
if (!strcmp(recibido,"mas"))
is true when I have sent 'mas' through the serial port.

Could you explain me why I can't use the first form?
*recibido is a char array.

Now, I think that the program works right.
Thank you very much for your help.
5  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem sending datas from Arduino to the computer. on: March 10, 2013, 03:19:10 am
Quote
I have to save 'recibido' in a String because and array has the character '\0' at the end, so if I use 'recibido' in the statement of 'if' function, this doesn't works.
No, you don't. The NULL at the end of a char array makes it a string, and people dealt with strings for 40 years before there were Strings. The strcmp() function will tell you if two strings are equal, with wasting ANY resources.


Code:
longitud=Serial.readBytes(recibido,11);
I tried using 'recibido' in the stament of 'if' functions and this didn't work and in the way that I am doing now the program works.
I don't know much about programming, so many times I try in a way or in another way until I get that the program works.
I don't know much English too, so I am sure that my explanations are no clear and my grammar is not good enough.

I will use the function that you say to compate 'recibido' and what I need in the 'if' statement in order to understand what is the problem.

Thank you for your help.
I will write my progress.
6  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem sending datas from Arduino to the computer. on: March 09, 2013, 02:05:19 pm
I have changed:
Code:
longitud=Serial.readBytesUntil('\0',recibido,11);

For:
Code:
longitud=Serial.readBytes(recibido,11);

And the program works right.

I have to save 'recibido' in a String because and array has the character '\0' at the end, so if I use 'recibido' in the statement of 'if' function, this doesn't works.
Then:
Code:
String instruccion;
instruccion=recibido;
Are necessary.

This is the way that I have been able to make that the program works.
7  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem sending datas from Arduino to the computer. on: March 09, 2013, 01:41:56 pm
Code:
String instruccion;
First thing to do is get rid of that line.

Code:
      longitud=Serial.readBytesUntil('\0',recibido,11);
What is sending the serial data? None of the standard methods sends a NULL.

Code:
      instruccion=recibido;
Completely unnecessary.

You have a lot of Serial.print() statements. Perhaps the issue is that you are running out of SRAM. Wasting it using the String class does not help.
http://playground.arduino.cc/Code/AvailableMemory

I will look at all you say. I think you are right and I am creating unnecessary variables wasting memory.
I don't know much C language, so the most of the times I do the things in a long and complicate way; not efficient way.
However, I think that the code is in program memory (flash memory) and the variables are in the data memory (SRAM).
8  Using Arduino / Interfacing w/ Software on the Computer / Problem sending datas from Arduino to the computer. on: March 09, 2013, 06:19:21 am
I am doing a program which I sent an instruction from the computer to Arduino, and Arduino respond sending a short text to the computer.
My problem is that there is a line where if I try to send something different from "mas", Arduino doesn't send anything. Even the other Serial.println() instructions don't work.
When I send "mas" everything works right. I don't understand what is the problem and how to solve it.

The problematic line has a comment in the end, where I explain briefly the problem.

Code:
//Codigo adapted to Arduino Mega2560

int led = 13;
int dispCentenas = 6;
int dispDecenas = 7;
int dispUnidades = 8;
int A_1 = 2;
int B_1 = 3;
int C_1 = 4;
int D_1 = 5;
int A_2 = 9;
int B_2 = 10;
int C_2 = 11;
int D_2 = 12;
int sonda = A0;
int ledGrill = 14;
int ledRestAbajo = 15;
int ledEncendido = 16;
int ledRestEncendidas = 17;
int elegirRest = 18;
int marcha= 19;
int dismTemp = 20;
int aumTemp = 21;
int actGrill = 22;
int actRestAbajo = 23;
int tempSeleccionada = 100;
int tempHorno = 0;
int numero1[3] = {0,0,0};
int numero2[3] = {0,0,0};
int restElegidas;
boolean funcionando = false;
char recibido[11];
String instruccion;
int longitud;
int valorSonda;
int valores[4]={0,0,0,0};
int puntero=0;

void setup() {   
   
  pinMode(led, OUTPUT);
  pinMode(dispCentenas, OUTPUT);
  pinMode(dispDecenas, OUTPUT);
  pinMode(dispUnidades, OUTPUT);
  pinMode(A_1, OUTPUT);
  pinMode(B_1, OUTPUT);
  pinMode(C_1, OUTPUT);
  pinMode(D_1, OUTPUT);
  pinMode(A_2, OUTPUT);
  pinMode(B_2, OUTPUT);
  pinMode(C_2, OUTPUT);
  pinMode(D_2, OUTPUT);
  pinMode(ledGrill, OUTPUT);
  pinMode(ledRestAbajo, OUTPUT);
  pinMode(ledEncendido, OUTPUT);
  pinMode(ledRestEncendidas, OUTPUT);
  pinMode(elegirRest, INPUT);
  pinMode(marcha, INPUT);
  pinMode(dismTemp, INPUT);
  pinMode(aumTemp, INPUT);
  pinMode(actGrill, OUTPUT);
  pinMode(actRestAbajo, OUTPUT);
  analogReference(DEFAULT);
  Serial.begin(9600);
}

void loop() {
 // number=analogRead(A0);
  //number=/4;
  tempHorno=analogRead(A0);
  trocear(tempHorno, numero1);
  trocear(tempSeleccionada, numero2);
  digitalWrite(actGrill, LOW);
  digitalWrite(actRestAbajo, LOW);

  while (!funcionando)
  {
    if (Serial.available() > 0)
    {
      for (int i=0; i<12; i++)
      {
        recibido[i]='\0';
      }
      longitud=Serial.readBytesUntil('\0',recibido,11);
      instruccion=recibido;
    }
   
    valorSonda=analogRead(A0);
    convTemperatura();
    trocear(tempHorno, numero1);
    mostrar();
   
  if (digitalRead(elegirRest) || instruccion=="resist")
  {
    instruccion[0]='\0';
   
    if (restElegidas==2)
    {
      restElegidas=0;
    }
    else
    {
      restElegidas++;
    }
    switch (restElegidas)
    {
      case 1:
        digitalWrite (ledGrill, HIGH);
        break;
      case 2:
        digitalWrite (ledRestAbajo, HIGH);
        break;
       
      default:
        digitalWrite (ledGrill, LOW);
        digitalWrite (ledRestAbajo, LOW);
    }
    while (digitalRead(elegirRest))
    {
      mostrar();
    } 
    Serial.println("I resist");
  }
  if (digitalRead(marcha) || instruccion=="marcha")
  {
    instruccion[0]='\0';
    funcionando = true;
    digitalWrite(ledEncendido, HIGH);
    Serial.println("I marcha");
    while (digitalRead(marcha))
    {
      mostrar();
    }
  }
 
  if (digitalRead(dismTemp) || instruccion=="menos")
  {
    instruccion[0]='\0';
    tempSeleccionada = tempSeleccionada-10;
    trocear(tempSeleccionada, numero2);
    while (digitalRead(dismTemp))
    {
    delay(10);
    mostrar();
    }
    Serial.println("I menos");
  }
 
  if (digitalRead(aumTemp) || instruccion=="mas")
  {
    instruccion[0]='\0';
    tempSeleccionada = tempSeleccionada+10;
    trocear(tempSeleccionada, numero2);
    while (digitalRead(aumTemp))
    {
    delay(10);
    mostrar();
    }

    Serial.print("mas");  // If I try to send something different from "mas" in this line, Arduino doesn't send anything
  }

  }
  //-------------------FUNCIONANDO-----------------------
  while (funcionando)
  {   
    if (Serial.available() > 0)
    {
      for (int i=0; i<12; i++)
      {
        recibido[i]='\0';
      }
      longitud=Serial.readBytesUntil('\0',recibido,11);
      instruccion=recibido;
    }
    tempHorno=analogRead(A0);
    convTemperatura();
    trocear(tempHorno, numero1);
    mostrar();
    Serial.println(tempHorno);
    if (tempHorno < tempSeleccionada && restElegidas!=0)
    {
      tempHorno=analogRead(A0);
      trocear(tempHorno, numero1);
      mostrar();
      Serial.println(tempHorno);
      digitalWrite(ledRestEncendidas, HIGH);
      switch(restElegidas)
      {
        case 1:
          digitalWrite(actGrill, HIGH);
          break;
        case 2:
          digitalWrite(actGrill, HIGH);
          digitalWrite(actRestAbajo, HIGH);
          break;
        default:
          delay (20);
      }
    }
    else
    {
      tempHorno=analogRead(A0);
      Serial.println(tempHorno);
      digitalWrite(ledRestEncendidas, LOW);
      digitalWrite(actGrill, LOW);
      digitalWrite(actRestAbajo, LOW);
    }
    if (digitalRead(marcha) || instruccion=="paro")
    {
      instruccion[0]='\0';
      funcionando = false;
      digitalWrite(ledEncendido, LOW);
      digitalWrite(ledRestEncendidas, LOW);
      digitalWrite(actGrill, LOW);
      digitalWrite(actRestAbajo, LOW);
      Serial.println("parada");   
      while (digitalRead(marcha))
      {
        mostrar();
        delay(10);
      }     
    }
   
  }
}
//-----------------FIN DEL BUCLE LOOP---------------------
void trocear(int numero, int trozos[3])
{
  trozos[0]=numero %10;
  numero/=10;
  trozos[1]=numero % 10;
  numero/=10;
  trozos[2]=numero %10;
}

void mostrar()
{
  digitalWrite(dispUnidades, LOW);
  digitalWrite(dispDecenas, LOW);
  digitalWrite(dispCentenas, LOW);
  digitalWrite(A_1, numero2[0] & B0001);
  digitalWrite(B_1, numero2[0] & B0010);
  digitalWrite(C_1, numero2[0] & B0100);
  digitalWrite(D_1, numero2[0] & B1000);
  digitalWrite(A_2, numero1[0] & B0001);
  digitalWrite(B_2, numero1[0] & B0010);
  digitalWrite(C_2, numero1[0] & B0100);
  digitalWrite(D_2, numero1[0] & B1000);
  digitalWrite(dispUnidades, HIGH);
  delay(10);
 
  digitalWrite(dispUnidades, LOW);
  digitalWrite(dispDecenas, LOW);
  digitalWrite(dispCentenas, LOW);
  digitalWrite(A_1, numero2[1] & B0001);
  digitalWrite(B_1, numero2[1] & B0010);
  digitalWrite(C_1, numero2[1] & B0100);
  digitalWrite(D_1, numero2[1] & B1000);
  digitalWrite(A_2, numero1[1] & B0001);
  digitalWrite(B_2, numero1[1] & B0010);
  digitalWrite(C_2, numero1[1] & B0100);
  digitalWrite(D_2, numero1[1] & B1000);
  digitalWrite(dispDecenas, HIGH);
  delay(10);
 
  digitalWrite(dispUnidades, LOW);
  digitalWrite(dispDecenas, LOW);
  digitalWrite(dispCentenas, LOW);
  digitalWrite(A_1, numero2[2] & B0001);
  digitalWrite(B_1, numero2[2] & B0010);
  digitalWrite(C_1, numero2[2] & B0100);
  digitalWrite(D_1, numero2[2] & B1000);
  digitalWrite(A_2, numero1[2] & B0001);
  digitalWrite(B_2, numero1[2] & B0010);
  digitalWrite(C_2, numero1[2] & B0100);
  digitalWrite(D_2, numero1[2] & B1000);
  digitalWrite(dispCentenas, HIGH);
  delay(10);
  digitalWrite(led, LOW);
}

void convTemperatura()
{
  if (puntero==4) puntero=0;
  valores[puntero]=valorSonda;
  puntero++;
  for (int i=0; i<4; i++)
  {
    tempHorno+=valores[i];
  }
  tempHorno/=5;
  // queda realizar la conversion a grados
}
9  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem communicating Arduino Mega 2560 with computer using PHP on: March 09, 2013, 05:41:12 am
I will try to do the communication using Processing. In fact I am doing a simple interface and later I will do the communication.
I think that this will be better than PHP. Better interface and more posibilities.
Thank you for your help. Sincerely, I think I won't be able to do the communication using PHP.
10  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem communicating Arduino Mega 2560 with computer using PHP on: March 07, 2013, 02:37:37 am
Quote
Notice: Undefined variable: msg in /opt/lampp/htdocs/Arduino/prueba2.php on line 30
Fatal error: Call to a member function sendMessage() on a non-object in /opt/lampp/htdocs/Arduino/prueba2.php on line 30
Undefined variable: msg, the $msg = new phpSerial(); never run, since it at an other if block.
break code to one inline code, remove all the session code,  html link. if, else if. then test it again.
Code:
<?php
error_reporting
(E_ALL);
ini_set('display_errors''1');
require(
"php_serial.class.php");
$msg = new phpSerial();
$msg->deviceSet("/dev/ttyACM0"); // Arduino usb-port
$msg->confBaudRate(9600);  //baud rate
$msg->confParity("none");  //Parity
$msg->confCharacterLength(8); //Character length  
$msg->confStopBits(1);  //Stop bits
$msg->confFlowControl("none");
$msg->deviceOpen(); // open connection
//sleep(3);
//$mensaje='mas';
$mensaje='menos';
$msg->sendMessage($mensaje);
$msg->deviceClose();
?>


Quote
So the next time that I turn on the computer, I will have to give permissions again

U might need write udev rule to persistant device node name.


Using your code (activating 'sleep(3)') the program works, but with the problem that each time that I reload the page, Arduino is reseted. I need to avoid to reset Arduino each time that I send an instruction, and due to this I tried to avoid to open the connection each time that I need to send an instruction.
I think that I won't be able to do it using PHP.
11  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem communicating Arduino Mega 2560 with computer using PHP on: March 06, 2013, 05:07:36 pm
So I think the object is destroyed when I reload the page.
I don't have any idea to solve it  smiley-sad.

You answer yourself, you need put "require("php_serial.class.php");"   before you call session_start(), otherwise the object will not be deserialized correctly.

Code:
require("php_serial.class.php");
session_start();

<h3>If you're running Linux: remember to give permissions to www-data to access usbport ($ sudo chmod 777 /dev/ttyACM0)</h3>

No, No, No!!! unless you want open arduino port access for every one under the sun.

For Apache to access /dev/ttyACM0 www-data must be in the dialout or whatever group.

sudo adduser www-data dialout, restart Apache is the way to go.

BWT. did you prove  your code run well without put it into session variable?

I saw your PM today, sorry and hope this answer is not too later.






The way that I give permissions to access to the port, last only until I turn off the computer. So the next time that I turn on the computer, I will have to give permissions again. But that text is because was written in a example that I saw in a blog.
I don't know much about linux and PHP, so more or less I do what I see in other places or the people say here. Sometimes I don't know exactly what I am doing.
If I don't put it using session variables, I get this error:
Quote
Notice: Undefined variable: msg in /opt/lampp/htdocs/Arduino/prueba2.php on line 30

Fatal error: Call to a member function sendMessage() on a non-object in /opt/lampp/htdocs/Arduino/prueba2.php on line 30
And this is the code:
Code:
<?php
require("php_serial.class.php");
session_start();

if(isset(
$_GET['message'])){
if($_GET['message'] == "iniciar")
{
error_reporting(E_ALL);
ini_set('display_errors''1');
// echo ($msg);
$msg = new phpSerial();
$msg->deviceSet("/dev/ttyACM0"); // Arduino usb-port
$msg->confBaudRate(9600);  //baud rate
$msg->confParity("none");  //Parity
$msg->confCharacterLength(8); //Character length  
$msg->confStopBits(1);  //Stop bits
$msg->confFlowControl("none");
$msg->deviceOpen(); // open connection
}
else if($_GET['message'] == "cerrar")
{
session_close();
$msg->deviceClose();
}
else
{

$mensaje=$_GET['message'];

$msg->sendMessage($mensaje); //send the message
}

}
?>

    <html><body>


    <a href="prueba2.php?message=mas">Send instruction "mas"</a></br>
    <a href="prueba2.php?message=menos">Send instruction "menos"</a></br></br>
    <a href="prueba2.php?message=iniciar">Open session</a></br>
    <a href="prueba2.php?message=cerrar">Close session</a>

    </body></html>

This is because when I click on send any instruction, the page is reloaded and the object is destroyed. That is the reason because I thought of use session variables, but I think that the session variables don't save objects.
12  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem communicating Arduino Mega 2560 with computer through PHP on: March 06, 2013, 04:23:57 am
Hi,

Or maybe you can use php_serial.class.php class file. I used for couple project off my without any problem.

R.

I am trying using this clas but I have the same problem; when i open the connection, the Arduino is reset so I am trying toopen the connection only once but I don't know how to do it. I have tried using a session, but it doesn't works.
This is the code:
Code:
<?php
session_start
();
if(isset(
$_GET['message'])){
if($_GET['message'] == "iniciar")
{
error_reporting(E_ALL);
ini_set('display_errors''1');
// echo ($msg);
require("php_serial.class.php");
$_SESSION['serial'] = new phpSerial();
$_SESSION['serial']->deviceSet("/dev/ttyACM0"); // Arduino usb-port
$_SESSION['serial']->confBaudRate(9600);  //baud rate
$_SESSION['serial']->confParity("none");  //Parity
$_SESSION['serial']->confCharacterLength(8); //Character length  
$_SESSION['serial']->confStopBits(1);  //Stop bits
$_SESSION['serial']->confFlowControl("none");
$_SESSION['serial']->deviceOpen(); // open connection
}
else if($_GET['message'] == "cerrar")
{
session_close();
deviceClose();
}
else
{

$msg=$_GET['message'];

$_SESSION['serial']->sendMessage($msg); //send the message
}

}
?>

    <html><body>

    <h3>If you're running Linux: remember to give permissions to www-data to access usbport ($ sudo chmod 777 /dev/ttyACM0)</h3>

    <a href="prueba2.php?message=mas">Send instruction "mas"</a></br>
    <a href="prueba2.php?message=menos">Send instruction "menos"</a></br></br>
    <a href="prueba2.php?message=iniciar">Open session</a></br>
    <a href="prueba2.php?message=cerrar">Close session</a>

    </body></html>

When I have oppened the session and I try to send an instruction, I get this error:
Quote
Fatal error: main(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition &quot;phpSerial&quot; of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in /opt/lampp/htdocs/Arduino/prueba2.php on line 28

So I think the object is destroyed when I reload the page.
I don't have any idea to solve it  smiley-sad.
13  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem communicating Arduino Mega 2560 with computer through PHP on: February 23, 2013, 12:03:29 pm
Quote
What could I do to communicate the pc with Arduino? I don't mind to use other language.
Typically, you'd open the serial port, letting the Arduino reset. Then, you'd read from and write to the port. When the application on the PC ends, you'd close the serial port, letting the Arduino reset.

Opening the serial port, writing one message, and closing the serial port is what is killing you.

Why did you choose PHP? Perhaps if we knew that we could suggest alternatives.

I'll try to open the port, write and read from this and close at the end.
This communication is to control a oven (change the temperature, what resistor will be on, put on and off the oven,....)
I use PHP because is the language that I know a bit.
14  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem communicating Arduino Mega 2560 with computer through PHP on: February 23, 2013, 10:23:01 am
Quote
Without any doubt the reset happens when fopen() is executed
Yes, it does. And again then fclose() is executed.

Quote
but I want to solve it through software.
You won't solve it on the Arduino side with software alone. You might solve it on the PC side by making sure that fopen() does not do whatever explicitly causes the reset, but that would affect the entire kernel, so I don't think that's a good idea.

My idea is solve it with software on the pc.
What could I do to communicate the pc with Arduino? I don't mind to use other language.
15  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem communicating Arduino Mega 2560 with computer through PHP on: February 23, 2013, 03:51:30 am
Without any doubt the reset happens when fopen() is executed because the reset happens and a little bit later the variable is increased. This delay is due to the sleep() instruction.
Pages: [1] 2 3 4