Recent Posts

Pages: [1] 2 3 ... 10
1
Arduino Due / DUE. Problems with Serial.Read...
Last post by eppeeppe - Today at 10:29 am
I have problems getting a code working on the DUE, that works on the UNO with no problems. I am trying to read a HTTP-request from a 3G-shield (by Cooking-Hacks) through Serial.Read.

However, when I run this code, it does't return any values to data[]. I read that the DUE sometimes has problems with serial.available/serial.read and needs a firmware update. Is this also the case with the 3rd revision board I just bought? Or is it something else that blocks it? I also use the atoi(), that I tried to comment out.

Here is my code for reference:
Code: [Select]
//

int8_t answer;
int onModulePin = 2, aux;
int data_size = 0;
int end_file = 0;
char *p, *i;
char *text;
char *filename;
char *date;
char *time2;


char aux_str[50];

char data[400];
int http_x;
int http_status;   // 0: end, 1 waiting data with header, 2 waiting data without header, 3 timeout waiting data, 4 unknow response
int x = 0;
long previous;


//Write here you SIM data
const char pin_number[] = "1001";
const char apn[] = "internet";
const char user_name[] = "";
const char password[] = "";

//


char url[ ]="www.infraordinary.dk";
int port= 80;
char request[ ]="GET /display_vars.php?test HTTP/1.1\r\nHost: www.infraordinary.dk\r\nContent-Length: 0\r\n\r\n";



void setup(){

  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);

  Serial.println("Starting...");
  power_on();

  delay(3000);

  //sets the PIN code
  sprintf(aux_str, "AT+CPIN=%s", pin_number);
  sendATcommand(aux_str, "OK", 2000);

  delay(3000);

  while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) ||
    sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );


  // sets APN, user name and password
  sprintf(aux_str, "AT+CGSOCKCONT=1,\"IP\",\"%s\"", apn);
  sendATcommand(aux_str, "OK", 2000);

  sprintf(aux_str, "AT+CSOCKAUTH=1,1,\"%s\",\"%s\"", user_name, password);
  sendATcommand(aux_str, "OK", 2000);



}
void loop(){

  // request the url
  sprintf(aux_str, "AT+CHTTPACT=\"%s\",%d", url, port);
  answer = sendATcommand(aux_str, "+CHTTPACT: REQUEST", 60000);

  // Sends the request
  Serial.println(request);
  // Sends <Ctrl+Z>
  Serial.write(0x1A);
  http_status = 1;

  while ((http_status == 1) || (http_status == 2))
  {
    answer = sendATcommand("", "+CHTTPACT: ", 60000);
    if (answer == 0)
    {
      http_status = 3;
    }
    else
    {
      // answer == 1
      while(Serial.available()==0);
      aux_str[0] = Serial.read();

      if ((aux_str[0] == 'D') && (http_status == 1))
      {
        // Data packet with header
        while(Serial.available()<4);
        Serial.read();  // A
        Serial.read();  // T
        Serial.read();  // A
        Serial.read();  // ,

        // Reads the packet size
        x=0;
        do{
          while(Serial.available()==0);
          aux_str[x] = Serial.read();
          x++;
        }
        while((aux_str[x-1] != '\r') && (aux_str[x-1] != '\n'));

        aux_str[x-1] = '\0';
        data_size = atoi(aux_str); // ATOI COULD BE THE PROBLEM

        // Now, search the end of the HTTP header (\r\n\r\n)
        do{
          while (Serial.available() < 3);

          data_size--;
          if (Serial.read() == '\r')
          {
            data_size--;
            if (Serial.read() == '\n')
            {
              data_size--;
              if (Serial.read() == '\r')
              {
                data_size--;
                if (Serial.read() == '\n')
                {
                  // End of the header found
                  http_status = 2;
                }
              }
            }  
          }
        }
        while ((http_status == 1) && (data_size > 0));

        if (http_status == 2)
        {
          // Reads the data
          http_x = 0;
          do{
            if(Serial.available() != 0){
              data[http_x] = Serial.read();
              http_x++;
              data_size--;
            }
            else
            {
              delay(1);
            }
          }
          while(data_size > 0);
          data[http_x] = '\0';
        }
      }
      else if ((aux_str[0] == 'D') && (http_status == 2))
      {
        // Data packet with header
        while(Serial.available()<4);
        Serial.read();  // A
        Serial.read();  // T
        Serial.read();  // A
        Serial.read();  // ,

        // Reads the packet size
        x=0;
        do{
          while(Serial.available()==0);
          aux_str[x] = Serial.read();
          x++;
        }
        while((aux_str[x-1] != '\r') && (aux_str[x-1] != '\n'));

        aux_str[x-1] = '\0';
        data_size = atoi(aux_str);

        do{
          if(Serial.available() != 0){
            data[http_x] = Serial.read();
            http_x++;
          }
          else
          {
            delay(1);
          }
        }
        while(data_size > 0);
        data[http_x] = '\0';

      }
      else if (aux_str[0] == '0')
      {
        // end of the AT command
        http_status = 0;
      }
      else
      {
        // unknow response
        http_status = 4;
        Serial.print(char(aux_str[0]));
        Serial.print(char(Serial.read()));
        Serial.print(char(Serial.read()));
        Serial.print(char(Serial.read()));
        Serial.print(char(Serial.read()));
        Serial.print(char(Serial.read()));
        Serial.print(char(Serial.read()));
        Serial.print(char(Serial.read()));
        Serial.print(char(Serial.read()));
      }        
    }      
  }

  if (http_status == 0)
  {
    Serial.print(F("HTTP data: "));
    Serial.println(data);
    Serial.print("Data_Size: ");
    Serial.println(data_size);
    Serial.print("HTTP_x: ");
    Serial.println(http_x);
    Serial.print("Serial buffer: ");
    Serial.println(SERIAL_BUFFER_SIZE);
        Serial.print("aux_str: ");
    Serial.println(aux_str);
    
  }
  else
  {
    Serial.print(F("http_status: "));
    Serial.println(http_status, DEC);
  }



  //delay(1000);

}

