"Best" way for Android Arduino communication via ethernet - android

I have an Arduino and an Android app which are communicating to each other via ethernet. The arduino shows a very simple webpage with only some values and an ID as seen below.
$1$201 //Value 1 ($1$): Temperature 20.1 degrees
$2$66 //Value 1 ($2$): Humidity 66%
$2$1 //Value 2 ($2$): Heating relay is on (1)
etc. up to 50 values
The Android app will read the data from this webpage as a string using a HTTP get request, filters the data and shows the values on a custom made screen. It is also possible to send some data to the arduino to change some settings or switch a relay or something. You can see is as some kind of thermostat. So far so good.
The "problem" is that I need to manualy update the data using a button. The question is: How can I update my values automatically?
I was thinking to just send a httpRequest every few seconds (polling), but I'm not sure if this is the way to go because it seems to use of lot of data.
Who can advise me what would be a good solution?
Regards,
Bas

The 'best' choice here will depend on your goals. Polling is easy to implement on the client (android) side. You could experiment with the optimal polling time depending on how 'fresh' your data needs to be compared to how much data you want your app to use. Alternatively, you could find or implement an http socket server such as ArduinoWebsocketServer, keeping in mind that the processor in your Arduino may or may not have the power needed to run this.

Related

It takes too long to read data from the bluetooth chip

I'm working on a project which using bluetooth to send two bytes data to HC-05 module and receive from it. Everything is going well but there's one thing that I can't bear with, which is stated below.
I use System.currentTimeMills() to get the time interval between sending data and receiving ,and it takes no more than 1ms`` to detect whether there's data in the buffer ofinputStream. However, it takes about30~200msto readtwo bytesof data from thebluetooth` chip on my cell phone.
Dose anyone know how to reduce the time ? Or it's insolvable?
The primary bottleneck should be the protocol scheduling. I don't know how this particular module works, but in Bluetooth in general you have to wait for your timeslot to send or receive.
Suggestions:
Check if you can send more than 2 bytes at the time. The read time is stable, but you get more data transmitted each time.
Check if the API gives you scheduling options, so the wait time goes down.
For your use case I think things would be simpler with Bluetooth low energy (BLE). You will need another module, but IMO it is worth it.

filter packet data based on the type of content they carry

I want to filter the packet data based on the type of data they carry. I mean is it possible to recognize whether the packet carries text/audio/video/other type of information by analyzing the packet header or the payload?
Also, I want to be able to do this in real time; so that I can keep track of How much of data has been used up in text/audio/video etc.. Can these things be done using tcpdump? I want to run in a tizen/android phone..
A few things might be needed to achieve this.
In Tizen, you need su access to the device and install tcpdump and a host of other libraries like libpcap for example.
For HTTP you could rely on MIME headers.
You could use pre-defined standard ports in certain cases.
For Audio and Video you could examine the payload and see if the header matches and then classify.
But to generalize it across all data formats is going to take some work beyond simply reusing existing tools like tcpdump.

How to start a method in the same time on 2 devices

I have 2 android phones phones, both connected to the same wifi, both with bluetooth.
I want some method that syncs somehow the phones and starts a function on the same time on both phones.
For example playing a song at the same time.
I already tried with bluetooth but its with lag, sometimes 0.5 secs. I want something in +- 0.01sec if possible.
Someone suggesting playing it in the future with 2-3 seconds, sending the time-stamp, but how do you sync the internal clocks of the devices then ?
Before calling that particular method, try to measure the latency between the two devices:
1.First device says Hi(store the current time)
2.Second device receives the Hi.
3.Second device says back Hi !!
4.First device receives the Hi.((storedTime - currentTime) / 2 )
Now you have the latency, send your request to second device to start your particular method and start it on first one after the latency.
Try to measure the latency 5 to 10 times to be more accurate.
you have a way to transfer data between the devices right ?
if so you can send a time-stamp which is in the future,
ex: if the present time stamp is 1421242326 you send 1421242329 or something and start the function at that time on both devices.
Basically use #Dula's suggestion (device 1 sends command to device 2 and gives a "start time" which lies in the future). Both devices then start the action at the same time (in the future).
To make sure that the devices are synchronized, you can use a server-based time sync (assuming that both devices have Internet access). To do this, each device contacts the same server (using NTP, or HTTP-based NTP, or contacts a known HTTP server, like www.google.com and uses the value in the "Date" header of the HTTP response). The "server-date" is compared to the system clock on the device, and the difference is the "time-offset from server-time". The time-offsets can be used to synchronize on the "server-time", which is then used as the time base for the actual action (playing the media, etc.).
If your WiFi router allows clients to talk to each other (many public hotspots disable this), you could implement a simple socket listener on one (or each) device and have the initiating device broadcast a message.
For more complicated things and network flexibility, I've had good success with connected sessions using AllJoin. There is a bit of a learning curve to do interesting things, but the simple stuff is pretty easy once you understand the architecture.
Use a server to provide a synchronous event to just the two clients who have decclared their mutual affinity (random as a parm and pair serializer Partner-1 or Partner-2 which they share prior to their respectve calls for the sync event).
Assume both clients on same subnet (packets from 2 events serialized on the server , arrive across the network at the 2 clients simultaneously client-side) This provides synchronous PLays by 2 , bound clients.
The event delivered by server is either a confirm to play queued selected track OR a broadcast( decoupled, more formal)
The only tricky thing is the server side algorythm implementing this:
Queue a pair of requests or error
Part1, part2 with same Random value constitute valid pair if both received before either times out.
On a valid pair schedule both to the same future event in their respective , committed responses.
OnSchedule do the actual IO for 2 paired requests. Respective packets will arrive back at respective clients at same time, each response having been subject to equal network latency
Ng if two diff carrier 4G or lte networks involved. (Oops)
This thing is possible via socket, you will send a event via socket then the other device receive that event. For learn socket io chat
maybe it's not the answer you are looking for but i think that due to the high precision you are wanting , you should look for a push technology, i advice you to take look at SignalR. It's real time technology which gives you abstraction of sending methods , it have a built-in methods like Clients.All.Broadcast that fit your needs.
You can try to use some MQTT framework to send message between two device, or into a set with more number of devices.

Estimating Delay in WiFi Using Android

I have a simple and clear question: Is it possible to send a set of data packets, one packet at a time after the acknowledgement signal from the access point is received using an Android device? The type of signal is not important just the total time passed into the process( within a resolution of ns or us suits my application) is important for me and I don't know a thing about Android, but if such a task can be pulled off in Android, I will learn Android.
Thanks
You can send a System.nanoTime() value to the other device and then substract...the thing is if the 2 devices dont have exactly the same hour, this value will not be real... Anyways, you can sync. both devices sending a "test package" from a fixed distance, sou you can measure the difference according to the test package.

Android data backend one way synchronisation

I create a monitoring application who :
if there is a network connection available : she sends periodically
measurement data to the server using json
if there is no network available, she stores the data in the sd card and sends it when the network connection is back.
Actually I use a circular buffer in memory that I empty when data are sent
Is there already something usefull in the framework or I have to write that completly ?
Thanks
I would check tape library from square. I've never tried it but looks what you are looking for.

Categories

Resources