Skip to content

Serial Read string output #35

@evski

Description

@evski

Firstly, thanks so much for everyone's hard work on cordovarduino! After wasting a lot of time on chrome packaged apps, I found myself here. I now have a working app reading serial data on my android device! However:

I'm using serial read in cordovarduino to write the value of a potentiometer onto a html page. Currently I get 1 character of the string from the Arduino at a time, I'm told this is by design, however I cannot seem to get the output to concat and split in order to give me a complete string.

I've set the arduino to output a '/n' with each value, I'm then concatenating the string using += and looking for /n and splitting into an array and outputting the first item in it. The values from the potentiometer are in the range 0 to 680. So what I see on android is, 6, then 68, then 680 and then it clears and repeats (this is the same with and without the 'split' part of the code). Here's a screen grab showing my tracing of the variables to screen (as in the code below).screengrab

I'm running Android 4.4.2 and an Arduino Uno. Apologies in advance if I'm doing something stupid, I'm new to android and javascript and have been trying to get this going for a few days now. Once I get this working I'm planning to pass more data from arduino for buttons, additional pots etc, so being able to simply split the data using a unique identifier, or as parts of an array will be crucial.

Any help very much appreciated! Thanks in advance.

Here is the js code:

//define variables
var serialString = "0";
var view;
var s;
var s2;
var s3;
var i;

//UTF8 to string conversion
function ua2text(ua) {
    s = "";
    for (i = 0; i < ua.length; i++) {

        //convert to string and concat items in array with +=
        s += String.fromCharCode(ua[i]);

        //trace to app for debug
   document.getElementById("logging").innerHTML = "s: " +s;

     //split into array at \n character
s2 = s.split("\n")

 //trace to app for debug
    document.getElementById("logging2").innerHTML = "s2: " +s2;


    //select first item from new array
s3 = s2[0];

 //trace to app for debug
    document.getElementById("logging3").innerHTML = "s3: " +s3;

     }
     //return final output
    return s3;  
}

//device ready event listener
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {

    //error callback for serial comms
    var errorCallback = function(message) {
    alert('Error: ' + message);
     document.getElementById('logging').innerHTML = 'Error: ' + message;
};

//serial initialise/open
        serial.requestPermission(
            function(successMessage) {
                serial.open(
                    //set baud rate to match code from Arduino
                    //set dtr to true, as it seems to be mentioned in the FAQ for the driver
                    {baudRate: 9600, dtr: true},
                    function(successMessage) {

                       //Read serial data
                       serial.registerReadCallback(
                           function success(data){

                                //convert array buffer to string
                               view = new Uint8Array(data);

                               //call UTF 8 conversion function and pass it data
                              serialString = ua2text(view)

                            //set graphics variable
                            msg = serialString;

                             //trace to app for debug
    document.getElementById("buffer").innerHTML = "serialString: " +serialString;

                           },
                           function error(){
                               new Error("Failed to register read callback");
                           });
                    },
                    errorCallback
                );
            },
            errorCallback
        );
}

and here is the arduino code feeding it:

int pot1pin = A0;  //pot input 1

void setup() {
  Serial.begin(9600);
}


void loop() {
  delay(100);
    printPotValue();

}


// Output pot value to serial
void printPotValue () {
    int potStatus = analogRead(pot1pin);
    int potStatusVar = potStatus;

    // Print via serial
    Serial.print(potStatusVar);
    Serial.print("\n");

}```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions