Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_example_TTN_connect by
Revision 8:ebec2e50d421, committed 2016-08-02
- Comitter:
- elwinong
- Date:
- Tue Aug 02 19:49:55 2016 +0000
- Parent:
- 7:609e7bb06486
- Commit message:
- Arduino I2C to mDot example
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Nov 19 20:15:09 2015 +0000
+++ b/main.cpp Tue Aug 02 19:49:55 2016 +0000
@@ -1,25 +1,63 @@
+/* Adafruit I2C Temp Sensor MCP9808 -> Multitech mDot -> The Things Network (TTN)
+ *
+ * This program connects the Adafruit temp sensor board MCP9808 through I2C
+ * and sends the temperature output to the The Things Network.
+ *
+ * Based on:
+ * - mDot TTN Connect Example: https://developer.mbed.org/users/ropu/code/mDot_LoRa_example_TTN_connect/rev/609e7bb06486
+ * - Adafruit MCP9808 Library: https://github.com/adafruit/Adafruit_MCP9808_Library
+ * - mbed I2C Example: https://developer.mbed.org/handbook/I2C
+ * - TTN Backend: https://www.thethingsnetwork.org/wiki/Backend/Overview
+ *
+ * Requiremens:
+ * - Multitech UDK board and mDot: https://developer.mbed.org/platforms/MTS-mDot-F411/
+ * - Adafruit MCP9809: https://learn.adafruit.com/adafruit-mcp9808-precision-i2c-temperature-sensor-guide/overview
+ * - 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
+ * - Default serial port baudrate is 9600
+ *
+ * Wiring:
+ * MCP9808 : UDK
+ * - Vdd : - D8 3.3V
+ * - Gnd : - D3 GND
+ * - SCL : - D15
+ * - SDA : - D14
+ */
+
#include "mbed.h"
#include "mDot.h"
#include "MTSLog.h"
#include <string>
#include <vector>
#include <algorithm>
-// these options must match the settings on your Conduit
-// TTN Keys
-static const uint8_t netowork_session_key_array[] = {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
-static const uint8_t data_session_key_array[] = {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
-// uncomment the following lines and edit their values to match your configuration
-//static const uint8_t network_address_array[] = {0x02, 0x01, 0xBA, 0x01}; // use yours based on http://thethingsnetwork.org/wiki/AddressSpace
+
+// TTN Keys, setup your application and get your own keys at https://www.thethingsnetwork.org/wiki/Backend/Overview
+static const uint8_t netowork_session_key_array[] = { 0x35, 0x41, 0x32, 0x67, 0x79, 0x76, 0x49, 0x8B, 0xBE, 0x98, 0x10, 0x80, 0x76, 0xB7, 0x61, 0x8B };
+static const uint8_t data_session_key_array[] = { 0xD3, 0x6F, 0x5E, 0x66, 0x4A, 0x1B, 0xEC, 0x0C, 0x4A, 0x63, 0x8E, 0x1C, 0x2D, 0xB3, 0x18, 0xA4 };
+static const uint8_t network_address_array[] = { 0x10, 0x81, 0xDA, 0x6F };
+
static std::vector<uint8_t> netowork_session_key (netowork_session_key_array, netowork_session_key_array + sizeof(netowork_session_key_array) / sizeof(uint8_t));
static std::vector<uint8_t> data_session_key (data_session_key_array, data_session_key_array + sizeof(data_session_key_array) / sizeof(uint8_t));
static std::vector<uint8_t> network_address (network_address_array, network_address_array + sizeof(network_address_array) / sizeof(uint8_t));
-static uint8_t config_frequency_sub_band = 4;
+
+static uint8_t config_frequency_sub_band = 2;
+
+// Initialize I2C
+I2C i2c(I2C_SDA , I2C_SCL );
+const int addr7bit = 0x18; // 7 bit I2C address
+const int addr8bit = addr7bit << 1; // 8bit I2C address, 0x90
+
int main() {
+
+ // Declare and initialize variables
int32_t ret;
mDot* dot;
- std::vector<uint8_t> data;
- std::string data_str = "hello ropu!";
+ char cmd[2]; // I2C command address byte 8-bit
+ char my_data[2]; // I2C return address bytes 16-bit
+ uint16_t amb; // Intermediate variable
+ std::vector<uint8_t> data; // mDot data->send variable
+ char data_str[64]; // Intermediate conversion variable
+ uint32_t update_interval = 15000; // TTN transmission interval (loop interval)
// get a mDot handle
dot = mDot::getInstance();
@@ -46,7 +84,6 @@
}
-
// request receive confirmation of packets from the gateway
logInfo("enabling ACKs");
if ((ret = dot->setAck(0)) != mDot::MDOT_OK) {
@@ -70,20 +107,57 @@
osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
}
- // format data for sending to the gateway
- for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
- data.push_back((uint8_t) *it);
+ while (true) {
+
+ // Wake temp sensor
+ cmd[0] = 0x01;
+ cmd[1] = 0x00;
+ i2c.write(addr8bit, cmd, 2);
+ logInfo("Wake temp sensor");
- while (true) {
+ // Read ambient temperature
+ cmd[0] = 0x05; // For list of MCP9808 commands and addresses, see https://github.com/adafruit/Adafruit_MCP9808_Library/blob/master/Adafruit_MCP9808.h
+ i2c.write(addr8bit, cmd, 1);
+ i2c.read( addr8bit, my_data, 2);
+
+ // This section is converted from Arduino version: https://github.com/adafruit/Adafruit_MCP9808_Library/blob/master/Adafruit_MCP9808.cpp
+ amb = my_data[0];
+ amb <<= 8;
+ amb |= my_data[1];
+
+ float tmpC = float(amb & 0x0FFF);
+ tmpC /= 16.0;
+ if (amb & 0x1000) tmpC -= 256;
+ float tmpF = (tmpC * 9.0 / 5.0 + 32.0);
+
+ logInfo("Temp = %4.2f*C \t\t %4.2f*F", tmpC, tmpF);
+
+ // Shutdown temp sensor
+ cmd[0] = 0x00;
+ cmd[1] = 0x00;
+ i2c.write(addr8bit, cmd, 2);
+ logInfo("Shutdown temp sensor\n");
+
+ // Empty data vector
+ data.clear();
+
+ // Push temperature value into data array
+ sprintf(data_str, "%4.2f", tmpF);
+ for (int i = 0; i<strlen(data_str); i++)
+ {
+ data.push_back(((char*)data_str)[i]);
+ }
+
// send the data to the gateway
if ((ret = dot->send(data)) != mDot::MDOT_OK) {
logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
} else {
- logInfo("successfully sent data to gateway");
+ logInfo("successfully sent data to gateway: %s", data_str);
}
// in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
- osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
+ osDelay(std::max(update_interval, (uint32_t)dot->getNextTxMs()));
+
}
return 0;
