DEV Community

Iago
Iago

Posted on • Edited on

Arduino remotely controlled

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.
  2. 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
  2. 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
  2. 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

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

Code

// 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);
}
Enter fullscreen mode Exit fullscreen mode

Results

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

On and Off

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

Next steps

  1. Connect the Arduino to a server
    1. receive commands
    2. return data
    3. Create an API to get/set the hubs information
  2. Create a React Native app to handle the data

Top comments (0)