Arduino Uno with ethernet hat

The other day, while cleaning out the garage, I found my old Arduino Uno. A few years ago, I received this as a new year's present by my employer at that time. Back then, I thought I was going to do something usefull with it. But apparently I didn't, since it was gathering dust on a shelf in the garage.

I also did not recall the fact there was an ethernet hat available in the package.

So it has an ethernet hat, what can we do with it? Well, we can connect it to a network, let it serve pages, let it perform REST calls, ... After doing all of the samples available in the Arduino IDE, I wanted to use this setup for something useful.

The project

This will be something very simple. I would like to use the Arduino to report the status of my garage door. Now that is something useful. Should someone open the door, I would like to receive an e-mail stating the current status of the door, OPEN in this case. Should the door be closed again, another e-mail should be sent, indicating the status is CLOSED again. Writing an app for my phone that shows the current status of the door will add to the coolness factor.

The picture above shows how this should work. Some kind of switch will be used to detect the door is open or closed.

The software on the Arduino should detect the state change of the switch and send an e-mail upon each change in state.

Nothing complicated, but how are we going to send e-mails?

Sending e-mails

On a different project, I had to send e-mails to a list of people. The content of the mail was personalized and also contained some values, parsed from a Google spreadsheet.
The easiest way to do this is to use an external service that can send transactional e-mails. A service like this also provides some kind of templating technology. To be able to send the mail, the only thing you need to do is perform a REST call to an exposed API (using a personal secret key), fill in the variables together with the ID of the template and The service should do the rest.

There are different providers out there (free and paying), but I settled with Sendinblue. Sendinblue has all that I needed (and more) and is free if you want to send no more than 300 e-mails a day.

After creating the template, you can send your mail performing the following REST call:

curl --request POST \
  --url https://api.sendinblue.com/v3/smtp/email \
  --header 'accept: application/json' \
  --header 'api-key:very-secret-key'
  --header 'content-type: application/json' \
  --data '{  
   "to":[  
      {  
         "email":"dude@dude.com",
         "name":"John Doe"
      }
   ],
   "templateId":3,
   "params":{  
      "status":"CLOSED"
   },
   "headers":{
      "charset":"iso-8859-1"
   }
}'

This will merge the variable status = 'CLOSED' with the template (3 in this case) and send the mail.

Arduino Uno doing HTTPS?

This is where the problems started. After trying to find some sample code on the Internet on how to do an HTTPS POST, I came across this post. Apparently the hardware is not powerful enough to do the S part of HTTPS. So what to do?

Fortunately there is more recent hardware available capable of doing encryption.

Arduino's makerboard WIFI 1010 was capable of performing encryption and should allow me to do an HTTPS POST to Sendinblue's API. The board is much more powerful than the Uno and even has an on board WIFI module.

The code

I am a Java developer, not a C++ developer, so I had to do some reading before I could write my first lines of code. I could write the program in a procedural way, but hey, this is C++, let's do the OO way.

I would like to have an interface NotificationSender with a method that will send some kind of notification based on the status. For debugging purposes I can use a NotificationSender that merely logs to the serial console. When this works, I can simply replace the SerialConsoleNotificationSender with the actual MailNotificationSender and it should start sending mails.

Again, I am not a C++ programmer, so with the help of Jetbrain's CLion I wrote some code:

NotificationSender.h
#include "Status.h"

class NotificationSender {
public:
    virtual void sendNotification(Status status) = 0;

    virtual ~NotificationSender() = default;
};
SerialConsoleNotificationSender.h
#include "Stream.h"
#include "NotificationSender.h"

class SerialConsoleNotificationSender : public NotificationSender {

private:
    Stream *stream;

public:
    explicit SerialConsoleNotificationSender(Stream *stream);

    void sendNotification(Status status) override;
};
SerialConsoleNotificationSender.cpp
#include "SerialConsoleNotificationSender.h"

SerialConsoleNotificationSender::SerialConsoleNotificationSender(Stream *stream) {
    this->stream = stream;
}

void SerialConsoleNotificationSender::sendNotification(Status status) {
    this->stream->print("sendNotification: ");
    switch (status) {
        case open:
            this->stream->println("OPEN");
            break;
        case closed:
            this->stream->println("CLOSED");
            break;
    }
}

CLion has great support for your Arduino hardware, using PlatformIO. I could go with the much cheaper (free) option and use Arduino's IDE, but I am very used to Jetbrain's IntelliJ IDEA to write Java and I was very pleased to find all the same shortcuts and features for the C++ platform.

You can find the rest of the code in my github project.

At the time of writing this is still a work in progress, so code will be added soon. In a next post, I will connect a switch to the makerboard and log stuff to the above serial console based on the received status. Stay tuned!

Comments

Popular posts from this blog

Remove copy protection from PDF documents

The story of the Cobalt Qube

Jori Hulkkonen feat. Jerry Valuri - Lo-Fiction