Start to move from Arduino (C/C++) to MicroPython / CircuitPython (teensy=>Feather M0 express)

This commit is contained in:
Pierrick C 2018-07-28 18:42:27 +02:00
parent f421bda621
commit 01ed6e1658
19 changed files with 143 additions and 1492 deletions

View File

@ -1,4 +0,0 @@
#Fichier de configuration du contrôleur du projet
# Camétéo
time_step=2000
data_file=datalog3.csv

View File

@ -1,42 +0,0 @@
#include "cameteo_teensy.h"
void stopRPI() {
//Stop the Raspberry Pi via the MOSFET (grid connected to the RPI_PWR_PIN)
pinMode(RPI_PWR_PIN, INPUT); //Eteint via le MOSFET
rpi_status = false;
}
bool isStartedPI() {
//Return true if the R-Pi respond to an simple request
return rpi_status;
}
void startRPI() {
//Start the Raspberry Pi via the MOSFET (grid connected to the RPI_PWR_PIN)
if (!isStartedPI()) {
pinMode(RPI_PWR_PIN, OUTPUT);
digitalWrite(RPI_PWR_PIN, LOW);
rpi_status = true;
}
}
void sendDataToSerial(String data) {
if (isStartedPI()) {
char c[100]; // char buffer for conversion String->char
data.toCharArray(c, sizeof(data)); //convert data string to char array
// //start serial comm. if needed
// if(!SERIAL_PORT) {
// SERIAL_PORT.begin(SERIAL_BAUD_RATE);
// //while(!SERIAL_PORT);
// }
SERIAL_PORT.print(c); // send data on serial port
}
else {
//error RPI is not started
}
}

View File

@ -1,55 +0,0 @@
#include "cameteo_teensy.h"
void BootMessage(String s) {
char c[13]; // char buffer for conversion String->char
s.toCharArray(c, sizeof(s));
SERIAL_PORT.printf("%-12s", c);
}
void BootOK() {
SERIAL_PORT.println("OK");
}
void BootError() {
SERIAL_PORT.println("EE");
}
void sendDataOnSerial() {
//Print date & hour on serial port
SERIAL_PORT.printf("%04d/%02d/%02d_%02d:%02d:%02d\n", year(), month(), day(), hour(), minute(), second());
// if (isnan(dht22_event_hum.relative_humidity) ||
// isnan(dht22_event_temp.temperature)) {
// SERIAL_PORT.printf("Failed to read from DHT sensor!\n");
// }
// else {
// SERIAL_PORT.printf("Temperature:%8.2f %cC | Humidity:%8.0f %%\n",
// dht22_event_temp.temperature, DEGREE, dht22_event_hum.relative_humidity);
// }
// if (bme280_event.pressure) {
SERIAL_PORT.printf("Temperature:%8.2f %cC | Humidity: %8.2f % | Pressure: %8.2f hPa | Altitude:%8.2f m\n",
bme280_temp, DEGREE, bme280_press, bme280_alti);
// }
// else {
// SERIAL_PORT.printf("BME280 Sensor error\n");
// }
SERIAL_PORT.printf("Lightning strikes (from start) : %d | Perturb.: %d | Noise : %d | Unknown detect.: %d\n", lightning_nb_total,
as3935_perturb_total,
as3935_noise_total,
as3935_unknown_total);
//SERIAL_PORT.printf("Temperature:%8.2f %cC\n", tcn75a_temp, DEGREE);
SERIAL_PORT.printf("GPS data: %04d/%02d/%02d_%02d:%02d:%02d (%d)\n", gps_year, gps_month, gps_day,
gps_hour, gps_minutes, gps_second,
gps_fix_age);
SERIAL_PORT.printf(" Latitude: %11.8f | Longitude: %11.8f | Altitude: %5.2f m\n", gps_latitude, gps_longitude, gps_altitude);
SERIAL_PORT.printf(" Speed: %4.1f km/h | Course : %4.1f %c\n", gps_speed, gps_course, DEGREE);
SERIAL_PORT.printf(" Chars: %11d | Sentences: %11d | Failed cheksum: %4d\n", gps_chars, gps_sentences, gps_failed_checksum);
SERIAL_PORT.printf("Battery : %10d mV | Low Battery : %d\n", batt_voltage, low_battery_flag);
}

View File

@ -1,36 +0,0 @@
/*
* CAMETEO project
*
* This is a personnal project of weather station with
* automatic photo taking.
* This code is the weather station part and is meant
* to be run on a PJRC Teensy 3.2 board.
*
* Author : Arofarn
*
* Licence : GPL v3
*
*/
//Protocols
#include <Wire.h> // library used with I2C protocol
#include <SPI.h> // SPI protocol
//Teensy3.x Real Time Clock
#include <TimeLib.h>
//SD card
#include <SD.h>
// Sensors
#include <Adafruit_Sensor.h> // Generic
//#include <DHT.h> // DHT22
//#include <DHT_U.h> // DHT22 unified
//#include <Adafruit_BMP085_U.h> // BMP180
#include <Adafruit_BME280.h> // BME280
#include <PWFusion_AS3935.h>
//GPS
//#include <Adafruit_GPS.h> // Adafruit Ultimate GPS
#include <TinyGPS.h> //Builtin GPS lib

View File

