Commit 9229f58c authored by Nelso Jost's avatar Nelso Jost

DEV: untested meteorolog command

parent 3f46c099
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
include ../../utils/platformio/Makefile
BAUD = 115200
BOARD = uno
#include <Arduino.h>
#define MAX_COLS 25
class StringCols
{
public:
StringCols(String line, String col_names, String sep);
String get_col(String name);
protected:
String col[MAX_COLS];
String name[MAX_COLS];
int count=0;
};
void setup()
{
StringCols * f = new StringCols(
String("setrtc 2017 06 19 00 14 00"),
String("command year month day hour minute second"), String(" "));
}
void loop()
{
}
StringCols::StringCols(String line, String col_names, String sep);
{
this->fields[0] = line;
}
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
#include <Arduino.h> #include <Arduino.h>
#include <Ticker.h> #include <Ticker.h>
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
#include "nodemcu_pinout.h" #include "nodemcu_pinout.h"
...@@ -21,11 +23,19 @@ ...@@ -21,11 +23,19 @@
#define ARDUINO_TX D1 // (soft RX) #define ARDUINO_TX D1 // (soft RX)
#define ARDUINO_RX D2 // (soft TX) #define ARDUINO_RX D2 // (soft TX)
const int chipSelect = 4;
void REPL_ARDUINO(REPL * repl); void REPL_ARDUINO(REPL * repl);
void REPL_METEOROLOG(REPL * repl);
//typedef struct
//{
//REPL * repl;
//float interval;
//String command;
//} METEORLOG_args;
SoftwareSerial arduinoSerial(ARDUINO_TX, ARDUINO_RX); // RX, TX //void meteorolog_callback(METEOROLOG_args * args);
REPL_COMMAND commands[] =\ REPL_COMMAND commands[] =\
...@@ -35,10 +45,10 @@ REPL_COMMAND commands[] =\ ...@@ -35,10 +45,10 @@ REPL_COMMAND commands[] =\
ALLCMD_REPL_WIFI, ALLCMD_REPL_WIFI,
{"arduino", REPL_ARDUINO, "arduino <command>", {"arduino", REPL_ARDUINO, "arduino <command>",
"Sendo command to Arduino via software serial."}, "Send command to Arduino via software serial."},
{"ad", REPL_ARDUINO, "ad <command>", {"meteorolog", REPL_METEOROLOG, "meteorolog <interval> <command>",
"Sendo command to Arduino via software serial."}, "Activate meterolog timer on given interval (seconds)."},
{NULL} {NULL}
}; };
...@@ -48,12 +58,20 @@ int led_state = 0; ...@@ -48,12 +58,20 @@ int led_state = 0;
int button_state = 0; int button_state = 0;
Ticker timer_button; Ticker timer_button;
Ticker timer_meteorolog;
SoftwareSerial arduinoSerial(ARDUINO_TX, ARDUINO_RX); // RX, TX
void setup() void setup()
{ {
Serial.begin(BAUDRATE); Serial.begin(BAUDRATE);
arduinoSerial.begin(9600); arduinoSerial.begin(9600);
if (!SD.begin(chipSelect))
{
repl.log("\n[ERROR] Unable to initialize SD card.");
}
pinMode(PIN_BUTTON, INPUT); pinMode(PIN_BUTTON, INPUT);
timer_button.attach(0.1, toggle_led); timer_button.attach(0.1, toggle_led);
} }
...@@ -92,3 +110,74 @@ void REPL_ARDUINO(REPL * repl) ...@@ -92,3 +110,74 @@ void REPL_ARDUINO(REPL * repl)
Serial.write(arduinoSerial.read()); Serial.write(arduinoSerial.read());
} }
} }
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.");
}
}
...@@ -14,7 +14,3 @@ void REPL_SDINFO(REPL * repl) ...@@ -14,7 +14,3 @@ void REPL_SDINFO(REPL * repl)
} }
void REPL_SDLOG(REPL * repl)
{
}
...@@ -13,6 +13,7 @@ SdFile root; ...@@ -13,6 +13,7 @@ SdFile root;
const int chipSelect = 4; const int chipSelect = 4;
/* Credits: /* Credits:
* https://github.com/esp8266/Arduino/tree/master/libraries/SD/examples/CardInfo * https://github.com/esp8266/Arduino/tree/master/libraries/SD/examples/CardInfo
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment