pH works but not temp

Dependencies:   BLE_nRF8001 DebounceIn mbed mbed_BLEtry2

Fork of mbed_BLEtry2 by SmartD

Revision:
2:f4cedbb71ef9
Parent:
1:d356fed1a9fa
--- a/main.cpp	Tue Mar 31 20:44:42 2015 +0000
+++ b/main.cpp	Fri Apr 10 00:49:19 2015 +0000
@@ -19,8 +19,12 @@
 
 //for BLE
 int count = 0;
-unsigned char txbuf[16] = {0};
-unsigned char txlen = 0;
+unsigned char tempbuf[16] = {0};
+unsigned char templen = 0;
+unsigned char decbuf[16] = {0};
+unsigned char declen = 0;
+unsigned char pHbuf[16] = {0};
+unsigned char pHlen = 0;
 
 /*----- BLE Utility -------------------------------------------------------------------------*/
 // create peripheral instance, see pinouts above
@@ -30,10 +34,9 @@
 BLEService               uartService          = BLEService("713d0000503e4c75ba943148f18d941e");
  
 // create characteristic
-//BLECharacteristic    txCharacteristic = BLECharacteristic("713d0002503e4c75ba943148f18d941e", BLENotify, 20);
-BLECharacteristic    rxCharacteristic = BLECharacteristic("713d0003503e4c75ba943148f18d941e", BLENotify, 20);
+BLECharacteristic    txCharacteristic = BLECharacteristic("713d0002503e4c75ba943148f18d941e", BLENotify, 30, 0);
+BLECharacteristic    rxCharacteristic = BLECharacteristic("713d0003503e4c75ba943148f18d941e", BLENotify, 20, 1);
 /*--------------------------------------------------------------------------------------------*/
- 
 unsigned int interval = 0;
 unsigned char count_on = 0;
 
@@ -62,25 +65,25 @@
 
 void calibration()
 {
-    serial.printf("enter calibration loop\n");
+    //serial.printf("enter calibration loop\n");
     //Loop to take calibration value
     calib = takeReading();
-    serial.printf("calib is %d\n", calib);
+    //serial.printf("calib is %d\n", calib);
 }
 
 void calculate()
 {
-    serial.printf("entered calc\n");
+    //serial.printf("entered calc\n");
     //Take average of readings over 10 seconds        
     avgReading = takeReading();
-    serial.printf("took reading\n");
+    //serial.printf("took reading\n");
     ready = true;
 }
 
 void measure()
 {
     serial.baud(115200);
-    serial.printf("start measurement");
+    //serial.printf("start measurement");
     //cb.mode(PullUp);
     if(calibrationORnot)
     {
@@ -90,8 +93,8 @@
 //Loop to take measurement        
     else if(!calibrationORnot) 
     {
-        serial.printf("enter measurement loop\n");
-        serial.printf("calib is %d\n", calib);
+    //  serial.printf("enter measurement loop\n");
+     //   serial.printf("calib is %d\n", calib);
         calculate();
     }
 }
@@ -103,7 +106,7 @@
     serial.baud(115200);
     serial.printf("Hello SmartD!\n");
     serial.printf("Serial begin!\r\n");
-    int pH,yint,tempC,tempF;
+    float pH,yint,tempC,tempF;
 
      /*----- BLE Utility ---------------------------------------------*/
     // set advertised local name and service UUID
@@ -114,14 +117,14 @@
     // add service and characteristic
     blePeripheral.addAttribute(uartService);
     blePeripheral.addAttribute(rxCharacteristic);
-    //blePeripheral.addAttribute(txCharacteristic);
+    blePeripheral.addAttribute(txCharacteristic);
 
     // begin initialization
     blePeripheral.begin();
     /*---------------------------------------------------------------*/
 
     //serial.printf("GOT IT pH is %d, Temp is %d\n\r", reading[0], reading[1]);    
-    serial.printf("BLE UART Peripheral begin!\r\n");
+    serial.printf("\nBLE UART Peripheral begin!\r\n");
     if (calibrationORnot)
     {    
         button.rise(&measure);
@@ -129,46 +132,57 @@
     
     while(1)
     {
-        //serial.printf("GOT IT pH is %d, Temp is %d\n\r", reading[0], reading[1]);    
         BLECentral central = blePeripheral.central();
         if (ready) 
         {
             //calcuating yintercept of the ADC -> pH equation using calibration value and given that calibration liquid is pH4
             yint = (68.97*calib)-4;
-            serial.printf("yint is %f", yint);
+            //serial.printf("yint is %f", yint);
             //assumes linear relationship between pin readout -> voltage ->pH
             pH = (0.0209*3300)*(avgReading)-yint;
             //conversion to degrees C from sensor output voltage
             tempC = (((3000*avgReading/1000) - 1.022129)/-0.0018496);
             tempF = (tempC *9/5) + 32;
             //print current pH and temp
-            serial.printf("pH = %5.2d & temp = %5.2d F\n\r", pH, tempF);
+            serial.printf("pH = %f & temp = %f F\n\r", pH, tempF);
             ready = !ready;
         }
+        
         if (central) 
         {
             // central connected to peripheral
             serial.printf("Connected to central\r\n");
             while (central.connected()) 
             {
-                if (!pb)  // if button pushed
+                if(!pb)
                 {
-                    serial.printf("pH is %d, Temp is %d\n\r", pH, tempF);
-                    // Read and send out pH value from measure function
-                    unsigned int value = pH;
-                    serial.printf("VALUE IS %d\n\r", value);
-                    const unsigned char val[1] = {value};
-                    serial.printf("VALUE AFTER IS %d\n\r", val[1]);
-                    rxCharacteristic.setValue(val,1);
-                    unsigned int value2 = tempF;
-                    serial.printf("VALUE FIRST IS %d\n\r", value2);
-                    const unsigned char val2[1] = {value2};
-                    serial.printf("VALUE AFTER IS %d\n\r", val2[1]);
-                    rxCharacteristic.setValue(val2,1);
+                    tempF = 98.5;
+                    int tempint = tempF;
+                    int tempdecimal = (tempF-tempint)*100;
+                    int pHint = pH;
+                    int pHdecimal = (pH-pHint)*100;                    
+//                    int tempint = 20;
+//                    int tempdecimal = 23;
+//                    int pHint = 9;
+                    tempbuf[0]= tempint;
+                    tempbuf[1] = tempdecimal;
+                    //tempbuf[templen] = tempint;
+                    //decbuf[declen] = decimal;
+                    pHbuf[0] = pHint;
+                    pHbuf[1] = pHdecimal;
+                    interval++;
+                }
 
+                if(interval == 1) 
+                { // If there is no char available last the interval, send the received chars to central.
+                    //serial.printf("Received from terminal: %d bytes\r\n", templen);
+                    txCharacteristic.setValue((const unsigned char *)tempbuf, 2);
+                    txCharacteristic.setValue((const unsigned char *)pHbuf, 2);
+                    serial.printf("done");
+                    wait(2);
+                    interval = 0;
                 }
             }
-        
             // central disconnected
             serial.printf("Disconnected from central\r\n");
         }