esplogger.ino 4 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 <Ticker.h>
8 9
#include <SPI.h>
#include <SD.h>
10 11 12
#include <SoftwareSerial.h>

#include "nodemcu_pinout.h"
13
#include "repl.h"
14 15
#include "repl_gpio.h"
#include "repl_sdcard.h"
16
#include "repl_wifi.h"
17

18
#define     BAUDRATE        115200
19

20 21
#define     PIN_LED         D3
#define     PIN_BUTTON      D0
22

23 24
#define     ARDUINO_TX      D1    // (soft RX)
#define     ARDUINO_RX      D2    // (soft TX)
25

26
const int chipSelect = 4;
27 28

void REPL_ARDUINO(REPL * repl);
29
void REPL_METEOROLOG(REPL * repl);
30

31 32 33 34 35 36
//typedef struct
//{
    //REPL * repl;
    //float interval;
    //String command;
//} METEORLOG_args;
37

38
//void meteorolog_callback(METEOROLOG_args * args);
39

40 41 42

REPL_COMMAND commands[] =\
{
43 44
    ALLCMD_REPL_GPIO,
    ALLCMD_REPL_SDCARD,
45
    ALLCMD_REPL_WIFI,
46 47

    {"arduino", REPL_ARDUINO, "arduino <command>",
48
     "Send command to Arduino via software serial."},
49

50 51
    {"meteorolog", REPL_METEOROLOG, "meteorolog <interval> <command>",
     "Activate meterolog timer on given interval (seconds)."},
52

53
    {NULL}
54 55
};
REPL repl(commands);
56

57 58 59 60
int led_state = 0;
int button_state = 0;

Ticker  timer_button;
61
Ticker  timer_meteorolog;
62

63
SoftwareSerial arduinoSerial(ARDUINO_TX, ARDUINO_RX);  // RX, TX
64

65 66
void setup()
{
67
    Serial.begin(BAUDRATE);
68
    arduinoSerial.begin(9600);
69 70 71 72 73 74

	if (!SD.begin(chipSelect))
	{
        repl.log("\n[ERROR] Unable to initialize SD card.");
	}

75 76
    pinMode(PIN_BUTTON, INPUT);
    timer_button.attach(0.1, toggle_led);
77 78
}

79

80 81
void loop()
{
82 83 84
    repl.update();
}

85

86 87 88 89 90 91 92 93 94 95 96 97 98
void toggle_led()
{
    int reading = digitalRead(PIN_BUTTON);
    if (button_state != reading)
    {
        button_state = reading;
        if (button_state == HIGH)
        {
            led_state = !led_state;
            pinMode(PIN_LED, OUTPUT);
            digitalWrite(PIN_LED, led_state);
        }
    }
99 100 101 102
}



103 104
void REPL_ARDUINO(REPL * repl)
{
105 106 107 108 109 110 111
    Serial.println();
    arduinoSerial.print(repl->get_arg(1));
    delay(1500);
    while (arduinoSerial.available())
    {
        Serial.write(arduinoSerial.read());
    }
112
}
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183


void REPL_METEOROLOG(REPL * repl)
{
    METEOROLOG_args * args = new METEOROLOG_args;
    float interval = repl->get_arg(1).toFloat();
    //args->command = repl->get_arg(2);

    if (interval == 0)
    {
        timer_meteorolog.detach();
        repl->log("\n[INFO] Deactivated meteorolog timer.");
    }
    else
    {
        timer_meteorolog.attach(interval, meteorolog_callback, repl);
        repl->log("\n[INFO] Activated meteorolog timer with " +
                  String(interval) + " s interval.");
        meteorolog_callback(repl);
    }
}


void meteorolog_callback(REPL * repl)
{
	Sd2Card  sd_card;
	SdVolume sd_volume;

    //repl->log("\n[INFO] Performing datalog..");

    if (!sd_card.init(SPI_HALF_SPEED, chipSelect))
    {
        repl->log("\n[ERROR] Unable to detect/init SD card.");
        return;
    }
    if (!sd_volume.init(sd_card))
    {
        repl->log("\n[ERROR] SD card is plugged but no volume was found.");
        return;
    }

    File sd_file = SD.open("datalog.txt", FILE_WRITE);
    if (!sd_file)
    {
        repl->log("\n[ERROR] Unable to open 'datalog.txt' for write.");
        return;
    }

    String arduino_response;
    Serial.println();
    arduinoSerial.print(repl->get_arg(2));
    repl->log("\n[INFO] Sent command '" + repl->get_arg(2) + "' to arduino.");
    delay(1500);
    while (arduinoSerial.available())
    {
        arduino_response += arduinoSerial.read();
    }
    arduino_response.trim();
    repl->log("\n[INFO] Got response: '" + arduino_response + "'");

    if (arduino_response.length() > 0)
    {
        sd_file.println(arduino_response);
        sd_file.close();
        repl->log("\n[INFO] Written at 'datalog.txt'");
    }
    else
    {
        repl->log("\n[ERROR] Nothing to write on the sd card.");
    }
}