ROHM board with IBM BlueMix Quickstart for Avnet class
Fork of Dragonfly_Cellular_HTTPS_Example by
main.cpp@3:ff2bf7a1ece8, 2016-08-12 (annotated)
- Committer:
- BlueShadow
- Date:
- Fri Aug 12 18:16:13 2016 +0000
- Revision:
- 3:ff2bf7a1ece8
- Parent:
- 2:1df4b48824b1
- Child:
- 4:d16e07588838
Rohm ALS and Proximity are sent to BlueMix Quickstart
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BlueShadow | 2:1df4b48824b1 | 1 | /** Combination of MultiTech HTTPS Example using json with ROHM Sennsor board and IBM Bluemix |
BlueShadow | 2:1df4b48824b1 | 2 | * |
BlueShadow | 2:1df4b48824b1 | 3 | * Configures the Sensor board for Ambient Light, cellular radio, brings up the cellular link, |
BlueShadow | 2:1df4b48824b1 | 4 | * and does HTTPS POST requests. |
mfiore | 0:6ce1803b7a03 | 5 | * To do HTTPS requests with a certain server, the root certificate used to validate that server's certificate must be installed. See ssl_certificates.h for information on how to get the proper root certificate. |
mfiore | 0:6ce1803b7a03 | 6 | * |
BlueShadow | 2:1df4b48824b1 | 7 | * |
BlueShadow | 2:1df4b48824b1 | 8 | * |
BlueShadow | 2:1df4b48824b1 | 9 | * The following hardware is required to successfully run this program: |
BlueShadow | 2:1df4b48824b1 | 10 | * - MultiTech UDK2 (4" square white PCB with Arduino headers, antenna |
BlueShadow | 2:1df4b48824b1 | 11 | * connector, micro USB ports, and 40-pin connector for Dragonfly) |
BlueShadow | 2:1df4b48824b1 | 12 | * - MultiTech Dragonfly (1"x2" green PCB with Telit radio) |
BlueShadow | 2:1df4b48824b1 | 13 | * - Rohm Electronics Sensor Board |
BlueShadow | 2:1df4b48824b1 | 14 | * - Expansion board (LSM6DS0 |
BlueShadow | 2:1df4b48824b1 | 15 | * 3-axis accelerometer + 3-axis gyroscope, LIS3MDL 3-axis |
BlueShadow | 2:1df4b48824b1 | 16 | * magnetometer, HTS221 humidity and temperature sensor and LPS25HB |
BlueShadow | 2:1df4b48824b1 | 17 | * pressure sensor) |
BlueShadow | 2:1df4b48824b1 | 18 | * |
BlueShadow | 2:1df4b48824b1 | 19 | * What this program does: |
BlueShadow | 2:1df4b48824b1 | 20 | * - reads data from all sensors on board |
BlueShadow | 2:1df4b48824b1 | 21 | * - prints all sensor data to debug port on a periodic basis |
BlueShadow | 2:1df4b48824b1 | 22 | * - optionally send data to BlueMix |
BlueShadow | 2:1df4b48824b1 | 23 | * - All data is sent to a specific location determined by the student login. |
BlueShadow | 2:1df4b48824b1 | 24 | * - BlueMix cloud platform (user must create own account and configure a device |
BlueShadow | 2:1df4b48824b1 | 25 | * - you need to set the "VENDOR" and "MODEL" |
BlueShadow | 2:1df4b48824b1 | 26 | |
BlueShadow | 2:1df4b48824b1 | 27 | * - you need to set the "do_cloud_post" flag to true for this to |
BlueShadow | 2:1df4b48824b1 | 28 | * work |
BlueShadow | 2:1df4b48824b1 | 29 | * |
BlueShadow | 2:1df4b48824b1 | 30 | * Setup: |
BlueShadow | 2:1df4b48824b1 | 31 | * - Correctly insert SIM card into Dragonfly |
BlueShadow | 2:1df4b48824b1 | 32 | * - Seat the Dragonfly on the UDK2 board |
BlueShadow | 2:1df4b48824b1 | 33 | * - Connect an antenna to the connector on the Dragonfly labled "M" |
BlueShadow | 2:1df4b48824b1 | 34 | * - Stack the Base Shield on the UDK2 Arduino headers |
BlueShadow | 2:1df4b48824b1 | 35 | * - Stack the MEMs board on top of the Base Shield |
BlueShadow | 2:1df4b48824b1 | 36 | * - Plug in the power cable |
BlueShadow | 2:1df4b48824b1 | 37 | * - Plug a micro USB cable into the port below and slightly to the |
BlueShadow | 2:1df4b48824b1 | 38 | * left of the Dragonfly (NOT the port on the Dragonfly) |
BlueShadow | 2:1df4b48824b1 | 39 | * |
BlueShadow | 2:1df4b48824b1 | 40 | * Go have fun and make something cool! |
BlueShadow | 2:1df4b48824b1 | 41 | * |
BlueShadow | 2:1df4b48824b1 | 42 | ************************************************************************/ |
BlueShadow | 2:1df4b48824b1 | 43 | /* |
BlueShadow | 2:1df4b48824b1 | 44 | Sample Program Description: |
BlueShadow | 2:1df4b48824b1 | 45 | This Program will enable to Multi-Tech Dragonfly platform to utilize ROHM's Multi-sensor Shield Board. |
BlueShadow | 2:1df4b48824b1 | 46 | This program will initialize all sensors on the shield and then read back the sensor data. |
BlueShadow | 2:1df4b48824b1 | 47 | Data will then be output to the UART Debug Terminal every 1 second. |
BlueShadow | 2:1df4b48824b1 | 48 | |
BlueShadow | 2:1df4b48824b1 | 49 | Sample Program Author: |
BlueShadow | 2:1df4b48824b1 | 50 | ROHM USDC |
BlueShadow | 2:1df4b48824b1 | 51 | |
BlueShadow | 2:1df4b48824b1 | 52 | Additional Resources: |
BlueShadow | 2:1df4b48824b1 | 53 | ROHM Sensor Shield GitHub Repository: https://github.com/ROHMUSDC/ROHM_SensorPlatform_Multi-Sensor-Shield |
mfiore | 0:6ce1803b7a03 | 54 | * NOTE: This example changes the baud rate of the debug port to 115200 baud! |
mfiore | 0:6ce1803b7a03 | 55 | */ |
mfiore | 0:6ce1803b7a03 | 56 | |
mfiore | 0:6ce1803b7a03 | 57 | #include "mbed.h" |
mfiore | 0:6ce1803b7a03 | 58 | #include "mtsas.h" |
mfiore | 0:6ce1803b7a03 | 59 | #include "ssl_certificates.h" |
BlueShadow | 2:1df4b48824b1 | 60 | #include <string> // added for string manipulation |
BlueShadow | 2:1df4b48824b1 | 61 | #include <sstream> |
BlueShadow | 2:1df4b48824b1 | 62 | |
BlueShadow | 2:1df4b48824b1 | 63 | |
BlueShadow | 2:1df4b48824b1 | 64 | |
BlueShadow | 2:1df4b48824b1 | 65 | //-------- Customise these values ----------- |
BlueShadow | 2:1df4b48824b1 | 66 | |
BlueShadow | 2:1df4b48824b1 | 67 | #define ORG "quickstart" // your organization or "quickstart" |
BlueShadow | 2:1df4b48824b1 | 68 | #define DEVICE_TYPE "dragonflytype" // use this default for quickstart or customize to your registered device type |
BlueShadow | 2:1df4b48824b1 | 69 | #define DEVICE_ID "dragonfly01" // use this default for quickstart or customize to your registered device id |
BlueShadow | 2:1df4b48824b1 | 70 | #define TOKEN "pauljaeger" // not used with "quickstart" |
BlueShadow | 2:1df4b48824b1 | 71 | #define EVENT "myEvent" // use this default or customize to your event type |
BlueShadow | 2:1df4b48824b1 | 72 | //-------- Customise the above values -------- |
BlueShadow | 2:1df4b48824b1 | 73 | |
BlueShadow | 2:1df4b48824b1 | 74 | |
BlueShadow | 2:1df4b48824b1 | 75 | //char BlueMix_HEADER[] = "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n"; |
BlueShadow | 2:1df4b48824b1 | 76 | char BlueMix_HEADER[] = "Content-Type: application/json"; |
BlueShadow | 2:1df4b48824b1 | 77 | |
BlueShadow | 2:1df4b48824b1 | 78 | const char BlueMix_URL[] = "http://" ORG ".internetofthings.ibmcloud.com/api/v0002/device/types/" DEVICE_TYPE "/devices/" DEVICE_ID "/events/" EVENT; |
BlueShadow | 2:1df4b48824b1 | 79 | // replace typeID and deviceId with "" and delete () |
BlueShadow | 2:1df4b48824b1 | 80 | // replace $eventId = IBM will define in class. |
BlueShadow | 2:1df4b48824b1 | 81 | |
BlueShadow | 2:1df4b48824b1 | 82 | |
BlueShadow | 2:1df4b48824b1 | 83 | //***************************************************************************************************************************************************** |
BlueShadow | 2:1df4b48824b1 | 84 | DigitalOut Led1Out(LED1); |
BlueShadow | 2:1df4b48824b1 | 85 | |
mfiore | 0:6ce1803b7a03 | 86 | |
mfiore | 1:26b8af61d0ac | 87 | // This line controls the regulator's battery charger. |
mfiore | 1:26b8af61d0ac | 88 | // BC_NCE = 0 enables the battery charger |
mfiore | 1:26b8af61d0ac | 89 | // BC_NCE = 1 disables the battery charger |
mfiore | 1:26b8af61d0ac | 90 | DigitalOut bc_nce(PB_2); |
mfiore | 1:26b8af61d0ac | 91 | |
mfiore | 0:6ce1803b7a03 | 92 | bool init_mtsas(); |
mfiore | 0:6ce1803b7a03 | 93 | char* httpResToStr(HTTPResult res); |
mfiore | 0:6ce1803b7a03 | 94 | |
mfiore | 0:6ce1803b7a03 | 95 | // The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio. |
mfiore | 0:6ce1803b7a03 | 96 | mts::MTSSerialFlowControl* io; |
mfiore | 0:6ce1803b7a03 | 97 | // The Cellular object represents the cellular radio. |
mfiore | 0:6ce1803b7a03 | 98 | mts::Cellular* radio; |
mfiore | 0:6ce1803b7a03 | 99 | |
mfiore | 0:6ce1803b7a03 | 100 | // An APN is required for GSM radios. |
BlueShadow | 2:1df4b48824b1 | 101 | static const char apn[] = "iot.aer.net"; |
mfiore | 0:6ce1803b7a03 | 102 | |
mfiore | 0:6ce1803b7a03 | 103 | bool radio_ok = false; |
mfiore | 0:6ce1803b7a03 | 104 | |
BlueShadow | 2:1df4b48824b1 | 105 | |
BlueShadow | 2:1df4b48824b1 | 106 | #define RPR0521 //RPR0521 Ambient Light Sensor. |
BlueShadow | 2:1df4b48824b1 | 107 | //Define Pins for I2C Interface |
BlueShadow | 2:1df4b48824b1 | 108 | I2C i2c(I2C_SDA, I2C_SCL); |
BlueShadow | 2:1df4b48824b1 | 109 | bool RepStart = true; |
BlueShadow | 2:1df4b48824b1 | 110 | bool NoRepStart = false; |
BlueShadow | 2:1df4b48824b1 | 111 | |
BlueShadow | 2:1df4b48824b1 | 112 | #ifdef RPR0521 |
BlueShadow | 2:1df4b48824b1 | 113 | int RPR0521_addr_w = 0x70; //7bit addr = 0x38, with write bit 0 |
BlueShadow | 2:1df4b48824b1 | 114 | int RPR0521_addr_r = 0x71; //7bit addr = 0x38, with read bit 1 |
BlueShadow | 2:1df4b48824b1 | 115 | char RPR0521_ModeControl[2] = {0x41, 0xE6}; |
BlueShadow | 2:1df4b48824b1 | 116 | char RPR0521_ALSPSControl[2] = {0x42, 0x03}; |
BlueShadow | 2:1df4b48824b1 | 117 | char RPR0521_Persist[2] = {0x43, 0x20}; |
BlueShadow | 2:1df4b48824b1 | 118 | char RPR0521_Addr_ReadData = 0x44; |
BlueShadow | 2:1df4b48824b1 | 119 | char RPR0521_Content_ReadData[6]; |
BlueShadow | 2:1df4b48824b1 | 120 | int RPR0521_PS_RAWOUT = 0; //this is an output |
BlueShadow | 2:1df4b48824b1 | 121 | float RPR0521_PS_OUT = 0; |
BlueShadow | 2:1df4b48824b1 | 122 | int RPR0521_ALS_D0_RAWOUT = 0; |
BlueShadow | 2:1df4b48824b1 | 123 | int RPR0521_ALS_D1_RAWOUT = 0; |
BlueShadow | 2:1df4b48824b1 | 124 | float RPR0521_ALS_DataRatio = 0; |
BlueShadow | 2:1df4b48824b1 | 125 | float RPR0521_ALS_OUT = 0; //this is an output |
BlueShadow | 2:1df4b48824b1 | 126 | float RPR0521_ALS[2]; // is this ok taking an int to the [0] value and float to [1]??????????? |
BlueShadow | 2:1df4b48824b1 | 127 | #endif |
BlueShadow | 2:1df4b48824b1 | 128 | |
BlueShadow | 2:1df4b48824b1 | 129 | bool init_mtsas(); |
BlueShadow | 2:1df4b48824b1 | 130 | void ReadRPR0521_ALS (); |
BlueShadow | 2:1df4b48824b1 | 131 | char* httpResToStr(HTTPResult res); |
BlueShadow | 2:1df4b48824b1 | 132 | |
BlueShadow | 2:1df4b48824b1 | 133 | |
BlueShadow | 2:1df4b48824b1 | 134 | namespace patch // I have no idea why this is in the code ********************* |
BlueShadow | 2:1df4b48824b1 | 135 | { |
BlueShadow | 2:1df4b48824b1 | 136 | template < typename T > std::string to_string( const T& n ) |
BlueShadow | 2:1df4b48824b1 | 137 | { |
BlueShadow | 2:1df4b48824b1 | 138 | std::ostringstream stm ; |
BlueShadow | 2:1df4b48824b1 | 139 | stm << n ; |
BlueShadow | 2:1df4b48824b1 | 140 | return stm.str() ; |
BlueShadow | 2:1df4b48824b1 | 141 | } |
BlueShadow | 2:1df4b48824b1 | 142 | } |
BlueShadow | 2:1df4b48824b1 | 143 | |
BlueShadow | 2:1df4b48824b1 | 144 | |
BlueShadow | 2:1df4b48824b1 | 145 | /**************************************************************************************************** |
BlueShadow | 2:1df4b48824b1 | 146 | // main |
BlueShadow | 2:1df4b48824b1 | 147 | ****************************************************************************************************/ |
BlueShadow | 2:1df4b48824b1 | 148 | |
BlueShadow | 2:1df4b48824b1 | 149 | int main() |
BlueShadow | 2:1df4b48824b1 | 150 | { |
mfiore | 1:26b8af61d0ac | 151 | // Disable the battery charger unless a battery is attached. |
mfiore | 1:26b8af61d0ac | 152 | bc_nce = 1; |
BlueShadow | 2:1df4b48824b1 | 153 | |
BlueShadow | 3:ff2bf7a1ece8 | 154 | static int post_interval_ms = 500; //************* I don't want to wait 30 seconds ************************/ |
BlueShadow | 2:1df4b48824b1 | 155 | |
mfiore | 0:6ce1803b7a03 | 156 | // Change the baud rate of the debug port from the default 9600 to 115200. |
mfiore | 0:6ce1803b7a03 | 157 | Serial debug(USBTX, USBRX); |
BlueShadow | 2:1df4b48824b1 | 158 | |
mfiore | 0:6ce1803b7a03 | 159 | debug.baud(115200); |
BlueShadow | 2:1df4b48824b1 | 160 | |
mfiore | 0:6ce1803b7a03 | 161 | //Sets the log level to INFO, higher log levels produce more log output. |
mfiore | 0:6ce1803b7a03 | 162 | //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE |
BlueShadow | 2:1df4b48824b1 | 163 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
BlueShadow | 2:1df4b48824b1 | 164 | |
BlueShadow | 2:1df4b48824b1 | 165 | |
BlueShadow | 2:1df4b48824b1 | 166 | |
BlueShadow | 2:1df4b48824b1 | 167 | //**************************************************************************************************** |
BlueShadow | 2:1df4b48824b1 | 168 | // Initialize I2C Devices ************ |
BlueShadow | 2:1df4b48824b1 | 169 | //****************************************************************************************************/ |
BlueShadow | 2:1df4b48824b1 | 170 | |
BlueShadow | 2:1df4b48824b1 | 171 | #ifdef RPR0521 |
BlueShadow | 2:1df4b48824b1 | 172 | i2c.write(RPR0521_addr_w, &RPR0521_ModeControl[0], 2, false); |
BlueShadow | 2:1df4b48824b1 | 173 | i2c.write(RPR0521_addr_w, &RPR0521_ALSPSControl[0], 2, false); |
BlueShadow | 2:1df4b48824b1 | 174 | i2c.write(RPR0521_addr_w, &RPR0521_Persist[0], 2, false); |
BlueShadow | 2:1df4b48824b1 | 175 | #endif |
BlueShadow | 2:1df4b48824b1 | 176 | |
BlueShadow | 2:1df4b48824b1 | 177 | //****************************************************************************************************/ |
BlueShadow | 2:1df4b48824b1 | 178 | // Initialization Radio Section ********************************************************** |
BlueShadow | 2:1df4b48824b1 | 179 | //****************************************************************************************************/ |
mfiore | 0:6ce1803b7a03 | 180 | logInfo("initializing cellular radio"); |
mfiore | 0:6ce1803b7a03 | 181 | radio_ok = init_mtsas(); |
mfiore | 0:6ce1803b7a03 | 182 | if (! radio_ok) { |
mfiore | 0:6ce1803b7a03 | 183 | while (true) { |
mfiore | 0:6ce1803b7a03 | 184 | logError("failed to initialize cellular radio"); |
mfiore | 0:6ce1803b7a03 | 185 | wait(1); |
mfiore | 0:6ce1803b7a03 | 186 | } |
mfiore | 0:6ce1803b7a03 | 187 | } |
BlueShadow | 2:1df4b48824b1 | 188 | |
BlueShadow | 2:1df4b48824b1 | 189 | |
mfiore | 0:6ce1803b7a03 | 190 | logInfo("setting APN"); |
mfiore | 0:6ce1803b7a03 | 191 | if (radio->setApn(apn) != MTS_SUCCESS) |
mfiore | 0:6ce1803b7a03 | 192 | logError("failed to set APN to \"%s\"", apn); |
BlueShadow | 2:1df4b48824b1 | 193 | logInfo("APN set successful"); |
BlueShadow | 2:1df4b48824b1 | 194 | |
BlueShadow | 2:1df4b48824b1 | 195 | Timer post_timer; |
BlueShadow | 2:1df4b48824b1 | 196 | post_timer.start(); |
BlueShadow | 2:1df4b48824b1 | 197 | int timeStamp; |
BlueShadow | 3:ff2bf7a1ece8 | 198 | int countingLoop = 0; |
BlueShadow | 3:ff2bf7a1ece8 | 199 | |
BlueShadow | 2:1df4b48824b1 | 200 | logInfo("Entering loop"); |
BlueShadow | 2:1df4b48824b1 | 201 | while (true) { |
BlueShadow | 2:1df4b48824b1 | 202 | if (post_timer.read_ms() > post_interval_ms ) { // can this be changed to seconds? |
BlueShadow | 2:1df4b48824b1 | 203 | timeStamp = post_timer.read_ms(); |
BlueShadow | 2:1df4b48824b1 | 204 | logDebug("timer read %d", timeStamp); |
BlueShadow | 2:1df4b48824b1 | 205 | logDebug("timer value %d", post_interval_ms ); |
BlueShadow | 3:ff2bf7a1ece8 | 206 | logDebug("loop count value %d", countingLoop ); |
BlueShadow | 2:1df4b48824b1 | 207 | |
BlueShadow | 2:1df4b48824b1 | 208 | #ifdef RPR0521 //als digital |
BlueShadow | 2:1df4b48824b1 | 209 | ReadRPR0521_ALS (); |
BlueShadow | 2:1df4b48824b1 | 210 | int sensor_data = RPR0521_ALS[0]; |
BlueShadow | 2:1df4b48824b1 | 211 | int proximity_data = RPR0521_ALS[1]; |
BlueShadow | 2:1df4b48824b1 | 212 | #else |
BlueShadow | 2:1df4b48824b1 | 213 | int sensor_data = -1; |
BlueShadow | 2:1df4b48824b1 | 214 | int proximity_data = -1; |
BlueShadow | 2:1df4b48824b1 | 215 | #endif |
BlueShadow | 3:ff2bf7a1ece8 | 216 | logDebug("\r\nPosting Light Reading: %d Proximity Reading: %d",sensor_data,proximity_data); |
BlueShadow | 3:ff2bf7a1ece8 | 217 | |
BlueShadow | 3:ff2bf7a1ece8 | 218 | logDebug("https://quickstart.internetofthings.ibmcloud.com"); |
BlueShadow | 3:ff2bf7a1ece8 | 219 | |
BlueShadow | 2:1df4b48824b1 | 220 | // http_tx.clear(); |
BlueShadow | 2:1df4b48824b1 | 221 | |
BlueShadow | 2:1df4b48824b1 | 222 | logInfo("bringing up the link"); |
BlueShadow | 2:1df4b48824b1 | 223 | if (! radio->connect()) { |
BlueShadow | 2:1df4b48824b1 | 224 | logError("failed to bring up the link"); |
BlueShadow | 2:1df4b48824b1 | 225 | //return 0; |
BlueShadow | 2:1df4b48824b1 | 226 | } else { |
BlueShadow | 2:1df4b48824b1 | 227 | |
BlueShadow | 2:1df4b48824b1 | 228 | // HTTPClient object used for HTTP requests. |
BlueShadow | 2:1df4b48824b1 | 229 | HTTPClient http; |
BlueShadow | 2:1df4b48824b1 | 230 | |
BlueShadow | 2:1df4b48824b1 | 231 | // Enable strict certificate validation. |
BlueShadow | 2:1df4b48824b1 | 232 | http.setPeerVerification(VERIFY_PEER); |
BlueShadow | 2:1df4b48824b1 | 233 | |
BlueShadow | 2:1df4b48824b1 | 234 | // Load certificates defined in ssl_certificates.h. |
BlueShadow | 2:1df4b48824b1 | 235 | // See comments in ssl_certificates.h for information on how to get and format root certificates. |
BlueShadow | 2:1df4b48824b1 | 236 | if (http.addRootCACertificate(ssl_certificates) != HTTP_OK) |
BlueShadow | 2:1df4b48824b1 | 237 | logError("loading SSL certificates failed"); |
BlueShadow | 2:1df4b48824b1 | 238 | |
BlueShadow | 2:1df4b48824b1 | 239 | // HTTP POST example - QUickstart |
BlueShadow | 2:1df4b48824b1 | 240 | |
BlueShadow | 2:1df4b48824b1 | 241 | char http_rx_buf[1024]; |
BlueShadow | 2:1df4b48824b1 | 242 | char http_tx_buf[1024]; |
BlueShadow | 2:1df4b48824b1 | 243 | |
BlueShadow | 2:1df4b48824b1 | 244 | memset(http_tx_buf, 0, sizeof(http_tx_buf)); |
BlueShadow | 2:1df4b48824b1 | 245 | memset(http_rx_buf, 0, sizeof(http_rx_buf)); |
BlueShadow | 3:ff2bf7a1ece8 | 246 | snprintf(http_tx_buf, sizeof(http_tx_buf), "{ \"Light\": \"%d\" , \"Proximity\": \"%d\" }", sensor_data,proximity_data); |
BlueShadow | 2:1df4b48824b1 | 247 | logDebug("%s",http_tx_buf); |
BlueShadow | 2:1df4b48824b1 | 248 | HTTPResult res; |
BlueShadow | 2:1df4b48824b1 | 249 | |
BlueShadow | 2:1df4b48824b1 | 250 | // IHTTPDataIn object - will contain data received from server. |
BlueShadow | 2:1df4b48824b1 | 251 | HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf)); |
BlueShadow | 2:1df4b48824b1 | 252 | |
BlueShadow | 2:1df4b48824b1 | 253 | // IHTTPDataOut object - contains data to be posted to server. |
BlueShadow | 2:1df4b48824b1 | 254 | // HTTPJson automatically adds the JSON content-type header to the request. |
BlueShadow | 2:1df4b48824b1 | 255 | HTTPJson http_tx(http_tx_buf, strlen(http_tx_buf)+1); |
BlueShadow | 2:1df4b48824b1 | 256 | |
BlueShadow | 2:1df4b48824b1 | 257 | // Make a HTTP POST request to http://httpbin.org/ |
BlueShadow | 3:ff2bf7a1ece8 | 258 | res = http.post("http://quickstart.internetofthings.ibmcloud.com/api/v0002/device/types/dragonflytype/devices/dragonfly02/events/myEvent", http_tx, &http_rx); |
BlueShadow | 2:1df4b48824b1 | 259 | if (res != HTTP_OK) |
BlueShadow | 2:1df4b48824b1 | 260 | logError("HTTPS POST to Bluemix failed [%d][%s]", res, httpResToStr(res)); |
BlueShadow | 2:1df4b48824b1 | 261 | else |
BlueShadow | 2:1df4b48824b1 | 262 | logInfo("HTTPS POST to Bluemix succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf); |
BlueShadow | 2:1df4b48824b1 | 263 | |
BlueShadow | 2:1df4b48824b1 | 264 | //logInfo("finished - bringing down link"); |
BlueShadow | 2:1df4b48824b1 | 265 | radio->disconnect(); |
BlueShadow | 2:1df4b48824b1 | 266 | post_timer.reset(); |
BlueShadow | 3:ff2bf7a1ece8 | 267 | countingLoop +=1; |
BlueShadow | 2:1df4b48824b1 | 268 | } |
mfiore | 0:6ce1803b7a03 | 269 | } |
BlueShadow | 2:1df4b48824b1 | 270 | |
BlueShadow | 2:1df4b48824b1 | 271 | //return 0; |
mfiore | 0:6ce1803b7a03 | 272 | } |
BlueShadow | 2:1df4b48824b1 | 273 | //radio->disconnect(); |
mfiore | 0:6ce1803b7a03 | 274 | } |
mfiore | 0:6ce1803b7a03 | 275 | |
BlueShadow | 2:1df4b48824b1 | 276 | bool init_mtsas() |
BlueShadow | 2:1df4b48824b1 | 277 | { |
mfiore | 0:6ce1803b7a03 | 278 | io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS); |
mfiore | 0:6ce1803b7a03 | 279 | if (! io) |
mfiore | 0:6ce1803b7a03 | 280 | return false; |
BlueShadow | 2:1df4b48824b1 | 281 | |
mfiore | 0:6ce1803b7a03 | 282 | // radio default baud rate is 115200 |
mfiore | 0:6ce1803b7a03 | 283 | io->baud(115200); |
mfiore | 0:6ce1803b7a03 | 284 | radio = mts::CellularFactory::create(io); |
mfiore | 0:6ce1803b7a03 | 285 | if (! radio) |
mfiore | 0:6ce1803b7a03 | 286 | return false; |
BlueShadow | 2:1df4b48824b1 | 287 | |
mfiore | 0:6ce1803b7a03 | 288 | // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created |
mfiore | 0:6ce1803b7a03 | 289 | Transport::setTransport(radio); |
BlueShadow | 2:1df4b48824b1 | 290 | |
mfiore | 0:6ce1803b7a03 | 291 | return true; |
mfiore | 0:6ce1803b7a03 | 292 | } |
mfiore | 0:6ce1803b7a03 | 293 | |
BlueShadow | 2:1df4b48824b1 | 294 | char* httpResToStr(HTTPResult res) |
BlueShadow | 2:1df4b48824b1 | 295 | { |
mfiore | 0:6ce1803b7a03 | 296 | switch(res) { |
mfiore | 0:6ce1803b7a03 | 297 | case HTTP_PROCESSING: |
mfiore | 0:6ce1803b7a03 | 298 | return "HTTP_PROCESSING"; |
mfiore | 0:6ce1803b7a03 | 299 | case HTTP_PARSE: |
mfiore | 0:6ce1803b7a03 | 300 | return "HTTP_PARSE"; |
mfiore | 0:6ce1803b7a03 | 301 | case HTTP_DNS: |
mfiore | 0:6ce1803b7a03 | 302 | return "HTTP_DNS"; |
mfiore | 0:6ce1803b7a03 | 303 | case HTTP_PRTCL: |
mfiore | 0:6ce1803b7a03 | 304 | return "HTTP_PRTCL"; |
mfiore | 0:6ce1803b7a03 | 305 | case HTTP_NOTFOUND: |
mfiore | 0:6ce1803b7a03 | 306 | return "HTTP_NOTFOUND"; |
mfiore | 0:6ce1803b7a03 | 307 | case HTTP_REFUSED: |
mfiore | 0:6ce1803b7a03 | 308 | return "HTTP_REFUSED"; |
mfiore | 0:6ce1803b7a03 | 309 | case HTTP_ERROR: |
mfiore | 0:6ce1803b7a03 | 310 | return "HTTP_ERROR"; |
mfiore | 0:6ce1803b7a03 | 311 | case HTTP_TIMEOUT: |
mfiore | 0:6ce1803b7a03 | 312 | return "HTTP_TIMEOUT"; |
mfiore | 0:6ce1803b7a03 | 313 | case HTTP_CONN: |
mfiore | 0:6ce1803b7a03 | 314 | return "HTTP_CONN"; |
mfiore | 0:6ce1803b7a03 | 315 | case HTTP_CLOSED: |
mfiore | 0:6ce1803b7a03 | 316 | return "HTTP_CLOSED"; |
mfiore | 0:6ce1803b7a03 | 317 | case HTTP_REDIRECT: |
mfiore | 0:6ce1803b7a03 | 318 | return "HTTP_REDIRECT"; |
mfiore | 0:6ce1803b7a03 | 319 | case HTTP_OK: |
mfiore | 0:6ce1803b7a03 | 320 | return "HTTP_OK"; |
mfiore | 0:6ce1803b7a03 | 321 | default: |
mfiore | 0:6ce1803b7a03 | 322 | return "HTTP Result unknown"; |
mfiore | 0:6ce1803b7a03 | 323 | } |
mfiore | 0:6ce1803b7a03 | 324 | } |
BlueShadow | 2:1df4b48824b1 | 325 | |
BlueShadow | 2:1df4b48824b1 | 326 | |
BlueShadow | 2:1df4b48824b1 | 327 | #ifdef RPR0521 //als digital |
BlueShadow | 2:1df4b48824b1 | 328 | void ReadRPR0521_ALS () |
BlueShadow | 2:1df4b48824b1 | 329 | { |
BlueShadow | 2:1df4b48824b1 | 330 | i2c.write(RPR0521_addr_w, &RPR0521_Addr_ReadData, 1, RepStart); |
BlueShadow | 2:1df4b48824b1 | 331 | i2c.read(RPR0521_addr_r, &RPR0521_Content_ReadData[0], 6, NoRepStart); |
BlueShadow | 2:1df4b48824b1 | 332 | |
BlueShadow | 2:1df4b48824b1 | 333 | RPR0521_ALS[1] = (RPR0521_Content_ReadData[1]<<8) | (RPR0521_Content_ReadData[0]); |
BlueShadow | 2:1df4b48824b1 | 334 | RPR0521_ALS_D0_RAWOUT = (RPR0521_Content_ReadData[3]<<8) | (RPR0521_Content_ReadData[2]); |
BlueShadow | 2:1df4b48824b1 | 335 | RPR0521_ALS_D1_RAWOUT = (RPR0521_Content_ReadData[5]<<8) | (RPR0521_Content_ReadData[4]); |
BlueShadow | 2:1df4b48824b1 | 336 | RPR0521_ALS_DataRatio = (float)RPR0521_ALS_D1_RAWOUT / (float)RPR0521_ALS_D0_RAWOUT; |
BlueShadow | 2:1df4b48824b1 | 337 | |
BlueShadow | 2:1df4b48824b1 | 338 | if(RPR0521_ALS_DataRatio < (float)0.595) { |
BlueShadow | 2:1df4b48824b1 | 339 | RPR0521_ALS[0] = ((float)1.682*(float)RPR0521_ALS_D0_RAWOUT - (float)1.877*(float)RPR0521_ALS_D1_RAWOUT); |
BlueShadow | 2:1df4b48824b1 | 340 | } else if(RPR0521_ALS_DataRatio < (float)1.015) { |
BlueShadow | 2:1df4b48824b1 | 341 | RPR0521_ALS[0] = ((float)0.644*(float)RPR0521_ALS_D0_RAWOUT - (float)0.132*(float)RPR0521_ALS_D1_RAWOUT); |
BlueShadow | 2:1df4b48824b1 | 342 | } else if(RPR0521_ALS_DataRatio < (float)1.352) { |
BlueShadow | 2:1df4b48824b1 | 343 | RPR0521_ALS[0] = ((float)0.756*(float)RPR0521_ALS_D0_RAWOUT - (float)0.243*(float)RPR0521_ALS_D1_RAWOUT); |
BlueShadow | 2:1df4b48824b1 | 344 | } else if(RPR0521_ALS_DataRatio < (float)3.053) { |
BlueShadow | 2:1df4b48824b1 | 345 | RPR0521_ALS[0] = ((float)0.766*(float)RPR0521_ALS_D0_RAWOUT - (float)0.25*(float)RPR0521_ALS_D1_RAWOUT); |
BlueShadow | 2:1df4b48824b1 | 346 | } else { |
BlueShadow | 2:1df4b48824b1 | 347 | RPR0521_ALS[0] = 0; |
BlueShadow | 2:1df4b48824b1 | 348 | } |
BlueShadow | 2:1df4b48824b1 | 349 | logDebug("RPR-0521 ALS/PROX Sensor Data:\r\n"); |
BlueShadow | 2:1df4b48824b1 | 350 | logDebug(" ALS = %0.2f lx\r\n", RPR0521_ALS[0]); |
BlueShadow | 2:1df4b48824b1 | 351 | logDebug(" PROX= %0.2f ADC Counts\r\n", RPR0521_ALS[1]); //defined as a float but is an unsigned. |
BlueShadow | 2:1df4b48824b1 | 352 | |
BlueShadow | 2:1df4b48824b1 | 353 | } |
BlueShadow | 2:1df4b48824b1 | 354 | #endif |