IM920地温観測システム CQ 2017ARMセミナー用サンプルプログラム
Dependencies: C027_Support_ForIM920
Fork of C027_SupportTest by
Diff: main.cpp
- Revision:
- 35:7838543282c2
- Parent:
- 34:1cbf923d4ca7
--- a/main.cpp Sun Jul 09 11:35:56 2017 +0000 +++ b/main.cpp Thu Jul 13 00:44:49 2017 +0000 @@ -1,21 +1,15 @@ #include "mbed.h" +#include "rtos.h" //------------------------------------------------------------------------------------ -/* This example was tested on C027-U20 and C027-G35 with the on board modem. - - Additionally it was tested with a shield where the SARA-G350/U260/U270 RX/TX/PWRON - is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this - configuration the following platforms were tested (it is likely that others - will work as well) - - U-BLOX: C027-G35, C027-U20, C027-C20 (for shield set define C027_FORCE_SHIELD) - - NXP: LPC1549v2, LPC4088qsb - - Freescale: FRDM-KL05Z, FRDM-KL25Z, FRDM-KL46Z, FRDM-K64F - - STM: NUCLEO-F401RE, NUCLEO-F030R8 - mount resistors SB13/14 1k, SB62/63 0R +/* + 地中温度観測システム親機プログラム + C027_SupportTestをベースにしているがmain.cのコードはほとんで入れ替え + (c) NT System Design */ -#include "GPS.h" #include "MDM.h" #include "DebugPrint.h" +#include "RingBuf.h" //------------------------------------------------------------------------------------ // You need to configure these cellular modem / SIM parameters. @@ -33,8 +27,6 @@ #define PASSWORD "sora" //------------------------------------------------------------------------------------ -//#define CELLOCATE - #define IM920_TX P4_28 #define IM920_RX P4_29 Serial im920(IM920_TX, IM920_RX); @@ -49,6 +41,12 @@ #define M2X_API_KEY "602d38e9e8dfcf62e7f87a59c80784ec" #define M2X_CHID_TEMP0 "temp0" +DigitalOut led(LED1); + +// リングバッファサイズ +#define RINGBUF_SIZE 60 // 毎分データで1時間ぶん +// リングバッファ +RingBuf ring(RINGBUF_SIZE); /* IM920から受信したADデータをデコードする @@ -124,7 +122,6 @@ if (ret > 0) { TRACE("Socket Recv \"%s\"\r\n", data); // HTTPレスポンスcheck - // ERR retryは呼び出し元でしている // 1行目抽出 ptr = strtok_r(data, "\r\n", &strtokptr); // Status Code抽出 @@ -153,61 +150,65 @@ * @param *data JSONデータ (mag+powerv data) * @return 0=OK -1=ERR */ -static char m2x_http_send(MDMSerial *mdm, int sn, int *ad) +static char m2x_http_send(MDMSerial *mdm, RingBufType *rd) { char http_data[512]; - char data[128]; -// char buf[128]; + char json[128]; + // JSONデータ作る +// sprintf(json, "{ \"value\": \"%6.1f\" }", (rd->ad[0]*MV_LSB-500)*DEGC_MV); + sprintf(json, "{ \"values\": {\"%04d-temp0\": %6.2f,\"%04d-temp1\": %6.2f,\"%04d-power\": %6.2f}}", + rd->sn, (rd->ad[0]*MV_LSB-500)*DEGC_MV, + rd->sn, (rd->ad[1]*MV_LSB-500)*DEGC_MV, + rd->sn, rd->ad[2]*PV_LSB); -#if 0 - data[0] = 0; - strncat(data, "{\"values\": {\r\n", sizeof(data)); - sprintf(buf, "\"%s\": [\r\n", M2X_CHID_TEMP0); - strncat(data, buf, sizeof(data)); - - sprintf(buf, "{ \"value\":%6.1f}", (ad[0]*MV_LSB-500)*DEGC_MV); - strncat(data, buf, sizeof(data)); - strncat(data, "],\r\n", sizeof(data)); - strncat(data, "}}", sizeof(data)); -#endif - sprintf(data, "{ \"value\": \"%6.1f\" }", (ad[0]*MV_LSB-500)*DEGC_MV); - // HTTP PUTデータ作る - snprintf(http_data, sizeof(http_data), "PUT /v2/devices/%s/streams/%04d-temp0/value HTTP/1.0\r\n" +// snprintf(http_data, sizeof(http_data), "PUT /v2/devices/%s/streams/%04d-temp0/value HTTP/1.0\r\n" + snprintf(http_data, sizeof(http_data), "POST /v2/devices/%s/update HTTP/1.0\r\n" "X-M2X-KEY: %s\r\n" "Host: %s\r\n" "Content-Type: application/json\r\n" "Content-Length: %d\r\n\r\n%s\r\n", - M2X_DEVICE_ID, sn, M2X_API_KEY, M2X_SERVER, - strlen(data), data); + M2X_DEVICE_ID, M2X_API_KEY, M2X_SERVER, + strlen(json), json); TRACE(http_data); // HTTP PUTする return cloud_http_socket_send(mdm, http_data, M2X_SERVER); } +/* + M2Xへの送信スレッド +*/ +void m2x_post_thread(void const *args) { + RingBufType *rd; + MDMSerial *mdm = (MDMSerial*)args; // 3Gモデムのインスタンス + + while (true) { + // リングバッファにデータあれば送信 + if (ring.len_get() > 0) { + led = 1; + rd = ring.pop(); // リングバッファからPOP + m2x_http_send(mdm, rd); // M2Xへデータ送信 + led = 0; + } + Thread::wait(1000); // 1sec + } +} int main(void) { int ret; -#ifdef LARGE_DATA - char buf[2048] = ""; -#else char buf[512] = ""; -#endif const int wait = 100; int ad[AD_NUM]; int sn; + RingBufType data_im920; + led = 1; + printf("start\r\n"); im920.baud(19200); im920.format(8, Serial::None, 1); - // Create the GPS object -#if 1 // use GPSI2C class -// GPSI2C gps; -#else // or GPSSerial class -// GPSSerial gps; -#endif - // Create the modem object - MDMSerial mdm; // use mdm(D1,D0) if you connect the cellular shield to a C027 + // modem object + MDMSerial mdm; //mdm.setDebug(4); // enable this for debugging issues // initialize the modem MDMParser::DevStatus devStatus = {}; @@ -230,8 +231,12 @@ mdm.dumpIp(ip); } } else { - goto ErrOut; + printf("Modem init ERROR!"); + return 0; } + led = 0; + // M2X送信スレッド + Thread thread(m2x_post_thread,&mdm); /* MAIN LOOP */ @@ -242,17 +247,16 @@ if (!im920_conv(buf, &sn, ad)) { printf ("IM920: %06d, %04d, %04d, %04d\r\n", sn, ad[0], ad[1], ad[2]); printf ("IM920: %06d, %f, %f, %f\r\n", sn, (ad[0]*MV_LSB-500)*DEGC_MV, (ad[1]*MV_LSB-500)*DEGC_MV, ad[2]*PV_LSB); - m2x_http_send(&mdm, sn, ad); + // リングバッファに保存 + data_im920.sn = sn; + for(int i = 0; i < AD_NUM; i++) { + data_im920.ad[i] = ad[i]; + } + ring.push(&data_im920); } } -#ifdef RTOS_H Thread::wait(wait); -#else - ::wait_ms(wait); -#endif } -ErrOut: -// gps.powerOff(); mdm.powerOff(); return 0; }