send GPS data via LoRaWAN

Dependencies:   TinyGPS mbed ADXL345_I2C Sht31

Committer:
koyo_take
Date:
Sat Jun 24 03:45:00 2017 +0000
Revision:
0:97a57be77fbb
Child:
1:195da8230785
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
koyo_take 0:97a57be77fbb 1 #include "Eeabp1.h"
koyo_take 0:97a57be77fbb 2 //#include "rtos.h"
koyo_take 0:97a57be77fbb 3
koyo_take 0:97a57be77fbb 4 //Thread thread;
koyo_take 0:97a57be77fbb 5 static const int lora_msg_max_len = 11;
koyo_take 0:97a57be77fbb 6
koyo_take 0:97a57be77fbb 7 Eeabp1::Eeabp1() : pwr_en(P0_0), led(P0_23), lora_reset(P0_22), lora_power(P0_5), grove_power(P0_6)
koyo_take 0:97a57be77fbb 8 {
koyo_take 0:97a57be77fbb 9 this->led_state = LED_OFF;
koyo_take 0:97a57be77fbb 10 this->led = 0;
koyo_take 0:97a57be77fbb 11 this->serial = new RawSerial(P0_9, P0_11);
koyo_take 0:97a57be77fbb 12 this->serial->baud(9600); /* use for LoRa */
koyo_take 0:97a57be77fbb 13 this->pwr_en = 1; // メイン電源投入
koyo_take 0:97a57be77fbb 14 this->lora_power = 0;
koyo_take 0:97a57be77fbb 15 this->lora_reset = 0;
koyo_take 0:97a57be77fbb 16 this->grove_power = 0;
koyo_take 0:97a57be77fbb 17 this->lora_enabled = false;
koyo_take 0:97a57be77fbb 18 this->grove_enabled = false;
koyo_take 0:97a57be77fbb 19 }
koyo_take 0:97a57be77fbb 20
koyo_take 0:97a57be77fbb 21 int Eeabp1::setLedState(EeabLedState state)
koyo_take 0:97a57be77fbb 22 {
koyo_take 0:97a57be77fbb 23 this->led_state = state;
koyo_take 0:97a57be77fbb 24
koyo_take 0:97a57be77fbb 25 if (this->led_state == LED_OFF) {
koyo_take 0:97a57be77fbb 26 this->led = 0;
koyo_take 0:97a57be77fbb 27 } else {
koyo_take 0:97a57be77fbb 28 this->led = 1;
koyo_take 0:97a57be77fbb 29 }
koyo_take 0:97a57be77fbb 30
koyo_take 0:97a57be77fbb 31 return 0;
koyo_take 0:97a57be77fbb 32 }
koyo_take 0:97a57be77fbb 33
koyo_take 0:97a57be77fbb 34 void Eeabp1::loop(void)
koyo_take 0:97a57be77fbb 35 {
koyo_take 0:97a57be77fbb 36 switch (this->led_state) {
koyo_take 0:97a57be77fbb 37 case LED_OFF:
koyo_take 0:97a57be77fbb 38 /* fall through */
koyo_take 0:97a57be77fbb 39 case LED_ON:
koyo_take 0:97a57be77fbb 40 break;
koyo_take 0:97a57be77fbb 41 case LED_BLINK_FAST:
koyo_take 0:97a57be77fbb 42 case LED_BLINK_MID:
koyo_take 0:97a57be77fbb 43 case LED_BLINK_SLOW:
koyo_take 0:97a57be77fbb 44 this->led = !this->led;
koyo_take 0:97a57be77fbb 45 break;
koyo_take 0:97a57be77fbb 46 }
koyo_take 0:97a57be77fbb 47 }
koyo_take 0:97a57be77fbb 48
koyo_take 0:97a57be77fbb 49 int Eeabp1::debug(const char * format, ...)
koyo_take 0:97a57be77fbb 50 {
koyo_take 0:97a57be77fbb 51 char tmp[80];
koyo_take 0:97a57be77fbb 52 int ret;
koyo_take 0:97a57be77fbb 53
koyo_take 0:97a57be77fbb 54 std::va_list arg;
koyo_take 0:97a57be77fbb 55 va_start(arg, format);
koyo_take 0:97a57be77fbb 56 vsnprintf(tmp, sizeof(tmp), format, arg);
koyo_take 0:97a57be77fbb 57 va_end(arg);
koyo_take 0:97a57be77fbb 58
koyo_take 0:97a57be77fbb 59 delete this->serial;
koyo_take 0:97a57be77fbb 60 this->serial = new RawSerial(P0_8, P0_10);
koyo_take 0:97a57be77fbb 61 ret = this->serial->puts(tmp);
koyo_take 0:97a57be77fbb 62 delete this->serial;
koyo_take 0:97a57be77fbb 63 this->serial = new RawSerial(P0_9, P0_11);
koyo_take 0:97a57be77fbb 64
koyo_take 0:97a57be77fbb 65 return ret;
koyo_take 0:97a57be77fbb 66 }
koyo_take 0:97a57be77fbb 67
koyo_take 0:97a57be77fbb 68 int Eeabp1::setLoRaPower(bool on)
koyo_take 0:97a57be77fbb 69 {
koyo_take 0:97a57be77fbb 70 int ret;
koyo_take 0:97a57be77fbb 71
koyo_take 0:97a57be77fbb 72 if (on) {
koyo_take 0:97a57be77fbb 73 if (lora_enabled)
koyo_take 0:97a57be77fbb 74 return 0; /* power is already on, do nothing */
koyo_take 0:97a57be77fbb 75
koyo_take 0:97a57be77fbb 76 this->lora_power= 1;
koyo_take 0:97a57be77fbb 77 this->lora_reset = 1;
koyo_take 0:97a57be77fbb 78 wait_us(500000);
koyo_take 0:97a57be77fbb 79 serial->printf("mod set_echo off\r\n"); // ローカルエコー:無効
koyo_take 0:97a57be77fbb 80 wait(1);
koyo_take 0:97a57be77fbb 81 flushSerial();
koyo_take 0:97a57be77fbb 82 serial->printf("lorawan join otaa\r\n"); // Gatewayに接続(OTAA)
koyo_take 0:97a57be77fbb 83 ret = chkSerialCharRes('a'); // 成功 ">> accepted" 失敗 ">> unsuccess"
koyo_take 0:97a57be77fbb 84 if (ret != 0)
koyo_take 0:97a57be77fbb 85 return ret;
koyo_take 0:97a57be77fbb 86 serial->printf("lorawan set_dr 2\r\n"); // データレートの設定(11byte)
koyo_take 0:97a57be77fbb 87 ret = chkSerialCharOk();
koyo_take 0:97a57be77fbb 88 if (ret != 0)
koyo_take 0:97a57be77fbb 89 return ret;
koyo_take 0:97a57be77fbb 90
koyo_take 0:97a57be77fbb 91 this->lora_enabled = true;
koyo_take 0:97a57be77fbb 92 } else { /* off */
koyo_take 0:97a57be77fbb 93 if (!lora_enabled)
koyo_take 0:97a57be77fbb 94 return 0; /* power is already off, do nothing */
koyo_take 0:97a57be77fbb 95
koyo_take 0:97a57be77fbb 96 lora_power= 0;
koyo_take 0:97a57be77fbb 97 lora_enabled = false;
koyo_take 0:97a57be77fbb 98 }
koyo_take 0:97a57be77fbb 99
koyo_take 0:97a57be77fbb 100 return 0;
koyo_take 0:97a57be77fbb 101 }
koyo_take 0:97a57be77fbb 102
koyo_take 0:97a57be77fbb 103 int Eeabp1::sendLoRaString(const char * format, ...)
koyo_take 0:97a57be77fbb 104 {
koyo_take 0:97a57be77fbb 105 char str[lora_msg_max_len+1] = {0x00, };
koyo_take 0:97a57be77fbb 106 char msg[64];
koyo_take 0:97a57be77fbb 107 char *p = msg;
koyo_take 0:97a57be77fbb 108
koyo_take 0:97a57be77fbb 109 std::va_list arg;
koyo_take 0:97a57be77fbb 110 va_start(arg, format);
koyo_take 0:97a57be77fbb 111 vsnprintf(str, sizeof(str), format, arg);
koyo_take 0:97a57be77fbb 112 va_end(arg);
koyo_take 0:97a57be77fbb 113
koyo_take 0:97a57be77fbb 114 p += sprintf(msg, "lorawan tx ucnf 16 ");
koyo_take 0:97a57be77fbb 115 for (unsigned int i = 0; i < strlen(str); i++) {
koyo_take 0:97a57be77fbb 116 p += sprintf(p, "%02x", str[i]);
koyo_take 0:97a57be77fbb 117 }
koyo_take 0:97a57be77fbb 118 sprintf(p, "\r\n");
koyo_take 0:97a57be77fbb 119
koyo_take 0:97a57be77fbb 120 flushSerial();
koyo_take 0:97a57be77fbb 121 serial->puts(msg);
koyo_take 0:97a57be77fbb 122 //serial->printf("lorawan tx ucnf 16 74657374\r\n");
koyo_take 0:97a57be77fbb 123
koyo_take 0:97a57be77fbb 124 return 0;
koyo_take 0:97a57be77fbb 125 }
koyo_take 0:97a57be77fbb 126
koyo_take 0:97a57be77fbb 127 void Eeabp1::setGrovePower(bool on)
koyo_take 0:97a57be77fbb 128 {
koyo_take 0:97a57be77fbb 129 if (on) {
koyo_take 0:97a57be77fbb 130 if (grove_enabled)
koyo_take 0:97a57be77fbb 131 return; /* power is already on, do nothing */
koyo_take 0:97a57be77fbb 132
koyo_take 0:97a57be77fbb 133 grove_power = 1;
koyo_take 0:97a57be77fbb 134 } else {
koyo_take 0:97a57be77fbb 135 if (!grove_enabled)
koyo_take 0:97a57be77fbb 136 return; /* power is already off, do nothing */
koyo_take 0:97a57be77fbb 137
koyo_take 0:97a57be77fbb 138 grove_power = 0;
koyo_take 0:97a57be77fbb 139 }
koyo_take 0:97a57be77fbb 140 }
koyo_take 0:97a57be77fbb 141
koyo_take 0:97a57be77fbb 142 /* private functions */
koyo_take 0:97a57be77fbb 143 void Eeabp1::flushSerial()
koyo_take 0:97a57be77fbb 144 {
koyo_take 0:97a57be77fbb 145 while(serial->readable() == true) {
koyo_take 0:97a57be77fbb 146 serial->getc();
koyo_take 0:97a57be77fbb 147 wait_us(1000);
koyo_take 0:97a57be77fbb 148 }
koyo_take 0:97a57be77fbb 149 }
koyo_take 0:97a57be77fbb 150
koyo_take 0:97a57be77fbb 151 int Eeabp1::chkSerialChar(const char ch,uint16_t timeout_ms)
koyo_take 0:97a57be77fbb 152 {
koyo_take 0:97a57be77fbb 153 uint32_t timeoutCount = 0;
koyo_take 0:97a57be77fbb 154
koyo_take 0:97a57be77fbb 155 while(serial->readable() == false) {
koyo_take 0:97a57be77fbb 156 wait_us(50);
koyo_take 0:97a57be77fbb 157 timeoutCount++;
koyo_take 0:97a57be77fbb 158 if((timeoutCount / 20) >= timeout_ms)
koyo_take 0:97a57be77fbb 159 return -2;
koyo_take 0:97a57be77fbb 160 }
koyo_take 0:97a57be77fbb 161
koyo_take 0:97a57be77fbb 162 return (serial->getc() == ch) ? 0 : -1;
koyo_take 0:97a57be77fbb 163 }
koyo_take 0:97a57be77fbb 164
koyo_take 0:97a57be77fbb 165 int Eeabp1::waitSerialChar(const char ch,uint16_t timeout_ms)
koyo_take 0:97a57be77fbb 166 {
koyo_take 0:97a57be77fbb 167 uint32_t timeoutCount = 0;
koyo_take 0:97a57be77fbb 168
koyo_take 0:97a57be77fbb 169 do {
koyo_take 0:97a57be77fbb 170 while(serial->readable() == false) {
koyo_take 0:97a57be77fbb 171 wait_us(50);
koyo_take 0:97a57be77fbb 172 timeoutCount++;
koyo_take 0:97a57be77fbb 173 if((timeoutCount / 20) >= timeout_ms)
koyo_take 0:97a57be77fbb 174 return -2;
koyo_take 0:97a57be77fbb 175 }
koyo_take 0:97a57be77fbb 176 } while(serial->getc() != ch);
koyo_take 0:97a57be77fbb 177
koyo_take 0:97a57be77fbb 178 return 0;
koyo_take 0:97a57be77fbb 179 }
koyo_take 0:97a57be77fbb 180
koyo_take 0:97a57be77fbb 181 int Eeabp1::chkSerialCharOk()
koyo_take 0:97a57be77fbb 182 {
koyo_take 0:97a57be77fbb 183 int ret;
koyo_take 0:97a57be77fbb 184 ret = waitSerialChar('>',4000);
koyo_take 0:97a57be77fbb 185 if (ret != 0)
koyo_take 0:97a57be77fbb 186 return __LINE__;
koyo_take 0:97a57be77fbb 187
koyo_take 0:97a57be77fbb 188 ret = chkSerialChar('>',2000);
koyo_take 0:97a57be77fbb 189 if (ret != 0)
koyo_take 0:97a57be77fbb 190 return __LINE__;
koyo_take 0:97a57be77fbb 191
koyo_take 0:97a57be77fbb 192 ret = chkSerialChar(' ',2000);
koyo_take 0:97a57be77fbb 193 if (ret != 0)
koyo_take 0:97a57be77fbb 194 return __LINE__;
koyo_take 0:97a57be77fbb 195
koyo_take 0:97a57be77fbb 196 ret = chkSerialChar('O',2000);
koyo_take 0:97a57be77fbb 197 if (ret != 0)
koyo_take 0:97a57be77fbb 198 return __LINE__;
koyo_take 0:97a57be77fbb 199
koyo_take 0:97a57be77fbb 200 ret = chkSerialChar('k',2000);
koyo_take 0:97a57be77fbb 201 if (ret != 0)
koyo_take 0:97a57be77fbb 202 return __LINE__;
koyo_take 0:97a57be77fbb 203
koyo_take 0:97a57be77fbb 204 ret = waitSerialChar('>',4000);
koyo_take 0:97a57be77fbb 205 if (ret != 0)
koyo_take 0:97a57be77fbb 206 return __LINE__;
koyo_take 0:97a57be77fbb 207
koyo_take 0:97a57be77fbb 208 wait_us(1000);
koyo_take 0:97a57be77fbb 209
koyo_take 0:97a57be77fbb 210 return 0;
koyo_take 0:97a57be77fbb 211 }
koyo_take 0:97a57be77fbb 212
koyo_take 0:97a57be77fbb 213 int Eeabp1::chkSerialCharRes(char chkchr)
koyo_take 0:97a57be77fbb 214 {
koyo_take 0:97a57be77fbb 215 int ret;
koyo_take 0:97a57be77fbb 216 ret = waitSerialChar('>', 10000);
koyo_take 0:97a57be77fbb 217 if (ret != 0)
koyo_take 0:97a57be77fbb 218 return __LINE__;
koyo_take 0:97a57be77fbb 219
koyo_take 0:97a57be77fbb 220 ret = chkSerialChar('>',2000);
koyo_take 0:97a57be77fbb 221 if (ret != 0)
koyo_take 0:97a57be77fbb 222 return __LINE__;
koyo_take 0:97a57be77fbb 223
koyo_take 0:97a57be77fbb 224 ret = chkSerialChar(' ',2000);
koyo_take 0:97a57be77fbb 225 if (ret != 0)
koyo_take 0:97a57be77fbb 226 return __LINE__;
koyo_take 0:97a57be77fbb 227
koyo_take 0:97a57be77fbb 228 ret = chkSerialChar('O',2000);
koyo_take 0:97a57be77fbb 229 if (ret != 0)
koyo_take 0:97a57be77fbb 230 return __LINE__;
koyo_take 0:97a57be77fbb 231
koyo_take 0:97a57be77fbb 232 ret = chkSerialChar('k',2000);
koyo_take 0:97a57be77fbb 233 if (ret != 0)
koyo_take 0:97a57be77fbb 234 return __LINE__;
koyo_take 0:97a57be77fbb 235
koyo_take 0:97a57be77fbb 236 ret = waitSerialChar('>',16000);
koyo_take 0:97a57be77fbb 237 if (ret != 0)
koyo_take 0:97a57be77fbb 238 return __LINE__;
koyo_take 0:97a57be77fbb 239
koyo_take 0:97a57be77fbb 240 ret = chkSerialChar('>',2000);
koyo_take 0:97a57be77fbb 241 if (ret != 0)
koyo_take 0:97a57be77fbb 242 return __LINE__;
koyo_take 0:97a57be77fbb 243
koyo_take 0:97a57be77fbb 244 ret = chkSerialChar(' ',2000);
koyo_take 0:97a57be77fbb 245 if (ret != 0)
koyo_take 0:97a57be77fbb 246 return __LINE__;
koyo_take 0:97a57be77fbb 247
koyo_take 0:97a57be77fbb 248 ret = chkSerialChar(chkchr,2000);
koyo_take 0:97a57be77fbb 249 if (ret != 0)
koyo_take 0:97a57be77fbb 250 return ret * __LINE__;
koyo_take 0:97a57be77fbb 251
koyo_take 0:97a57be77fbb 252 ret = waitSerialChar('>',4000);
koyo_take 0:97a57be77fbb 253 if (ret != 0)
koyo_take 0:97a57be77fbb 254 return __LINE__;
koyo_take 0:97a57be77fbb 255
koyo_take 0:97a57be77fbb 256 wait_us(1000);
koyo_take 0:97a57be77fbb 257
koyo_take 0:97a57be77fbb 258 return 0;
koyo_take 0:97a57be77fbb 259 }