void power_on(){

  uint8_t answer=0;

  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0)
  {
    // power on pulse
    digitalWrite(onModulePin,HIGH);
    delay(3000);
    digitalWrite(onModulePin,LOW);

    // waits for an answer from the module
    while(answer == 0){
      // Send AT every two seconds and wait for the answer
      answer = sendATcommand("AT", "OK", 2000);
    }
  }

}


int8_t sendATcommand(   char* ATcommand,
char* expected_answer1,
unsigned int timeout)
{

  return sendATcommand2(   ATcommand,
  expected_answer1,
  expected_answer1,
  timeout);
}

int8_t sendATcommand2(   char* ATcommand,
char* expected_answer1,
char* expected_answer2,
unsigned int timeout)
{

  uint8_t x=0,  answer=0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100);    // Initialize the string

  delay(100);

  while( Serial.available() >= 2) Serial.read();    // Clean the input buffer

  Serial.println(ATcommand);    // Send the AT command


    x = 0;
  previous = millis();

  // this loop waits for the answer
  do{

    if(Serial.available() != 0){
      response[x] = Serial.read();
      x++;
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer1) != NULL)
      {
        answer = 1;
      }
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer2) != NULL)
      {
        answer = 2;
      }
    }
    // Waits for the asnwer with time out
  }
  while((answer == 0) && ((millis() - previous) < timeout));

  return answer;
}



On the UNO I had to increase the buffer to 400. I tried the same in RingBuffer.h for due DUE (Using IDE 1.5.4 and 1.6.1), without it helping.

I would really appreciate if someone out there have any ideas to what might cause the problem.

Thanks!
2
When I try to compile ardupilot library 2.8.1 using Arduino 1.6.2 it gives following error.(Please see attached text file). But this library compile without any error with Arduino 1.0.6. Why this happen????  :-\ 
3
Project Guidance / Re: Please help in creating pr...
Last post by Robin2 - Today at 10:29 am
However, by analizing the commands Flexscan3D is sending to the Arduino, I realized there are no end markers, it just jumps from one command to the next. So I am thinking some kind of limit of characters (in this case, 6 is the max amount of characters sent by Flexscan) could be used.
Is there a start marker?
Is there a distinct time interval between messages?
Even if all the commands are the same length it would be almost impossible to guarantee reliable reception without some form of marker.

