Example code of sakura.io Evaluation board.
Dependencies: AQM0802A BME280 MPU9250_SPI SakuraIO gps mbed
SakuraIo Evaluation Board Standard
Overview
This program is example code of sakura.io Evaluation board.
Functions
- Periodic measure from onboard sensors(period is 200ms)
- Motion sensor(gyro, accelometer, magnetometer)
- Environment sensor(temperatur, humidity, airpressur)
- GPS(longitude, latitude, timestamp)
- Periodic send the measuring datas to sakura.io platform(period is 300sec)
- Output the measured datas output to USB-Serial port
- baudrate is 9600bps
- Can select on / off of periodic running with switch `SW5`
Description
See the Getting Started page.
Diff: main.cpp
- Revision:
- 1:faf04d99f302
- Parent:
- 0:db3ec2781484
- Child:
- 3:e5e53c1f6fda
--- a/main.cpp Thu Sep 07 08:32:10 2017 +0000
+++ b/main.cpp Wed Nov 29 06:30:47 2017 +0000
@@ -5,6 +5,7 @@
#include <SakuraIO.h>
#include "SakuraPinNames.h"
#include "sensors.h"
+#include "gps.h"
// Serial over CMSIS_DAP
Serial pc(DAP_UART_TX, DAP_UART_RX, 9600);
@@ -44,6 +45,14 @@
SensorData sensor_data;
+// GPS UART buffer
+char uart_buffer[128] = {0};
+int uart_buffer_index = 0;
+
+// NMEA Decoder
+GPSDecoder gps_decoder;
+
+void gps_uart_buffering_handler();
void setup()
{
@@ -66,7 +75,9 @@
mpu9250.calib_acc();
mpu9250.AK8963_calib_Magnetometer();
+ // active high
gps_en = 1;
+ gps.attach(&gps_uart_buffering_handler, Serial::RxIrq);
}
void read_sensor_data()
@@ -90,44 +101,71 @@
sakuraio.enqueueTx(1, sensor_data.bme280.temperature);
sakuraio.enqueueTx(2, sensor_data.bme280.pressure);
sakuraio.enqueueTx(3, sensor_data.bme280.humidity);
-
- sakuraio.enqueueTx(4, sensor_data.mpu9250.temperature); // これいる?
- sakuraio.enqueueTx(5, sensor_data.mpu9250.accelerometer[0]);
- sakuraio.enqueueTx(6, sensor_data.mpu9250.accelerometer[1]);
- sakuraio.enqueueTx(7, sensor_data.mpu9250.accelerometer[2]);
- sakuraio.enqueueTx(8, sensor_data.mpu9250.gyroscope[0]);
- sakuraio.enqueueTx(9, sensor_data.mpu9250.gyroscope[1]);
- sakuraio.enqueueTx(10, sensor_data.mpu9250.gyroscope[2]);
- sakuraio.enqueueTx(11, sensor_data.mpu9250.magnetometer[0]);
- sakuraio.enqueueTx(12, sensor_data.mpu9250.magnetometer[1]);
- sakuraio.enqueueTx(13, sensor_data.mpu9250.magnetometer[2]);
- //sakuraio.enqueueTx(14, lon);
- //sakuraio.enqueueTx(15, lat);
+ sakuraio.enqueueTx(4, sensor_data.mpu9250.accelerometer[0]);
+ sakuraio.enqueueTx(5, sensor_data.mpu9250.accelerometer[1]);
+ sakuraio.enqueueTx(6, sensor_data.mpu9250.accelerometer[2]);
+ sakuraio.enqueueTx(7, sensor_data.mpu9250.gyroscope[0]);
+ sakuraio.enqueueTx(8, sensor_data.mpu9250.gyroscope[1]);
+ sakuraio.enqueueTx(9, sensor_data.mpu9250.gyroscope[2]);
+ sakuraio.enqueueTx(10, sensor_data.mpu9250.magnetometer[0]);
+ sakuraio.enqueueTx(11, sensor_data.mpu9250.magnetometer[1]);
+ sakuraio.enqueueTx(12, sensor_data.mpu9250.magnetometer[2]);
+ sakuraio.enqueueTx(13, gps_decoder.get_longitude());
+ sakuraio.enqueueTx(14, gps_decoder.get_latitude());
+ sakuraio.enqueueTx(15, gps_decoder.get_unixtime());
+}
+
+void gps_uart_buffering_handler()
+{
+ while (gps.readable() == 1) {
+ char c = gps.getc();
+ if (c == '\r') {
+ continue;
+ }
+ uart_buffer[uart_buffer_index] = c;
+ uart_buffer_index++;
+ if (c == '\n') {
+ uart_buffer[uart_buffer_index - 1] = '\0';
+ gps_decoder.set_nmea_message(uart_buffer);
+ gps_decoder.decode();
+ uart_buffer_index = 0;
+ }
+ }
}
void loop()
{
- pc.printf("\r\n\r\nGPS\r\n");
- while(gps.readable()) {
- pc.putc(gps.getc());
- }
- pc.printf("\r\n");
+ pc.printf("\r\n\r\n--------------------\r\n");
+
static int i = 1;
read_sensor_data();
wait(1);
pc.printf("This program runs since %d seconds.\r\n", i);
pc.printf("BME280\r\n");
- pc.printf("\tTemp: %fC\r\n", sensor_data.bme280.temperature);
- pc.printf("\tPres: %fhPa\r\n", sensor_data.bme280.pressure);
- pc.printf("\tHum: %f%%\r\n", sensor_data.bme280.humidity);
+ pc.printf("\tTemp: %.2fC\r\n", sensor_data.bme280.temperature);
+ pc.printf("\tPres: %.2fhPa\r\n", sensor_data.bme280.pressure);
+ pc.printf("\tHum: %.2f%%\r\n", sensor_data.bme280.humidity);
pc.printf("MPU9250\r\n");
- pc.printf("\tTemp: %fC\r\n", sensor_data.mpu9250.temperature);
+ pc.printf("\tTemp: %.2fC\r\n", sensor_data.mpu9250.temperature);
for (int j = 0; j < 3; j++) {
- pc.printf("\tacc[%d]: %f\r\n", j, sensor_data.mpu9250.accelerometer[j]);
- pc.printf("\tgyro[%d]: %f\r\n", j, sensor_data.mpu9250.gyroscope[j]);
- pc.printf("\tmag[%d]: %f\r\n", j, sensor_data.mpu9250.magnetometer[j]);
+ pc.printf("\tacc[%d]: %.2f\r\n", j, sensor_data.mpu9250.accelerometer[j]);
+ pc.printf("\tgyro[%d]: %.2f\r\n", j, sensor_data.mpu9250.gyroscope[j]);
+ pc.printf("\tmag[%d]: %.2f\r\n", j, sensor_data.mpu9250.magnetometer[j]);
}
+ pc.printf("GPS\r\n");
+ pc.printf("\tlat: %f%c\r\n",
+ gps_decoder.get_latitude(),
+ gps_decoder.get_latitude() >= 0 ? 'N' : 'S');
+ pc.printf("\tlon: %f%c\r\n",
+ gps_decoder.get_longitude(),
+ gps_decoder.get_longitude() >= 0 ? 'E' : 'W');
+ pc.printf("\tspeed: %fkm/h\r\n", gps_decoder.get_speed());
+ pc.printf("\tmove_direction: %f\r\n", gps_decoder.get_move_direction());
+ pc.printf("\tdate: %d/%02d/%02d %02d:%02d:%02d (UTC)\r\n",
+ gps_decoder.get_year(), gps_decoder.get_month(), gps_decoder.get_day(),
+ gps_decoder.get_hour(), gps_decoder.get_min(), gps_decoder.get_sec());
+ pc.printf("\tUNIX time: %d\r\n", gps_decoder.get_unixtime());
lcd.cls();
if( (sakuraio.getConnectionStatus() & 0x80) == 0x80 ) {
lcd.printf("Online\n", i);
@@ -138,7 +176,7 @@
i++;
led_1 = !led_1;
led_4 = !sw_4;
-
+
enqueue_sensor_data(i);
sakuraio.send();
}
SAKURA Internet