Good Afternoon, I am still very new to ESP32/android studio coding so I apologize for my beginner terminology. I am currently coding a project where I can control multiple stepper motors at the same exact time from the press of a button on my android application and the motors are connected to certain ESP32 GPIO pins, I am using the okhttp3 client as well. My code is below.
public class Connectivity {
public static String geturl (String url_esp32){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url_esp32)
.build();
try
{
Response response = client.newCall(request).execute();
return response.body().string();
} catch(IOException error) {
return error.toString();
}
}
}
above is my connectivity page for connecting to the requests for the esp32.
PBNow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// request information from esp32
// PB sandwich now, disable jelly motor
request_to_url("STEP");
request_to_url("DIR");
request_to_url("STEP2");
request_to_url("DIR2");
request_to_url("STEP4");
request_to_url("DIR4");
request_to_url("ledRED");
request_to_url("ledGREEN");
}
});
above is how im calling the requests for the esp32.
The problem I am having is that when these request_to_url lines are going line by line but I want them to all run at the exact same time. Is this possible.
Below are also my request_to_url function and request_data function.
public void request_to_url (String command) {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnected()) {
new request_data().execute("http://" + ip_address + "/" + command);
}else {
Toast.makeText(activity_2.this, "Not connected ", Toast.LENGTH_LONG).show();
}
}
private class request_data extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... url)
{
return Connectivity.geturl(url[0]);
}
#Override
protected void onPostExecute(String result_data) {
if(result_data != null)
{
}else{
Toast.makeText(activity_2.this, "Null data", Toast.LENGTH_LONG).show();
}
}
}
I apologize if the code is very sloppy, I am still very new. Thank you very much.
Besides the messy code in your app required to fire off multiple requests, the ESP32 has very limited network stack and resources, and cannot handle many simultaneous connections. If your app opens too many HTTP connections to the ESP32 at once, some will likely fail or have to wait for others to close.
Instead, you can do it all in a single request and tell the ESP32 to do multiple things at once. Pass the requests as parameters in the URL, like so:
http://ip-address/cmd?step=1&dir=0&ledGREEN=0&ledRED=1
Just have the handler on the ESP32 for the path /cmd look for the presence of each possible parameter and respond to it appropriately.
Related
Actully i working in a app, but i have problems to connect my Web services, i have this code:
try{
HttpServices post = new HttpServices ("http://sotem.com.mx/WebServices/controller.php");
post.add("funcion", "test");
System.out.println("Si lo mande///////////////////Jhgfdsa");
String respuesta = post.getRespueta();
System.out.println(respuesta);
Toast.makeText(getApplicationContext(),"Cool: "+respuesta, Toast.LENGTH_SHORT).show();
}catch (Exception ex) {
Toast.makeText(getApplicationContext(),"error: "+ex.toString(), Toast.LENGTH_SHORT).show();
}
but i can make connection, i try to make other thinks, but i can make the thread, i'am new in this part, the app launcher this error:
android os network on main thread exception
It is not okay to do the Network Operation on main thread.. You can use AsyncTask to perform such operations and handle the result in onPostExecute method.
class YourNetworkingTasks extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... voids) {
try{
HttpServices post = new HttpServices ("http://sotem.com.mx/WebServices/controller.php");
post.add("funcion", "test");
String respuesta = post.getRespueta();
Log.d("Output", respuesta);
// DON'T DO ANY UI CHANGES LIKE TOAST FROM BACKGROUND THREAD.. Toast.makeText(getApplicationContext(),"Cool: "+respuesta, Toast.LENGTH_SHORT).show();
}catch (Exception ex) {
// DON'T DO ANY UI CHANGES LIKE TOAST FROM BACKGROUND THREAD.. Toast.makeText(getApplicationContext(),"error: "+ex.toString(), Toast.LENGTH_SHORT).show();
}
return null;
}
protected void onPostExecute(RSSFeed feed) {
// TODO: YOU CAN MAKE U.I. Changes Like Display text in TextView, TOAST HERE.
// TODO: do something with the result
}
}
And write new YourNetworkingTasks().execute(); to run that code in background thread.
Please also not that since you are using http and not https you may get Network Security Exception and may not get any output due to recent security change in android.
I want to have an algorithm for part of my android app code which will execute a particular code segment again and again if it doesnt meet the condition with different time intervals. in simple words retry the code multiple times after different time intervals.
For examaple,
I am uploading some string data, when there is an internet connectionI do a network connection check, and when there is no internet, it should check again automatically after 1 min. if positive, data is uploaded and if not, it should retry again after next 3 mins and for the third time also if positive, data is uploaded and if not, it should do the final check after next 5 mins and then if it cant get network then it should show toast no network.
How to do that with a simple algorithm?
Here the network connection scenario is just as an example. I want the algorithm for the 'retry' as i have explained there.
Thanks in advance.
Firstly, you have to declare a method to check if there's internet connection.
Using ConnectivityManager and NetworkInfo
public static boolean hasInternetConnection(final Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
return false;
} else
return true;
}
Then...
try{
if(Util.hasInternetConnection(mcontext)){
//todo
#Override
public void onSuccess(String message) {
showAlertDialog("Alert",message);
}
#Override
public void onFail(String errorcode, String errormessage) {
showAlertDialog("Alert",errormessage);
}
});
}else{
showInternetRequiredDialog(getString(R.string.title_internet_require), getString(R.string.msg_no_internet_connection_setup));
return;
}
}catch (Exception ex){
ex.printStackTrace();
}
As for how you wanna write your checking logic, I'll leave it up to you
Using Handler, Timer can help.
I'm getting this exception in my logs: "clarifai2.exception.ClarifaiException: Maximum attempts reached of getting a default model." which is being generated by a large number of my android app users, but I am unable to replicate the exception or determine what is causing it. Any help on how to recreate or even better prevent this exception from occurring would be very helpful.
UPDATE:
I found the issue and am able to reproduce on demand, if no internet connection is available the Clarifai library throws this exception, there is no check for network connection state within the library. I can check for network connection in my app before building the clarifai client, but if the network connection is lost after the client is built this exception is generated, any ideas on how to prevent this? Thank you.
1 - Ensure data connection to internet is available on phone.
2 - Build clarifai client in onCreate
3 - Send clarifai predict request to food model
4 - Disable wifi and mobile data connections on phone
5 - Wait 10 to 15 seconds, can navigate to other activities, then clarifai throws "Clarifai Exception: Maximum attempts reached of getting a default model" and crashes the app.
Clarifai Library class which can throw this exception is available HERE
I call the buildClarifaiClient method below in my onCreate of the requesting activity.
private void buildClarifaiClient(){
if(clarifaiClient == null){
clarifaiClient = new ClarifaiBuilder("KeyString")
.client(new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
//.addInterceptor(new HttpLoggingInterceptor(logger::info).setLevel(HttpLoggingInterceptor.Level.BASIC))
.build()
)
.buildSync();
}
}
Request to Clarifai on ActivityResult after a picture is taken:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
if(clarifaiClient != null) {
snapSearchActivityResult = true;
taskClarifaiRequest = new AsyncTask<Void, Void, ClarifaiResponse<List<ClarifaiOutput<Concept>>>>() {
#Override
protected void onPreExecute() {
}
#Override
protected ClarifaiResponse<List<ClarifaiOutput<Concept>>> doInBackground(Void... params) {
// The default Clarifai model that identifies concepts in images
// Use this model to predict, with the image that the user just selected as the input
return clarifaiClient.getDefaultModels().foodModel().predict()
.withInputs(ClarifaiInput.forImage(ClarifaiImage.of(getPicByteData())))
.executeSync();
}
#Override
protected void onPostExecute(ClarifaiResponse<List<ClarifaiOutput<Concept>>> response) {
//setBusy(false);
if (!response.isSuccessful()) {
showErrorSnackbar(getString(R.string.clarifaiAPIContactError));
return;
}
final List<ClarifaiOutput<Concept>> predictions = response.get();
if (predictions.isEmpty()) {
showErrorSnackbar(getString(R.string.clarifaiAPIResultsError));
return;
}
List<Concept> concepts = predictions.get(0).data();
int conceptsSize = concepts.size();
Log.d("conceptsSize", String.valueOf(conceptsSize));
for (Concept c : concepts) {
// Do something with the value
Log.d("foodName", String.valueOf(c.name()));
Log.d("foodProb", String.valueOf(c.value()));
}
}
private void showErrorSnackbar(String errorString) {
Snackbar.make(
parentLayout,
errorString,
Snackbar.LENGTH_LONG
).show();
}
};
taskClarifaiRequest.execute();
} else {
Snackbar.make(
parentLayout,
"Unable to connect to Image API, try again.",
Snackbar.LENGTH_LONG
).show();
buildClarifaiClient();
}
}
Stacktrace:
Exception clarifai2.exception.ClarifaiException: Maximum attempts
reached of getting a default model.
clarifai2.dto.model.DefaultModels.update ()
clarifai2.dto.model.DefaultModels.access$000 ()
clarifai2.dto.model.DefaultModels$1.run ()
java.util.concurrent.ThreadPoolExecutor.runWorker
(ThreadPoolExecutor.java:1113)
java.util.concurrent.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:588)
java.lang.Thread.run (Thread.java:818)
I'd say that the API key you use here doesn't have permissions for doing get model request. When you access the food model via getDefaultModels(), the model is first updated (via get model request) and only after that the predict request is run. If the first one fails, the second one doesn't run.
In the Java API client 2.3 which is about to be released next week, the call getDefaultModels() will no longer initiate get model request since most of the time you require only an ID of a default model, not the complete model data which get model request returns. Since only predict request will be performed, you will not need to have get model request permissions on your API key.
In the meanwhile, you can do one of two things. Either:
Add Models:Get permission to your API key. You do this by going to the API keys page, editing your API key, clicking ADVANCED under Scopes and checking Models:Get. You probably already have Predict permission checked.
Or use the model ID directly on the predict call. I've inserted the food default model ID (which you can also see in the DefaultModels.java link you posted above) into the predict call for you: client.predict("bd367be194cf45149e75f01d59f77ba7").withInputs(...).executeSync();.
I am making a simple Android Wear app to control my thermostats, and I'm sending POST requests with Volley to control them. Everything works great in the Android Wear simulator (the request works), but, while the app does load on my Moto 360, the volley request gets called but invariably times out.
Why could my volley request be failing on my watch but working on the simulator? Other apps' requests succeed on my watch (for example, the built-in weather app can load up weather data in about 3 seconds). And, the weirdest part: I had the app working (successfully making volley requests) on my watch, and, about a day after I installed it to my watch from Android Studio, it suddenly stopped loading data for no apparent reason.
What I've tried so far:
I have requested the Internet permission in my manifest.xml.
I have increased the timeout to 30 seconds (see my code below), which didn't change anything.
I have tried tethering my computer and the simulator to my phone's connection via Bluetooth (to replicate the Bluetooth connection my physical watch has to my phone), and the simulator made the request successfully still (albeit with a two-second delay), ruling out the possibility of Bluetooth being too slow.
I made sure the API level is low enough for my Marshmallow-running watch (my watch and the app are both API level 23).
I tried doing a quick test request to Google before the request to the company's servers with my thermostat data, and while the Google request returns the site's HTML code in the simulator, it times out on my watch (thirty seconds after the request is initiated).
I tried putting some dummy data into the recycler view data should be loaded into, and the dummy data indeed showed up, ruling out that the recycler view is broken.
I deleted the app from my watch and reinstalled it, and deleted the companion from my phone, reinstalled it, and deleted it again, all to no avail.
A lengthy chat with Google Support did not produce anything meaningful.
Here's my code (from my main view's adapter):
public void refreshThermostatsRecyclerView(RequestQueue queue) {
String url = "https://mobile.skyport.io:9090/login"; // login call to the thermostats server Skyport
Log.w("myApp", "Starting /login call to Skyport"); // this gets called on simulator and watch
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the response string.
Log.w("myApp", "Response is: " + response); // this gets called on the simulator but not the watch
try {
// there's some code to parse the data.
} catch (JSONException e) {
Log.w("myApp", "catching an error parsing the json."); // never gets called.
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.w("myApp", "Skyport request didn't work! " + error); // this always gets called on the watch, with the error being a timeout error (com.Android.Volley.timeouterror) but never gets called in the simulator
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> m = new HashMap<>();
m.put("Referer", "app:/VenstarCloud.swf");
// here I put some more headers
return m;
}
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> m = new HashMap<>();
m.put("version", "3.0.5");
m.put("email", userEmail);
m.put("password", userToken);
return m;
}
};
// Add the request to the RequestQueue.
int socketTimeout1 = 30000; // times out 30 seconds after the request starts on the watch
RetryPolicy policy1 = new DefaultRetryPolicy(socketTimeout1, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy1);
queue.add(stringRequest);
}
Which is called from the onCreate() method in my Main Activity with this code:
RequestQueue queue = Volley.newRequestQueue(this);
refreshThermostatsRecyclerView(queue);
If you'd like to view the logs created by running this in the simulator and on the watch, they're on Google Drive here.
Edit 1: A reboot of my watch fixes the issue temporarily and allows the watch to make HTTP Requests again, but it breaks again once the watch disconnects from Bluetooth, connects to WiFi, disconnects from WiFi, and reconnects to Bluetooth (so it breaks every time I go across my apartment without my phone and then return).
Edit 2: I switched the volley requests all over to HTTPURLConnection Requests in an Async thread, and the same issues occur as with volley.
tl;dr: My app's Volley requests are working in the simulator but not on my Android Wear watch anymore (though Play Store-downloaded apps' similar requests work), how can I get a volley request to work again on my app on the watch?
As per these two conversations below, it seems that WiFi connectivity only allows Android Wear to connect to a phone over WiFi and not directly to the Internet. However, Android Wear 2.0 lets you use regular network APIs.
Direct internet connection on Android Wear?
Does Android Wear support direct access to the Internet?
So, for Android Wear 2.0+ Volley requests from wearable app should work.
If you want to use Android Wear <2.0, then:
On Wearable, in onCreate() add a key that indicates whether the phone should start collecting data.
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/shouldStart");
putDataMapReq.getDataMap().putBoolean(SHOULD_START_KEY, true);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
PendingResult pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
On phone, in onDataChanged, check if wearable wants to start collecting data. If yes, start Volley request.
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
// DataItem changed
DataItem item = event.getDataItem();
if (item.getUri().getPath().compareTo("/shouldStart") == 0) {
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
boolean shouldStart = dataMap.getBoolean(SHOULD_START_KEY));
if(shouldStart) {
Volley.newRequestQueue(this).add(request);
}
}
} else if (event.getType() == DataEvent.TYPE_DELETED) {
// DataItem deleted
}
}
Then, your Volley request's onResponse should pass data back to Wearable.
public void onResponse(String response) {
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/data");
putDataMapReq.getDataMap().putString(DATA_KEY, true);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
PendingResult pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
}
Finally, you can access data in your Wearable using onDataChanged and store it in your model for passing it onto adapter:
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
// DataItem changed
DataItem item = event.getDataItem();
if (item.getUri().getPath().compareTo("/data") == 0) {
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
parseAndpassToAdapter(dataMap.getString(DATA_KEY));
}
} else if (event.getType() == DataEvent.TYPE_DELETED) {
// DataItem deleted
}
}
You'll need Wearable.API to implement this and your class should implement DataApi.DataListener. For more information getting started, refer to Accessing the Wearable Data Layer and Syncing Data Items
Hope this helps.
I am also using volley on an Android wear app I built and I am running it on a Moto 360, I have run into the same problem a couple o times. Try restarting the device. Go to Settings > Restart. It sounds silly but it has worked for me.
You could try an alternative to volley if you can rule out the connection as the problem:
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.0'
compile 'com.android.support:design:23.1.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.google.api-client:google-api-client:1.20.0'
The versions are important.
Then to your request:
Map<String, String> contentParams = new HashMap<>();
InputStream is = null;
NetHttpTransport transport = null;
HttpRequest request = null;
HttpResponse resp = null;
HttpHeaders headers = new HttpHeaders();
JSONObject json = null;
try {
transport = new NetHttpTransport();
HttpRequestFactory factory = transport.createRequestFactory();
request = factory.buildPostRequest(new GenericUrl(url), null);
contentParams = getContentParameters();
headers.putAll(getHeaderParameters());
request.setHeaders(headers);
request.getUrl().putAll(contentParams);
resp = request.execute();
is = resp.getContent();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
string = getJSONFromInputStream(is);
json = new JSONObject(string);
}
} catch (Exception e) {
e.printStackTrace();
}
}
transport.shutdown();
protected Map<String, String> getContentParameters() {
Map<String, String> m = new HashMap<>();
m.put("version", "3.0.5");
m.put("email", userEmail);
m.put("password", userToken);
return m;
}
protected Map<String, String> getHeaderParameters() {
Map<String, String> m = new HashMap<>();
m.put("Referer", "app:/VenstarCloud.swf");
return m;
}
protected String getJSONFromInputStream(InputStream is) {
if (is == null)
throw new NullPointerException();
//instantiates a reader with max size
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8 * 1024);
StringBuilder sb = new StringBuilder();
try {
//reads the response line by line (and separates by a line-break)
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//closes the inputStream
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
Then just execute your code from a thread/asynctask/have it delay your frontend slightly
Edit:
Just in case there is a problem with appending a map:
for (Entry<String, String> entry : getHeaderParameters()) {
headers.put(entry.getKey(), entry.getValue());
}
for (Entry<String, String> entry : getContentParameters()) {
request.getUrl().put(entry.getKey(), entry.getValue());
}
Also as another note, make sure to change the return type from void on both those methods to Map
Is this not just the case of when the watch is connected to the phone via bluetooth the internet will not work, as wifi is turned off. If the watch is using wifi to connect to the phone then it will work.
I'm working on wear 2.0 app and just turn blueooth off on my phone for my watch to get internet connection.
After reading a lot of topics on SOF & blog post, I have got the BroadcastReceiver working. But I have noticed sometime it does not work, specially when I am on call. I just want to know, does BroadcastReceiver work if I get SMS in-between the call ? or there is something is wrong in the code.
SmsReceiver.java
public class SmsReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.d("SmsReceiver Broadcast", "OK");
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
Log.d("Network is connected. Executing TheTask()", "OK");
new TheTask().execute("http://somedomain.tld/index.php?userId=12345678");
}
if (networkInfo == null) {
Log.d("Network is NOT connected.", "FAIL");
}
}
class TheTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... arg0) {
String text = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(arg0[0]);
HttpResponse resp = httpclient.execute(httppost);
HttpEntity ent = resp.getEntity();
text = EntityUtils.toString(ent);
} catch (Exception e) {
e.printStackTrace();
}
return text;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
String tocheck = "Some text";
if(result.contains(tocheck))
{
Log.d("string contains some text", result);
}
}
}
}
AndroidManifest.xml
<receiver android:name=".SmsReceiver" android:enabled="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
I am using 3G network. It may be due to network disconnects. If so, is there any workaround to execute AsyncTask when my phone again get data network ?
EDIT:
Wrapped the AsyncTask into isConnected. Now if the phone is not connected to Internet & I get SMS the AsyncTask will not be executed. In that situation I want to use ScheduledExecutorService to schedule AsynckTask to be executed 4 times at the interval of 5 minutes within next 20 minutes. I will be highly obliged anyone can help me in this.
I cannot be sure about the disconnects since you haven't provided any stack trace. But I am pretty sure the SMS_RECEIVED broadcast action is being broadcasted system-wide when you receive sms. Now if you do not receive SMS due to bad connection or no connection at all it's perfectly logical for the system not to trigger an SMS_RECEIVED action since no SMS was received.
As for the
is there any workaround to execute AsyncTask when my phone again get
data network
you can implement another Broadcast Receiver that will listen for network changes but that doesn't seem such a good idea. You should try and reproduce the problem and check for any exceptions in your stack trace.
is something is wrong in the code.
Your receiver seems to be properly registered in your manifest,
the onReceive() method seems to be properly launching the AsyncTask,
the doInBackground doesn't seem to have any problem and onPostExecute (although the official example seems to omit any call to super) seems to be just fine.
Conclusion: Try to reproduce the problem and gather data from the stack trace as to what might cause the error (network overhead might be one the causes but that's just speculation).