Commit 3f46c099 authored by Nelso Jost's avatar Nelso Jost

NEW: structure refactor; commom pio Makefile usage; DEV: esplogger with...

NEW: structure refactor; commom pio Makefile usage; DEV: esplogger with arduino software serial suport tested
parent b4ff0355
/home/nelso/devel/meteorolog/utils/pioman/Makefile
\ No newline at end of file
include ../utils/platformio/Makefile
INOPROJECT = meteorolog
BAUD = 9600
BOARD = uno
monitor: qtmonitor
#
PORT :=
BAUD := 115200
PIOBOARD = nodemcuv2
PIOPROJ = firmware/esplogger
PIODIR = .pio
.PHONY: firmware monitor reset getport
all: firmware
install-pip3:
wget https://bootstrap.pypa.io/get-pip.py && \
sudo python3 get-pip.py && rm get-pip.py
install-platformio:
wget https://bootstrap.pypa.io/get-pip.py && \
sudo python get-pip.py && rm get-pip.py
sudo pip install platformio
$(MAKE) add-udev-rules
$(MAKE) add-dialout
add-dialout:
sudo usermod -a -G dialout $$USER
add-udevrules:
wget https://raw.githubusercontent.com/platformio/platformio/develop/scripts/99-platformio-udev.rules
sudo cp -v 99-platformio-udev.rules /etc/udev/rules.d
sudo service udev restart
firmware: build upload monitor
build:
which platformio || $(MAKE) install-platformio
@ echo "$$PIO_BUILD" | sh
upload:
which platformio || $(MAKE) install-platformio
cd $(PIODIR) && platformio run -t upload
monitor:
platformio device monitor
reset: getport
@ echo "-----------------------------------------"
@ echo "Reseting the board"
esptool.py --port /dev/$(PORT) run
@ python -c "import time; time.sleep(1)"
getport:
@ $(eval PORT := `python utils/getport.py`)
@ echo $(PORT)
example-blink:
$(eval PIOPROJ := examples/blink)
@ echo "$$PIO_BUILD" | sh
example-wifiscan:
$(eval PIOPROJ := examples/wifiscan)
@ echo "$$PIO_BUILD" | sh
$(MAKE) monitor
example-sdcardinfo:
$(eval PIOPROJ := examples/sd_card_info)
@ echo "$$PIO_BUILD" | sh
$(MAKE) monitor
help:
@ echo "$$HELP_MAKEFILE"
clean:
rm -rfv $(PIODIR)
define HELP_MAKEFILE
Usage: make <target> where <target> can be:
firmware Build and upload the esplogger firmware using platformio
monitor Start the picocom serial monitor (autodetect port)
reset Attempt a soft reset of the ESP devkit board
example-____ Build and upload one of the examples/ projects:
blink -- turn on and off the led on pin 2
wifiscan -- detect and print all available SSIDs
endef
export HELP_MAKEFILE
define PY_GET_PORT
import os
ports = [x for x in os.listdir("/dev") if x.startswith("ttyUSB")
or x.startswith("ttyACM")]
if ports and isinstance(ports[0], str):
print(ports[0])
else:
print("Error: Unable to find a ttyUSB/ttyACM serial port")
raise SystemExit(1)
endef
export PY_GET_PORT
define PIO_BUILD
mkdir -p $(PIODIR)
rm -rf $(PIODIR)/src
platformio init --board $(PIOBOARD) -d $(PIODIR)
cp -rf $(PIOPROJ)/* $(PIODIR)/src/
cd $(PIODIR) && platformio run
endef
export PIO_BUILD
Purpose
-------
Data logger firmware that runs on the ESP12e NodeMCU board connected an Arduino via software serial for sensor reading. This firmware is being designed to accomplish the following tasks:
* Read sensor data from the Arduino board (established platform for plugging sensors);
* Save the data locally onto an SD card;
* Attempt to send the data onto the server (if fail, try again later);
* Serial REPL (Read-Eval-Print-Loop) interpreter for sending commands;
** Used on initial setup and debugging tasks;
This software is part o the project EMM at CTA-UFRGS. See the official page for more details:
http://cta.if.ufrgs.br/projects/estacao-meteorologica-modular/wiki/Wiki
Usage
-----
The Makefile present on the root directory provide a quick commands for the most commom operations. Type "make" or "make help" for showing the help message. Some commands require external tools to be installed, like Platformio.
The project firmware is under the "firmware/" directory and is ArduinoIDE-compatible. Alternatively you can build/upload it via terminal using "make firmware" command, that uses the excelent PlatformIO tool (use "make install-platformio" if not present).
The examples folder contains several Arduino-IDE compatible little projects for demonstration of key features of the ESP12e.
Development
-----------
The software is still on its early stages, so new functionality will be added frequently.
#include <Arduino.h>
#define LED_PIN 2
#define SLEEP_INTERVAL 500 // milliseconds
void setup()
{
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
digitalWrite(LED_PIN, HIGH);
delay(SLEEP_INTERVAL);
digitalWrite(LED_PIN, LOW);
delay(SLEEP_INTERVAL);
}
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
#include "WProgram.h" #include "WProgram.h"
#endif #endif
#include <Adafruit_Sensor.h> #include "Adafruit_Sensor.h"
#ifdef __AVR_ATtiny85__ #ifdef __AVR_ATtiny85__
#include "TinyWireM.h" #include "TinyWireM.h"
......
include ../../utils/platformio/Makefile
BAUD = 115200
BOARD = uno
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include "Adafruit_Sensor.h" #include "Adafruit_Sensor.h"
#include "Adafruit_BMP085_U.h" #include "Adafruit_BMP085_U.h"
#define BAUDRATE 9600 #define BAUDRATE 115200
/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor), /* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
which provides a common 'type' for sensor data and some helper functions. which provides a common 'type' for sensor data and some helper functions.
......
/home/nelso/devel/meteorolog/utils/pioman/Makefile
\ No newline at end of file
include ../../utils/platformio/Makefile
BAUD = 115200
BOARD = nodemcuv2
#include <Arduino.h> #include <Arduino.h>
#define LED_PIN 2 #define LED_PIN 2 // NODEMCU D4
#define SLEEP_INTERVAL 500 // milliseconds #define SLEEP_INTERVAL 500 // milliseconds
void setup() void setup()
......
include ../../utils/platformio/Makefile
BAUD = 115200
BOARD = nodemcuv2
include ../../utils/platformio/Makefile
BAUD = 115200
BOARD = nodemcuv2
include ../utils/platformio/Makefile
INOPROJECT = esplogger
BAUD = 115200
BOARD = nodemcuv2 # from $ platformio boards esp8266
#monitor: qtmonitor
#
...@@ -5,23 +5,23 @@ ...@@ -5,23 +5,23 @@
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
#include <Arduino.h> #include <Arduino.h>
#include <Ticker.h> #include <Ticker.h>
#include <SoftwareSerial.h>
#include "nodemcu_pinout.h"
#include "repl.h" #include "repl.h"
#include "repl_gpio.h" #include "repl_gpio.h"
#include "repl_sdcard.h" #include "repl_sdcard.h"
#include "wifi.h" #include "repl_wifi.h"
#include <SoftwareSerial.h> #define BAUDRATE 115200
#define PIN_LED 0 // D3 #define PIN_LED D3
#define PIN_BUTTON 2 // D4 #define PIN_BUTTON D0
#define ARDUINO_TX 5 // D1 (soft RX) #define ARDUINO_TX D1 // (soft RX)
#define ARDUINO_RX 4 // D2 (soft TX) #define ARDUINO_RX D2 // (soft TX)
void REPL_WIFISCAN(REPL * repl);
void REPL_WIFISTATUS(REPL * repl);
void REPL_WIFICONNECT(REPL * repl);
void REPL_ARDUINO(REPL * repl); void REPL_ARDUINO(REPL * repl);
...@@ -32,18 +32,13 @@ REPL_COMMAND commands[] =\ ...@@ -32,18 +32,13 @@ REPL_COMMAND commands[] =\
{ {
ALLCMD_REPL_GPIO, ALLCMD_REPL_GPIO,
ALLCMD_REPL_SDCARD, ALLCMD_REPL_SDCARD,
ALLCMD_REPL_WIFI,
{"wifiscan", REPL_WIFISCAN, "wifiscan",
"List all availables SSID in range for connection."},
{"wifistatus", REPL_WIFISTATUS, "wifistatus",
"Print WiFi debug info (connection status)."},
{"wificonnect", REPL_WIFICONNECT, "wificonnect <ssid> <password>",
"Connect to wifi network."},
{"arduino", REPL_ARDUINO, "arduino <command>", {"arduino", REPL_ARDUINO, "arduino <command>",
"Sendo command to arduino."}, "Sendo command to Arduino via software serial."},
{"ad", REPL_ARDUINO, "ad <command>",
"Sendo command to Arduino via software serial."},
{NULL} {NULL}
}; };
...@@ -54,13 +49,16 @@ int button_state = 0; ...@@ -54,13 +49,16 @@ int button_state = 0;
Ticker timer_button; Ticker timer_button;
void setup() void setup()
{ {
Serial.begin(9600); Serial.begin(BAUDRATE);
timer_button.attach(0.1, toggle_led);
arduinoSerial.begin(9600); arduinoSerial.begin(9600);
pinMode(PIN_BUTTON, INPUT);
timer_button.attach(0.1, toggle_led);
} }
void loop() void loop()
{ {
repl.update(); repl.update();
...@@ -83,22 +81,14 @@ void toggle_led() ...@@ -83,22 +81,14 @@ void toggle_led()
} }
void REPL_WIFISCAN(REPL * repl)
{
wifiscan();
}
void REPL_WIFISTATUS(REPL * repl)
{
wifistatus();
}
void REPL_WIFICONNECT(REPL * repl)
{
wificonnect(repl->get_arg(1), repl->get_arg(2), 10);
}
void REPL_ARDUINO(REPL * repl) void REPL_ARDUINO(REPL * repl)
{ {
arduinoSerial.print("baka"); Serial.println();
arduinoSerial.print(repl->get_arg(1));
delay(1500);
while (arduinoSerial.available())
{
Serial.write(arduinoSerial.read());
}
} }
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Mapping of NodeMCU pinout to real ESP GPIO numbers.
*---------------------------------------------------------------------------*/
#ifndef NODEMCU_PINOUT_H
#define NODEMCU_PINOUT_H
#define D0 16 // DEEP WAKE
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0 // FLASH
#define D4 2 // LED_BUILTIN (inverted logic)
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial)
#define D10 1 // TX0 (Serial)
#endif
...@@ -85,7 +85,7 @@ void REPL::_complete(void) ...@@ -85,7 +85,7 @@ void REPL::_complete(void)
full_name = String(rc->name); full_name = String(rc->name);
if (full_name.startsWith(partial_name)) if (full_name.startsWith(partial_name))
{ {
Serial.print("\n\n[HELP] " + String(rc->prototype) + "\n" + Serial.print("\n[HELP] " + String(rc->prototype) + "\n" +
this->prompt + " " + full_name + " "); this->prompt + " " + full_name + " ");
this->_command_buffer = full_name + " "; this->_command_buffer = full_name + " ";
} }
......
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com) * Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2 * License: GPLv2
* Purpose: Read-Eval-Print-Loop functionality * Purpose: Implementation of REPL GPIO functions.
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
#include <Arduino.h> #include <Arduino.h>
#include "repl.h" #include "repl.h"
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#ifndef REPL_GPIO_H #ifndef REPL_GPIO_H
#define REPL_GPIO_H #define REPL_GPIO_H
#define CMD_REPL_DWRITE {\ #define CMD_REPL_DWRITE {\
"dwrite", REPL_DWRITE, "dwrite <pin> <value>",\ "dwrite", REPL_DWRITE, "dwrite <pin> <value>",\
"Perform pinMode(pin, OUTPUT) and digitalWrite(pin, value)."} "Perform pinMode(pin, OUTPUT) and digitalWrite(pin, value)."}
...@@ -32,6 +33,7 @@ ...@@ -32,6 +33,7 @@
"Blink builtin led (pin 2) with given <interval> in seconds (int|float). " \ "Blink builtin led (pin 2) with given <interval> in seconds (int|float). " \
"Use \"blink 0\" to stop it."} "Use \"blink 0\" to stop it."}
#define ALLCMD_REPL_GPIO \ #define ALLCMD_REPL_GPIO \
CMD_REPL_DWRITE, \ CMD_REPL_DWRITE, \
CMD_REPL_AWRITE, \ CMD_REPL_AWRITE, \
...@@ -40,6 +42,7 @@ ...@@ -40,6 +42,7 @@
CMD_REPL_LOG, \ CMD_REPL_LOG, \
CMD_REPL_BLINK CMD_REPL_BLINK
void REPL_DWRITE(REPL * repl); void REPL_DWRITE(REPL * repl);
void REPL_AWRITE(REPL * repl); void REPL_AWRITE(REPL * repl);
void REPL_AREAD(REPL * repl); void REPL_AREAD(REPL * repl);
...@@ -47,4 +50,5 @@ void REPL_DREAD(REPL * repl); ...@@ -47,4 +50,5 @@ void REPL_DREAD(REPL * repl);
void REPL_LOG(REPL * repl); void REPL_LOG(REPL * repl);
void REPL_BLINK(REPL * repl); void REPL_BLINK(REPL * repl);
#endif #endif
...@@ -8,9 +8,13 @@ ...@@ -8,9 +8,13 @@
#include "sdcard.h" #include "sdcard.h"
void REPL_SDCARDINFO(REPL * repl) void REPL_SDINFO(REPL * repl)
{ {
print_sdcard_info(); print_sdcard_info();
} }
void REPL_SDLOG(REPL * repl)
{
}
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com) * Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2 * License: GPLv2
* GPIO controlurpose: Provide basic SD card management commands for the REPL. * Purpose: Wrap SD card management functions as commands for the REPL.
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
#ifndef REPL_SDCARD_H #ifndef REPL_SDCARD_H
#define REPL_SDCARD_H #define REPL_SDCARD_H
#define CMD_REPL_SDCARDINFO {\ #define CMD_REPL_SDINFO {\
"sdcardinfo", REPL_SDCARDINFO, "sdcardinfo", \ "sdinfo", REPL_SDINFO, "sdinfo", \
"Show a summary of the SD card info."} "Show a summary of the SD card info."}
#define ALLCMD_REPL_SDCARD\ #define ALLCMD_REPL_SDCARD\
CMD_REPL_SDCARDINFO\ CMD_REPL_SDINFO\
void REPL_SDCARDINFO(REPL * repl); void REPL_SDINFO(REPL * repl);
#endif #endif
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Read-Eval-Print-Loop functionality
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include "repl.h"
#include "wifi.h"
void REPL_WIFISCAN(REPL * repl)
{
wifiscan();
}
void REPL_WIFISTATUS(REPL * repl)
{
wifistatus();
}
void REPL_WIFICONNECT(REPL * repl)
{
wificonnect(repl->get_arg(1), repl->get_arg(2), 10);
}
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Wrap wifi functions into REPL commands.
*---------------------------------------------------------------------------*/
#ifndef REPL_WIFI_H
#define REPL_WIFI_H
#define CMD_REPL_WIFISCAN {\
"wifiscan", REPL_WIFISCAN, "wifiscan", \
"List all availables SSID in range for connection."}
#define CMD_REPL_WIFISTATUS {\
"wifistatus", REPL_WIFISTATUS, "wifistatus", \
"Print WiFi connection status (debug)."}
#define CMD_REPL_WIFICONNECT {\
"wificonnect", REPL_WIFICONNECT, "wificonnect <ssid> <password>" \
"Connect to a wifi network. If successful (threshold 10 sec), " \
"it will autoconnect if board reseted."}
#define ALLCMD_REPL_WIFI \
CMD_REPL_WIFISCAN, \
CMD_REPL_WIFISTATUS, \
CMD_REPL_WIFICONNECT
void REPL_WIFISCAN(REPL * repl);
void REPL_WIFISTATUS(REPL * repl);
void REPL_WIFICONNECT(REPL * repl);
#endif
...@@ -46,6 +46,7 @@ void wifiscan(void) ...@@ -46,6 +46,7 @@ void wifiscan(void)
void wifistatus(void) void wifistatus(void)
{ {
Serial.println();
WiFi.printDiag(Serial); WiFi.printDiag(Serial);
} }
......
from requests import post
from getpass import getpass
URL = 'http://dados.cta.if.ufrgs.br/emm/api/get/boardhash'
board_id = input('Board ID: ')
user_password = getpass('Password: ')
r = post(URL, json={'board_id': board_id,
'user_password': user_password})
try:
js = r.json()
if 'Error' in js:
raise Exception
filename = input('Filename: ')
with open(filename, 'w') as f:
f.write(js['board_hash'])
except Exception as e:
print(e)
import os
ports = [x for x in os.listdir("/dev") if x.startswith("ttyUSB")
or x.startswith("ttyACM")]
if ports and isinstance(ports[0], str):
print(ports[0])
else:
print("Error: Unable to find a ttyUSB/ttyACM serial port")
raise SystemExit(1)
BOARD := uno
PIODIR := .pio
MAKEFILEDIR := $(shell python -c "import os; print(os.path.dirname(\
'$(shell readlink Makefile)'))")
PIOMAN := python3 $(MAKEFILEDIR)/pioman.py
QTSERMON := python3 $(MAKEFILEDIR)/qtsermon.py
.PHONY: build monitor uno due nano mega qtsermon picocom clean baka
all: build monitor
monitor: qtsermon
build:
$(PIOMAN) build --board $(BOARD) --pio-dir $(PIODIR)
uno:
$(PIOMAN) build --board uno --pio-dir $(PIODIR)
due:
$(PIOMAN) build --board diecimilaatmega328 --pio-dir $(PIODIR)
nano:
$(PIOMAN) build --board nanoatmega328 --pio-dir $(PIODIR)
mega:
$(PIOMAN) build --board megaatmega2560 --pio-dir $(PIODIR)
qtsermon:
$(QTSERMON) --baud $(shell $(PIOMAN) getbaud)
picocom:
picocom --baud $(shell $(GET_BAUD)) $(shell $(GET_PORT))
getbaud:
@ $(PIOMAN) getbaud
clean:
rm -rf $(PIODIR) .travis.yml
define GET_PORT
platformio device list | head -1
endef
</
PIODIR = .pio
INOPROJECT = .
BAUD = 115200
BOARD = uno
MAKEFILEROOT := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
.PHONY: firmware build upload monitor help clean
all: firmware
install-pip:
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py && rm get-pip.py
install-platformio:
which pip || $(MAKE) install-pip
sudo pip install platformio
sudo usermod -a -G dialout $$USER
wget https://raw.githubusercontent.com/platformio/platformio/develop/scripts/99-platformio-udev.rules
sudo mv -v 99-platformio-udev.rules /etc/udev/rules.d
sudo service udev restart
firmware: build upload monitor
build:
which platformio || $(MAKE) install-platformio
@ echo "$$PIO_BUILD" | sh
upload:
which platformio || $(MAKE) install-platformio
cd $(PIODIR) && platformio run -t upload
monitor:
platformio device monitor --baud $(BAUD)
qtmonitor:
python3 $(MAKEFILEROOT)/../qtsermon.py -b $(BAUD)
help:
@ echo "$$HELP_MAKEFILE"