esplogger.ino 3.52 KB
Newer Older
1 2 3 4 5
/*-----------------------------------------------------------------------------
 *  Author: Nelso G. Jost (nelsojost@gmail.com)
 *  License: GPLv2
 *  Purpose: Entry point for the Arduino Toolchain.
 *---------------------------------------------------------------------------*/
6
#include <Arduino.h>
7
#include "repl.h"
8
#include "wifi.h"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "blinker.h"

#define  BUILTIN_LED  2
Blinker blinker(BUILTIN_LED);

Ticker alog_ticker;

bool LOGGING=true;


void REPL_WIFISCAN(String command);
void REPL_BLINK(String command);
void REPL_DWRITE(String command);
void REPL_AREAD(String command);
void REPL_ALOG(String command);

REPL_COMMAND commands[] =\
{
    {"wifiscan", REPL_WIFISCAN,
        "wifiscan",
        "List all availables SSID in range for connection."},
    {"blink", REPL_BLINK,
        "blink <interval>",
        "Blink builtin led (pin 2) with given <interval> in seconds (int|float). "
        "Use \"blink 0\" to stop it."},
    {"dwrite", REPL_DWRITE,
        "dwrite <pin> <value>",
        "Digital write <value> (number|high|low) on <pin>. "},
    {"aread", REPL_AREAD,
        "aread <pin>",
        "Read analogic pin and print out the value."},
    {"alog", REPL_ALOG,
        "alog <pin> <interval>",
        "Start datalog on analogic pin with given interval."},
    {NULL, NULL}
};
REPL repl(commands);
46 47 48 49


void setup()
{
50 51
    Serial.begin(9600);
    repl.run();
52 53 54 55
}

void loop()
{
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

}


void REPL_WIFISCAN(String command)
{
    wifiscan();
}


void REPL_BLINK(String command)
{
    float interval = get_arg(command, 1).toFloat();

    if (interval == 0)
71
    {
72 73 74 75 76 77
        blinker.deactivate();
        if (LOGGING)
        {
            Serial.print("\n[INFO] Deactivated blink builtin led (pin 2).");
        }

78 79 80
    }
    else
    {
81 82 83 84 85 86 87 88
        blinker.activate(interval);
        if (LOGGING)
        {
            Serial.print("\n[INFO] Activated blink builtin led (pin 2) with ");
            Serial.print(interval);
            Serial.print(" s interval.");
        }
    }
89 90
}

91 92

void REPL_DWRITE(String command)
93
{
94 95 96 97 98 99 100 101 102 103 104 105 106 107
    String arg_pin = get_arg(command, 1);
    String arg_value = get_arg(command, 2);

    int pin = arg_pin.toInt();
    int value = !arg_value.toInt();

    if (arg_pin == "led") pin = BUILTIN_LED;
    if (arg_value == "high") value = 0;
    if (arg_value == "low") value = 1;

    pinMode(pin, OUTPUT);
    digitalWrite(pin, value);

    if (LOGGING)
108
    {
109 110 111 112
        Serial.print("\n[INFO] Pin ");
        Serial.print(pin);
        Serial.print(" was set to ");
        Serial.print(value? "HIGH":"LOW");
113
    }
114 115 116 117 118 119 120 121
}


void REPL_AREAD(String command)
{
    int pin = get_arg(command, 1).toInt();

    if (LOGGING)
122
    {
123 124 125 126 127 128 129
        Serial.print("\n[INFO] Reading analog pin ");
        Serial.print(pin);
        Serial.println(":");
    }
    Serial.print(analogRead(pin));
}

130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
void alog_update(int pin)
{
    repl.log("a" + String(pin) + ":" + String(analogRead(pin)));
}

void REPL_ALOG(String command)
{
    int pin = get_arg(command, 1).toInt();
    int interval = get_arg(command, 2).toInt();

    if (interval == 0)
    {
        alog_ticker.detach();
        if (LOGGING)
        {
            Serial.print("\n[INFO] Deactivated datalog on analog pin ");
            Serial.print(pin);
            Serial.print(".");
        }
150 151 152
    }
    else
    {
153 154 155 156 157 158 159 160 161
        alog_ticker.attach(interval, alog_update, pin);
        if (LOGGING)
        {
            Serial.print("\n[INFO] Activated datalog on analog pin ");
            Serial.print(pin);
            Serial.print(" with ");
            Serial.print(interval);
            Serial.print(" s interval.");
        }
162 163
    }
}