レンタルサーバーと連携したsakura.ioの運用例
Dependencies: mbed SakuraIO MPU9250_SPI BME280 gps AQM0802A
Revision 0:d2c4ee597498, committed 2018-11-21
- Comitter:
- punix
- Date:
- Wed Nov 21 08:54:34 2018 +0000
- Child:
- 1:11957db1faa5
- Commit message:
- TEST 20181121
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AQM0802A.lib Wed Nov 21 08:54:34 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/tetsuya256/code/AQM0802A/#5cc8d4df601d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BME280.lib Wed Nov 21 08:54:34 2018 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/MACRUM/code/BME280/#c1f1647004c4
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LICENSE Wed Nov 21 08:54:34 2018 +0000 @@ -0,0 +1,19 @@ +Copyright (c) 2018 SAKURA Internet Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +OR OTHER DEALINGS IN THE SOFTWARE.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MAINTENANCE.md Wed Nov 21 08:54:34 2018 +0000 @@ -0,0 +1,32 @@ +# Maintenance + +## Sync with github.com + +### Setup hg-git + +```bash +pip install mercurial hg-git +``` + +#### ~/.hgrc + +``` +[extensions] +hggit = +``` + +### mbed.org to github.com + +```bash +hg clone https://developer.mbed.org/teams/SAKURA-Internet/code/SakuraIO_Evaluation_Board_Standard/ SakuraIO_Evaluation_Board_Standard-sync +cd SakuraIO_Evaluation_Board_Standard +hg push git@github.com:sakuraio/SakuraIO_Evaluation_Board_Standard.git +``` + +### github.com to mbed.org + +```bash +hg pull git@github.com:sakuraio/SakuraIO_Evaluation_Board_Standard.git +hg outgoing https://developer.mbed.org/teams/SAKURA-Internet/code/SakuraIO_Evaluation_Board_Standard/ # check diff +hg push https://developer.mbed.org/teams/SAKURA-Internet/code/SakuraIO_Evaluation_Board_Standard/ +```
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPU9250_SPI.lib Wed Nov 21 08:54:34 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/SAKURA-Internet/code/MPU9250_SPI/#1056a7f87bd0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Wed Nov 21 08:54:34 2018 +0000 @@ -0,0 +1,18 @@ +# SakuraIo Evaluation Board Standard + +## Overview +This program is example code of [sakura.io Evalaution board](https://os.mbed.com/platforms/SAKURAIO_EVB_01/). + +### 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](https://github.com/sakuraio/SakuraIO_Evaluation_Board_Standard/wiki/GettingStarted) page.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SakuraIO.lib Wed Nov 21 08:54:34 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/SAKURA-Internet/code/SakuraIO/#a9dbb86be232
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SakuraPinNames.h Wed Nov 21 08:54:34 2018 +0000 @@ -0,0 +1,63 @@ +#ifndef __SAKURA_PIN_NAMES_H__ +#define __SAKURA_PIN_NAMES_H__ + +#define DAP_UART_TX PB_6 +#define DAP_UART_RX PB_7 + +// GPIO +#define SW1 PC_14 +#define SW2 PH_0 +#define SW3 PC_0 +#define SW4 PC_1 +#define SW5 PC_2 +#define SW6 PC_3 +#undef LED1 +#undef LED2 +#undef LED3 +#undef LED4 +#define LED1 PC_13 +#define LED2 PC_15 +#define LED3 PH_1 +#define LED4 PC_4 + +// LCD +#define LED_LCD PC_5 + +// MPU-9250 +#define MPU9250_INT PB_2 + +// GPS +#define GPS_EN PA_11 +#define GPS_INT PB_5 +#define GPS_1PPS PB_4 +#define GPS_WAKEUP PD_2 +#define GPS_TX PC_6 +#define GPS_RX PC_7 + +// sakura.io +#define SCM_WAKE_IN PC_8 +#define SCM_WAKE_OUT PA_9 + +/* Bus */ + +// I2C (LCD, BME280) +#define I2C_INTERNAL_SCL PB_8 +#define I2C_INTERNAL_SDA PB_9 + +// I2C (sakura.io) +#undef I2C_SCL +#undef I2C_SDA +#define I2C_SCL PA_8 +#define I2C_SDA PC_9 + +// I2C (External) +#define I2C_EXTERNAL_SCL PB_10 +#define I2C_EXTERNAL_SDA PB_3 + +// SPI (MPU-9250) +#define SPI_MPU_MISO PA_12 +#define SPI_MPU_MOSI PA_10 +#define SPI_MPU_SCK PB_0 +#define SPI_MPU_CS PB_1 + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gps.lib Wed Nov 21 08:54:34 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/SAKURA-Internet/code/gps/#fd806214d7cc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Nov 21 08:54:34 2018 +0000
@@ -0,0 +1,303 @@
+#include <mbed.h>
+#include <AQM0802A.h>
+#include <BME280.h>
+#include <MPU9250.h>
+#include <SakuraIO.h>
+#include "SakuraPinNames.h"
+#include "sensors.h"
+#include "gps.h"
+
+#define MESG_NORMAL 0
+#define MESG_INIT 1
+
+
+// Serial over CMSIS_DAP
+Serial pc(DAP_UART_TX, DAP_UART_RX, 9600);
+
+// GPS
+Serial gps(GPS_TX, GPS_RX, 9600);
+DigitalOut gps_en(GPS_EN);
+
+// LED
+DigitalOut led_1(LED1);
+DigitalOut led_2(LED2);
+DigitalOut led_3(LED3);
+DigitalOut led_4(LED4);
+
+// LCD backlight
+DigitalOut lcd_led(LED_LCD);
+
+// Switch
+DigitalIn sw_1(SW1);
+DigitalIn sw_2(SW2);
+DigitalIn sw_3(SW3);
+DigitalIn sw_4(SW4);
+DigitalIn sw_5(SW5);
+DigitalIn sw_6(SW6);
+
+// Internal I2C
+I2C internal_i2c(I2C_INTERNAL_SDA, I2C_INTERNAL_SCL);
+AQM0802A lcd(internal_i2c);
+BME280 bme280(internal_i2c);
+
+// SPI
+SPI internal_mpu9250_spi(SPI_MPU_MOSI, SPI_MPU_MISO, SPI_MPU_SCK);
+mpu9250_spi mpu9250(internal_mpu9250_spi, SPI_MPU_CS);
+
+// sakura.io
+SakuraIO_I2C sakuraio(I2C_SDA, I2C_SCL);
+
+SensorData sensor_data;
+
+// GPS UART buffer
+char uart_buffer[128] = {0};
+int uart_buffer_index = 0;
+int connected_now = 0;
+
+// NMEA Decoder
+GPSDecoder gps_decoder;
+
+void gps_uart_buffering_handler();
+
+const int SEND_INTERVAL_TICKS_PAR_COUNT = 25;
+
+const int RETRY_INTERVAL_TICKS_PAR_COUNT = 25;
+
+void setup()
+{
+ lcd_led = 1;
+ pc.printf("Hello World !\r\n");
+ lcd.cls();
+ lcd.printf("Hello");
+ connected_now = 0;
+
+ // Initialize sensors
+ bme280.initialize();
+ pc.printf("BME280 ok.\r\n");
+ mpu9250.init(1, BITS_DLPF_CFG_188HZ);
+ pc.printf("MPU9250 ok. WHOAMI=%02x\r\n", mpu9250.whoami());
+ if (mpu9250.whoami() != 0x71) {
+ pc.printf("[ERROR] MPU9250 init fail.\r\n");
+ }
+
+ mpu9250.set_gyro_scale(BITS_FS_2000DPS); //Set full scale range for gyros
+ mpu9250.set_acc_scale(BITS_FS_16G); //Set full scale range for accs
+ mpu9250.calib_acc();
+ mpu9250.AK8963_calib_Magnetometer();
+
+ // 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()
+{
+ sensor_data.bme280.temperature = bme280.getTemperature();
+ sensor_data.bme280.pressure = bme280.getPressure();
+ sensor_data.bme280.humidity = bme280.getHumidity();
+
+ mpu9250.read_all();
+ sensor_data.mpu9250.temperature = mpu9250.Temperature;
+ 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, (int32_t)counter);
+ 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.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()
+{
+ static int cnt_send = 1;
+ static int tick_by_200ms = 0;
+ static int stat_sw5 = -1;
+ uint8_t rx_available = 0;
+ uint8_t rx_queued = 0;
+ uint8_t rx_ch=0;
+ uint8_t rx_type=0;
+ uint8_t rx_value[8];
+ int64_t rx_offset=0;
+ int64_t rx_val;
+ uint32_t tx_tmp;
+ uint8_t ch20_ctrl;
+ uint32_t ch20_id;
+ uint8_t ch20_mesg;
+
+ 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);
+ connected_now = 0;
+ return;
+ }
+ else {
+ if(0 == connected_now){
+ tx_tmp = (MESG_INIT << 8) | (lcd_led & 0xFF);
+ sakuraio.enqueueTx(20, tx_tmp);
+ sakuraio.send();
+ connected_now = 1;
+ stat_sw5 = -1;
+ }
+ if(1 == connected_now){
+ if((tick_by_200ms % RETRY_INTERVAL_TICKS_PAR_COUNT) == 0){
+ tx_tmp = (MESG_INIT<<8) | (lcd_led & 0xFF);
+ sakuraio.enqueueTx(20, tx_tmp);
+ sakuraio.send();
+ }
+ }
+ }
+
+
+
+
+ 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");
+ }
+ }
+
+ if (stat_sw5 == 1) {
+ if ((tick_by_200ms % SEND_INTERVAL_TICKS_PAR_COUNT) == 0) { //Send data intarval is 5 minutes.
+ 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());
+ 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));
+ }
+ }
+
+ do {
+ rx_available = 0;
+ if ( CMD_ERROR_NONE == sakuraio.getRxQueueLength(&rx_available, &rx_queued)) {
+ if (0 < rx_queued) {
+ if ( CMD_ERROR_NONE == sakuraio.dequeueRx(&rx_ch, &rx_type, rx_value, &rx_offset)) {
+ if ( (20 == rx_ch) && (0x49 == rx_type) ) {
+ rx_val = ( (int64_t)rx_value[3] << 24) + ( (int64_t)rx_value[2] << 16) + ( (int64_t)rx_value[1] << 8) + ( (int64_t)rx_value[0] << 0);
+ ch20_ctrl = rx_val & 0xFF;
+ ch20_mesg = (rx_val>>8) & 0xFF;
+ ch20_id = (rx_val>>16) & 0xFFFF;
+ if ( (1 == ch20_ctrl) && (MESG_NORMAL == ch20_mesg)) {
+ tx_tmp = (ch20_id<<16) | (MESG_NORMAL<<8) | (lcd_led & 0xFF);
+ sakuraio.enqueueTx(20, tx_tmp);
+ sakuraio.send();
+ }
+ if ( (1 == ch20_ctrl) && (MESG_INIT == ch20_mesg)) {
+ connected_now = 2;
+ }
+ }
+ if ( (21 == rx_ch) && (0x49 == rx_type) ) {
+ rx_val = ( (int64_t)rx_value[3] << 24) + ( (int64_t)rx_value[2] << 16) + ( (int64_t)rx_value[1] << 8) + ( (int64_t)rx_value[0] << 0);
+ ch20_ctrl = rx_val & 0xFF;
+ ch20_mesg = (rx_val>>8) & 0xFF;
+ ch20_id = (rx_val>>16) & 0xFFFF;
+ if ( 1 == ch20_ctrl || 0 == ch20_ctrl ) {
+ lcd_led = ch20_ctrl;
+ tx_tmp = (ch20_id<<16) | (MESG_NORMAL<<8) | (lcd_led & 0xFF);
+ sakuraio.enqueueTx(20, tx_tmp);
+ sakuraio.send();
+ }
+ }
+ }
+ }
+ }
+ }while(0 < rx_queued);
+
+
+
+ led_1 = !led_1;
+ led_4 = !sw_4;
+ tick_by_200ms++;
+ wait(0.2);
+}
+
+
+int main()
+{
+ setup();
+ while(1) {
+ loop();
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Nov 21 08:54:34 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/e95d10626187 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sensors.h Wed Nov 21 08:54:34 2018 +0000
@@ -0,0 +1,17 @@
+typedef struct {
+ float temperature;
+ float pressure;
+ float humidity;
+} BME280Data;
+
+typedef struct {
+ float temperature;
+ float accelerometer[3];
+ float gyroscope[3];
+ float magnetometer[3];
+} MPU9250Data;
+
+typedef struct {
+ BME280Data bme280;
+ MPU9250Data mpu9250;
+} SensorData;
SAKURA Internet