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:
- 3:e5e53c1f6fda
- Parent:
- 1:faf04d99f302
- Child:
- 4:99f7d7c6a408
--- a/main.cpp Wed Nov 29 06:36:12 2017 +0000
+++ b/main.cpp Mon May 07 06:17:24 2018 +0000
@@ -54,6 +54,8 @@
void gps_uart_buffering_handler();
+const int SEND_INTERVAL_TICKS_PAR_COUNT = 1500;
+
void setup()
{
lcd_led = 1;
@@ -78,6 +80,11 @@
// active high
gps_en = 1;
gps.attach(&gps_uart_buffering_handler, Serial::RxIrq);
+
+ led_1 = 1;
+ led_2 = 0;
+
+ pc.printf("Send par %d seconds.\r\n", (SEND_INTERVAL_TICKS_PAR_COUNT * 200) / 1000);
}
void read_sensor_data()
@@ -88,16 +95,16 @@
mpu9250.read_all();
sensor_data.mpu9250.temperature = mpu9250.Temperature;
- for (int i = 0; i < 3; i++) {
- sensor_data.mpu9250.accelerometer[i] = mpu9250.accelerometer_data[i];
- sensor_data.mpu9250.gyroscope[i] = mpu9250.gyroscope_data[i];
- sensor_data.mpu9250.magnetometer[i] = mpu9250.Magnetometer[i];
+ for (int cnt_send = 0; cnt_send < 3; cnt_send++) {
+ sensor_data.mpu9250.accelerometer[cnt_send] = mpu9250.accelerometer_data[cnt_send];
+ sensor_data.mpu9250.gyroscope[cnt_send] = mpu9250.gyroscope_data[cnt_send];
+ sensor_data.mpu9250.magnetometer[cnt_send] = mpu9250.Magnetometer[cnt_send];
}
}
void enqueue_sensor_data(int counter)
{
- sakuraio.enqueueTx(0, counter);
+ sakuraio.enqueueTx(0, (int32_t)counter);
sakuraio.enqueueTx(1, sensor_data.bme280.temperature);
sakuraio.enqueueTx(2, sensor_data.bme280.pressure);
sakuraio.enqueueTx(3, sensor_data.bme280.humidity);
@@ -136,49 +143,80 @@
void loop()
{
- pc.printf("\r\n\r\n--------------------\r\n");
+ static int cnt_send = 1;
+ static int tick_by_200ms = 0;
+ static int stat_sw5 = -1;
+
+ if((sakuraio.getConnectionStatus() & 0x80) == 0x00) {
+ //Offline
+ lcd.cls();
+ lcd.printf("Offline");
+ pc.printf("Network is offline.\r\n(After 1 sec to running retry.)\r\n");
+ wait(1);
+ return;
+ }
- 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: %.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: %.2fC\r\n", sensor_data.mpu9250.temperature);
- for (int j = 0; j < 3; 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]);
+ if (stat_sw5 != sw_5) {
+ stat_sw5 = sw_5;
+ led_3 = stat_sw5; //State: `Send Enable'
+ if (stat_sw5 == 0) {
+ lcd.cls();
+ lcd.printf("Send:OFF");
+ } else {
+ cnt_send = 1;
+ tick_by_200ms = 0;
+ lcd.cls();
+ lcd.printf("Send:ON");
+ }
}
- 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);
- } else {
- lcd.printf("Offline\n", i);
+
+ if (stat_sw5 == 1) {
+ if ((tick_by_200ms % SEND_INTERVAL_TICKS_PAR_COUNT) == 0) { //定期送信は5分に一回(31日動かしっぱなしでも無料分を超えないように)
+ pc.printf("\r\n\r\n--------------------\r\n");
+ read_sensor_data();
+ pc.printf("BME280\r\n");
+ 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: %.2fC\r\n", sensor_data.mpu9250.temperature);
+ for (int j = 0; j < 3; 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());
+ if ((sakuraio.getConnectionStatus() & 0x80) == 0x80) {
+ led_2 = 1;
+ pc.printf("Send:%d\r\n", cnt_send);
+ lcd.setCursor(0, 1);
+ lcd.printf("%d", cnt_send);
+ enqueue_sensor_data(cnt_send);
+ sakuraio.send();
+ cnt_send++;
+ led_2 = 0;
+ pc.printf("After %d sec to send.\r\n", (int)(SEND_INTERVAL_TICKS_PAR_COUNT * 0.2));
+ } else {
+ return;
+ }
+ }
}
- lcd.printf("%d", i);
- i++;
led_1 = !led_1;
led_4 = !sw_4;
-
- enqueue_sensor_data(i);
- sakuraio.send();
+ tick_by_200ms++;
+ wait(0.2);
}
@@ -188,4 +226,4 @@
while(1) {
loop();
}
-}
\ No newline at end of file
+}
SAKURA Internet