d
Dependencies: BME280 DOGS102 DS1820 MMA845x_timmeh MTS-Serial libmDot_Australia915Mhz mbed-rtos mbed
Fork of mDot_TTN_OTAA_Node by
Diff: main.cpp
- Revision:
- 17:55ea4f38710b
- Parent:
- 16:290c505e3851
--- a/main.cpp Thu Jan 05 05:55:42 2017 +0000 +++ b/main.cpp Wed Jan 11 04:12:39 2017 +0000 @@ -11,7 +11,7 @@ * (You need to use your own device IDs, the ones shown here are examples only) * *./ttnctl devices register 0080000000000000 - * INFO Generating random AppKey... + * INFO Generating random AppKey... * INFO Registered device AppKey=000102030405060708090A0B0C0D0E0F DevEUI=0080000000000000 * * or to specify the same AppKey for a new device or to reregister the same device again: @@ -43,16 +43,36 @@ #include "MTSText.h" #include <string> #include <vector> +#include "MMA845x.h" +#include "GPSPARSER.h" +#include "DOGS102.h" +#include "font_6x8.h" using namespace mts; #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) +#define NUMSAMPLES 1000 + + + +typedef struct { + int lat_d; + int lat_m; + int lat_s; + int lng_d; + int lng_m; + int lng_s; + int x; + int y; + int z; +} Coordinate; + // AppEUI -uint8_t AppEUI[8]={0x02, 0x00, 0x00, 0x00, 0x00, 0xEE, 0xFF, 0xC0}; +uint8_t AppEUI[8]= {0x02, 0x00, 0x00, 0x00, 0x00, 0xEE, 0xFF, 0xC0}; // AppKey -uint8_t AppKey[16]={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; +uint8_t AppKey[16]= {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; // Some defines for the LoRa configuration /* @@ -74,30 +94,59 @@ * DR3 - SF7BW125 * DR4 - SF8BW500 */ -#define LORA_SF mDot::DR5 +#define LORA_SF mDot::DR4 #define LORA_ACK 0 -#define LORA_TXPOWER 14 +#define LORA_TXPOWER 20 // Ignoring sub band for EU modules. static uint8_t config_frequency_sub_band = 1; +volatile int stoppls = 0; -// DS18B20 OneWire pin -// D13 on Dev Board, pin 18 on mDot, Compatible with Oxford Flood Network PCB temperature sensor. -//#define DATA_PIN PA_5 -// A1 on Dev Board, pin 19 on mDot -#define DATA_PIN PC_13 - -// Temperature sensor object -DS1820 probe(DATA_PIN); - -// BME280 Temperature/Humidity/Pressure sensor -//BME280 sensor(I2C_SDA, I2C_SCL); +SPI lcd_spi(SPI1_MOSI, SPI1_MISO, SPI1_SCK); +DigitalOut lcd_spi_cs(SPI1_CS, 1); +DigitalOut lcd_cd(XBEE_ON_SLEEP, 1); // Serial via USB for debugging only Serial pc(USBTX,USBRX); -int main() +MMA845x_DATA accldata; + +void gpsthread(void const *args) { + while(1) { + GPSPARSER *instance = (GPSPARSER*)args; + instance->readNemaSentence(); + } +} + +void acclthread(void const *args) { + + /* Create global i2c handle */ + I2C i2chandle = I2C(PC_9, PA_8); + + MMA845x instance = MMA845x(i2chandle, MMA845x::SA0_VSS); + + instance.setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF, + MMA845x::DR_50,MMA845x::OS_NORMAL,MMA845x::HPF_OFF ); + + instance.activeMode(); + + /* Accelerometer testing code */ + while(1) { + + if (!stoppls) { + int result = instance.getStatus(); + if((result & MMA845x::XYZDR) != 0 ) { + accldata = instance.getXYZ(); + + logInfo("Accl: %d %d %d\n\r", instance.getXYZ()._x, instance.getXYZ()._y, instance.getXYZ()._z); + } + } + + } +} + +int main() { int32_t ret; mDot* dot; std::vector<uint8_t> send_data; @@ -105,25 +154,29 @@ std::vector<uint8_t> nwkId; std::vector<uint8_t> nwkKey; - float temperature = 0.0; - float humidity = 0.0; - float pressure = 0.0; + dot = mDot::getInstance(); + dot->setLogLevel(MTSLog::INFO_LEVEL); + logInfo("Starting...\n\r"); - pc.baud(115200); - pc.printf("TTN OTAA mDot LoRa Temperature sensor\n\r"); + GPSPARSER* gps; + MTSSerial gps_serial(XBEE_DOUT, XBEE_DIN, 256, 2048); + gps = new GPSPARSER(&gps_serial); + Thread gpst(gpsthread, gps); - // get a mDot handle - dot = mDot::getInstance(); + Thread acclt(acclthread); -// dot->setLogLevel(MTSLog::WARNING_LEVEL); - dot->setLogLevel(MTSLog::TRACE_LEVEL); + DOGS102* lcd = new DOGS102(lcd_spi, lcd_spi_cs, lcd_cd); + + DigitalIn button(PA_11); + button.mode(PullUp); +#ifdef ENABLE_LORA logInfo("Checking Config"); uint8_t *it = AppEUI; for (uint8_t i = 0; i<8; i++) nwkId.push_back((uint8_t) *it++); - + it = AppKey; for (uint8_t i = 0; i<16; i++) nwkKey.push_back((uint8_t) *it++); @@ -209,21 +262,21 @@ // Display LoRa parameters // Display label and values in different colours, show pretty values not numeric values where applicable -/* - pc.printf("Public Network: %s\r\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") ); - pc.printf("Frequency: %s\r\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() ); - pc.printf("Sub Band: %s\r\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() ); - pc.printf("Join Mode: %s\r\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() ); - pc.printf("Join Retries: %d\r\n", dot->getJoinRetries() ); - pc.printf("Join Byte Order: %s\r\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") ); - pc.printf("Link Check Count: %d\r\n", dot->getLinkCheckCount() ); - pc.printf("Link Check Thold: %d\r\n", dot->getLinkCheckThreshold() ); - pc.printf("Tx Data Rate: %s\r\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() ); - pc.printf("Tx Power: %d\r\n", dot->getTxPower() ); - pc.printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" )); - pc.printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") ); - pc.printf("Ack: %s\r\n", (dot->getAck() ? "Y" : "N") ); -*/ + /* + pc.printf("Public Network: %s\r\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") ); + pc.printf("Frequency: %s\r\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() ); + pc.printf("Sub Band: %s\r\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() ); + pc.printf("Join Mode: %s\r\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() ); + pc.printf("Join Retries: %d\r\n", dot->getJoinRetries() ); + pc.printf("Join Byte Order: %s\r\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") ); + pc.printf("Link Check Count: %d\r\n", dot->getLinkCheckCount() ); + pc.printf("Link Check Thold: %d\r\n", dot->getLinkCheckThreshold() ); + pc.printf("Tx Data Rate: %s\r\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() ); + pc.printf("Tx Power: %d\r\n", dot->getTxPower() ); + pc.printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" )); + pc.printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") ); + pc.printf("Ack: %s\r\n", (dot->getAck() ? "Y" : "N") ); + */ logInfo("Joining Network"); while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { @@ -234,49 +287,98 @@ logInfo("Joined Network"); // Display Network session key and data session key from Join command -/* - std::vector<uint8_t> tmp = dot->getNetworkSessionKey(); - pc.printf("Network Session Key: "); - pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str()); + /* + std::vector<uint8_t> tmp = dot->getNetworkSessionKey(); + pc.printf("Network Session Key: "); + pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str()); - tmp = dot->getDataSessionKey(); - pc.printf("Data Session Key: "); - pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str()); -*/ - // Set the Temperature sesnor resolution, 9 bits is enough and makes it faster to provide a reading. - probe.setResolution(9); + tmp = dot->getDataSessionKey(); + pc.printf("Data Session Key: "); + pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str()); + */ +#endif /* ENABLE_LORA */ char dataBuf[50]; + uint16_t count = 0; + Coordinate points[NUMSAMPLES]; + int mode = 0; while( 1 ) { - // Output data as JSON e.g. {"t":21.3} -// temperature = sensor.getTemperature(); -// humidity = sensor.getHumidity(); -// pressure = sensor.getPressure(); + + lcd->writeText(0, 0, font_6x8, "Not saving", 10); + + if (!button.read()) { + count = 0; + mode = 1; + } + + if (mode) { + + lcd->writeText(0, 0, font_6x8, "Saving ", 10); + + Coordinate a = { gps->getLongitude().degrees, gps->getLongitude().minutes, gps->getLongitude().seconds, gps->getLatitude().degrees, gps->getLatitude().minutes, gps->getLatitude().seconds , accldata._x, accldata._y, accldata._z}; + points[count] = a; + count++; + } + + lcd->writeText(0, 1, font_6x8, (gps->getLockStatus() ? "GPS Locked " : "No GPS Lock"), 11); + + if (gps->getLockStatus()) { + + sprintf(dataBuf, "Lat: %d,%d,%d", gps->getLatitude().degrees, gps->getLatitude().minutes, gps->getLatitude().seconds); + lcd->writeText(0, 2, font_6x8, dataBuf, strlen(dataBuf)); + + sprintf(dataBuf, "Long: %d,%d,%d", gps->getLongitude().degrees, gps->getLongitude().minutes, gps->getLongitude().seconds); + lcd->writeText(0, 3, font_6x8, dataBuf, strlen(dataBuf)); + + sprintf(dataBuf, "Sat: %d", gps->getNumSatellites()); + lcd->writeText(0, 4, font_6x8, dataBuf, strlen(dataBuf)); + + } + + sprintf(dataBuf, "%04d,%04d,%04d ", accldata._x, accldata._y, accldata._z); + lcd->writeText(0, 5, font_6x8, dataBuf, strlen(dataBuf)); + + sprintf(dataBuf, "Time: %02d", gps->getTimestamp().tm_sec); + lcd->writeText(0, 6, font_6x8, dataBuf, strlen(dataBuf)); - //Start temperature conversion, wait until ready - probe.convertTemperature(true, DS1820::all_devices); - // Output data as JSON e.g. {"t":21.3} - temperature = probe.temperature(); - sprintf(dataBuf, "{\"t\":%3.1f}", temperature ); -// sprintf(dataBuf, "%3.1f,%3.1f,%04.2f", temperature,humidity,pressure ); - pc.printf("%s\n",dataBuf); - send_data.clear(); - // probably not the most efficent way to do this - for( int i=0; i< strlen(dataBuf); i++ ) - send_data.push_back( dataBuf[i] ); + +#ifdef ENABLE_LORA + send_data.clear(); + // probably not the most efficent way to do this + for( int i=0; i< strlen(dataBuf); i++ ) { + send_data.push_back( dataBuf[i] ); + } + + if ((ret = dot->send(send_data)) != mDot::MDOT_OK) { + logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); + } else { + logInfo("send data: %s", Text::bin2hexString(send_data).c_str()); + } +#endif /* ENABLE_LORA */ + + sprintf(dataBuf, "C: %03d B: %d", count, button.read()); + lcd->writeText(0, 7, font_6x8, dataBuf, strlen(dataBuf)); - if ((ret = dot->send(send_data)) != mDot::MDOT_OK) { - logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); - } else { - logInfo("send data: %s", Text::bin2hexString(send_data).c_str()); - } - - // Should sleep here and wakeup after a set 5 minute interval. - // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again - uint32_t sleep_time = std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()) / 1000; -//wait_ms(2000); - // go to sleep and wake up automatically sleep_time seconds later - dot->sleep(sleep_time, mDot::RTC_ALARM); + osDelay(1000/50); + + if (count == NUMSAMPLES && mode) { + // Stop sampling + stoppls = 1; + while (1) { + sprintf(dataBuf, "C: %03d B: %d", count, button.read()); + lcd->writeText(0, 7, font_6x8, dataBuf, strlen(dataBuf)); + + if (!button.read()) { + logInfo("Starting..."); + for (int i = 0; i < NUMSAMPLES; i++) { + logInfo("%d,%d,%d,%d,%d,%d,%d,%d,%d", points[i].lat_d, points[i].lat_m, points[i].lat_s, points[i].lng_d, points[i].lng_m, points[i].lng_s, points[i].x, points[i].y, points[i].z); + osDelay(1); + } + logInfo("DONE"); + } + + osDelay(100); + } + } } - }