Initial Program for MultiTech Dragonfly to communicate to PTC ThingWorx. Sensors are from Rohm version1

Dependencies:   MbedJSONValue mbed mtsas

Fork of UUU_MultiTech_Dragonfly_Sprint by Paul Jaeger

Revision:
9:6475e1b83491
Parent:
8:e78dcfad254b
Child:
10:1c44f9375dd2
--- a/main.cpp	Fri Apr 15 21:31:39 2016 +0000
+++ b/main.cpp	Fri Apr 15 22:29:23 2016 +0000
@@ -90,7 +90,7 @@
 int32_t gyro_mdps[3];
 
 // misc variables
-
+static char wall_of_dash[] = "--------------------------------------------------";
 static int post_interval_ms = 30000;
 int debug_baud = 115200;
 
@@ -258,12 +258,12 @@
 
 namespace patch
 {
-    template < typename T > std::string to_string( const T& n )
-    {
-        std::ostringstream stm ;
-        stm << n ;
-        return stm.str() ;
-    }
+template < typename T > std::string to_string( const T& n )
+{
+    std::ostringstream stm ;
+    stm << n ;
+    return stm.str() ;
+}
 }
 
 /****************************************************************************************************
@@ -282,44 +282,131 @@
             wait(1);
         }
     }
-    
+
     logInfo("Configuring http...\r\n");
     HTTPClient http;
     HTTPResult result;
     http.setHeader(EXOSITE_HEADER);
-    
+
     char http_rx_buf[1024];
     // IHTTPDataIn object - will contain data received from server.
     HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf));
-    
+
     // IHTTPDataOut object - contains data to be posted to server.
     HTTPMap http_tx;
-        
-    
+
+
 //****************************************************************************************************
 //        Initialize I2C Devices ************
 //****************************************************************************************************/
 
+#ifdef RPR0521
+    i2c.write(RPR0521_addr_w, &RPR0521_ModeControl[0], 2, false);
+    i2c.write(RPR0521_addr_w, &RPR0521_ALSPSControl[0], 2, false);
+    i2c.write(RPR0521_addr_w, &RPR0521_Persist[0], 2, false);
+#endif
+
+#ifdef KMX62
+    i2c.write(KMX62_addr_w, &KMX62_CNTL2[0], 2, false);
+#endif
+
+#ifdef COLOR
+    i2c.write(BH1745_addr_w, &BH1745_persistence[0], 2, false);
+    i2c.write(BH1745_addr_w, &BH1745_mode1[0], 2, false);
+    i2c.write(BH1745_addr_w, &BH1745_mode2[0], 2, false);
+    i2c.write(BH1745_addr_w, &BH1745_mode3[0], 2, false);
+#endif
+
+#ifdef KX022
+    i2c.write(KX022_addr_w, &KX022_Accel_CNTL1[0], 2, false);
+    i2c.write(KX022_addr_w, &KX022_Accel_ODCNTL[0], 2, false);
+    i2c.write(KX022_addr_w, &KX022_Accel_CNTL3[0], 2, false);
+    i2c.write(KX022_addr_w, &KX022_Accel_TILT_TIMER[0], 2, false);
+    i2c.write(KX022_addr_w, &KX022_Accel_CNTL2[0], 2, false);
+#endif
+
+#ifdef Pressure
+    i2c.write(Press_addr_w, &PWR_DOWN[0], 2, false);
+    i2c.write(Press_addr_w, &SLEEP[0], 2, false);
+    i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
+#endif
+
 
 //****************************************************************************************************/
 //End I2C Initialization Section **********************************************************
 //****************************************************************************************************/
-    
+
     Timer post_timer;
-    post_timer.start(); 
+    post_timer.start();
     logInfo("Setup complete.");
     logInfo("Waiting for %d ms to trigger connect...", post_interval_ms);
 
