I want to make simple client-server program where arduino behave as a client. For example I have a LED connected in arduino board that is controlled by a switch. With the switch I can turn off and on the LED. Now I want to send the status of that LED to another computer wirelessly (let's say within the Local network). That computer will act as a server and update the change as soon as the LED changes its status. I am not expecting any complete code, I just want to know how could it be done in general? May be some links can help me? Because this is my first time working with client-server in arduino sketch, I need some kind of guidelines. Thank you.
2 Answers
A simple HC-05 module will give you a wireless bluetooth connection. They're fairly simple to setup, and you can write data to them the same as one would the Arduino serial console. Obviously they can read data too.
SoftwareSerial bt_serial(2, 3); // RX | TX
bt_serial.begin(9600);
[...]
if (led_state != last_led_state)
{
if (led_state == HIGH)
bt_serial.print("1");
else
bt_serial.print("0");
last_led_state = led_state;
}
On the "server" side, a simple program reading the mapped bluetooth serial port would do the job. For example, some python code:
import serial
import sys
serial_port = serial.Serial(port='COM8:', baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE)
try:
serial_port.open()
except:
print("Failed to open port")
sys.exit(2)
while (True):
try:
led_state = serial_port.read()
if (led_state != 0):
print("LED is on")
else:
print("LED is off")
except:
pass
serial_port.close() # yes, it never gets here
Even though I like Kingsley's answer I thought I'd add to this using Wi-Fi becuase you had mentioned LAN.
These two links should be enough to help you: WifiChatServer and C# Telnet Library
Those links can sort of be dissected to fit your needs, but if you need some actual code just tell me.
Now I am aware that the Arduino is acting as a server and the computer acts as a client, but I believe this setup is better because it's easier to manipulate connecting to servers on the computer rather than on the arduino side.