Can you post an example of a succession of (say) 6 commands from Flexscan.

...R
4
Hardware / Re: RESISTORI
Last post by astrobeed - Today at 10:29 am
Lascia perdere, recenti studi hanno evidenziato un bug di principio nella teoria (il miele attira gli insetti, e quando la rana cerca di mangiarseli, si aggroviglia su se stessa) ... :smiley-mr-green: :smiley-mr-green: :smiley-mr-green:
Ecco perché non riesco mai a farlo funzionare per più di cinque minuti prima di dover sostituire la rana  :smiley-mr-green:
5
Another question, how come the compiler won't just let me add

Code: [Select]
  sendMp3Command2( 0x0002 ); // play 0002.mp3

Underneath your first command?
6
Hi ,
I am new to Arduino programming
I have a laser sensor with serial output of baudrate or 115200
i want to store its values in an array and later transmit it via I2C
but still i am unable to store it in an array
help needed its urgent please
7
Bar Sport / Re: [OT ITA] Lo spamm bar
Last post by astrobeed - Today at 10:26 am
Se vuoi posso deprimerti ancora di piu raccontandoti come va a finire ... :P :P :P
Il fumetto lo leggo pure io, visto che la serie lo segue abbastanza fedelmente so come dovrebbe evolvere la situazione, però un conto è vedere dei disegni su carta, un conto è vedere gli zombi fatti a pezzi dal vivo  :smiley-mr-green:

E' incredibile come tutti i film e le serie che parlano di zombie riescono ad ottenere sempre un discreto successo anche se sono trash come Znation di SyFy :)
8
Programming Questions / Re: Displaying DTMF Buffer Arr...
Last post by AWOL - Today at 10:26 am
Please post your code.
9
Français / Re: [besoins d'aide sur mon Pr...
Last post by ZiederSTI - Today at 10:25 am
En gros mon problème est : moduler l'éclairage lorsque un usager est détecté .

J'ai essayé de rentrer ce programme sous arduino:

Code: [Select]
const int detectorPin = 3; //detecteur connecté sur la broche 3
const int ledPin = 9; // LED connectée sur la broche 9(broche PWM)

int value = 0; // variable pour stocker ma valeur de la tension lue
byte flag=0;

void setup()
{
 pinMode(ledPin, OUTPUT);  // configure la broche en sortie 
 pinMode(detectorPin, INPUT); // Configure la breche en entrée
 analogWrite (ledPin, 0); // résultat d'analogWrite entre 0 et 255, donc la la led est éteinte.
}

void loop() {

if (detectorPin == HIGH && flag == 0) {

flag = 1; // evite que le programme se répète en boucle

do
{   

 analogWrite(ledPin, value);
value+=5 ;
delay(5); // pour donner un effet "fade : atténué la luminosité"
}while (value<255); // jusqu'à 255 (100%)
 
delay (500) ;  //tempo de 500ms


}


if (detectorPin == LOW && flag ==1) {

flag =0;

do
{   

 analogWrite(ledPin, value);
value-=5 ;
delay(5);
}while (value>0);
 
delay (500) ; 

}

}


Voila j'aimerais savoir si ce programme peut fonctionner pour ma problématique ou si il manque des choses, des variables ou un branchement sur un mauvais port etc ...
Car pour l'instant cela ne fonctionne pas. Après je me suis peut être trompé dans le câblage mais j'aimerais m'assurer que le programme est bon :)
10
Thank you for answering.
I'm not using any library. This is the code I use to test the motors and esc.
Code: [Select]
int motorSpeed = 0;
void setup()
{
  Serial.begin(9600);
pinMode(9,OUTPUT);

}
void loop(){
 
  analogWrite(9,motorSpeed);
 
  if (Serial.available()){
 motorSpeed = Serial.parseInt();   
  }
}


It only works on pins 9, 10 and 11. I think it doesn't work on pins 5 and 6 because of the different frequency, but I don't understand what happens on pin 3.
Pages: [1] 2 3 ... 10

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy