send GPS data via LoRaWAN

Dependencies:   TinyGPS mbed ADXL345_I2C Sht31

Revision:
0:97a57be77fbb
Child:
1:195da8230785
diff -r 000000000000 -r 97a57be77fbb EEAB-P1/Eeabp1.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EEAB-P1/Eeabp1.cpp	Sat Jun 24 03:45:00 2017 +0000
@@ -0,0 +1,259 @@
+#include "Eeabp1.h"
+//#include "rtos.h"
+
+//Thread thread;
+static const int lora_msg_max_len = 11;
+
+Eeabp1::Eeabp1() : pwr_en(P0_0), led(P0_23), lora_reset(P0_22), lora_power(P0_5), grove_power(P0_6)
+{
+    this->led_state = LED_OFF;
+    this->led = 0;
+    this->serial = new RawSerial(P0_9, P0_11);
+    this->serial->baud(9600); /* use for LoRa */
+    this->pwr_en = 1; // メイン電源投入
+    this->lora_power = 0;
+    this->lora_reset = 0;
+    this->grove_power = 0;
+    this->lora_enabled = false;
+    this->grove_enabled = false;
+}
+
+int Eeabp1::setLedState(EeabLedState state)
+{
+    this->led_state = state;
+
+    if (this->led_state == LED_OFF) {
+        this->led = 0;
+    } else {
+        this->led = 1;
+    }
+
+    return 0;
+}
+
+void Eeabp1::loop(void)
+{
+    switch (this->led_state) {
+        case LED_OFF:
+            /* fall through */
+        case LED_ON:
+            break;
+        case LED_BLINK_FAST:
+        case LED_BLINK_MID:
+        case LED_BLINK_SLOW:
+            this->led = !this->led;
+            break;
+    }
+}
+
+int Eeabp1::debug(const char * format, ...)
+{
+    char tmp[80];
+    int ret;
+
+    std::va_list arg;
+    va_start(arg, format);
+    vsnprintf(tmp, sizeof(tmp), format, arg);
+    va_end(arg);
+
+    delete this->serial;
+    this->serial = new RawSerial(P0_8, P0_10);
+    ret = this->serial->puts(tmp);
+    delete this->serial;
+    this->serial = new RawSerial(P0_9, P0_11);
+
+    return ret;
+}
+
+int Eeabp1::setLoRaPower(bool on)
+{
+    int ret;
+
+    if (on) {
+        if (lora_enabled)
+            return 0;  /* power is already on, do nothing */
+
+        this->lora_power= 1;
+        this->lora_reset = 1;
+        wait_us(500000);
+        serial->printf("mod set_echo off\r\n"); // ローカルエコー:無効
+        wait(1);
+        flushSerial();
+        serial->printf("lorawan join otaa\r\n"); // Gatewayに接続(OTAA)
+        ret = chkSerialCharRes('a'); // 成功 ">> accepted" 失敗 ">> unsuccess"
+        if (ret != 0)
+            return ret;
+        serial->printf("lorawan set_dr 2\r\n"); // データレートの設定(11byte)
+        ret = chkSerialCharOk();
+        if (ret != 0)
+            return ret;
+
+        this->lora_enabled = true;
+    } else { /* off */
+        if (!lora_enabled)
+            return 0;  /* power is already off, do nothing */
+
+        lora_power= 0;
+        lora_enabled = false;
+    }
+
+    return 0;
+}
+
+int Eeabp1::sendLoRaString(const char * format, ...)
+{
+    char str[lora_msg_max_len+1] = {0x00, };
+    char msg[64];
+    char *p = msg;
+
+    std::va_list arg;
+    va_start(arg, format);
+    vsnprintf(str, sizeof(str), format, arg);
+    va_end(arg);
+
+    p += sprintf(msg, "lorawan tx ucnf 16 ");
+    for (unsigned int i = 0; i < strlen(str); i++) {
+        p += sprintf(p, "%02x", str[i]);
+    }
+    sprintf(p, "\r\n");
+
+    flushSerial();
+    serial->puts(msg);
+    //serial->printf("lorawan tx ucnf 16 74657374\r\n");
+
+    return 0;
+}
+
+void Eeabp1::setGrovePower(bool on)
+{
+    if (on) {
+        if (grove_enabled)
+            return; /* power is already on, do nothing */
+
+        grove_power = 1;
+    } else {
+        if (!grove_enabled)
+            return; /* power is already off, do nothing */
+        
+        grove_power = 0;
+    }
+}
+
+/* private functions */
+void Eeabp1::flushSerial()
+{
+    while(serial->readable() == true) {
+        serial->getc();
+        wait_us(1000);
+    }
+}
+
+int Eeabp1::chkSerialChar(const char ch,uint16_t timeout_ms)
+{
+    uint32_t timeoutCount = 0;
+
+    while(serial->readable() == false) {
+        wait_us(50);
+        timeoutCount++;
+        if((timeoutCount  / 20) >= timeout_ms)
+            return -2;
+    }
+
+    return (serial->getc() == ch) ? 0 : -1;
+}
+
+int Eeabp1::waitSerialChar(const char ch,uint16_t timeout_ms)
+{
+    uint32_t timeoutCount = 0;
+
+    do {
+        while(serial->readable() == false) {
+            wait_us(50);
+            timeoutCount++;
+            if((timeoutCount  / 20) >= timeout_ms)
+                return -2;
+        }
+    } while(serial->getc() != ch);
+
+    return 0;
+}
+
+int Eeabp1::chkSerialCharOk()
+{
+    int ret;
+    ret = waitSerialChar('>',4000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = chkSerialChar('>',2000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = chkSerialChar(' ',2000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = chkSerialChar('O',2000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = chkSerialChar('k',2000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = waitSerialChar('>',4000);
+    if (ret != 0)
+        return __LINE__;
+
+    wait_us(1000);
+
+    return 0;
+}
+
+int Eeabp1::chkSerialCharRes(char chkchr)
+{
+    int ret;
+    ret = waitSerialChar('>', 10000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = chkSerialChar('>',2000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = chkSerialChar(' ',2000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = chkSerialChar('O',2000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = chkSerialChar('k',2000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = waitSerialChar('>',16000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = chkSerialChar('>',2000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = chkSerialChar(' ',2000);
+    if (ret != 0)
+        return __LINE__;
+
+    ret = chkSerialChar(chkchr,2000);
+    if (ret != 0)
+        return ret * __LINE__;
+
+    ret = waitSerialChar('>',4000);
+    if (ret != 0)
+        return __LINE__;
+
+    wait_us(1000);
+
+    return 0;
+}