@ -1,399 +0,0 @@
/*
* CAMETEO project
*
* This is a personnal project of weather station with
* automatic photo taking.
* This code is the weather station part and is meant
* to be run on a PJRC Teensy 3.2 board.
*
* Author : Arofarn
*
* Licence : GPL v3
*
*/
#include <Arduino.h>
#include "cameteo_teensy.h"
#include "SerialMessages.cpp"
#include "RaspBerryPi_COM.cpp"
//sensors_event_t bme280_event;
float seaLevelPressure = 1015.0;
float bme280_press;
float bme280_temp;
float bme280_alti;
float bme280_hum;
enum strike_sources { UNKNOWN_SRC, LIGHTNING, PERTURBATION, NOISE };
volatile int8_t AS3935_ISR_Trig = 0; // Trigger for AS3935 lightning sensor
void AS3935_ISR() {
AS3935_ISR_Trig = 1;
}
PWF_AS3935 lightning(AS3935_CS_PIN, AS3935_IRQ_PIN, 33);
int as3935_src;
int as3935_distance;
long lightning_nb_total = 0;
int lightning_nb_hour = 0;
int lightning_nb_day = 0;
int as3935_perturb_total = 0;
int as3935_noise_total = 0;
int as3935_unknown_total = 0;
char lightning_log_file[12] = "lghtnng.log";
//GPS
//Adafruit_GPS GPS(&GPS_SERIAL_PORT);
TinyGPS GPS;
float gps_latitude, gps_longitude, gps_altitude; // returns +- latitude/longitude in degrees
float gps_speed, gps_course;
unsigned long gps_time, gps_date, gps_fix_age;
int gps_year;
byte gps_month, gps_day, gps_hour, gps_minutes, gps_second, gps_hundreths;
unsigned long gps_chars, gps_hdop;
unsigned short gps_sentences, gps_failed_checksum, gps_satellites;
//Miscellaneous
bool rpi_status;
rpi_status = false;
int batt_voltage;
bool low_battery_flag = false;
// Tasks timers
elapsedMillis since_bme280;
//elapsedMillis since_dht;
elapsedMillis since_gps;
elapsedMillis since_display;
elapsedMillis since_serial_send;
elapsedMillis since_sd_log;
elapsedMillis since_batt_chk;
//SD Card
#define CONFIGFILE "config.txt"
//Configuration
char data_file[13] = "datalog.csv";
//Delays
unsigned int bme280_delay = 500;
//unsigned int dht_delay = 3000;
unsigned int gps_delay = 100;
unsigned int serial_send_delay = 5000;
unsigned int sd_log_delay = 5000;
unsigned int batt_chk_delay = 5000;
//Date and time
int TZ = 1; //Time zone
/*
* SETUP
*/
void setup() {
//To be sure Raspberry-Pi won't be turned on unexpectedly
stopRPI();
pinMode(LOW_BATT_PIN, INPUT_PULLUP);
low_battery_flag = !digitalRead(LOW_BATT_PIN);
pinMode(GPS_BUT_PIN, INPUT_PULLUP);
pinMode(GPS_EN_PIN, OUTPUT);
gps_power();
SERIAL_PORT.begin(SERIAL_BAUD_RATE);
delay(1000);
//while (!SERIAL_PORT) { }; //Wait for serial port to start
SERIAL_PORT.printf("Serial Comm. OK\nNow booting...\n");
BootMessage("SDcard");
// see if the card is present and can be initialized:
if (!SD.begin(SD_CS_PIN)) {
BootError();
while(1);
}
BootOK();
BootMessage("RT Clock");
// set the Time library to use Teensy 3.0's RTC to keep time
setSyncProvider(getTeensy3Time);
if (timeStatus()!= timeSet) {
BootError();
while(1);
}
BootOK();
// BootMessage("AM2302/DHT22 (Humidity and Temperature)");
// dht.begin();
// //sensor_t sensor;
// BootOK();
BootMessage("BME280 (Pressure and Temperature)");
/* Initialise the sensor */
if(!bme.begin())
{
/* There was a problem detecting the BMP085 ... check your connections */
BootError();
while(1);
}
BootOK();
BootMessage("AS3935 (Lightning)");
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV16); // SPI speed to SPI_CLOCK_DIV16/1MHz (max 2MHz, NEVER 500kHz!)
SPI.setDataMode(SPI_MODE1); // MAX31855 is a Mode 1 device --> clock starts low, read on rising edge
SPI.setBitOrder(MSBFIRST); // data sent to chip MSb first
lightning.AS3935_DefInit(); // set registers to default
// now update sensor cal for your application and power up chip
lightning.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_INDOORS, AS3935_DIST_EN);
// enable interrupt (hook IRQ pin to Arduino Uno/Mega interrupt input: 0 -> pin 2, 1 -> pin 3 )
attachInterrupt(AS3935_IRQ_PIN, AS3935_ISR, RISING);
BootOK();
BootMessage("GPS");
GPS_SERIAL_PORT.begin(GPS_SERIAL_BAUD_RATE);
while (!GPS_SERIAL_PORT) {} ;
BootOK();
}
/*
* LOOP
*/
void loop() {
//Power ON/OFF GPS
gps_power();
//Lightning detection
if (AS3935_ISR_Trig != 0) {
AS3935_ISR_Trig = 0;
time_t t = now();
int distance = -9999;
int energy = -9999;
switch (as3935_src) {
case UNKNOWN_SRC:
//source inconnue
as3935_unknown_total++;
SERIAL_PORT.printf("Interruption (AS3935) : unkown source (not lightning)\n");
break;
case LIGHTNING:
//Foudre !!!
lightning_nb_total++;
distance = lightning.AS3935_GetLightningDistKm();
energy = lightning.AS3935_GetStrikeEnergyRaw();
SERIAL_PORT.printf("Interruption (AS3935) : Lightningbolt !!!\n");
SERIAL_PORT.printf("Distance : %4d km | Energy : %d \n", distance, energy);
break;
case PERTURBATION:
//Perturbation
as3935_perturb_total++;
SERIAL_PORT.printf("Interruption (AS3935) : perturbation...\n");
break;
case NOISE:
//Trop de bruit électromagnétique
as3935_noise_total++;
SERIAL_PORT.printf("Interruption (AS3935) : Too much electromagnetic noise!\n");
break;
}
writeLightningToSD(as3935_src, t, distance, energy);
}
if (since_batt_chk >= batt_chk_delay) {
since_batt_chk -= batt_chk_delay;
//Check battery status and voltage
low_battery_flag = !digitalRead(LOW_BATT_PIN);
batt_voltage = getBatteryVoltage();
}
// if (since_dht >= dht_delay) {
// since_dht = since_dht - dht_delay;
// // Read temperature or humidity
// dht.humidity().getEvent(&dht22_event_hum);
// dht.temperature().getEvent(&dht22_event_temp);
// }
if (since_bme280 >= bme280_delay) {
since_bme280 = since_bme280 - bme280_delay;
/* Get a new sensor event */
bmp.getEvent(&bme280_event);
/* Get the values (barometric pressure is measure in hPa) */
if (bme280_event.pressure)
{
bme280_press = bme280_event.pressure;
bmp.getTemperature(&bme280_temp);
bme280_alti = bmp.pressureToAltitude(seaLevelPressure, bme280_press);
}
}
if (since_gps >= gps_delay) {
since_gps = since_gps - gps_delay;
while (GPS_SERIAL_PORT.available()) {
char c = GPS_SERIAL_PORT.read();
if (GPS.encode(c)) {
GPS.get_datetime(&gps_date, &gps_time, &gps_fix_age);
GPS.crack_datetime(&gps_year, &gps_month, &gps_day,
&gps_hour, &gps_minutes, &gps_second,
&gps_hundreths, &gps_fix_age);
GPS.f_get_position(&gps_latitude, &gps_longitude, &gps_fix_age);
gps_altitude = GPS.f_altitude();
gps_speed = GPS.f_speed_kmph();
GPS.stats(&gps_chars, &gps_sentences, &gps_failed_checksum);
gps_satellites = GPS.satellites();
gps_hdop = GPS.hdop();
}
}
}
if (since_sd_log >= sd_log_delay) {
since_sd_log = since_sd_log - sd_log_delay;
writeDataToSD();
}
if (since_serial_send >= serial_send_delay) {
since_serial_send = since_serial_send - serial_send_delay;
sendDataOnSerial();
}
}
/*
* FUNCTIONS
*/
time_t getTeensy3Time() {
return Teensy3Clock.get();
}
void gps_power() {
if (digitalRead(GPS_BUT_PIN) == 0) { digitalWrite(GPS_EN_PIN, LOW); }
else { digitalWrite(GPS_EN_PIN, HIGH); }
}
int getBatteryVoltage() {
long mean = 0;
int U = 0;
int n = 10;
int res = 12;
analogReadResolution(res);
for (int i=0; i<n; i++) {
mean += analogRead(BATT_VOLT_PIN);
//delay(2);
}
mean /= n;
U = map(mean, 0, pow(2, res)-1, 0, 3300); // Convert data from ADC into input voltage in mV
U *= 2; //Multiple by 2 for the voltage divider
return U;
}
void writeDataToSD() {
char dir[20];
char path[60];
String directory = dayDirectory();
directory.toCharArray(dir, sizeof(directory));
sprintf(path, "%s/%s", dir, data_file);
// Test to know if the file exists before opening it, if it doesn't exist
// we will write first an header
bool no_header = SD.exists(path);
// 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(path, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
if (!no_header) {
SERIAL_PORT.printf("Creating file with CSV header : %s\n", path);
dataFile.printf("Date");
dataFile.printf(";bme280_Pressure(hPa);BMP180_Temperature(degC);BMP180_Altitude(m)");
// dataFile.printf(";DHT22_Humidity(%);DHT22_Temperature(degC)");
dataFile.printf(";TotalLightningCount;TotalPerturbationEvents;TotalNoiseDetection;TotalUnknownDetection");
dataFile.printf(";GPS_Latitude;GPS_Longitude;GPS_Altitude;GPS_Satellites;GPS_HDOP;GPS_Date");
dataFile.printf(";Battery_voltage(mV);Low_Battery_Status");
dataFile.printf("\n");
}
dataFile.printf("%04d/%02d/%02d_%02d:%02d:%02d", year(), month(), day(), hour(), minute(), second());
dataFile.printf(";%.2f;%.2f;%.1f", bme280_press, bme280_temp, bme280_alti);
// dataFile.printf(";%.0f;%.2f", dht22_event_hum.relative_humidity, dht22_event_temp.temperature);
dataFile.printf(";%d;%d;%d;%d", lightning_nb_total, as3935_perturb_total, as3935_noise_total, as3935_unknown_total);
dataFile.printf(";%.8f;%.8f;%.2f;%d;%d;%04d/%02d/%02d_%02d:%02d:%02d", gps_latitude, gps_longitude, gps_altitude,
gps_satellites, gps_hdop,
gps_year, gps_month, gps_day,
gps_hour, gps_minutes, gps_second);
dataFile.printf(";%d;%d", batt_voltage, low_battery_flag);
dataFile.printf("\n");
dataFile.close();
}
SERIAL_PORT.printf("Data writen : %s\n", path);
}
void writeLightningToSD(int type, time_t t, int dist, int energy) {
char dir[20];
char path[60];
String directory = dayDirectory();
directory.toCharArray(dir, sizeof(directory));
sprintf(path, "%s/%s", dir, lightning_log_file);
// Test to know if the file exists before opening it, if it doesn't exist
// we will write first an header
bool no_header = SD.exists(path);
// 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(path, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
if (!no_header) {
SERIAL_PORT.printf("Creating file with CSV header : %s\n", path);
dataFile.printf("Date");
dataFile.printf(";distance(km);energie_raw");
dataFile.printf(";TotalLightningCount;TotalPerturbationEvents;TotalNoiseDetection;TotalUnknownDetection");
dataFile.printf(";BME280_Pressure(hPa);BME280_Temperature(degC);BME280_Altitude(m)");
// dataFile.printf(";DHT22_Humidity(%);DHT22_Temperature(degC)");
dataFile.printf("\n");
}
dataFile.printf("%04d/%02d/%02d_%02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t));
dataFile.printf(";%d;%d;%d", type, dist, energy);
dataFile.printf(";%d;%d;%d;%d", lightning_nb_total, as3935_perturb_total, as3935_noise_total, as3935_unknown_total);
dataFile.printf(";%.2f;%.2f;%.1f", bme280_press, bme280_temp, bme280_alti);
// dataFile.printf(";%.0f;%.2f", dht22_event_hum.relative_humidity, dht22_event_temp.temperature);
dataFile.printf("\n");
dataFile.close();
}
SERIAL_PORT.printf("Data writen : %s\n", path);
}
String dayDirectory() {
char dir[20] ;
sprintf(dir, "data/%04d/%02d/%02d/", year(), month(), day());
if (!SD.exists(dir))
{
SERIAL_PORT.printf("Creating directory : %s ...", dir);
SD.mkdir(dir);
SERIAL_PORT.printf(" DONE!\n");
}
return String(dir);
}

