Arduino reboot sketch missed

Discussion in 'UDOO NEO' started by nomida, Feb 26, 2016.

  1. nomida

    nomida New Member

    Joined:
    Feb 22, 2016
    Messages:
    15
    Likes Received:
    0
    Hi all,
    I am hit this problem. I upload a sketch program through arduino IDE and it works fine, however when I reboot the board I must have to re-upload the sketch.
    After the reboot run minicom and I saw some values from M4 but after that is freeze and when I re-upload the skecth it start to work fine.

    Do you know what can be?

    The arduino programm is usigin all the ADC ports plus SDA and SCL ports (16,17)

    Thank you
    Matteo
     
  2. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Do you use serial communication in the sketch? Does it send data right after reboot? The m4 is much faster live after reboot and can fill the serial buffer before the A9 side can read it.
    So wait with sending data after reboot.
     
  3. nomida

    nomida New Member

    Joined:
    Feb 22, 2016
    Messages:
    15
    Likes Received:
    0
    Yes it is exactly what I have.

    So, do you think the best approach it will be to wait in setup section for almost 1min?
     
  4. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Could be possible, but better is the A9 side send a serial command to the sketch to start sending data. How do you read the data on the A9/Linux side?
     
    Andrea Rovai likes this.
  5. nomida

    nomida New Member

    Joined:
    Feb 22, 2016
    Messages:
    15
    Likes Received:
    0
    I am reading the serial port with Python.

    import serial
    ser = serial.Serial('/dev/ttyMCC', 115200, serial.EIGHTBITS , serial.PARITY_NONE ,timeout=1)
    lineread = ser.readline().rstrip()

    ok, I think with your solution also resolved another my problem.
    In another post I wrote that after 9 hours the board was stuck, so for sure the serial communication is the problem because
    I have another udoo that is working since last week without serial communication and it is still running.


    Thank you
     
  6. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    I am not close to my board but I believe I have timeout set to zero. As this is a virtual serial connection it is not necessary to have a timeout of 1, it only slows it down.
     
    Andrea Rovai likes this.
  7. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    This is what I use:
    Python:
    arduino = serial.Serial('/dev/ttyMCC',115200,timeout=0)
    arduino.flushOutput()
    arduino.flushInput()
    serial_data = arduino.read()

    Arduino:
    Serial.begin(115200);
    delay(100);
    Serial.println("W"); //send commands to python
     
    Andrea Rovai likes this.
  8. nomida

    nomida New Member

    Joined:
    Feb 22, 2016
    Messages:
    15
    Likes Received:
    0
    Hi,
    the problem is still there when I reboot the board I have to re-upload the sketch.


    However if I want to share this code with you:


    void setup() {
    // initialize serial:
    Serial.begin(115200);

    }
    int count = 0;
    void loop() {
    Serial.println(count);
    count = count + 1;
    if(Serial.available() > 0)
    {
    count =0;
    }

    }

    When I send I a value to the serial from A9:
    echo "3" > /dev/ttyMCC

    the board is not working anymore.

    However when I reboot the Udoo the sketch is there and running.

    Matteo
     
  9. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Matteo, what are you trying to do?
    You are sending but not reading.

    What I understand is that you have a sketch that is sending serial data right after it starts.
    I propose you do the following in your Arduino Sketch (in basic, it is not programming code):
    Also check https://www.arduino.cc/en/Tutorial/SerialEvent for reading serial events

    Setup {
    setup serial connection
    Program_state = wait // for command
    }
    Loop {
    If not program_state = wait {
    Read sensor data
    Send data to serial
    }
    else {
    read serial connection for command
    if something received, parse it
    if valid set program_state = go​
    }

    When you start the Udoo Neo the arduino sketch will do nothing until you give the command through the serial connection.
    Command can be any string like for example "3"
    You can give the command with echo "command" > /dev/ttyMCC or do it in Python where you also read your serial data coming from the arduino sketch.
    In this way you are always certain that the Neo is booted properly else you cannot give the serial command.
    Also no buffer can be filled with data until you read it.
     
    Last edited: Feb 29, 2016
  10. nomida

    nomida New Member

    Joined:
    Feb 22, 2016
    Messages:
    15
    Likes Received:
    0
    I just want to see count increase when I am not sending any data form the A9 and when I send something I wish to reset the variable count.

    Code:
    void setup() {
      // initialize serial:
      Serial.begin(115200);
      // reserve 200 bytes for the inputString:
    
    }
    int count = 0;
    void loop() {
      Serial.println(count);
      count = count + 1;
      if(Serial.available() > 0)
      {
        int a = Serial.read();
        count =0;
      }
    
    }
    If I do it, minicom stays stuck and the other terminal, where I lunched the echo command, too.
     
  11. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    As I already told you Neo does not like to fill the serial buffer. You are now only sending, not reading, already right after reboot and filling that serial buffer.
    For Serial monitoring you can also use the build in serial monitor of the Arduino IDE.

    What happens if you do this, if you send a string to arduino it will return it.
    Code:
    int incomingByte = 0;   // for incoming serial data
    
    void setup() {
             Serial.begin(115200);     // opens serial port, sets data rate to 115200 bps
             delay(100);  // small delay to be sure connection is established
    }
    
    void loop() {
    
             // send data only when you receive data:
             if (Serial.available() > 0) {
                     // read the incoming byte:
                     incomingByte = Serial.read();
    
                     // say what you got:
                     Serial.print("I received: ");
                     Serial.println(incomingByte, DEC);
             }
    }
     
  12. nomida

    nomida New Member

    Joined:
    Feb 22, 2016
    Messages:
    15
    Likes Received:
    0
    I had already tried it and with our example I have the same problem.
    I attach the screenshot. I don't understand if the problem is in minicom or the serial port is losing some data.
     

    Attached Files:

  13. nomida

    nomida New Member

    Joined:
    Feb 22, 2016
    Messages:
    15
    Likes Received:
    0
    First when I do

    while true; do echo "1" > /dev/ttyMCC ; sleep 1; done
    I can see several times blink the LED, instead of to see the LED blink once.

    After few times the board is stuck.
    This is my code,

    Code:
    void setup()
    {
      Serial.begin(115200);
      delay(100);
      ctrl = 1;
      pinMode(LED, OUTPUT); 
    
    
    
      digitalWrite(pinLed, HIGH);
    //I2C
      ads1015.begin();
      ads1015.setGain(GAIN_ONE);
    //  pinMode(LED, OUTPUT); 
    }
    
    
    int count = 0;
    void loop()
    {
    
    
    
          if (Serial.available() > 0) {
            int16_t temp = ads1015.readADC_SingleEnded(0);
            int16_t pm25 = ads1015.readADC_SingleEnded(1);
            Serial.print(analogRead(CO_WE));
            Serial.print(",");
            Serial.print(analogRead(CO_AE));
            Serial.print(",");
            Serial.print(analogRead(O3_WE));
            Serial.print(",");
            Serial.print(analogRead(O3_AE));
            Serial.print(",");
            Serial.print(analogRead(NO2_WE));
            Serial.print(",");
            Serial.print(analogRead(NO2_AE));
            Serial.print(",");
            Serial.print(pm25);
            Serial.print(",");
            Serial.print(temp);
            Serial.println();
            Serial.print(count);
            Serial.println();
    
            digitalWrite(LED, HIGH);
            delay(100);
            digitalWrite(LED, LOW);
          }
     
    
    
    //  delay(1000);
    }
     
  14. nomida

    nomida New Member

    Joined:
    Feb 22, 2016
    Messages:
    15
    Likes Received:
    0
    Ok,
    I think that I found the error.
    First of all my mistake because I didn't read well what flush was doing, so you are right I have to read the Serial communication to pop out the data from the buffer.

    BTW my main problem still there. if I reboot the board the sketch program gone.
    Code:
    void setup()
    {
      Serial.begin(115200);
      while (!Serial);
      delay(100);
      ctrl = 1;
      analogReadResolution(12);
      pinMode(LED, OUTPUT); 
    
      digitalWrite(pinLed, HIGH);
    }
    int delayADC = 10;
    int count = 0;
    void loop()
    {
    if (Serial.available() > 0 && count == 1) {
           
            while(Serial.available() > 0){
              Serial.read();
            }
             
            int16_t temp = ads1015.readADC_SingleEnded(0);
            int16_t pm25 = ads1015.readADC_SingleEnded(1);
    
            int16_t CO_w = analogRead(CO_WE);
            delay(delayADC);
            int16_t CO_a = analogRead(CO_AE);
            delay(delayADC);
            int16_t O3_w = analogRead(O3_WE);
            delay(delayADC);
            int16_t O3_a = analogRead(O3_AE);
            delay(delayADC);
            int16_t NO2_w = analogRead(NO2_WE);
            delay(delayADC);
            int16_t NO2_a = analogRead(NO2_AE);
            String ss = String(CO_w)+ ','+String(CO_a)+ ','+String(O3_w)+ ','+String(O3_a)+ ',' +String(NO2_w)+ ','+String(NO2_a)+ ',' + String(pm25)+ ','+String(temp);
    //        String ss = String(CO_w)+ ','+String(CO_a)+ ','+String(O3_w)+ ','+String(O3_a)+ ',' +String(NO2_w)+ ','+String(NO2_a)+ ',' + String(temp)+ ','+String(pm25)+',' +String(Serial.available());
    
            Serial.println(ss);
           
           
            digitalWrite(LED, HIGH);
            delay(100);
            digitalWrite(LED, LOW);
    
          }
     
  15. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    You are still not waiting with sending data. Your sketch is starts as soon as the serial connection is ready. But are you ready for reading it right after a reboot? Do not rely on serial.available() for starting to send. But program it to wait until you send a command to it to start sending.

    And if you load the arduino blink demo and reboot it will stay loaded?
     
  16. nomida

    nomida New Member

    Joined:
    Feb 22, 2016
    Messages:
    15
    Likes Received:
    0
    Uhm, I am lost.
    To summarize:
    The python program ask M4 to send some data. What I am undressing from you last post is to use Serial.avaiable() -> read the value -> see if the data received is a know command -> if yes send data otherwise skip.

    You said: "You are still not waiting with sending data.Your sketch is starts as soon as the serial connection is ready."
    Yes the sketch start as soon as the serial connection is ready, do I have to place a delay before to Serial.begin() ? or I just need to fix the previous point?

    Right now I don't have with me and I can't try.

    Thank you
     
  17. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Serial.Read will not wait for something to receive. It will just read and go on wether something received or not. You have to validate that you really have received something and compare it with the string you would like to send as a command.
     
  18. nomida

    nomida New Member

    Joined:
    Feb 22, 2016
    Messages:
    15
    Likes Received:
    0
    Ok great.

    I used the blink example and I added the serial communication and it works also after reboot.

    The last problem I have

    Code:
    void setup() {
      Serial.begin(115200);
      while (!Serial);
      pinMode(13, OUTPUT);
    }
    
    
    void loop() {
      digitalWrite(13, HIGH);
      delay(1000);
      digitalWrite(13, LOW);
      delay(1000);
      if (Serial.available() > 0 ) {    
            while(Serial.available() > 0){
              char ii= (char)Serial.read();       
              if(ii==49){
                Serial.println(ii);
                Serial.flush();
              }
            }
          
      }
    }
         
     
  19. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Great! I don't see a problem here, just some quick and dirty code :)
    You don't have to serial.flush Just be sure you are reading the data on the A9 side also else you will fill the buffer.
     
  20. nomida

    nomida New Member

    Joined:
    Feb 22, 2016
    Messages:
    15
    Likes Received:
    0
    Ops, I upload the wrong CODE and I didn't finish the sentence. Sorry.

    The problem wasn't in serial communication but in I2C

    When I setup I2C into the sketch after the reboot I have the following errors in dmesg

    [ 43.601094] systemd-udevd[240]: timeout 'accelerometer /devices/virtual/input/input3'
    [ 44.638614] systemd-udevd[240]: timeout: killing 'accelerometer /devices/virtual/input/input3' [351]
    [ 44.831250] systemd-udevd[240]: 'accelerometer /devices/virtual/input/input3' [351] terminated by signal 9 (Killed)

    and when I try access to the values from the I2C the board becomes stuck.
    But when I re-upload the sketch everything works fine.
    Now, I am using IC2_2 so the pinout SDC and SDA.

    Which are the IC2 port available in M4 side?

    Thank you
     

Share This Page