+//*******************************************************************************************************/
+//Beging loop for Main
+//*******************************************************************************************************/
+
     while (true) {
         if (post_timer.read_ms() > post_interval_ms && do_cloud_post) {
             logInfo("bringing up the link");
+
+#ifdef AnalogTemp
+            ReadAnalogTemp ();
+#endif
+
+#ifdef AnalogUV
+            ReadAnalogUV ();
+#endif
+
+#ifdef HallSensor
+            ReadHallSensor ();
+#endif
+
+#ifdef COLOR
+            ReadCOLOR ();
+#endif
+
+#ifdef RPR0521       //als digital
+            ReadRPR0521_ALS ();
+#endif
+
+#ifdef Pressure
+            ReadPressure();
+#endif
+         
+#ifdef KMX62
+            ReadKMX62_Accel ();
+            ReadKMX62_Mag ();
+#endif
+
+#ifdef KX022
+            ReadKX022 ();
+#endif
+
+            logDebug("%s", wall_of_dash);
+            logDebug("SENSOR DATA");
+            logDebug("temperature: %0.2f C", BM1383[0]);
+            logDebug("analog uv: %.1f mW/cm2", ML8511_output);
+            logDebug("ambient Light  %0.3f", RPR0521_ALS[0]);
+            logDebug("proximity count  %0.3f", RPR0521_ALS[1]);
+            logDebug("hall effect: South %d\t North %d",  Hall_Return[0],Hall_Return[1]);
+            logDebug("pressure: %0.2f hPa", BM1383[1]);
+            logDebug("magnetometer:\r\n\tx: %0.3f\ty: %0.3f\tz: %0.3f\tuT", MEMS_Mag[0], MEMS_Mag[1], MEMS_Mag[2]);
+            logDebug("accelerometer:\r\n\tx: %0.3f\ty: %0.3f\tz: %0.3f\tg", MEMS_Accel[0], MEMS_Accel[1], MEMS_Accel[2]);
+            logDebug("color:\r\n\tred: %ld\tgrn: %ld\tblu: %ld\t", BH1745[0], BH1745[1], BH1745[2]);
+            logDebug("%s", wall_of_dash);
+
+
+//*******************************************************************************************************/
+//Connect to the radio to send data
+//*******************************************************************************************************/
+
             if (radio->connect()) {
-                float sensor_data = RPR0521_ALS_OUT;
+                int sensor_data = RPR0521_ALS[0];
                 logDebug("posting sensor data");
-                logDebug("%f",sensor_data);
+                logDebug("%d",sensor_data);
 
-                http_tx.put("ALS", patch::to_string(sensor_data).c_str()); 
-                
+                http_tx.put("ALS", patch::to_string(sensor_data).c_str());
+
                 // Make HTTP POST request
                 result = http.post(EXOSITE_URL, http_tx, &http_rx);
                 if (result != HTTP_OK) {
@@ -334,6 +421,7 @@
             }
 
             post_timer.reset();
+
             logInfo("Waiting for %d ms to trigger connect...", post_interval_ms);
         }
     }
@@ -361,7 +449,8 @@
     return true;
 }
 
