send and receive data with bluetooth module hc05

Dependencies:   atterm hc_05 mbed

Revision:
10:fe868725023a
Parent:
9:0d5c2787a01f
--- a/main.cpp	Mon Aug 27 17:44:20 2018 +0000
+++ b/main.cpp	Sat Sep 01 18:42:30 2018 +0000
@@ -2,11 +2,16 @@
 #include "mbed.h"
 #include "at.h"
 #include "hc05.h"
+#include "stdio.h"
+#include "math.h"
 
 bt_modul_hc05 hc05;
 
 DigitalOut led1(LED1);
-char *receive, *ok;
+AnalogIn ad(PA_1);
+char *receive;
+char buff[32];
+float volt = 0.00;
 
 void menu();
 void getInfo();
@@ -21,8 +26,19 @@
     pc.pc_send("\r\nInfo: please type '?' for menue options\r\n", "");
 
     while(1)  {
-        wait(0.5);
 
+        char *data = hc05.data_receive();
+        
+        if(strlen(data) != 0)  {
+                                               
+            if(strncmp(data, "AD0", 3) == 0)  {
+                pc.pc_send("Data received!: %s\r\n", data);
+                volt = ad.read();
+                sprintf(buff, "The voltage is: %f", volt); 
+                hc05.data_send(buff);
+            }
+                    
+        }
         menu();
 
     }
@@ -39,7 +55,7 @@
     }
 
     else if(strncmp(pc.buffer, "role", 4) == 0)  {
-        
+
         if(strcmp(pc.buffer + 5, "1") == 0)  {
             pc.pc_send("state role: %s", hc05.set_modul_role(MASTER));
         } else if(strcmp(pc.buffer + 5, "0") == 0)  {
@@ -53,17 +69,22 @@
     else if(strncmp(pc.buffer, "uart", 4) == 0)  {
         pc.pc_send("set uart: %s", hc05.set_modul_uart(pc.buffer + 5));
     }
-    
+
     else if(strncmp(pc.buffer, "connect", 7) == 0)  {
         pc.pc_send("connect fixed: %s", hc05.link_device_fixed(pc.buffer + 8));
     }
 
-    
+
     else if(strncmp(pc.buffer, "mastermode", 10) == 0)  {
-        hc05.init_master_fixed("0000", "nucleo", "2015,11,27524");
+        hc05.init_master_fixed(pc.buffer + 11);
         pc.pc_send("master init OK\r\n", "");
     }
 
+    else if(strncmp(pc.buffer, "slavemode", 9) == 0)  {
+        hc05.init_slave();
+        pc.pc_send("slave init OK\r\n", "");
+    }
+
     else if(strcmp(pc.buffer, "info") == 0)  {
         getInfo();
     }
@@ -78,7 +99,7 @@
     }
 
     else if(strncmp(pc.buffer, "send", 4) == 0)  {
-        hc05.data_send(pc.buffer + 5);
+        hc05.data_send(pc.buffer + 4);
         pc.pc_send("Data send\r\n", "");
     }
 
@@ -110,7 +131,8 @@
     pc.pc_send("Set to Role            type in 'role 0, 1 or 2'\r\n", "");
     pc.pc_send("Set to UART            type in 'uart baud, startbit, parity'\r\n", "");
     pc.pc_send("Connect to fixed addr  type in 'connect XX..,XX..,XX..'\r\n", "");
-    pc.pc_send("Set to Mastermode      type in 'mastermode'\r\n", "");
+    pc.pc_send("Set to Mastermode      type in 'mastermode fixed address'\r\n", "");
+    pc.pc_send("Set to slavemode       type in 'slavemode'\r\n", "");
     pc.pc_send("Info Modul settings    type in 'info'\r\n", "");
     pc.pc_send("Search other BT-Moduls type in 'search'\r\n", "");
     pc.pc_send("Cancel search          type in 'end search'\r\n", "");