Arduino remotely controlled (part 2)
Tags: arduino, cpp, iot, tutorial
Published: 2023-09-30 19:16:21

## Introduction

In the [previous post](https://dev.to/iagows/arduino-remote-io-1mc7), we learned how to create an Arduino hub using the [Simple Repository IO](https://www.arduino.cc/reference/en/libraries/simple-repository-io/) library.

Now it's time to make it talk with an server.

## What are the problems I want to solve?

I want to create Arduino Hubs with actuators and sensors, but they must be controlled remotely.
This will (?) ease my mind that wants to create something.

## What is the solution?

After creating an Arduino library, I must create a server to do the remote communication.

## Weak points

1. The server is intended to run on your LAN only
  1. You can do changes to make it work on the WEB
1. ~~I'll create an NPM library to make it easier to call the commands without duplicating code over your servers' code.~~ Packages [created](https://www.npmjs.com/search?q=%40arduino_io).
1. It relies on Multicast
  1. I don't know your LAN configuration, but if you're here I hope you know how to handle that
    1. Right?

## Intended public

I expect you to have some knowledge regarding: TCP, UDP, IP ranges, and Typescript (not only C/C++)

## Let's start

### Library

In your Arduino IDE install the [Simple Repository IO](https://www.arduino.cc/reference/en/libraries/simple-repository-io/)

### Changing the wiring from previous post

| What | Amount|
| --- | --- |
| Arduino* |1|
|breadboard|1|
|220 ohm resistor|1|
|Led (built in) |1|
|Led|1|

* I hope you have a WeMos D1 R1 compatible Arduino board or an Wi-Fi module compatible with `ESP8266WiFi` and know how to wire it

Pin D10 to resistor
Resistor to LED
LED to GND

![Wiring](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o7etc657jiwhjil7v18c.jpg)

## Code

I created a tag so you can use the same version I used for this article.

- [Hub](https://gitlab.com/arduino_io/hub/-/tree/3.0.5?ref_type=tags) >> arduino code files
  1. It starts defining the components (see last article to better understanding)
  1. Then it [MULTICASTS](https://en.wikipedia.org/wiki/Multicast) a message via [UDP](https://en.wikipedia.org/wiki/User_Datagram_Protocol). Something like "I live, again"
  1. Wait for WiFi [TCP](https://en.wikipedia.org/wiki/Transmission_Control_Protocol) messages with some command
- [Server](https://gitlab.com/arduino_io/server/-/tree/3.0.5?ref_type=tags) >> typescript node server
  1. It starts two servers
    1. UDP server to add hubs to its list
    1. TCP server to act as an API
      - each endpoint calls the Arduino

## Results

When calling `http://localhost:3000/Hub_A/write/led/true`

![Led on](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3snr95dpxumtnp3cpf91.jpg)

When calling `http://localhost:3000/Hub_A/write/led/false`

![Led off](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbphcxuxyev99wor0le1.jpg)

When the `pwm_led` is set to 0 (zero) and calling `http://localhost:3000/write/Hub_A/pwm_led/100`

![Animated image of an led turning on](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kmwmj8mqa1h6p9tjxgsx.gif)

## Previous step

1. [Create an Arduino library to remote communication](https://dev.to/iagows/arduino-remote-io-1mc7)

## Next step

1. [Create a React Native app to handle the data](https://dev.to/iagows/arduino-remotely-controlled-part-3-229j)