esplogger.ino 3.75 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
#include "blinker.h"
10
#include "sdcard.h"
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#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);
25
void REPL_SDCARDINFO(String command);
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

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."},
45
46
47
    {"sdcardinfo", REPL_SDCARDINFO,
        "sdcardinfo",
        "Show a summary of the SD card info."},
48
49
50
    {NULL, NULL}
};
REPL repl(commands);
51
52
53
54


void setup()
{
55
56
    Serial.begin(9600);
    repl.run();
57
58
59
60
}

void loop()
{
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

}


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


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

    if (interval == 0)
76
    {
77
78
79
80
81
82
        blinker.deactivate();
        if (LOGGING)
        {
            Serial.print("\n[INFO] Deactivated blink builtin led (pin 2).");
        }

83
84
85
    }
    else
    {
86
87
88
89
90
91
92
93
        blinker.activate(interval);
        if (LOGGING)
        {
            Serial.print("\n[INFO] Activated blink builtin led (pin 2) with ");
            Serial.print(interval);
            Serial.print(" s interval.");
        }
    }
94
95
}

96
97

void REPL_DWRITE(String command)
98
{
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    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)
113
    {
114
115
116
117
        Serial.print("\n[INFO] Pin ");
        Serial.print(pin);
        Serial.print(" was set to ");
        Serial.print(value? "HIGH":"LOW");
118
    }
119
120
121
122
123
124
125
126
}


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

    if (LOGGING)
127
    {
128
129
130
131
132
133
134
        Serial.print("\n[INFO] Reading analog pin ");
        Serial.print(pin);
        Serial.println(":");
    }
    Serial.print(analogRead(pin));
}

135

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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(".");
        }
155
156
157
    }
    else
    {
158
159
160
161
162
163
164
165
166
        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.");
        }
167
168
    }
}
169
170
171
172
173

void REPL_SDCARDINFO(String command)
{
    print_sdcard_info();
}