Programming Arduino Sketch <--> Android App

Discussion in 'UDOO NEO' started by frudman, Oct 19, 2016.

  1. Miguel Palacios

    Miguel Palacios New Member

    Joined:
    Nov 1, 2016
    Messages:
    9
    Likes Received:
    0
    but, how? how you did? i can´t.
     
  2. Miguel Palacios

    Miguel Palacios New Member

    Joined:
    Nov 1, 2016
    Messages:
    9
    Likes Received:
    0
    how you did connect your Mac remotely with m4 and UDOO NEO with Android? i can't add udoofota in android
     
  3. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    @miguel if you download this unofficial and work in progress TEST image.
     
  4. Francesco

    Francesco Active Member

    Joined:
    Jun 23, 2015
    Messages:
    220
    Likes Received:
    110
    1. Do you mean serial (via J7) or the USB OTG? You can offload the apk using adb (the best thing is adbwireless) without cables. Same thing to program the M4, just use the network by hacking the platform.txt file in the Arduino15 directory.
    2. adb is not afficted by M4 at all (maybe did i misunderstood your question?)
    3. No ADK (that was released and deprecated instantly by Google). We have hidden all the complexity of the shared memory with the virtual serial, so you can read/write ttyMCC.
    4. Blocking or non-blocking is done at application level. I mean, most serial libraries allows you to set a read timeout, so you can get a blocking/non-blocking read behavoiur.

    Please note that I did not try to read/write /dev/ttyMCC yet! Permission denied issues would be probably fixed by chmod. For other issues I will investigate in the next days.
     
  5. frudman

    frudman Member

    Joined:
    Apr 15, 2014
    Messages:
    64
    Likes Received:
    14
    glad to hear about no need for ADK and USB. Those two made things quite complex at the app level.

    Reading/Writing ttyMMC seems a much simpler and cleaner solution.

    Unfortunately this is not [yet] working from the app side (on Android) and it's not clear what/where to apply chmod (or did you mean this is to be done by your code/kernel?).

    Since I use Xamarin/C# I probably have deeper access to the OS/kernel (via Mono) then through only Art/Java. But I'm not sure what file/device calls/methods I need to make (e.g. open ttyMCC as a simple file read/write stream? exclusive or shared? As a serial port?).

    I'm experimenting as we speak but any feedback would be appreciated. Thanks.

    Freddy.
     
  6. frudman

    frudman Member

    Joined:
    Apr 15, 2014
    Messages:
    64
    Likes Received:
    14
    [some] SUCCESS!!!
    Here's the code I used:

    On the Arduino side:
    Code:
    // basic constants
    const int buttonPin = 2;     // the number of the pushbutton pin
    const int ledPin =  13;      // the number of the LED pin
    
    // variables for pushbutton status
    int buttonState = 0;
    int notified = 0;
    
    void setup() {
    
      Serial.begin(115200);
     
      // initialize the LED pin as an output:
      pinMode(ledPin, OUTPUT);
      // initialize the pushbutton pin as an input:
      pinMode(buttonPin, INPUT);
    }
    
    void loop() {
    
      // read the state of the pushbutton value:
      buttonState = digitalRead(buttonPin);
    
      // check if the pushbutton is pressed (it is, the buttonState is HIGH)
      if (buttonState == HIGH) {
        // turn LED on:
        digitalWrite(ledPin, HIGH);
        if (notified == 0) {
          notified = 1;
          Serial.print("got a press\r\n");
        }
      } else {
        // turn LED off:
        digitalWrite(ledPin, LOW);
        notified = 0;
      }
    }
    And on the Android side (using C# but simple enough to get the idea):
    Code:
            private void readArduino ()
            {
                System.Threading.Tasks.Task.Run (() => {
                    try {
                        Console.WriteLine ("STARTED listenening for Arduino");
    
                        var tty = new System.IO.FileInfo ("/dev/ttyMCC");
                        var buffer = new byte [1024];
    
                        using (var strm = tty.Open (wio.FileMode.Open, wio.FileAccess.ReadWrite, wio.FileShare.None)) {
                            while (true) {
                                Console.WriteLine ("waiting for serial...");
                                var numRead = strm.Read (buffer, 0, buffer.Length); // block here?
                                if (numRead > 0) {
                                    Console.WriteLine ($"...got {numRead} bytes");
                                } else
                                    Console.WriteLine ("...nope");
                            }
                        }
                    } catch (Exception ex) {
                        Console.WriteLine ($"STOPPED listenening for Arduino: {ex.Message}");
                    }
                });
            }
    
    A couple of notes:
    1. I needed to CHMOD (as per your earlier note) to 777 (probably lesser perms will work as well); did this using ADB console and going into SU
    2. It seems (with no tests yet) that the Serial.print MUST end with '\r\n' otherwise the app, which blocks on the .READ (yey) doesn't come back and just waits there. Perhaps serial doesn't send data until '\r\n'? Or maybe my earlier code had a bug. I'll be doing more tests and let you know.
    Freddy
     
    Last edited: Nov 4, 2016
  7. frudman

    frudman Member

    Joined:
    Apr 15, 2014
    Messages:
    64
    Likes Received:
    14
    ...easy come, easy go...
    Tried it again (reloaded same app and reloaded same sketch) and no longer working: no problem opening the file (/dev/ttyMCC) but not reading anything from the Arduino.

    The sketch is working (LED goes on and off)
    The App is working (can see in console log "waiting for serial")
    But while pressing the button lights up the LED, nothing is seen from the app

    hmmm...
     
  8. frudman

    frudman Member

    Joined:
    Apr 15, 2014
    Messages:
    64
    Likes Received:
    14
    OK, got it working again, somewhat:
    1. I had to reboot the hardware (using tiny reset button)
    2. I had to re-CHMOD ttyMCC again (as per above) to 777
    3. CONFIRMED: MUST end Serial.print with '\n' for data to be transmitted to Android side
    4. Restarted the app and ttyMCC was being read again
    Tested it for a few button presses and it worked.
    Reloaded the App via (ADB) and the button presses were working for a bit
    ...BUT...
    Stopped being read after a few reads (about 15 presses). It's as if the app-side of the file was no longer working (maybe the Arduino side as well but not sure how to test for that).

    more testing to follow...
     
  9. Francesco

    Francesco Active Member

    Joined:
    Jun 23, 2015
    Messages:
    220
    Likes Received:
    110
    I have repacked the image: beta 2.1 https://drive.google.com/open?id=0B400VKg9Vc0reDRENzBPR3FIWmc
    - /dev/ttyMCC has now 666 permissions
    - added Serial port test app (flash a sketch using the serial port, like echo) - that's not totally stable btw
    I can privately share the source for this app during this stage if you need it (everything will be on github when ready). It uses JNI to natively open the serial port and standard java code for the application.
     
  10. Francesco

    Francesco Active Member

    Joined:
    Jun 23, 2015
    Messages:
    220
    Likes Received:
    110
    2. is fixed on the new beta
    3. it looks so strange! I had no time to check your code btw.

    even with the serial port test app the communication stops after a few messages. I'm looking on that.
     
  11. frudman

    frudman Member

    Joined:
    Apr 15, 2014
    Messages:
    64
    Likes Received:
    14
    great. downloading new image as we speak. The new perm will simplify things.
    But I think the problem is somwehere else. ttyMCC works for a bit then stops.
    Do you know if there's a way to "test" the state of Serial on the Arduino side and "reset it" also on the Arduino side?
     
  12. frudman

    frudman Member

    Joined:
    Apr 15, 2014
    Messages:
    64
    Likes Received:
    14
    Oh, good. Will keep trying other stuff here as well.

    And don't worry about my code. If something works in Java (Android/Art) it'll work with C#.

    Also, I note that all your examples finish their Serial with a printLN so I guess that info was there already (I hadn't picked up on that earlier: explains the '\n' so all good)
     
    Andrea Rovai likes this.
  13. frudman

    frudman Member

    Joined:
    Apr 15, 2014
    Messages:
    64
    Likes Received:
    14
    loaded your new image and no luck. In fact, the serial doesn't read at all anymore (from the app side).

    Did you change anything in the code?

    Also, serialport app not working at all for me. Maybe interaction between the 2 apps? Tried to remove our app but serial port still not working (and yes, I had modified the arduino code to echo input: code below)

    Code:
    // pins
    const int buttonPin = 2;     // the number for the pushbutton pin
    const int ledPin =  13;      // the number for the LED pin
    
    // status
    int buttonState = 0;
    int notified = 0;
    int pressed = 0;
    byte byteRead;
    
    void setup() {
      Serial.begin(115200);
      pinMode(ledPin, OUTPUT);
      pinMode(buttonPin, INPUT);
    }
    
    void loop() {
      buttonState = digitalRead(buttonPin); // read state of pushbutton...
      digitalWrite(ledPin, buttonState); // ...then make LED reflects that state
    
      if (buttonState == HIGH) {
        if (notified == 0) {
          notified = 1;
          if (pressed++ > 3) {
            pressed = 0;
            //Serial.end();
            //Serial.begin(115200);
            //Serial.flush(); // maybe periodic flush will help?
          }
          else
            //Serial.write(122);
            Serial.print("123-press\n"); // terminating '\n' REQUIRED for actual transmit to Android...
        }
      } else {
        notified = 0;
      }
    
      // echo, as fyi...
      if (Serial.available()) {
        byteRead = Serial.read(); // read most recent byte...
        Serial.write(byteRead); // ..then echo it
      }
    }
     
    Last edited: Nov 4, 2016
  14. Francesco

    Francesco Active Member

    Joined:
    Jun 23, 2015
    Messages:
    220
    Likes Received:
    110
    No, did not change other things related to serial.
    Please try this bootimage: https://drive.google.com/open?id=0B400VKg9Vc0rb3ZQOG1uYk5wYUE
    flash it over the first partition (eg. sdc1 or mmcblk0p1)

    Using the following sketch and the serial port api sample app I get something usable:
    Code:
    byte byteRead;
    
    void setup() {                
    // Turn the Serial Protocol ON
      Serial.begin(9600);
      pinMode(13, OUTPUT);
    }
    
    void loop() {
      if (Serial.available()) {
        byteRead = Serial.read();
        Serial.write(byteRead);
        digitalWrite(13, HIGH);
        delay(50);
        digitalWrite(13, LOW);
        delay(10);
      }
    }
     
  15. frudman

    frudman Member

    Joined:
    Apr 15, 2014
    Messages:
    64
    Likes Received:
    14
    I downloaded your image but don't know how to flash over first partition.

    Also, if you want to send me your private [java] source code, I can look at it and see if I can adapt it to our app (then maybe we can help you debug it). Xamarin/Mono/C# lets us use JNI as well. (my email is frederic.rudman@simplytel.com).
     
  16. Francesco

    Francesco Active Member

    Joined:
    Jun 23, 2015
    Messages:
    220
    Likes Received:
    110
  17. Miguel Palacios

    Miguel Palacios New Member

    Joined:
    Nov 1, 2016
    Messages:
    9
    Likes Received:
    0
    but, how i upload a sketch of arduino when my UDOO have Android as operative system? i don´t understand.
     
  18. frudman

    frudman Member

    Joined:
    Apr 15, 2014
    Messages:
    64
    Likes Received:
    14
  19. Francesco

    Francesco Active Member

    Joined:
    Jun 23, 2015
    Messages:
    220
    Likes Received:
    110
    Follow this: http://www.udoo.org/docs-neo/Arduino_M4_Processor/Programming_Arduino_M4_from_External_PC.html
    Install the IDE on your computer, add the board manager JSON and install the NEO package. Patch the NEO BSP to allow the programming via network instead of USB ("Upload a sketch over Network") replacing 192.168.7.2 with your board IP address.
    Then just click "upload sketch" on your IDE.

    When everything will be stable, proper docs will be published. I promise!

    @frudman: did it work?
     
  20. frudman

    frudman Member

    Joined:
    Apr 15, 2014
    Messages:
    64
    Likes Received:
    14
    unfortunately, no. spent the weekend working on it (even tried to go directly to linux via shared .so libraries and skipping the whole jni layer). I'm afraid I hit multiple issues (on my end, mostly due to limitations on the platform I'm using: I have an additional layer of complications because I'm trying to go through Monotouch: this simplifies some aspect of dev but complicates the jni interface). Therefore no progress on my end. Interestingly, your demo used the same code (from Cedric Priscal) as I I tried a few weeks ago. In that place I did notice some open issues which may or may not have anything to do with our problem. Look in particular at the [unresolved] issue #51 (also issues 44, 45, and 46) Others as well (e.g. 40, 41).

    I'm thinking maybe there's an error thrown (or not monitored) by the code that is not propagated through to java. But I'm out of my depth so not sure where to go from here.

    [SIDE NOTE: the reason I tried to go directly to linux-base via shared libraries (.so) is that the Monotouch/Xamarin/C# framework sits directly on linux: there's no need to go through android/java at all, which is a much cleaner interface, if you don't need android services (which we don't in this case)). Problem is I was never able to cross-compile .c/.h code for the Neo (i.e. armeabi-v7a) directly on a mac (or on a ubuntu vm on same machine). Directions to do this explicitly are very obscure, at least from my searches. One alternative was to go through Android Studio/NDK, compile, then extract .so output from either .apk or directly from generated assets, but those are generated as jni code, which... well, you get the idea!)
    Any chance you know the "command line incantation" for generating simple .so libraries (non-jni) for the Neo? If you do, importing .so in a C# app (on Neo) is a 1 code line solution, then just use p/invoke: that gives us full access to underlying linux/os functions)]
     
    Last edited: Nov 7, 2016

Share This Page