Gregory Kielian / Mbed 2 deprecated PollutionSensor

Dependencies:   libmDot mbed-rtos mbed

Fork of Adafruit_I2C_9dof_mDot_TTN by Gregory Kielian

Committer:
gkielian
Date:
Mon Sep 12 00:23:43 2016 +0000
Revision:
10:798cac7d3dcb
Parent:
9:e3756ad55310
Child:
11:a2a6b1214ae4
analog input implemented on AO

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elwinong 8:ebec2e50d421 1 /* Adafruit I2C Temp Sensor MCP9808 -> Multitech mDot -> The Things Network (TTN)
elwinong 8:ebec2e50d421 2 *
elwinong 8:ebec2e50d421 3 * This program connects the Adafruit temp sensor board MCP9808 through I2C
elwinong 8:ebec2e50d421 4 * and sends the temperature output to the The Things Network.
elwinong 8:ebec2e50d421 5 *
elwinong 8:ebec2e50d421 6 * Based on:
elwinong 8:ebec2e50d421 7 * - mDot TTN Connect Example: https://developer.mbed.org/users/ropu/code/mDot_LoRa_example_TTN_connect/rev/609e7bb06486
elwinong 8:ebec2e50d421 8 * - Adafruit MCP9808 Library: https://github.com/adafruit/Adafruit_MCP9808_Library
elwinong 8:ebec2e50d421 9 * - mbed I2C Example: https://developer.mbed.org/handbook/I2C
elwinong 8:ebec2e50d421 10 * - TTN Backend: https://www.thethingsnetwork.org/wiki/Backend/Overview
elwinong 8:ebec2e50d421 11 *
elwinong 8:ebec2e50d421 12 * Requiremens:
elwinong 8:ebec2e50d421 13 * - Multitech UDK board and mDot: https://developer.mbed.org/platforms/MTS-mDot-F411/
elwinong 8:ebec2e50d421 14 * - Adafruit MCP9809: https://learn.adafruit.com/adafruit-mcp9808-precision-i2c-temperature-sensor-guide/overview
elwinong 8:ebec2e50d421 15 * - Serial to USB like this or similar: https://www.amazon.com/Sabrent-Serial-RS-232-Converter-CB-DB9P/dp/B00IDSM6BW/ref=sr_1_5?ie=UTF8&qid=1470167137&sr=8-5&keywords=serial+usb
elwinong 8:ebec2e50d421 16 * - Default serial port baudrate is 9600
elwinong 8:ebec2e50d421 17 *
elwinong 8:ebec2e50d421 18 * Wiring:
elwinong 8:ebec2e50d421 19 * MCP9808 : UDK
elwinong 8:ebec2e50d421 20 * - Vdd : - D8 3.3V
elwinong 8:ebec2e50d421 21 * - Gnd : - D3 GND
elwinong 8:ebec2e50d421 22 * - SCL : - D15
elwinong 8:ebec2e50d421 23 * - SDA : - D14
elwinong 8:ebec2e50d421 24 */
elwinong 8:ebec2e50d421 25
mfiore 0:09250cd371d2 26 #include "mbed.h"
mfiore 0:09250cd371d2 27 #include "mDot.h"
mfiore 4:36e214ebfa56 28 #include "MTSLog.h"
mfiore 0:09250cd371d2 29 #include <string>
mfiore 0:09250cd371d2 30 #include <vector>
mfiore 4:36e214ebfa56 31 #include <algorithm>
elwinong 8:ebec2e50d421 32
elwinong 8:ebec2e50d421 33 // TTN Keys, setup your application and get your own keys at https://www.thethingsnetwork.org/wiki/Backend/Overview
gkielian 9:e3756ad55310 34 static const uint8_t netowork_session_key_array[] = { 0x22, 0xDF, 0x20, 0x0C, 0x71, 0x5A, 0xCC, 0x35, 0xB8, 0x20, 0x5F, 0xAE, 0xC7, 0x00, 0x5A, 0x63 };//
gkielian 9:e3756ad55310 35
gkielian 9:e3756ad55310 36 //app session key
gkielian 9:e3756ad55310 37 static const uint8_t data_session_key_array[] = { 0x66, 0xF2, 0x8B, 0x88, 0x76, 0x0D, 0xB0, 0xE2, 0xAE, 0x16, 0x29, 0x81, 0x29, 0xC9, 0x25, 0x11 };
gkielian 9:e3756ad55310 38
gkielian 9:e3756ad55310 39 //dev address (keep in mind these are all under abp)
gkielian 9:e3756ad55310 40 static const uint8_t network_address_array[] = { 0x30, 0xE2, 0x6C, 0xAF };
gkielian 9:e3756ad55310 41
gkielian 9:e3756ad55310 42 //static const uint8_t netowork_session_key_array[] = { 0x35, 0x41, 0x32, 0x67, 0x79, 0x76, 0x49, 0x8B, 0xBE, 0x98, 0x10, 0x80, 0x76, 0xB7, 0x61, 0x8B };
gkielian 9:e3756ad55310 43 //static const uint8_t data_session_key_array[] = { 0xD3, 0x6F, 0x5E, 0x66, 0x4A, 0x1B, 0xEC, 0x0C, 0x4A, 0x63, 0x8E, 0x1C, 0x2D, 0xB3, 0x18, 0xA4 };
gkielian 9:e3756ad55310 44 //static const uint8_t network_address_array[] = { 0x10, 0x81, 0xDA, 0x6F };
elwinong 8:ebec2e50d421 45
ropu 6:8f7276e7d206 46 static std::vector<uint8_t> netowork_session_key (netowork_session_key_array, netowork_session_key_array + sizeof(netowork_session_key_array) / sizeof(uint8_t));
ropu 6:8f7276e7d206 47 static std::vector<uint8_t> data_session_key (data_session_key_array, data_session_key_array + sizeof(data_session_key_array) / sizeof(uint8_t));
ropu 6:8f7276e7d206 48 static std::vector<uint8_t> network_address (network_address_array, network_address_array + sizeof(network_address_array) / sizeof(uint8_t));
elwinong 8:ebec2e50d421 49
elwinong 8:ebec2e50d421 50 static uint8_t config_frequency_sub_band = 2;
elwinong 8:ebec2e50d421 51
elwinong 8:ebec2e50d421 52 // Initialize I2C
elwinong 8:ebec2e50d421 53 I2C i2c(I2C_SDA , I2C_SCL );
gkielian 9:e3756ad55310 54 const int addr7bit = 0x1E; // 7 bit I2C address
elwinong 8:ebec2e50d421 55 const int addr8bit = addr7bit << 1; // 8bit I2C address, 0x90
elwinong 8:ebec2e50d421 56
mfiore 0:09250cd371d2 57
mfiore 0:09250cd371d2 58 int main() {
elwinong 8:ebec2e50d421 59
elwinong 8:ebec2e50d421 60 // Declare and initialize variables
mfiore 0:09250cd371d2 61 int32_t ret;
mfiore 0:09250cd371d2 62 mDot* dot;
elwinong 8:ebec2e50d421 63 char cmd[2]; // I2C command address byte 8-bit
gkielian 9:e3756ad55310 64 char my_data[6]; // I2C return address bytes 16-bit
gkielian 9:e3756ad55310 65 uint16_t mag_x; // Intermediate variable
gkielian 9:e3756ad55310 66 uint16_t mag_y; // Intermediate variable
gkielian 9:e3756ad55310 67 uint16_t mag_z; // Intermediate variable
elwinong 8:ebec2e50d421 68 std::vector<uint8_t> data; // mDot data->send variable
elwinong 8:ebec2e50d421 69 char data_str[64]; // Intermediate conversion variable
elwinong 8:ebec2e50d421 70 uint32_t update_interval = 15000; // TTN transmission interval (loop interval)
gkielian 10:798cac7d3dcb 71 int16_t analog_input;
gkielian 10:798cac7d3dcb 72 //initialize pins for analog input
gkielian 10:798cac7d3dcb 73 AnalogIn ain(A0);
mfiore 2:6e2c378339d9 74
mfiore 0:09250cd371d2 75 // get a mDot handle
mfiore 0:09250cd371d2 76 dot = mDot::getInstance();
ropu 6:8f7276e7d206 77
ropu 6:8f7276e7d206 78 dot->resetConfig();
mfiore 0:09250cd371d2 79
ropu 7:609e7bb06486 80 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
ropu 7:609e7bb06486 81
ropu 7:609e7bb06486 82 // too lazzy to check all errors
ropu 6:8f7276e7d206 83 dot->setJoinMode(mDot::MANUAL);
ropu 6:8f7276e7d206 84 dot->setPublicNetwork(true);
ropu 6:8f7276e7d206 85 dot->setFrequencySubBand(config_frequency_sub_band);
ropu 6:8f7276e7d206 86 dot->setNetworkSessionKey(netowork_session_key);
ropu 6:8f7276e7d206 87 dot->setDataSessionKey(data_session_key);
ropu 6:8f7276e7d206 88 dot->setNetworkAddress(network_address);
mfiore 4:36e214ebfa56 89
mfiore 4:36e214ebfa56 90
mfiore 4:36e214ebfa56 91 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 92 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 93 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
mfiore 4:36e214ebfa56 94 logInfo("setting TX spreading factor");
mfiore 4:36e214ebfa56 95 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 96 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 97 }
mfiore 4:36e214ebfa56 98
ropu 6:8f7276e7d206 99
mfiore 4:36e214ebfa56 100 // request receive confirmation of packets from the gateway
mfiore 4:36e214ebfa56 101 logInfo("enabling ACKs");
ropu 6:8f7276e7d206 102 if ((ret = dot->setAck(0)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 103 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 104 }
mfiore 4:36e214ebfa56 105
mfiore 4:36e214ebfa56 106 // save this configuration to the mDot's NVM
mfiore 2:6e2c378339d9 107 logInfo("saving config");
mfiore 2:6e2c378339d9 108 if (! dot->saveConfig()) {
mfiore 2:6e2c378339d9 109 logError("failed to save configuration");
mfiore 0:09250cd371d2 110 }
mfiore 2:6e2c378339d9 111 //*******************************************
mfiore 2:6e2c378339d9 112 // end of configuration
mfiore 2:6e2c378339d9 113 //*******************************************
mfiore 0:09250cd371d2 114
mfiore 0:09250cd371d2 115 // attempt to join the network
mfiore 2:6e2c378339d9 116 logInfo("joining network");
mfiore 0:09250cd371d2 117 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 118 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 119 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 120 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 121 }
mfiore 0:09250cd371d2 122
elwinong 8:ebec2e50d421 123 while (true) {
elwinong 8:ebec2e50d421 124
gkielian 10:798cac7d3dcb 125 analog_input = ain;
gkielian 10:798cac7d3dcb 126 logInfo("analog input = %d", analog_input);
elwinong 8:ebec2e50d421 127
elwinong 8:ebec2e50d421 128
elwinong 8:ebec2e50d421 129 // Empty data vector
elwinong 8:ebec2e50d421 130 data.clear();
elwinong 8:ebec2e50d421 131
elwinong 8:ebec2e50d421 132 // Push temperature value into data array
gkielian 10:798cac7d3dcb 133 sprintf(data_str, "%d", analog_input);
elwinong 8:ebec2e50d421 134 for (int i = 0; i<strlen(data_str); i++)
elwinong 8:ebec2e50d421 135 {
elwinong 8:ebec2e50d421 136 data.push_back(((char*)data_str)[i]);
elwinong 8:ebec2e50d421 137 }
elwinong 8:ebec2e50d421 138
mfiore 4:36e214ebfa56 139 // send the data to the gateway
mfiore 0:09250cd371d2 140 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 141 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 142 } else {
elwinong 8:ebec2e50d421 143 logInfo("successfully sent data to gateway: %s", data_str);
mfiore 0:09250cd371d2 144 }
mfiore 0:09250cd371d2 145
mfiore 4:36e214ebfa56 146 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
elwinong 8:ebec2e50d421 147 osDelay(std::max(update_interval, (uint32_t)dot->getNextTxMs()));
elwinong 8:ebec2e50d421 148
mfiore 0:09250cd371d2 149 }
mfiore 0:09250cd371d2 150
mfiore 0:09250cd371d2 151 return 0;
mfiore 0:09250cd371d2 152 }