-char* httpResToStr(HTTPResult result) {
+char* httpResToStr(HTTPResult result)
+{
     switch(result) {
         case HTTP_PROCESSING:
             return "HTTP_PROCESSING";
@@ -392,3 +481,207 @@
     }
 }
 
+
+//************************************************************************************************/
+// Sensor data acquisition functions
+//************************************************************************************************/
+#ifdef AnalogTemp
+void ReadAnalogTemp ()
+{
+    BDE0600_Temp_value = BDE0600_Temp.read_u16();
+
+    BDE0600_output = (float)BDE0600_Temp_value * (float)0.000050354; //(value * (3.3V/65535))
+    BDE0600_output = (BDE0600_output-(float)1.753)/((float)-0.01068) + (float)30;
+
+//    printf("BDE0600 Analog Temp Sensor Data:\r\n");
+//    printf(" Temp = %.2f C\r\n", BDE0600_output);
+}
+#endif
+
+#ifdef AnalogUV
+void ReadAnalogUV ()
+{
+    ML8511_UV_value = ML8511_UV.read_u16();
+    ML8511_output = (float)ML8511_UV_value * (float)0.000050354; //(value * (3.3V/65535))   //Note to self: when playing with this, a negative value is seen... Honestly, I think this has to do with my ADC converstion...
+    ML8511_output = (ML8511_output-(float)2.2)/((float)0.129) + 10;                           // Added +5 to the offset so when inside (aka, no UV, readings show 0)... this is the wrong approach... and the readings don't make sense... Fix this.
+
+//    printf("ML8511 Analog UV Sensor Data:\r\n");
+//    printf(" UV = %.1f mW/cm2\r\n", ML8511_output);
+
+}
+#endif
+
+
+#ifdef HallSensor
+void ReadHallSensor ()
+{
+
+    Hall_Return[0] = Hall_GPIO0;
+    Hall_Return[1] = Hall_GPIO1;
+
+//    printf("BU52011 Hall Switch Sensor Data:\r\n");
+//    printf(" South Detect = %d\r\n", Hall_Return[0]);
+//    printf(" North Detect = %d\r\n", Hall_Return[1]);
+
+
+}
+#endif
+
+#ifdef COLOR
+void ReadCOLOR ()
+{
+
+    //Read color data from the IC
+    i2c.write(BH1745_addr_w, &BH1745_Addr_color_ReadData, 1, RepStart);
+    i2c.read(BH1745_addr_r, &BH1745_Content_ReadData[0], 6, NoRepStart);
+
+    //separate all data read into colors
+    BH1745[0] = (BH1745_Content_ReadData[1]<<8) | (BH1745_Content_ReadData[0]);
+    BH1745[1] = (BH1745_Content_ReadData[3]<<8) | (BH1745_Content_ReadData[2]);
+    BH1745[2] = (BH1745_Content_ReadData[5]<<8) | (BH1745_Content_ReadData[4]);
+
+    //Output Data into UART
+//    printf("BH1745 COLOR Sensor Data:\r\n");
+//    printf(" Red   = %d ADC Counts\r\n",BH1745[0]);
+//    printf(" Green = %d ADC Counts\r\n",BH1745[1]);
+//    printf(" Blue  = %d ADC Counts\r\n",BH1745[2]);
+
+}
+#endif
+
+#ifdef RPR0521       //als digital
+void ReadRPR0521_ALS ()
+{
+    i2c.write(RPR0521_addr_w, &RPR0521_Addr_ReadData, 1, RepStart);
+    i2c.read(RPR0521_addr_r, &RPR0521_Content_ReadData[0], 6, NoRepStart);
+
+    RPR0521_ALS[1] = (RPR0521_Content_ReadData[1]<<8) | (RPR0521_Content_ReadData[0]);
+    RPR0521_ALS_D0_RAWOUT = (RPR0521_Content_ReadData[3]<<8) | (RPR0521_Content_ReadData[2]);
+    RPR0521_ALS_D1_RAWOUT = (RPR0521_Content_ReadData[5]<<8) | (RPR0521_Content_ReadData[4]);
+    RPR0521_ALS_DataRatio = (float)RPR0521_ALS_D1_RAWOUT / (float)RPR0521_ALS_D0_RAWOUT;
+
+    if(RPR0521_ALS_DataRatio < (float)0.595) {
+        RPR0521_ALS[0] = ((float)1.682*(float)RPR0521_ALS_D0_RAWOUT - (float)1.877*(float)RPR0521_ALS_D1_RAWOUT);
+    } else if(RPR0521_ALS_DataRatio < (float)1.015) {
+        RPR0521_ALS[0] = ((float)0.644*(float)RPR0521_ALS_D0_RAWOUT - (float)0.132*(float)RPR0521_ALS_D1_RAWOUT);
+    } else if(RPR0521_ALS_DataRatio < (float)1.352) {
+        RPR0521_ALS[0] = ((float)0.756*(float)RPR0521_ALS_D0_RAWOUT - (float)0.243*(float)RPR0521_ALS_D1_RAWOUT);
+    } else if(RPR0521_ALS_DataRatio < (float)3.053) {
+        RPR0521_ALS[0] = ((float)0.766*(float)RPR0521_ALS_D0_RAWOUT - (float)0.25*(float)RPR0521_ALS_D1_RAWOUT);
+    } else {
+        RPR0521_ALS[0] = 0;
+    }
+//    printf("RPR-0521 ALS/PROX Sensor Data:\r\n");
+//    printf(" ALS = %0.2f lx\r\n", RPR0521_ALS[0]);
+//    printf(" PROX= %0.2f ADC Counts\r\n", RPR0521_ALS[1]);     //defined as a float but is an unsigned.
+
+}
+#endif
+
+#ifdef KMX62
+void ReadKMX62_Accel ()
+{
+    //Read Accel Portion from the IC
+    i2c.write(KMX62_addr_w, &KMX62_Addr_Accel_ReadData, 1, RepStart);
+    i2c.read(KMX62_addr_r, &KMX62_Content_Accel_ReadData[0], 6, NoRepStart);
+
+    //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte.
+    //      However, because we need the signed value, we will adjust the value when converting to "g"
+    MEMS_Accel_Xout = (KMX62_Content_Accel_ReadData[1]<<8) | (KMX62_Content_Accel_ReadData[0]);
+    MEMS_Accel_Yout = (KMX62_Content_Accel_ReadData[3]<<8) | (KMX62_Content_Accel_ReadData[2]);
+    MEMS_Accel_Zout = (KMX62_Content_Accel_ReadData[5]<<8) | (KMX62_Content_Accel_ReadData[4]);
+
+    //Note: Conversion to G is as follows:
+    //      Axis_ValueInG = MEMS_Accel_axis / 1024
+    //      However, since we did not remove the LSB previously, we need to divide by 4 again
+    //      Thus, we will divide the output by 4096 (1024*4) to convert and cancel out the LSB
+    MEMS_Accel[0] = ((float)MEMS_Accel_Xout/4096/2);
+    MEMS_Accel[1] = ((float)MEMS_Accel_Yout/4096/2);
+    MEMS_Accel[2] = ((float)MEMS_Accel_Zout/4096/2);
+
+    // Return Data to UART
+//    printf("KMX62 Accel+Mag Sensor Data:\r\n");
+//    printf(" AccX= %0.2f g\r\n", MEMS_Accel[0]);
+//    printf(" AccY= %0.2f g\r\n", MEMS_Accel[1]);
+//    printf(" AccZ= %0.2f g\r\n", MEMS_Accel[2]);
+
+}
+
+void ReadKMX62_Mag ()
+{
+
+    //Read Mag portion from the IC
+    i2c.write(KMX62_addr_w, &KMX62_Addr_Mag_ReadData, 1, RepStart);
+    i2c.read(KMX62_addr_r, &KMX62_Content_Mag_ReadData[0], 6, NoRepStart);
+
+    //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte.
+    //      However, because we need the signed value, we will adjust the value when converting to "g"
+    MEMS_Mag_Xout = (KMX62_Content_Mag_ReadData[1]<<8) | (KMX62_Content_Mag_ReadData[0]);
+    MEMS_Mag_Yout = (KMX62_Content_Mag_ReadData[3]<<8) | (KMX62_Content_Mag_ReadData[2]);
+    MEMS_Mag_Zout = (KMX62_Content_Mag_ReadData[5]<<8) | (KMX62_Content_Mag_ReadData[4]);
+
+    //Note: Conversion to G is as follows:
+    //      Axis_ValueInG = MEMS_Accel_axis / 1024
+    //      However, since we did not remove the LSB previously, we need to divide by 4 again
+    //      Thus, we will divide the output by 4095 (1024*4) to convert and cancel out the LSB
+    MEMS_Mag[0] = (float)MEMS_Mag_Xout/4096*(float)0.146;
+    MEMS_Mag[1] = (float)MEMS_Mag_Yout/4096*(float)0.146;
+    MEMS_Mag[2] = (float)MEMS_Mag_Zout/4096*(float)0.146;
+
+    // Return Data to UART
+//    printf(" MagX= %0.2f uT\r\n", MEMS_Mag[0]);
+//    printf(" MagY= %0.2f uT\r\n", MEMS_Mag[1]);
+//    printf(" MagZ= %0.2f uT\r\n", MEMS_Mag[2]);
+
+}
+#endif
+
+#ifdef KX022
+void ReadKX022 ()
+{
+
+    //Read KX022 Portion from the IC
+    i2c.write(KX022_addr_w, &KX022_Addr_Accel_ReadData, 1, RepStart);
+    i2c.read(KX022_addr_r, &KX022_Content_ReadData[0], 6, NoRepStart);
+
+    //Format Data
+    KX022_Accel_X_RawOUT = (KX022_Content_ReadData[1]<<8) | (KX022_Content_ReadData[0]);
+    KX022_Accel_Y_RawOUT = (KX022_Content_ReadData[3]<<8) | (KX022_Content_ReadData[2]);
+    KX022_Accel_Z_RawOUT = (KX022_Content_ReadData[5]<<8) | (KX022_Content_ReadData[4]);
+
+    //Scale Data
+    KX022_Accel[0] = (float)KX022_Accel_X_RawOUT / 16384;
+    KX022_Accel[1] = (float)KX022_Accel_Y_RawOUT / 16384;
+    KX022_Accel[2] = (float)KX022_Accel_Z_RawOUT / 16384;
+
+    //Return Data through UART
+//    printf("KX022 Accelerometer Sensor Data: \r\n");
+//    printf(" AccX= %0.2f g\r\n", KX022_Accel[0]);
+//    printf(" AccY= %0.2f g\r\n", KX022_Accel[1]);
+//    printf(" AccZ= %0.2f g\r\n", KX022_Accel[2]);
+
+}
+#endif
+
+
+#ifdef Pressure
+void ReadPressure ()
+{
+
+    i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart);
+    i2c.read(Press_addr_r, &Press_Content_ReadData[0], 6, NoRepStart);
+
+    BM1383_Temp_Out = (Press_Content_ReadData[0]<<8) | (Press_Content_ReadData[1]);
+    BM1383[0] = (float)BM1383_Temp_Out/32;
+
+    BM1383_Var  = (Press_Content_ReadData[2]<<3) | (Press_Content_ReadData[3] >> 5);
+    BM1383_Deci = ((Press_Content_ReadData[3] & 0x1f) << 6 | ((Press_Content_ReadData[4] >> 2)));
+    BM1383_Deci = (float)BM1383_Deci* (float)0.00048828125;  //0.00048828125 = 2^-11
+    BM1383[1] = (BM1383_Var + BM1383_Deci);   //question pending here...
+
+//    printf("BM1383 Pressure Sensor Data:\r\n");
+//    printf(" Temperature= %0.2f C\r\n", BM1383[0]);
+//    printf(" Pressure   = %0.2f hPa\r\n", BM1383[1]);
+
+}
+#endif