Multithreaded Server via Arduino - android

Socket programming question here.
My interface comprises of an Android device(client), A wireless module hosting an access point, and an Arduino Uno(server).
My Android client class works when 1 user connects and sends commands to my Arduino server class.
I want to make my program friendly to many users at the same time. My server will only read in values from 1 connected client at a time.
After some research I've found that Arduino does not allow multithreading naturally, which is why i've decided to look into a number of libraries.
The Arduino library that I've decided to use for this specific issue is protothreads. Unfortunately, i've been unable to find any examples of creating a server with Arduino to accept a client's commands. Furthermore I'm having a lot of trouble figuring out how to make this library work in my favor.
Now for my question,
How exactly can I allow my Arduino server to constantly listen for
incoming messages from more than 1 client device?
Or is there something I can do on the client side that would make this communication possible?

Could you get away with accepting the socket responding and the closing it. Similar to a web page. As not to have multiple sockets open at once. Note uno only has 2k of ram flash goes quick. By one and close each stands alone and naturaly allow many different connections. Just one at a time.

Related

Sending Data from Android Device to a RaspberryPi

I need some help in terms of choosing a design option for my problem.
I currently managed to implement a RaspberryPi acting as a Server and my local machine to act as the client. This client send JSON-Data to the Server which processes these. Everything is working as expected and I am using TCP-Sockets for the communication.
My problem:
The next step of my project will be, that I will use instead of a PC an Android-Device as client. What I want to achieve is, to send data to the server on the go. What I mean by that is, I do not want to restrict the server to be in a special network neither the client. What can be expected, is that server and client are next to each other, like in the range of a bluetooth connection. My question is, is there a relatively simple way to implement this communication? Is TCP a possible solution for this (even working in mobile networks?) or do I need to use Bluetooth, or is the way to go, to create some kind of network the client/server connects to and communicate here?
Sorry for propably stupid questions, but I am new to all this network stuff.
EDIT:
Since there were no respones, maybe I can do a more precise question. Is there a proper way to scan a network for a device name?
The only way I currently can imagine is to do a bruteforce like check on every IP-Address and resolve the names?
I first tried to let the Pi host an ad-hoc network, but it seems that non-rooted android smartphones do not have the possibility to access ad-hoc networks.
Therefore I made the Pi acting as an access point.
The communication now is very simple realized by a tcp server-client system.

Data transfer via bluetooth between paired Android and Raspberry PI

I am working on a project where I need to transfer data between android and raspberry pi via Bluetooth. However, I am new to this and I don't have deep understanding on what happens when two devices are paired. Based on assumption that the two devices of interest are already paired, where would the starting point be for programming for such task? I've been reading on BluetoothSocket, but I am still unsure of where to start. Can anyone help me please?
Thank you so much in advance!
I have been looking into this same issue, here is the reading I found on my end. I was looking to specifically code in python so that's the angle of the first one, the second is C++, but has a really thorough intro.
https://people.csail.mit.edu/albert/bluez-intro/index.html
and this one is really good too, the intro isnt too dense:
http://beej.us/guide/bgnet/output/html/multipage/index.html
The specific parts to look at involve the planning aspect. The intro of the beej programming guide shows specifically what sockets are and how they fit in a network sense. This means streaming sockets and datagram sockets. It also shows which of the sockets are used and which are availible. Chapters 1 through 3 gave me a solid enough reading basis to use the second document to determine a few things.
Chapter 2 of the MIT document goes into specific detail about each of the steps that must occur at a structural level including L2CAP + UDT, RCOMM, and whatever the stream one was. From reading these I was able to determine that the network I wanted to use was an L2CAP. I hope these help point you in the right direction though as far as what network you want to setup and what language you want to program in.
I've been working on the same task little while ago. The point is, that in order to start sending and receiving data you have to establish connection first. There is two side- device which connects (creates socket) and the other receiving connection (bluetooth server socket), giving out connection once connection is complete. After connection is established, you should stop receiving incoming connection or attempting establishing any connections.
From implementation perspective, you should implement few threads for managing all those stages - connection thread, accept thread, communucation thread. There is a great example from Google: https://github.com/googlesamples/android-BluetoothChat . It uses that technic. The only drawback is that it uses Handler (Android feature, allowing thereads to communicate) for informing user about Bluetooth events. I modified it a bit by introducing another thread, receiving status updates and calling methods from callback interface, feel free to use code from project: https://github.com/AlexShutov/LEDLights.

(Android, iOS, Windows, Linux) Server Polling Vs Push vs Implement Server

