Skip to content
Snippets Groups Projects
WallVis.h 5.63 KiB
Newer Older
Dave Murray-Rust's avatar
Dave Murray-Rust committed
#ifndef WALLVIS_H
#define WALLVIS_H
#include "Arduino.h"
#include "Behaviours.h"
#include "ServoBehaviours.h"

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define MQTT_topic  "new001"


Dave Murray-Rust's avatar
Dave Murray-Rust committed

class WallVis {
  char* _ssid;
  char* _wifi_pass;
  char* _id;
  char* _server;
  int _port;
  BehaviourTable _behaviours;
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  Behaviour* _active = nullptr;
  int _loop_time = 5;
  Adafruit_MQTT_Client* _mqtt;
  Adafruit_MQTT_Subscribe* _device_subscription;
  WiFiClient* _client;

public:
  WallVis(char* id, char* ssid="WallVisNet", char* wifi_pass="wallvisAP",
    char* server="192.168.4.1",int port=1883) : _id(id), _server(server), _port(port), _ssid(ssid), _wifi_pass(wifi_pass) {} ;

  void command_callback(char *data, uint16_t len) {
    Serial.println("Got command: ");
    Serial.println(data);
  }



  /*
   * Set up the WallVis node - WiFi, MQTT
   */
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  void init() {
    Serial.setTimeout(100);
    Serial.println();
    Serial.println(F("WallVis Node starting up"));
    Serial.println("Initialising " + String(_id));
Dave Murray-Rust's avatar
Dave Murray-Rust committed

    WiFi.mode(WIFI_STA);

    // Connect to WiFi access point.
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(_ssid);
Dave Murray-Rust's avatar
Dave Murray-Rust committed

    WiFi.begin(_ssid, _wifi_pass);
    while (WiFi.status() != WL_CONNECTED) {
Dave Murray-Rust's avatar
Dave Murray-Rust committed
     delay(500);
     Serial.print(".");
    }
    Serial.println();
Dave Murray-Rust's avatar
Dave Murray-Rust committed

    Serial.println("WiFi connected");
    Serial.println("IP address: "); Serial.println(WiFi.localIP());
    // Done Wifi
Dave Murray-Rust's avatar
Dave Murray-Rust committed

    // Setup MQTT
    _client = new WiFiClient();
    _mqtt = new Adafruit_MQTT_Client(_client, _server, _port, "" /* mqttt username */, "" /* mqtt pass*/);
    _device_subscription = new Adafruit_MQTT_Subscribe(_mqtt, _id);
Dave Murray-Rust's avatar
Dave Murray-Rust committed

    // Setup MQTT subscription for this device
    _mqtt->subscribe(_device_subscription);
    // This *would* setup a callback, but we're not doing this right now...
    //_device_subscription->setCallback(test_callback);
Dave Murray-Rust's avatar
Dave Murray-Rust committed

    MQTT_connect();

    Serial.println("Init finished");
  /*
   * Add a behaviour to the list of possible behaviours
   */
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  void add(Behaviour *b) {
    _behaviours.add(b);
  }

  /*
   * This is the main loop. It should be called from within loop() - really
   * this function is the only thing you should need to call. It will manage
   * it's own delay, so you can call as often as possible.
   */
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  void vis_loop() {
    int loop_start_time = millis();
    serial_command();
    //mqtt_command();
    if( _active ) {
      Serial.println("Updating "+_active->name());
      _active -> update();
      if( ! _active->is_running() ) _active = nullptr;
    }
    int loop_time_taken = millis()-loop_start_time;
    if( loop_time_taken < _loop_time ) {
      delay( _loop_time - loop_time_taken );
    }
  }

  /*
   * Read a command from the serial input and process it
   */
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  void serial_command() {
    if( Serial.available() ) {
      String cmd = Serial.readStringUntil('\n');
      Serial.println(process(cmd));
    }
  }

  /*
   * Read a command from the serial input and process it. It only waits for
   * 50ms to allow other behaviours to continue.
   */
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  void mqtt_command() {
      MQTT_connect(); //ensure connection
      Adafruit_MQTT_Subscribe *subscription;
      while ((subscription = _mqtt->readSubscription(50))) {
        if (subscription == _device_subscription) {
          Serial.print(F("Got: "));
          Serial.println((char *)_device_subscription->lastread);
          Serial.println(process((char *)_device_subscription->lastread));
        }
      }
  }

  /*
   * Process a command. This means:
   * - split the command name from the arguments
   * - call process_comman with the separated command and argument string
   */
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  String process(String input) {
    int index = input.indexOf(" ");
    String command = "";
    String args = "";

    if( index) {
      command = input.substring(0,index);
      args = input.substring(index+1);
    } else {
      command = input;
    }
    return process_command(command, args);
  }

  /*
   * Process a command and its arguments. This means:
   * - look for a Behaiour with the right name
   * - if found, then call that behaviour with the arguments (which are still a single string)
   */
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  String process_command(String command, String args) {
    Serial.println("Processing <"+command+"> <"+args+">");
    Behaviour* b = _behaviours.get(command);
    if(b) {
      if( _active ) { _active->stop(); }
      Serial.println( "Found behaviour: <"+command+">" );
      _active = b;
      return( b->start(args) );
    } else {
      return "Couldn't process command: " + command;
    }
  }

  /*
  * Function to connect and reconnect as necessary to the MQTT server.
  */
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  // Should be called in the loop function and it will take care if connecting.
  void MQTT_connect() {
    int8_t ret;

    // Stop if already connected.
    if (_mqtt->connected()) {
      return;
    }

    Serial.print("Connecting to MQTT... ");

    uint8_t retries = 3;
    while ((ret = _mqtt->connect()) != 0) { // connect will return 0 for connected
      Serial.println(_mqtt->connectErrorString(ret));
      Serial.println("Retrying MQTT connection in 5 seconds...");
      _mqtt->disconnect();
      delay(5000);  // wait 5 seconds
      retries--;
      if (retries == 0) {
        // basically die and wait for WDT to reset me
        while (1);
      }
Dave Murray-Rust's avatar
Dave Murray-Rust committed
    }
    Serial.println("MQTT Connected!");
  }

};


/* Left here temporarily - can't use a member function as a callback
   So this was the start of a way to create a static function that would work
void* current_wallvis;

void test_callback(char *data, uint16_t len) {
  Serial.println("Callback: ");
  Serial.println(data);
  ((WallVis*)current_wallvis)->process(String(data));
}
*/