Arduino I2C temp sensor on mDot
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_example_TTN_connect by
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
main.cpp
- Committer:
- mfiore
- Date:
- 2015-06-24
- Revision:
- 0:09250cd371d2
- Child:
- 2:6e2c378339d9
File content as of revision 0:09250cd371d2:
#include "mbed.h" #include "mDot.h" #include <string> #include <vector> // these options must match the settings on your Conduit in // /var/config/lora/lora-network-server.conf static std::string config_network_name = "<lora network id>"; static std::string config_network_pass = "<lora network key>"; static uint8_t config_frequency_sub_band = 1; void log_error(mDot* dot, const char* msg, int32_t retval); int main() { int32_t ret; mDot* dot; std::vector<uint8_t> data; std::string data_str = "hello world!"; // get a mDot handle dot = mDot::getInstance(); // reset to default config so we know what state we're in dot->resetConfig(); // print library version information printf("version: %s\r\n", dot->getId().c_str()); // set up the mDot with our network information printf("setting frequency sub band\r\n"); if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { log_error(dot, "failed to set frequency sub band", ret); } printf("setting network name\r\n"); if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { log_error(dot, "failed to set network name", ret); } printf("setting network password\r\n"); if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { log_error(dot, "failed to set network password", ret); } // attempt to join the network printf("joining network\r\n"); while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { log_error(dot, "failed to join network", ret); wait(2); } // 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) { // send the data // ACKs are enabled by default, so we're expecting to get one back if ((ret = dot->send(data)) != mDot::MDOT_OK) { log_error(dot, "failed to send", ret); } else { printf("successfully sent data to gateway\r\n"); } wait(5); } return 0; } void log_error(mDot* dot, const char* msg, int32_t retval) { printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str()); }