Hello,
I have tried reading 2 usb devices (one being an arduino) with a usb hub connected to a OTG cable on smartphone.
I give you my entire code below such as you can say if this is the correct syntax.
//Global variables.
var usb=null, usb2=null, stop, timer=0,timer2, reply="",timer_arduino=60,timer_gps=60,stopin=0;
var rate, id_db=1, day, month, year, hour, min, sec, redfield="non";
var s,log="", maxLines=5, long, lat, sqm;
//var ti,timewait;
//Called when application is started.
function OnStart()
{
Date_time();
rate=2;
app.PreventScreenLock("Full");
app.SetScreenBrightness( 0.2 );
app.SetOrientation( "Portrait" );
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
layh = app.CreateLayout( "Linear", "Horizontal" );
lay.AddChild(layh);
layh = app.CreateLayout( "Linear", "Horizontal" );
lay.AddChild(layh);
btn = app.CreateButton( "Acquisition mesures capteur", 0.2, 0.08 );
btn.SetMargins( 0.1, 0.08, 0, 0 );
lay.AddChild( btn );
btn4 = app.CreateButton( "Acquisition mesures arduino", 0.2, 0.08 );
btn4.SetMargins( 0.1, 0.08, 0, 0 );
lay.AddChild( btn4 );
//Set function to call when button pressed.
btn.SetOnTouch( capteur_measurements );
btn4.SetOnTouch( arduino_measurements );
//Create a text control to show data log.
txt2 = app.CreateText( "", 0.9, 0.129, "Multiline,Left" );
txt2.SetTextColor( "#ffffffff" );
lay.AddChild( txt2 );
//Create a read-only edit box to show sqm responses.
edtReply = app.CreateText( "", 1.0, 0.2, "MultiLine,Left,Monospace" );
edtReply.SetMargins( 0,0,0,0.01 );
edtReply.SetTextColor( "#ffffffff" );
edtReply.SetTextSize( 12 );
lay.AddChild( edtReply );
//Create a read-only edit box to show sqm responses.
edtReply_arduino = app.CreateText( "", 1.0, 0.2, "MultiLine,Left,Monospace" );
edtReply_arduino.SetMargins( 0,0,0,0.01 );
edtReply_arduino.SetTextColor( "#ffffffff" );
edtReply_arduino.SetTextSize( 12 );
lay.AddChild( edtReply_arduino );
//Create an edit box containing an example sqm program.
edt = app.CreateTextEdit( "", 1.0, 0.2, "NoSpell" );
lay.AddChild( edt );
//Add layout to app.
app.AddLayout( lay );
dbname = "/sdcard/capteur_"+day+"_"+month+"_"+year;
exists = app.FileExists( dbname );
if(exists === 0) app.WriteFile( dbname, "date;heure;lat;long;value\n")
dbname_arduino = "/sdcard/arduino_log_"+day+"_"+month+"_"+year+".txt";
exists = app.FileExists( dbname_arduino );
if(exists === 0) app.WriteFile( dbname_arduino, "date;heure;lux;freq;lat;long\n");
usb2 = app.CreateUSBSerial(115200,8,1,0);
usb = app.CreateUSBSerial( 115200,8,1,0,'2nd')
}
function capteur_measurements()
{
if( !usb )
{
app.ShowPopup( "Connecter le capteur" );
return;
}
btn.SetText("Arret des mesures capteur");
btn.SetOnTouch( btn_stop );
stop=setInterval( btn_OnTouch , 160);
}
function arduino_measurements()
{
if( !usb2 )
{
app.ShowPopup( "Connecter la carte Arduino" );
return;
}
btn4.SetText("Arret des mesures arduino");
btn4.SetOnTouch( btn_stop_arduino );
stop_arduino=setInterval( btn_OnTouch_arduino , 160);
}
function btn_stop()
{
clearInterval(stop);
btn.SetText("Acquisition des mesures capteur");
btn.SetOnTouch( capteur_measurements )
}
function btn_stop_arduino()
{
clearInterval(stop_arduino);
btn4.SetText("Acquisition des mesures arduino");
btn4.SetOnTouch( arduino_measurements );
}
function btn_OnTouch()
{
app.ShowPopup( "prise de mesures capteur", "Bottom");
if( usb )
{
setTimeout(usb.Write("ux"),20);
Date_time();
usb.SetOnReceive( usb_OnReceive );
}
else
{
app.ShowPopup( "Perte du signal du capteur ; Vérifier le branchement du cable" );
return;
}
}
function btn_OnTouch_arduino()
{
app.ShowPopup( "prise de mesures arduino", "Bottom");
if( usb2 )
{
usb2.SetOnReceive( usb_OnReceive_arduino );
}
}
function usb_OnReceive( data )
{
log +="capteur ";
//Speak the text at default pitch and speed.
var pitch = 4, speed = 1.;
if (timer==0) timer=parseFloat(min+sec/60);
timer2=parseFloat(min+sec/60);
data_capteur=data.substring(3,260);
var arr=data_capteur.split(","); // Split on commas
if (data.substring(3,9).indexOf("m") !== -1)
{
log += data.substring(3,9)+"\n";
var logLines = log.split("\n");
if( !maxLines ) maxLines = edtReply.GetMaxLines();
logLines = logLines.slice( -maxLines );
log = logLines.join("\n").toString();
edtReply.SetText( log );
if ( (timer2-timer > 0.133) || (timer2-timer < 0) )
{
app.TextToSpeech(data.substring(3,8), pitch, speed,null,"music" );
timer=0;
}
data_capteur=day+"/"+month+"/"+year+";"+hour+":"+min+":"+sec+":"+millisec+";"+lat+";"+long+";"+arr[0]+";"+arr[1]+";"+arr[2]+";"+arr[3]+";"+arr[4]+";"+redfield+";"+"\n";
app.WriteFile( dbname, data_capteur,"Append");
}
}
//Called when we get data from arduino.
function usb_OnReceive_arduino( data )
{
//Speak the text at default pitch and speed.
var pitch = 4;speed = 1.;
//console.log( data );
data_arduino=data;
app.WriteFile( dbname_arduino, data_arduino,"Append");
var arr=data_arduino.split(";");
if (arr[2]>1)
timer_arduino-=4;
else
timer_arduino--;
if (stopin==0 && timer_arduino<1)
{
log += arr[0]+" "+arr[1]+" "+arr[2]+" "+arr[3]+" "+arr[4]+" "+arr[5]+"\n";
var logLines = log.split("\n");
if( !maxLines ) maxLines = edtReply_arduino.GetMaxLines();
logLines = logLines.slice( -maxLines );
log = logLines.join("\n").toString();
edtReply_arduino.SetText( log );
if (arr[1]>1 )
app.TextToSpeech( Math.round(arr[1])+" lux", pitch, speed );
if ( timer_arduino<1) timer_arduino=60;
if ((arr[4]==0.0 || arr[5]==0.0) )
{
timer_gps-=10;
if ( timer_gps< 1 )
{
app.TextToSpeech( "Perte signal G P S ", pitch, speed);
timer_gps=60;
}
}
}
}
function Date_time()
{
d=new Date();
day=d.getDate();
month=1+d.getMonth();
year=d.getFullYear();
hour=d.getHours();
min=d.getMinutes();
sec=d.getSeconds();
millisec=d.getMilliseconds();
if (day < 10) day="0"+day;
if (month < 10) month="0"+month;
if (hour < 10) hour="0"+hour;
if (min < 10) min="0"+min;
if (sec < 10) sec="0"+sec;
if (millisec > 10 && millisec < 100) millisec="0"+millisec;
if (millisec < 10) millisec="00"+millisec;
}
I receive data from "capteur" but not from "arduino" ....
The 2 usb devices works well when they are used alone on another script.
The issue should come from the declaration of usb port
I'm looking for the whole forum but find nothing ....
Thank you in advance for your help,
Best regards,
Nicolas.