View File

@ -1,97 +0,0 @@
/*
* CAMETEO project
*
* This is a personnal project of weather station with
* automatic photo taking.
* This code is the weather station part and is meant
* to be run on a PJRC Teensy 3.2 board.
*
* Author : Arofarn
*
* Licence : GPL v3
*
*/
/*
* LIBRARIES
*/
//Protocols
#include <Wire.h> // library used with I2C protocol
#include <SPI.h> // SPI protocol
//Teensy3.x Real Time Clock
#include <TimeLib.h>
//SD card
#include <SD.h>
// Sensors
#include <Adafruit_Sensor.h> // Generic
//#include <DHT.h> // DHT22
//#include <DHT_U.h> // DHT22 unified
//#include <Adafruit_BMP085_U.h> // BMP180
#include <Adafruit_BME280.h> // BME280
#include <PWFusion_AS3935.h>
//GPS
//#include <Adafruit_GPS.h> // Adafruit Ultimate GPS
#include <TinyGPS.h> //Builtin GPS lib
/*
* DEFINE
* &
* DECLARE
*/
//Special characteres
#define DEGREE (char)176 //degree symbol in ISO 8859-1
#define DEGREE_LCD (char)222 //degree symbol for lcd display
// Pins used
#define RX1_PIN 0 // Raspberry Pi2 serial comm. - Hard wired
#define TX1_PIN 1 // Raspberry Pi2 serial comm. - Hard wired
#define RPI_PWR_PIN 2 // Raspberry Pi power control - Hard wired
#define SD_CS_PIN 4 // SPI SD CS - Hard wired
#define GPS_EN_PIN 6 // GPS ENable
#define RX3_PIN 7 // GPS serial comm.
#define TX3_PIN 8 // GPS serial comm.
#define AS3935_IRQ_PIN 9 // Interrupts from AS3935 lightning sensor
#define AS3935_CS_PIN 10 // SPI CS AS3935 lightning sensor
#define SPI_DI_PIN 11 // SPI MOSI AS3935 lightning sensor
#define SPI_DO_PIN 12 // SPI MISO AS3935 lightning sensor
#define SPI_CK_PIN 13 // SPI clock SD + AS3935 lightning sensor + built-in LED
//#define DHT_PIN 14 // Humidity sensor data
#define GPS_BUT_PIN 16 // GPS switch power control
#define I2C_SDA_PIN 18 // I2C SDA
#define I2C_SCL_PIN 19 // I2C SCL
#define BATT_VOLT_PIN 22 // Battery voltage monitoring (Analog Input)
#define LOW_BATT_PIN 23 // Low Battery signal from charger (digital input)
//I2C addresses
//#define TCN75A_ADDR 0x00 //Sensor I2C bus address
#define BMP180_ADDR 0x77
//#define BME280_ADDR 0x00 // ???
//Serial over USB communication
#define SERIAL_PORT Serial
#define SERIAL_BAUD_RATE 9600
//Raspberry Pi Serial Comm.
#define RPI_SERIAL_PORT Serial1
#define RPI_SERIAL_BAUD_RATE 115200
//GPS
#define GPS_SERIAL_PORT Serial3
#define GPS_SERIAL_BAUD_RATE 9600
// AS3935 Lightning sensor
#define AS3935_INDOORS 0
#define AS3935_OUTDOORS 1
#define AS3935_DIST_DIS 0
#define AS3935_DIST_EN 1
#define AS3935_CAPACITANCE 72 // 72pF for THIS board (from seller)