I'm building a multi-OS mirroring system which I would like to implement using a hybrid client-server and p2p communication method (at least that's the best way I have of describing it).
My issue is that at some point I have a central server (appengine, so there are limitations to what I could do because of time and networking capability constraints) that would need to get a message to a host of different devices which are not necessarily running the same OS (Windows, Android, iOS, Linux, etc...).
Android and iOS (or any other mobile platform) are the main problems it looks like I will be having on 2 levels.
1 - They are both limited by battery power (more so than a laptop and desktops shouldn't have that issue at all), so whichever method I use needs to take that into account.
2 - NAT (harder because the user has relatively less control over their firewall than on a network that they are running). My central server will maintain a table of which device has what IP address, but from what I understand if there is NAT or a firewall it won't be able to get to it if the port was not forwarded.
Since I will be writing a specific client for each OS I prefer a solution that is more universal. I have been leaning towards writing an extremely simple HTTP server that sits on each client and takes requests (which appengine is able to send) and treats them as messages that alert the client to perform an action (either with the server or another client). However, I run into the issue of NAT/firewall. For instance if appengine needs to send a message to AndroidDevice1 it would grab its IP address from a table and make a request to it. However this doesn't work if the ports aren't forwarded correctly, and if the user is on 3g/4g the firewall is controlled by the data provider.
Because of this, I started thinking about using Android C2DM but I want a solution I could implement across platforms.
The only other method I could think of is to just have the client poll the server for messages. This has the battery and network consumption issue though.
Would there be any other way to implement this, and if not, which one of the above methods are best in terms of balancing usability, power and data consumption and user input (the less the user has to do to get the client set up (ie port forwarding, etc...) the better)? Please note that I do not intend for this to become a discussion/flame war but a logical presentation of facts.
Thanks in advance!
You can create a persistent TCP connection from the device to the server and then communicate over this open connection. This would be a very simple connection with keepalive packets for the most part.
In theory this would consume some battery through the radio, but in practice I have experienced that the battery is not affected much at all. Key is to keep the communication over this line to a minimum.
If AppEngine does not allow this approach, you can run your own socket server and then communicate between this server and the appengine server using REST. A socket server I have used is Apache MINA and had no issues with scalability.
Another problem you will have with this approach or any other approach is that on iOS (afaik) you cannot keep a tcp socket open when the App goes into background. The only thing to communicate with the iOS device is Apple Push Notification Service
I would prefer rather than having HTTP Connection you should create TCP/IP tunnel and make communication fast and reliable. I have one Chat application which runs perfact for me using TCP/IP. If you use this you will have same logic for multiple platforms. Only thing you need to write is different code for iOS and android.

Android bluetooth - Discoverable and many open connections at the same time?

A few months ago I have coded a messenger program in C# which enables many clients to connect to a server and have a chat.
Now, I want to code the same program for android. After reading the bluetooth article in Android Developers, it seems that this is possible for a device to hold many bluetooth sockets at the same time + be discoverable (I'm talking about the server). The only action the said that should not work togather with bluetooth sockets is discovery, but since the client runs that, it doesn't change much.
But what made me unsure if it's possible to be done, is the fact that there is NO such an application in the android market! (Atleast not after a quick check.) There are many bluetooth chat apps, but none of them allows multi clients.
So before I begin coding that, I want to make sure it is possible, and make sure that holding, say, 5 tcp sockets won't kill the server phone.
Thanks in advance :)
Here is a link I found:
"Multi-device extension of the official Android Bluetooth Chat example"
Is that what you wanted?

Adapting Android Bluetooth Chat for multiple devices

I'm doing a college project on Bluetooth for Android, and I'm trying to understand how to manage communication between multiple connected devices. Eventually I'm going to develop a multiplayer Bluetooth Game.
Currently I've adapted Android's sample app BluetoothChat to connect my three Nexxus One phones.
1 connects to 2 who connects to 3
1 sends its messages successfully to 2. 3 sends its messages successfully to 2 as well. 2 can send its messages successfully to 1 and 3, as it shares a ConnectedThread with both. But I can't figure out how to handle getting communication from 1 to 3.
Does anyone have any examples of communication between multiple devices or has done this themselves? Thanks
One way is to annotate your messages with the sender and receiver, so that when 2 gets the message, it knows to deliver it on to 3. When 3 gets a message, it checks the receiver attribute to know it is from device 1. This extra layer allows you to send and receive messages through other devices and still be able to know who it is from.
First, though, you'll need to figure out how to make every device know about every other device on the network. If you're just connecting in a line, like 1-2-3-4, then every time a device enters the network, you could send an updated list through the network, but what happens if 2 drops out? Do you just quit the game? Wait for it to be re-paired? In this case, it may be better to look at a peer-to-peer network, or the typical client server architecture where you let one device be the host, especially if this is intended to later be a multiplayer game.
Hope that helps!
It is possible to have multiple Bluetooth sockets in use simultaneously so you may also consider configuring to act as a server, like an IRC or XMPP server, which brokers all communication from any client to another.
If you're going for fault-tollerance (for example server socket goes down) then upon connecting to the server, it might provide a list of recently-seen Bluetooth devices which you could fall-back on in the event the server goes down.

Categories

Resources