Arduino remotely controlled
Tags: arduino, cpp, iot, tutorial
Published: 2023-09-30 14:11:39

## Introduction

Hi all,

This is my first post here and I hope I write many more.

## What are the problems I want to solve?

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

## What is the solution?

I created an Arduino library that can do the following:

1. You assign pin, names and some typing for each actuator/sensor
1. Every item is assigned to a CentralCommandUnit (CCU) that will handle everything from that point

### What are the weak points?

1. It's in early stage
  1. Sometimes I see a problem that must be fixed with breaking changes
1. Currently only works for `bool` and `int` values

### Intended public

Please keep in mind that the code here, even for a 'blink' code, is more verbose than it would be in a beginner project. So it's not for those who are starting with Arduino. You won't see `pinMode`, `analogRead`... because the library handles that.

## Let's start

### Library

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

### Some wiring

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

Pin A0 connected to the resistor and photo resistor
The other resistor leg connected to GND
The other photo resistor leg connected to 5V

Let's use the built in led.

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

### Code

``` cpp
// imports
#include "bool_component.h"
#include "ccu.h"
#include "command_factory.h"
#include "percent_component.h"
#include "Simple_Repository_IO.h"

// for this library, we must know only the names
const String led = "led";
const String photoSensor = "photo";
// this one is responsible to execute commands
CentralCommandUnit ccu;

void setup()
{
  // creating a new Repository with 2 items
  Repository *hub = new Repository("hub", 2);

  // LED_BUILTIN >> your IDE knows the builtin led for your Arduino board
  // BoolComponent >> this led will turn on or off. This seems to be a boolean for me :)
  // Component::IoType::OUT >> we are not reading values, we're showing them
  BoolComponent *bc = new BoolComponent(LED_BUILTIN, led, Component::IoType::OUT);
  // A0 >> analogic input pin
  // PercentComponent >> the value starts at 0 (zero - pitch dark) and goes to 100 ("praise the sun")
  // Component::IoType::IN >> we are reading values
  PercentComponent *pc = new PercentComponent(A0, photoSensor, Component::IoType::IN);
  
  // adding to the repository
  hub->put(0, bc);
  hub->put(1, pc);
  // starting the Central Command Unit
  ccu.setup(hub);

  Serial.begin(9600);

  // ccu.execute >> tells the ccu to do some command
  // CommandFactory::describe() >> this is to create a JSON from your Arduino hub stored into the repository
  String result = ccu.execute(CommandFactory::describe());
  Serial.println(result);
}

// Please, don't use this library as the example bellow
// This is an example to it running. In the next article, we'll send commands remotely
void loop()
{
  // CommandFactory::read(photoSensor) >> asking the current value for the component with the name stored into the variable
  String value = ccu.execute(CommandFactory::read(photoSensor));

  const bool isBright = value.toInt() > 50;

  // CommandFactory::write(led, !isBright) >> change the value of an 'output' component with that name
  ccu.execute(CommandFactory::write(led, !isBright));
  delay(100);
}
```

### Results

- Turn on the lights = led off
- Turn off the lights (or cover the photo resistor) = led on

![On and Off](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3bkxve3tja7w2zozwgks.gif)

Yes, the code is VERY verbose. But this library is for remote control the Arduino.

## Next steps
1. [Connect the Arduino to a server](https://dev.to/iagows/arduino-remotely-controlled-part-2-1b1n)
  1. receive commands
  1. return data
  1. Create an API to get/set the hubs information
1. [Create a React Native app to handle the data](https://dev.to/iagows/arduino-remotely-controlled-part-3-229j)