View File

@ -1,553 +0,0 @@
/*
* CAMETEO project
*
* This is a personnal project of weather station with
* automatic photo taking.
* This code is the weather station part and is meant
* to be run on a PJRC Teensy 3.2 board.
*
* Author : Arofarn
*
* Licence : GPL v3
*
*/
/*
* LIBRARIES
*/
//Protocols
#include <Wire.h> // library used with I2C protocol
#include <SPI.h> // SPI protocol
//Teensy3.x Real Time Clock
#include <TimeLib.h>
//SD card
#include <SD.h>
// Sensors
#include <Adafruit_Sensor.h> // Generic
#include <Adafruit_BME280.h> // BME280
#include <PWFusion_AS3935.h>
//GPS
//#include <Adafruit_GPS.h> // Adafruit Ultimate GPS
#include <TinyGPS.h> //Builtin GPS lib
/*
* DEFINE
* &
* DECLARE
*/
//Special characteres
#define DEGREE (char)176 //degree symbol in ISO 8859-1
#define DEGREE_LCD (char)222 //degree symbol for lcd display
// Pins used
#define RX1_PIN 0 // Raspberry Pi2 serial comm. - Hard wired
#define TX1_PIN 1 // Raspberry Pi2 serial comm. - Hard wired
#define RPI_PWR_PIN 2 // Raspberry Pi power control - Hard wired
#define AS3935_CS_PIN 4 // SPI SD CS - Hard wired
#define GPS_EN_PIN 6 // GPS ENable
#define RX3_PIN 7 // GPS serial comm.
#define TX3_PIN 8 // GPS serial comm.
#define AS3935_IRQ_PIN 9 // Interrupts from AS3935 lightning sensor
#define SD_CS_PIN 10 // SPI CS AS3935 lightning sensor
#define SPI_DI_PIN 11 // SPI MOSI AS3935 lightning sensor
#define SPI_DO_PIN 12 // SPI MISO AS3935 lightning sensor
#define SPI_CK_PIN 13 // SPI clock SD + AS3935 lightning sensor + built-in LED
#define BATT_VOLT_PIN 16 // Battery voltage monitoring (Analog Input)
#define LOW_BATT_PIN 17 // Low Battery signal from charger (digital input)
#define I2C_SDA_PIN 18 // I2C SDA BME280 sensor
#define I2C_SCL_PIN 19 // I2C SCL BME280 sensor
#define GPS_BUT_PIN 20 // GPS switch power control (unused)
//I2C addresses
//#define TCN75A_ADDR 0x00 //Sensor I2C bus address
//#define BMP180_ADDR 0x77
//Serial over USB communication
#define SERIAL_PORT Serial
#define SERIAL_BAUD_RATE 9600
//Raspberry Pi Serial Comm.
#define RPI_SERIAL_PORT Serial1
#define RPI_SERIAL_BAUD_RATE 9600
//GPS
#define GPS_SERIAL_PORT Serial3
#define GPS_SERIAL_BAUD_RATE 9600
//Sensors
Adafruit_BME280 bme;
float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
float bme280_press;
float bme280_temp;
float bme280_alti;
float bme280_humi;
// AS3935 Lightning sensor
#define AS3935_INDOORS 0
#define AS3935_OUTDOORS 1
#define AS3935_DIST_DIS 0
#define AS3935_DIST_EN 1
#define AS3935_CAPACITANCE 72 // 72pF for THIS board (from seller)
enum strike_sources { UNKNOWN_SRC, LIGHTNING, PERTURBATION, NOISE };
volatile int8_t AS3935_ISR_Trig = 0; // Trigger for AS3935 lightning sensor
void AS3935_ISR() {
AS3935_ISR_Trig = 1;
}
PWF_AS3935 lightning(AS3935_CS_PIN, AS3935_IRQ_PIN, 33);
int as3935_src;
int as3935_distance;
long lightning_nb_total = 0;
int lightning_nb_hour = 0;
int lightning_nb_day = 0;
int as3935_perturb_total = 0;
int as3935_noise_total = 0;
int as3935_unknown_total = 0;
char lightning_log_file[12] = "lghtnng.log";
//GPS
//Adafruit_GPS GPS(&GPS_SERIAL_PORT);
TinyGPS GPS;
float gps_latitude, gps_longitude, gps_altitude; // returns +- latitude/longitude in degrees
float gps_speed, gps_course;
unsigned long gps_time, gps_date, gps_fix_age;
int gps_year;
byte gps_month, gps_day, gps_hour, gps_minutes, gps_second, gps_hundreths;
unsigned long gps_chars, gps_hdop;
unsigned short gps_sentences, gps_failed_checksum, gps_satellites;
//Miscellaneous
bool rpi_status = true;
int batt_voltage;
bool low_battery_flag = false;
// Tasks timers
elapsedMillis since_bme280;
elapsedMillis since_gps;
elapsedMillis since_display;
elapsedMillis since_serial_send;
elapsedMillis since_sd_log;
elapsedMillis since_batt_chk;
void BootMessage(String s) {
char c[13]; // char buffer for conversion String->char
s.toCharArray(c, sizeof(s));
SERIAL_PORT.printf("%-12s", c);
}
void BootOK() {
SERIAL_PORT.println("OK");
}
void BootError() {
SERIAL_PORT.println("EE");
}
void sendDataOnSerial() {
//Print date & hour on serial port
SERIAL_PORT.printf("%04d/%02d/%02d_%02d:%02d:%02d\n", year(), month(), day(), hour(), minute(), second());
//if (bme280_event.pressure) {
SERIAL_PORT.printf("Temperature:%8.2f %cC | Pressure: %8.2f hPa | Altitude:%8.2f m\n",
bme280_temp, DEGREE, bme280_press, bme280_alti);
// }
// else {
// SERIAL_PORT.printf("BMP180 Sensor error\n");
// }
SERIAL_PORT.printf("Lightning strikes (from start) : %d | Perturb.: %d | Noise : %d | Unknown detect.: %d\n", lightning_nb_total,
as3935_perturb_total,
as3935_noise_total,
as3935_unknown_total);
//SERIAL_PORT.printf("Temperature:%8.2f %cC\n", tcn75a_temp, DEGREE);
SERIAL_PORT.printf("GPS data: %04d/%02d/%02d_%02d:%02d:%02d (%d)\n", gps_year, gps_month, gps_day,
gps_hour, gps_minutes, gps_second,
gps_fix_age);
SERIAL_PORT.printf(" Latitude: %11.8f | Longitude: %11.8f | Altitude: %5.2f m\n", gps_latitude, gps_longitude, gps_altitude);
SERIAL_PORT.printf(" Speed: %4.1f km/h | Course : %4.1f %c\n", gps_speed, gps_course, DEGREE);
SERIAL_PORT.printf(" Chars: %11d | Sentences: %11d | Failed cheksum: %4d\n", gps_chars, gps_sentences, gps_failed_checksum);
SERIAL_PORT.printf("Battery : %10d mV | Low Battery : %d\n", batt_voltage, low_battery_flag);
}
bool isStartedPI() {
//Return true if the R-Pi respond to an simple request
return rpi_status;
}
void startRPI() {
//Start the Raspberry Pi via the MOSFET (grid connected to the RPI_PWR_PIN)
if (!isStartedPI()) {
pinMode(RPI_PWR_PIN, OUTPUT);
digitalWrite(RPI_PWR_PIN, LOW);
rpi_status = true;
}
}
void stopRPI() {
//Stop the Raspberry Pi via the MOSFET (grid connected to the RPI_PWR_PIN)
pinMode(RPI_PWR_PIN, INPUT); //Eteint via le MOSFET
rpi_status = false;
}
void sendDataToRPI() {
//if (isStartedPI()) {
//Print date & hour on serial port
RPI_SERIAL_PORT.printf("%04d/%02d/%02d_%02d:%02d:%02d\n", year(), month(), day(), hour(), minute(), second());
//if (bme280_event.pressure) {
RPI_SERIAL_PORT.printf("Temperature:%8.2f %cC | Pressure: %8.2f hPa | Altitude:%8.2f m\n",
bme280_temp, DEGREE, bme280_press, bme280_alti);
//}
//else {
// RPI_SERIAL_PORT.printf("BMP180 Sensor error\n");
//}
RPI_SERIAL_PORT.printf("Lightning strikes (from start) : %d | Perturb.: %d | Noise : %d | Unknown detect.: %d\n", lightning_nb_total,
as3935_perturb_total,
as3935_noise_total,
as3935_unknown_total);
RPI_SERIAL_PORT.printf("GPS data: %04d/%02d/%02d_%02d:%02d:%02d (%d)\n", gps_year, gps_month, gps_day,
gps_hour, gps_minutes, gps_second,
gps_fix_age);
RPI_SERIAL_PORT.printf(" Latitude: %11.8f | Longitude: %11.8f | Altitude: %5.2f m\n", gps_latitude, gps_longitude, gps_altitude);
RPI_SERIAL_PORT.printf(" Speed: %4.1f km/h | Course : %4.1f %c\n", gps_speed, gps_course, DEGREE);
RPI_SERIAL_PORT.printf(" Chars: %11d | Sentences: %11d | Failed cheksum: %4d\n", gps_chars, gps_sentences, gps_failed_checksum);
RPI_SERIAL_PORT.printf("Battery : %10d mV | Low Battery : %d\n", batt_voltage, low_battery_flag);
// }
// else {
// //error RPI is not started
// }
}
//SD Card
#define CONFIGFILE "config.txt"
//Configuration
char data_file[13] = "datalog.csv";
//Delays
unsigned int bme280_delay = 500;
//unsigned int dht_delay = 3000;
unsigned int gps_delay = 100;
unsigned int serial_send_delay = 5000;
unsigned int sd_log_delay = 5000;
unsigned int batt_chk_delay = 5000;
//Date and time
int TZ = 1; //Time zone
/*
* SETUP
*/
void setup() {
//To be sure Raspberry-Pi won't be turned on unexpectedly
stopRPI();
pinMode(LOW_BATT_PIN, INPUT_PULLUP);
low_battery_flag = !digitalRead(LOW_BATT_PIN);
pinMode(GPS_BUT_PIN, INPUT_PULLUP);
pinMode(GPS_EN_PIN, OUTPUT);
gps_power();
SERIAL_PORT.begin(SERIAL_BAUD_RATE);
delay(1000);
//while (!SERIAL_PORT) { }; //Wait for serial port to start
SERIAL_PORT.printf("Serial Comm. OK\nNow booting...\n");
RPI_SERIAL_PORT.begin(RPI_SERIAL_BAUD_RATE);
delay(1000);
//while (!RPI_SERIAL_PORT) { }; //Wait for serial port to start
RPI_SERIAL_PORT.printf("Raspberry's Serial Comm. OK\nNow booting...\n");
BootMessage("SDcard");
// see if the card is present and can be initialized:
if (!SD.begin(SD_CS_PIN)) {
BootError();
while(1);
}
BootOK();
BootMessage("RT Clock");
// set the Time library to use Teensy 3.0's RTC to keep time
setSyncProvider(getTeensy3Time);
if (timeStatus()!= timeSet) {
BootError();
while(1);
}
BootOK();
BootMessage("BME280 (Pressure, Humidity and Temperature)");
/* Initialise the sensor */
// if(!bme.begin())
// {
// /* There was a problem detecting the BMP085 ... check your connections */
// BootError();
// while(1);
// }
BootOK();
BootMessage("AS3935 (Lightning)");
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV16); // SPI speed to SPI_CLOCK_DIV16/1MHz (max 2MHz, NEVER 500kHz!)
SPI.setDataMode(SPI_MODE1); // MAX31855 is a Mode 1 device --> clock starts low, read on rising edge
SPI.setBitOrder(MSBFIRST); // data sent to chip MSb first
lightning.AS3935_DefInit(); // set registers to default
// now update sensor cal for your application and power up chip
lightning.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_OUTDOORS, AS3935_DIST_EN);
// enable interrupt (hook IRQ pin to Arduino Uno/Mega interrupt input: 0 -> pin 2, 1 -> pin 3 )
attachInterrupt(AS3935_IRQ_PIN, AS3935_ISR, RISING);
BootOK();
BootMessage("GPS");
GPS_SERIAL_PORT.begin(GPS_SERIAL_BAUD_RATE);
while (!GPS_SERIAL_PORT) {} ;
BootOK();
}
/*
* LOOP
*/
void loop() {
//Power ON/OFF GPS
gps_power();
//Lightning detection
if (AS3935_ISR_Trig != 0) {
AS3935_ISR_Trig = 0;
time_t t = now();
int distance = -9999;
int energy = -9999;
switch (as3935_src) {
case UNKNOWN_SRC:
//source inconnue
as3935_unknown_total++;
SERIAL_PORT.printf("Interruption (AS3935) : unkown source (not lightning)\n");
break;
case LIGHTNING:
//Foudre !!!
lightning_nb_total++;
distance = lightning.AS3935_GetLightningDistKm();
energy = lightning.AS3935_GetStrikeEnergyRaw();
SERIAL_PORT.printf("Interruption (AS3935) : Lightningbolt !!!\n");
SERIAL_PORT.printf("Distance : %4d km | Energy : %d \n", distance, energy);
break;
case PERTURBATION:
//Perturbation
as3935_perturb_total++;
SERIAL_PORT.printf("Interruption (AS3935) : perturbation...\n");
break;
case NOISE:
//Trop de bruit électromagnétique
as3935_noise_total++;
SERIAL_PORT.printf("Interruption (AS3935) : Too much electromagnetic noise!\n");
break;
}
writeLightningToSD(as3935_src, t, distance, energy);
}
if (since_batt_chk >= batt_chk_delay) {
since_batt_chk -= batt_chk_delay;
//Check battery status and voltage
low_battery_flag = !digitalRead(LOW_BATT_PIN);
batt_voltage = getBatteryVoltage();
}
// if (since_bme280 >= bme280_delay) {
// since_bme280 = since_bme280 - bme280_delay;
//
// bme280_press = bme.readPressure() / 100.0F;
// bme280_alti = bme.readAltitude(seaLevelPressure);
// bme280_temp = bme.readTemperature();
// bme280_humi = bme.readHumidity();
// }
if (since_gps >= gps_delay) {
since_gps = since_gps - gps_delay;
while (GPS_SERIAL_PORT.available()) {
char c = GPS_SERIAL_PORT.read();
if (GPS.encode(c)) {
GPS.get_datetime(&gps_date, &gps_time, &gps_fix_age);
GPS.crack_datetime(&gps_year, &gps_month, &gps_day,
&gps_hour, &gps_minutes, &gps_second,
&gps_hundreths, &gps_fix_age);
GPS.f_get_position(&gps_latitude, &gps_longitude, &gps_fix_age);
gps_altitude = GPS.f_altitude();
gps_speed = GPS.f_speed_kmph();
GPS.stats(&gps_chars, &gps_sentences, &gps_failed_checksum);
gps_satellites = GPS.satellites();
gps_hdop = GPS.hdop();
}
}
}
if (since_sd_log >= sd_log_delay) {
since_sd_log = since_sd_log - sd_log_delay;
writeDataToSD();
}
if (since_serial_send >= serial_send_delay) {
since_serial_send = since_serial_send - serial_send_delay;
sendDataOnSerial();
sendDataToRPI();
}
}
/*
* FUNCTIONS
*/
time_t getTeensy3Time() {
return Teensy3Clock.get();
}
void gps_power() {
if (digitalRead(GPS_BUT_PIN) == 0) { digitalWrite(GPS_EN_PIN, LOW); }
else { digitalWrite(GPS_EN_PIN, HIGH); }
}
int getBatteryVoltage() {
long mean = 0;
int U = 0;
int n = 10;
int res = 12;
analogReadResolution(res);
for (int i=0; i<n; i++) {
mean += analogRead(BATT_VOLT_PIN);
//delay(2);
}
mean /= n;
U = map(mean, 0, pow(2, res)-1, 0, 3300); // Convert data from ADC into input voltage in mV
U *= 2; //Multiple by 2 for the voltage divider
return U;
}
void writeDataToSD() {
char dir[20];
char path[60];
String directory = dayDirectory();
directory.toCharArray(dir, sizeof(directory));
sprintf(path, "%s/%s", dir, data_file);
// Test to know if the file exists before opening it, if it doesn't exist
// we will write first an header
bool no_header = SD.exists(path);
// 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(path, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
if (!no_header) {
SERIAL_PORT.printf("Creating file with CSV header : %s\n", path);
dataFile.printf("Date");
dataFile.printf(";BME280_Pressure(hPa);BME280_Temperature(degC);BME280_Humidity(%);BME280_Altitude(m)");
dataFile.printf(";TotalLightningCount;TotalPerturbationEvents;TotalNoiseDetection;TotalUnknownDetection");
dataFile.printf(";GPS_Latitude;GPS_Longitude;GPS_Altitude;GPS_Satellites;GPS_HDOP;GPS_Date");
dataFile.printf(";Battery_voltage(mV);Low_Battery_Status");
dataFile.printf("\n");
}
dataFile.printf("%04d/%02d/%02d_%02d:%02d:%02d", year(), month(), day(), hour(), minute(), second());
dataFile.printf(";%.2f;%.2f;%.0f;%.1f", bme280_press, bme280_temp, bme280_humi, bme280_alti);
dataFile.printf(";%d;%d;%d;%d", lightning_nb_total, as3935_perturb_total, as3935_noise_total, as3935_unknown_total);
dataFile.printf(";%.8f;%.8f;%.2f;%d;%d;%04d/%02d/%02d_%02d:%02d:%02d", gps_latitude, gps_longitude, gps_altitude,
gps_satellites, gps_hdop,
gps_year, gps_month, gps_day,
gps_hour, gps_minutes, gps_second);
dataFile.printf(";%d;%d", batt_voltage, low_battery_flag);
dataFile.printf("\n");
dataFile.close();
}
SERIAL_PORT.printf("Data writen : %s\n", path);
}
void writeLightningToSD(int type, time_t t, int dist, int energy) {
char dir[20];
char path[60];
String directory = dayDirectory();
directory.toCharArray(dir, sizeof(directory));
sprintf(path, "%s/%s", dir, lightning_log_file);
// Test to know if the file exists before opening it, if it doesn't exist
// we will write first an header
bool no_header = SD.exists(path);
// 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(path, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
if (!no_header) {
SERIAL_PORT.printf("Creating file with CSV header : %s\n", path);
dataFile.printf("Date");
dataFile.printf(";distance(km);energie_raw");
dataFile.printf(";TotalLightningCount;TotalPerturbationEvents;TotalNoiseDetection;TotalUnknownDetection");
dataFile.printf(";BME280_Pressure(hPa);BME280_Temperature(degC);BME280_Humidty(%);BME280_Altitude(m)");
dataFile.printf("\n");
}
dataFile.printf("%04d/%02d/%02d_%02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t));
dataFile.printf(";%d;%d;%d", type, dist, energy);
dataFile.printf(";%d;%d;%d;%d", lightning_nb_total, as3935_perturb_total, as3935_noise_total, as3935_unknown_total);
dataFile.printf(";%.2f;%.2f;%.0f;%.1f", bme280_press, bme280_temp, bme280_humi, bme280_alti);
dataFile.printf("\n");
dataFile.close();
}
SERIAL_PORT.printf("Data writen : %s\n", path);
}
String dayDirectory() {
char dir[20] ;
sprintf(dir, "data/%04d/%02d/%02d/", year(), month(), day());
if (!SD.exists(dir))
{
SERIAL_PORT.printf("Creating directory : %s ...", dir);
SD.mkdir(dir);
SERIAL_PORT.printf(" DONE!\n");
}
return String(dir);
}

