Arduino I2C temp sensor on mDot

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_LoRa_example_TTN_connect by bruno rovagnati

This program connects the Adafruit temp sensor board MCP9808 through I2C and sends the temperature output to the The Things Network.

Based on:

Requiremens:

Wiring:

  • MCP9808 : UDK
    • Vdd : D8 3.3V
    • Gnd : D3 GND
    • SCL : D15
    • SDA : D14
Committer:
elwinong
Date:
Tue Aug 02 19:49:55 2016 +0000
Revision:
8:ebec2e50d421
Parent:
7:609e7bb06486
Arduino I2C to mDot example

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
elwinong 8:ebec2e50d421 34 static const uint8_t netowork_session_key_array[] = { 0x35, 0x41, 0x32, 0x67, 0x79, 0x76, 0x49, 0x8B, 0xBE, 0x98, 0x10, 0x80, 0x76, 0xB7, 0x61, 0x8B };
elwinong 8:ebec2e50d421 35 static const uint8_t data_session_key_array[] = { 0xD3, 0x6F, 0x5E, 0x66, 0x4A, 0x1B, 0xEC, 0x0C, 0x4A, 0x63, 0x8E, 0x1C, 0x2D, 0xB3, 0x18, 0xA4 };
elwinong 8:ebec2e50d421 36 static const uint8_t network_address_array[] = { 0x10, 0x81, 0xDA, 0x6F };
elwinong 8:ebec2e50d421 37
ropu 6:8f7276e7d206 38 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 39 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 40 static std::vector<uint8_t> network_address (network_address_array, network_address_array + sizeof(network_address_array) / sizeof(uint8_t));
elwinong 8:ebec2e50d421 41
elwinong 8:ebec2e50d421 42 static uint8_t config_frequency_sub_band = 2;
elwinong 8:ebec2e50d421 43
elwinong 8:ebec2e50d421 44 // Initialize I2C
elwinong 8:ebec2e50d421 45 I2C i2c(I2C_SDA , I2C_SCL );
elwinong 8:ebec2e50d421 46 const int addr7bit = 0x18; // 7 bit I2C address
elwinong 8:ebec2e50d421 47 const int addr8bit = addr7bit << 1; // 8bit I2C address, 0x90
elwinong 8:ebec2e50d421 48
mfiore 0:09250cd371d2 49
mfiore 0:09250cd371d2 50 int main() {
elwinong 8:ebec2e50d421 51
elwinong 8:ebec2e50d421 52 // Declare and initialize variables
mfiore 0:09250cd371d2 53 int32_t ret;
mfiore 0:09250cd371d2 54 mDot* dot;
elwinong 8:ebec2e50d421 55 char cmd[2]; // I2C command address byte 8-bit
elwinong 8:ebec2e50d421 56 char my_data[2]; // I2C return address bytes 16-bit
elwinong 8:ebec2e50d421 57 uint16_t amb; // Intermediate variable
elwinong 8:ebec2e50d421 58 std::vector<uint8_t> data; // mDot data->send variable
elwinong 8:ebec2e50d421 59 char data_str[64]; // Intermediate conversion variable
elwinong 8:ebec2e50d421 60 uint32_t update_interval = 15000; // TTN transmission interval (loop interval)
mfiore 2:6e2c378339d9 61
mfiore 0:09250cd371d2 62 // get a mDot handle
mfiore 0:09250cd371d2 63 dot = mDot::getInstance();
ropu 6:8f7276e7d206 64
ropu 6:8f7276e7d206 65 dot->resetConfig();
mfiore 0:09250cd371d2 66
ropu 7:609e7bb06486 67 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
ropu 7:609e7bb06486 68
ropu 7:609e7bb06486 69 // too lazzy to check all errors
ropu 6:8f7276e7d206 70 dot->setJoinMode(mDot::MANUAL);
ropu 6:8f7276e7d206 71 dot->setPublicNetwork(true);
ropu 6:8f7276e7d206 72 dot->setFrequencySubBand(config_frequency_sub_band);
ropu 6:8f7276e7d206 73 dot->setNetworkSessionKey(netowork_session_key);
ropu 6:8f7276e7d206 74 dot->setDataSessionKey(data_session_key);
ropu 6:8f7276e7d206 75 dot->setNetworkAddress(network_address);
mfiore 4:36e214ebfa56 76
mfiore 4:36e214ebfa56 77
mfiore 4:36e214ebfa56 78 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 79 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 80 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
mfiore 4:36e214ebfa56 81 logInfo("setting TX spreading factor");
mfiore 4:36e214ebfa56 82 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 83 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 84 }
mfiore 4:36e214ebfa56 85
ropu 6:8f7276e7d206 86
mfiore 4:36e214ebfa56 87 // request receive confirmation of packets from the gateway
mfiore 4:36e214ebfa56 88 logInfo("enabling ACKs");
ropu 6:8f7276e7d206 89 if ((ret = dot->setAck(0)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 90 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 91 }
mfiore 4:36e214ebfa56 92
mfiore 4:36e214ebfa56 93 // save this configuration to the mDot's NVM
mfiore 2:6e2c378339d9 94 logInfo("saving config");
mfiore 2:6e2c378339d9 95 if (! dot->saveConfig()) {
mfiore 2:6e2c378339d9 96 logError("failed to save configuration");
mfiore 0:09250cd371d2 97 }
mfiore 2:6e2c378339d9 98 //*******************************************
mfiore 2:6e2c378339d9 99 // end of configuration
mfiore 2:6e2c378339d9 100 //*******************************************
mfiore 0:09250cd371d2 101
mfiore 0:09250cd371d2 102 // attempt to join the network
mfiore 2:6e2c378339d9 103 logInfo("joining network");
mfiore 0:09250cd371d2 104 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 105 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 106 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 107 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 108 }
mfiore 0:09250cd371d2 109
elwinong 8:ebec2e50d421 110 while (true) {
elwinong 8:ebec2e50d421 111
elwinong 8:ebec2e50d421 112 // Wake temp sensor
elwinong 8:ebec2e50d421 113 cmd[0] = 0x01;
elwinong 8:ebec2e50d421 114 cmd[1] = 0x00;
elwinong 8:ebec2e50d421 115 i2c.write(addr8bit, cmd, 2);
elwinong 8:ebec2e50d421 116 logInfo("Wake temp sensor");
mfiore 0:09250cd371d2 117
elwinong 8:ebec2e50d421 118 // Read ambient temperature
elwinong 8:ebec2e50d421 119 cmd[0] = 0x05; // For list of MCP9808 commands and addresses, see https://github.com/adafruit/Adafruit_MCP9808_Library/blob/master/Adafruit_MCP9808.h
elwinong 8:ebec2e50d421 120 i2c.write(addr8bit, cmd, 1);
elwinong 8:ebec2e50d421 121 i2c.read( addr8bit, my_data, 2);
elwinong 8:ebec2e50d421 122
elwinong 8:ebec2e50d421 123 // This section is converted from Arduino version: https://github.com/adafruit/Adafruit_MCP9808_Library/blob/master/Adafruit_MCP9808.cpp
elwinong 8:ebec2e50d421 124 amb = my_data[0];
elwinong 8:ebec2e50d421 125 amb <<= 8;
elwinong 8:ebec2e50d421 126 amb |= my_data[1];
elwinong 8:ebec2e50d421 127
elwinong 8:ebec2e50d421 128 float tmpC = float(amb & 0x0FFF);
elwinong 8:ebec2e50d421 129 tmpC /= 16.0;
elwinong 8:ebec2e50d421 130 if (amb & 0x1000) tmpC -= 256;
elwinong 8:ebec2e50d421 131 float tmpF = (tmpC * 9.0 / 5.0 + 32.0);
elwinong 8:ebec2e50d421 132
elwinong 8:ebec2e50d421 133 logInfo("Temp = %4.2f*C \t\t %4.2f*F", tmpC, tmpF);
elwinong 8:ebec2e50d421 134
elwinong 8:ebec2e50d421 135 // Shutdown temp sensor
elwinong 8:ebec2e50d421 136 cmd[0] = 0x00;
elwinong 8:ebec2e50d421 137 cmd[1] = 0x00;
elwinong 8:ebec2e50d421 138 i2c.write(addr8bit, cmd, 2);
elwinong 8:ebec2e50d421 139 logInfo("Shutdown temp sensor\n");
elwinong 8:ebec2e50d421 140
elwinong 8:ebec2e50d421 141 // Empty data vector
elwinong 8:ebec2e50d421 142 data.clear();
elwinong 8:ebec2e50d421 143
elwinong 8:ebec2e50d421 144 // Push temperature value into data array
elwinong 8:ebec2e50d421 145 sprintf(data_str, "%4.2f", tmpF);
elwinong 8:ebec2e50d421 146 for (int i = 0; i<strlen(data_str); i++)
elwinong 8:ebec2e50d421 147 {
elwinong 8:ebec2e50d421 148 data.push_back(((char*)data_str)[i]);
elwinong 8:ebec2e50d421 149 }
elwinong 8:ebec2e50d421 150
mfiore 4:36e214ebfa56 151 // send the data to the gateway
mfiore 0:09250cd371d2 152 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 153 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 154 } else {
elwinong 8:ebec2e50d421 155 logInfo("successfully sent data to gateway: %s", data_str);
mfiore 0:09250cd371d2 156 }
mfiore 0:09250cd371d2 157
mfiore 4:36e214ebfa56 158 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
elwinong 8:ebec2e50d421 159 osDelay(std::max(update_interval, (uint32_t)dot->getNextTxMs()));
elwinong 8:ebec2e50d421 160
mfiore 0:09250cd371d2 161 }
mfiore 0:09250cd371d2 162
mfiore 0:09250cd371d2 163 return 0;
mfiore 0:09250cd371d2 164 }