Skip to content

Latest commit

 

History

History
111 lines (89 loc) · 2.25 KB

README.md

File metadata and controls

111 lines (89 loc) · 2.25 KB

esp8266-observer-pattern

A sample implementation of Observer Pattern

This code was written using PlatformIO and vscode. Would you like This code to be compatible with the Arduino IDE?... tell me!

Goal

Implement Observer Design Pattern in a microcontroller.

Can we use design patterns in our code for microcontrollers? ... obviously yes (otherwise we would not be here).

OK, show me the code!

Diagram

class diagram

I said "code", not diagram!

Program

#include ...

SensorReader sensorReader;

void setup()
{
    ...

    sensorReader.attach(new LedIndicator());
    sensorReader.attach(new LoggerToSerial());
    sensorReader.attach(new MqttPublisher());
    
    ...
}

void loop()
{
    sensorReader.work();
}

Define a Subject

We define a class inheriting Subject with the type of notification argument

#define INTERVAL 2000

class SensorReader : public Subject<EventArgs>
{
  public:
    void work()
    {
        if (lastRead + INTERVAL < millis())
        {
            // Reading simulation
            EventArgs args;
            args.temperature = random(15, 35);
            args.humidity = random(20, 80);

            Subject::notify(args);

            lastRead = millis();
        }
    }

  private:
    long lastRead = 0;
};

Structure of the event argument used:

struct EventArgs
{
  float temperature;
  float humidity;
};

Yes, it's a struct but could be a class too

Define Observers

We define different classes inheriting Observer

Observer which send information over Serial

class LoggerToSerial : public Observer<EventArgs>
{
  public:
    void notify(EventArgs args) override
    {
        Serial.print("Temperature: ");
        Serial.print(args.temperature);
        Serial.print(", Humidity: ");
        Serial.println(args.humidity);
    }
};

Observer that turn on or off a LED

class LedIndicator : public Observer<EventArgs>
{
  public:
	void notify(EventArgs args) override
	{
		if (args.temperature > 30 || args.humidity > 50)
			digitalWrite(LED_BUILTIN, LOW);
		else
			digitalWrite(LED_BUILTIN, HIGH);
	}
};