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?

A lot of work is handled by my Wifly Module. I was able to telnet in and set my ipaddress/port#/etc.. Anything sent over IP goes straight through my Module and straight to my arduino.

My Client Code:

My Server:

WiFlySerial wifi(rxPin ,txPin);

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(led, OUTPUT);
}

void loop() {
  //Serial.println("I'm in the LOOP!!");
  androidLed();
  serialLed();

}

void androidLed()
{
  if(wifi.available() > 0){
    data = wifi.read();
    Serial.println(wifi.getDeviceStatus());
    Serial.println(data);
}
}

Without knowing your exact implementation details...

You should be OK as long as you don't have multiple clients connected at the same time. You may think that there are multiple clients connected to your server, but the connection is closed after the resource is obtained by the client over a protocol.*

The standard flow of a server on Arduino:

  1. A client connects to the Arduino.
  2. The Arduino code realizes this connection and reads the data provided by the client.
  3. The Arduino then sends some data back to the client.
  4. The Arduino closes the connection and waits for another client to connect.

This happens relatively fast so multiple people can connect.

If you call a function like .close(); after you send data to your client, you should be okay. If you don't, redesign your code to have this element (avoid loops and blocking functions) and make sure to have your client reconnect every time it sends a command.

I wouldn't mess with this and Protothreads because I'm not sure that the Arduino hardware is designed to be able to handle multiple connections at the same time. [Citation needed]

  • Good, I was hoping I was overthinking this. I'm not using a .close() and maybe put too much focus on the android aspect of my code. I will try this and see if it works. – The Tokenizer Nov 25 '14 at 4:08
  • @TheTokenizer are you using the Ethernet shield connected to a wireless access point or what? – Anonymous Penguin Nov 25 '14 at 4:13
  • I'm hosting an AP using an RN-131C. A wifly wireless module. With this I am able to create my AP and send commands through it via my Android device. I am using my Arduino as a server to respond to these commands and I am using the Wifly Serial Library. – The Tokenizer Nov 25 '14 at 4:23
  • @TheTokenizer you are doing this a different way than I thought. Can you post a minified working example of your code so I can look at it and see if it closes the connection each time? (I.e. remove everything except what's needed to make a connection) – Anonymous Penguin Nov 25 '14 at 4:30
  • 1
    @TheTokenizer looking at that it seems like it's using a UART type of protocol. I'm afraid that this device only allows one connection at a time at the hardware level, forget the software level. – Anonymous Penguin Nov 25 '14 at 5:06

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.