View File

@ -1,306 +0,0 @@
#include <ESP8266WiFi.h>
#include <MQTTClient.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <RTClib.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "arowifi2"
#define WLAN_PASS "nAGjywhCQ4iUBcbmf0PBn_srrghpOur4-aIzcJ_8Uxm1b58dH1c4Vy-LEMLd"
/************************* MQTT broker Setup *********************************/
#define MQTT_SERVER "192.168.0.18"
#define MQTT_SERVERPORT 1883 // use 8883 for SSL
#define MQTT_USERNAME "arofarn"
#define MQTT_KEY "WaKaW9XMGUZ3rRJD"
#define MQTT_ID "huzzah0"
#define MQTT_PUB_INTERVAL 15000 // milliseconds
/************************* Time and NTP Setup *********************************/
#define NTP_SERVER "fr.pool.ntp.org"
#define TIMEZONE 0 // UTC+0
#define DAYLIGHT false
#define NTP_REFRESH_INTERVAL 3600
/************************* Atmospheric Pressure Mode *********************************/
#define MODE_STATION true
#define DEFAULT_ALTITUDE 140.0
#define DEFAULT_MSLP 1013.25 //Mean Atmospheric Pressure at Mean Sea-Level
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient wifi_client;
MQTTClient mqtt_client;
//Create a class for the sensor
Adafruit_BME280 bme; // I2C
bool mode_station = MODE_STATION;
float altitude = DEFAULT_ALTITUDE;
float atm_press_sea_level = DEFAULT_MSLP;
bool led0_status = true ;
unsigned long previousPub = 0;
/*************************** Sketch Code ************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
void setup() {
bool wstatus=false;
Serial.begin(115200);
delay(10);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(2, wstatus);
digitalWrite(LED_BUILTIN, led0_status);
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
wstatus = !wstatus;
delay(500);
Serial.print(".");
digitalWrite(2, wstatus);
}
Serial.println();
digitalWrite(2, LOW);
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
delay(500);
NTP.begin(NTP_SERVER, TIMEZONE, DAYLIGHT); //Local french NTP server, timezone, summertime= false
NTP.setInterval(NTP_REFRESH_INTERVAL);
NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
if (error) {
Serial.print("Time Sync error: ");
if (error == noResponse)
Serial.println("NTP server not reachable");
else if (error == invalidAddress)
Serial.println("Invalid NTP server address");
}
else {
Serial.print("Got NTP time: ");
Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
}
});
time_t nowNTP;
nowNTP = NTP.getTime();
Serial.println(nowNTP);
// default settings
bool status;
status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
mqtt_client.begin(MQTT_SERVER, MQTT_SERVERPORT, wifi_client); //MQTT_SERVERPORT, MQTT_USERNAME, MQTT_KEY);
//client.onMessage(messageReceived);
MQTT_connect();
mqtt_client.onMessage(messageReceived);
previousPub = millis();
}
uint32_t x=0;
void loop() {
mqtt_client.loop();
if (!mqtt_client.connected()) {
MQTT_connect();
}
unsigned long currentMillis = millis();
if(currentMillis - previousPub > MQTT_PUB_INTERVAL) {
// save the last time you blinked the LED
previousPub = currentMillis;
float pressure_val = -99.9;
float temperature_val = -99.9;
float humidity_val = -99.9;
String date_str = "";
char date_val[20] = "00:00:00 00-00-0000";
char tosend[120];
pressure_val = bme.readPressure() / 100.0F;
temperature_val = bme.readTemperature();
humidity_val = bme.readHumidity();
date_str = NTP.getTimeDateString();
date_str.toCharArray(date_val,20);
// Now we can publish stuff!
Serial.print(F("Sending pressure value "));
Serial.print(pressure_val);
Serial.print("...");
val2json(pressure_val, date_str, "hPa", "AP").toCharArray(tosend, 120);
if (! mqtt_client.publish("huzzah0/AdaBME280_1/pressure", tosend, true, 2) ) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
if(mode_station) {
//Sea-level atmospheric pressure calculus
atm_press_sea_level = bme.seaLevelForAltitude(altitude, pressure_val);
} else {
altitude = bme.readAltitude(atm_press_sea_level);
}
Serial.print(F("Sending sea-level pressure value "));
Serial.print(atm_press_sea_level);
Serial.print("...");
val2json(atm_press_sea_level, date_str, "hPa", "MSLP").toCharArray(tosend, 120);
if (! mqtt_client.publish("huzzah0/AdaBME280_1/sea_level_pressure", tosend, true, 2) ) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
Serial.print(F("Sending altitude value "));
Serial.print(altitude);
Serial.print("...");
val2json(altitude, date_str, "m", "ALTI").toCharArray(tosend, 120);
if (! mqtt_client.publish("huzzah0/AdaBME280_1/altitude", tosend, true, 2) ) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
// Now we can publish stuff!
Serial.print(F("Sending humity value "));
Serial.print(humidity_val);
Serial.print("...");
val2json(humidity_val, date_str, "%", "RH").toCharArray(tosend, 120);
if (! mqtt_client.publish("huzzah0/AdaBME280_1/humidity", tosend, true, 2)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
// Now we can publish stuff!
Serial.print(F("Sending temperature value "));
Serial.print(temperature_val);
Serial.print("...");
val2json(temperature_val, date_str, "degC", "AT").toCharArray(tosend, 120);
if (! mqtt_client.publish("huzzah0/AdaBME280_1/temperature", tosend, true, 2)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
// __LAST__ thing to send : Date and Time
Serial.print(F("Sending date and time "));
Serial.print(date_val);
Serial.print("...");
if (! mqtt_client.publish("huzzah0/NTP/date", date_val, true, 2)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
}
delay(10);
}
void MQTT_connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while (!mqtt_client.connect(MQTT_ID, MQTT_USERNAME, MQTT_KEY)) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
mqtt_client.subscribe("huzzah0/led0", 1);
mqtt_client.subscribe("huzzah0/mode_station", 1);
if(mode_station) {
mqtt_client.unsubscribe("huzzah0/sea_level_pressure");
mqtt_client.subscribe("huzzah0/altitude", 2);
}
else {
mqtt_client.unsubscribe("huzzah0/altitude");
mqtt_client.subscribe("huzzah0/sea_level_pressure", 1);
}
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
if(topic == "huzzah0/led0") {
if(payload == "0") led0_status = HIGH;
if(payload == "1") led0_status = LOW;
digitalWrite(LED_BUILTIN, led0_status);
}
if(topic == "huzzah0/mode_station") {
if(payload == "0") {
mode_station = false;
mqtt_client.unsubscribe("huzzah0/altitude");
mqtt_client.subscribe("huzzah0/sea_level_pressure", 1);
}
if(payload == "1") {
mode_station = true;
mqtt_client.unsubscribe("huzzah0/sea_level_pressure");
mqtt_client.subscribe("huzzah0/altitude", 1);
}
}
if(topic == "huzzah0/altitude") {
String pl = payload;
altitude = pl.toFloat();
Serial.print("New altitude :");
Serial.println(altitude);
}
}
String int2str(int a) {
String ret = "";
if (a < 10) {
ret +="0";
}
if (a > 99)
a = 99;
ret += a;
return ret;
}
String val2json (float val, String date, String unit, String type) {
String json = "{\"date\": \"";
json += date;
json += "\", \"value\": \"";
json += val;
json += "\", \"unit\": \"";
json += unit;
json += "\", \"type\": \"";
json += type;
json += "\" }";
//Serial.println(json);
return json;
}

17
circuitpython/boot.py Normal file
View File

@ -0,0 +1,17 @@
# Selectively setting readonly to False on boot
import board
import digitalio
import storage
switch = digitalio.DigitalInOut(board.D5)
switch.direction = digitalio.Direction.INPUT
switch.pull = digitalio.Pull.UP
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
# If the D0 is connected to ground with a wire
# CircuitPython can write to the drive
storage.remount("/", switch.value)
print("Readonly : {}".format(switch.value))
led.value = switch.value

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

126
circuitpython/main.py Normal file
View File

@ -0,0 +1,126 @@
# Simple weather and GPS logger
import board
from busio import I2C, UART
from time import sleep
from analogio import AnalogIn
import microcontroller
import gc
import micropython
import os
from adafruit_bme280 import Adafruit_BME280_I2C
from adafruit_gps import GPS
import neopixel
#########
# Setup #
#########
# BME280 sensors (I2C)
i2c = I2C(board.SCL, board.SDA)
bme280 = Adafruit_BME280_I2C(i2c)
# Integrated Neopixel
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
# Battery voltage
vbat = AnalogIn(board.D9, )
# GPS on Feather board
gps_uart = UART(board.TX, board.RX, baudrate=9600, timeout=3000)
gps = GPS(gps_uart)
# Turn on the basic GGA and RMC info
gps.send_command('PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
gps.send_command('PMTK220,2000') # 1000 ms refresh
gc.collect()
micropython.mem_info()
temp, hum, press = 0.0, 0.0, 0.0
rouge, vert, bleu = 0, 0, 0
# Check if data directory exists
if 'data' not in os.listdir():
os.mkdir('data')
os.mkdir('data/hourly')
os.mkdir('data/daily')
#############
# Main loop #
#############
while True:
sleep(5)
gc.collect()
# micropython.mem_info(1)
# print('Memory free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
temp = bme280.temperature
hum = bme280.humidity
press = bme280.pressure
print("Temperature: {:>+.1f} degC | Humidite: {:>.1f} % | Pression: {:>.1f} hPa".format(temp, hum, press))
print("Tension batterie : {:>.2f} V | CPU Temp: {:>+.1f} degC".format((vbat.value*2*3.3/65536), microcontroller.cpu.temperature))
# 0.00644531 = 2*3.3/1024 :
# 2 : voltage is divided by 2
# 3.3 : Vref = 3.3V
# 1024 : 10bit ADC
# Conversion des donn?es en couleur
# ROUGE => temp?rature : max = 35?C, min =10?C soit une amplitude de 25?C
rouge = int((temp-10)*255/25)
if rouge > 255:
rouge = 255
if rouge < 0:
rouge = 0
# BLEU => humidit? : max= 100%, mini=0%
bleu = int(hum*255/100)
# VERT => Pression : mini=960hPa, maxi = 1030hPa soit une amplitude 70hPa
vert = int((press-960)*255/70)
if vert > 255:
vert = 255
if vert < 0:
vert = 0
rvb = (rouge, vert, bleu)
print("Couleur : {}".format(rvb))
pixel[0] = rvb
gps.update()
if not gps.has_fix:
# Try again if we don't have a fix yet.
print('Waiting for fix... {} - {}'.format(gps.has_fix, gps.satellites))
continue
print('Fix timestamp: {}/{}/{} {:02}:{:02}:{:02}'.format(
gps.timestamp_utc.tm_mon, # Grab parts of the time from the
gps.timestamp_utc.tm_mday, # struct_time object that holds
gps.timestamp_utc.tm_year, # the fix time. Note you might
gps.timestamp_utc.tm_hour, # not get all data like year, day,
gps.timestamp_utc.tm_min, # month!
gps.timestamp_utc.tm_sec))
if gps.altitude_m is not None:
print('Latitude: {} deg | Longitude: {} deg | Altitude: {} m'.format(gps.latitude,
gps.longitude,
gps.altitude_m))
else:
print('Latitude: {} deg | Longitude: {} deg'.format(gps.latitude,
gps.longitude))
print('Fix quality: {}'.format(gps.fix_quality))
# Some attributes beyond latitude, longitude and timestamp are optional
# and might not be present. Check if they're None before trying to use!
if gps.satellites is not None:
print('# satellites: {}'.format(gps.satellites))
if gps.track_angle_deg is not None:
print('Speed: {} knots'.format(gps.speed_knots))
if gps.track_angle_deg is not None:
print('Track angle: {} degrees'.format(gps.track_angle_deg))
if gps.horizontal_dilution is not None:
print('Horizontal dilution: {}'.format(gps.horizontal_dilution))
if gps.height_geoid is not None:
print('Height geo ID: {} meters'.format(gps.height_geoid))

Binary file not shown.

Binary file not shown.

Binary file not shown.