bunch of tesitng for F746NG

Dependencies:   BSP_DISCO_F746NG F746_GUI F7_Ethernet LCD_DISCO_F746NG SimpleSocket TMP36 GZ TS_DISCO_F746NG TextLCD WebSocketClient mbed-rtos mbed sMotor

Files at this revision

API Documentation at this revision

Comitter:
Maricius
Date:
Mon Jun 18 13:16:23 2018 +0000
Parent:
0:45610c4af223
Commit message:
initial commit

Changed in this revision

SimpleSocket.lib Show annotated file Show diff for this revision Revisions of this file
WebSocketClient.lib Show annotated file Show diff for this revision Revisions of this file
documentation/readme.txt Show annotated file Show diff for this revision Revisions of this file
input_functions.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
sMotor.lib Show annotated file Show diff for this revision Revisions of this file
socket_com.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SimpleSocket.lib	Mon Jun 18 13:16:23 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/yamaguch/code/SimpleSocket/#cbc8e17f7043
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebSocketClient.lib	Mon Jun 18 13:16:23 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/WebSocketClient/#4567996414a5
--- a/documentation/readme.txt	Mon Dec 11 09:46:58 2017 +0000
+++ b/documentation/readme.txt	Mon Jun 18 13:16:23 2018 +0000
@@ -25,4 +25,18 @@
 mbed-rtos: revision 2:bf7dc5f5bca9
 mbed: 144.0f02307a0877
 F746_GUI: 32:e6648167e8d3
-TextLCD: 8:308d188a2d3a
\ No newline at end of file
+TextLCD: 8:308d188a2d3a
+
+
+################################################################################
+Updated 15-12-2017
+
+The program main focus is now to function as a heartrate monitor, the device itself beeps and blinks when it registers af heartbeet via the earclip sensor.
+The idea it to send the hearrate and the timestamp via et tcp socket to a python server running on a server and then processing the data and storing in a database
+
+The following sensor/addons should be attached to the shield/board for the program to work as intended. 
+A0: Heartrate monitor
+A3: Temperatur sensor
+D3: Buzzer
+D4: LED
+If using the motor it should be attached as following: Normal pins to D8, D9, D10, D11 and ground VCR
--- a/input_functions.cpp	Mon Dec 11 09:46:58 2017 +0000
+++ b/input_functions.cpp	Mon Jun 18 13:16:23 2018 +0000
@@ -1,46 +1,114 @@
 #include "mbed.h"
 #include "rtos.h"
+#include "sMotor.h"
 
 
 AnalogIn heart_rate2(A0);
 AnalogIn adc_temp(A3);
 AnalogIn adc_sound(A1);
-//AnalogOut buzzer(D4);
+//AnalogIn hymid(A2);
+DigitalOut myled2(D4);
+DigitalOut buzzer(D3);
+
 //AnalogIn Proximity_measure(A2);
 void heartrate();
 void sound();
 void temp();
-//void write_to_socket();
-//void proximity();
+void smotor();
+
+void smotor(){
+ 
+    sMotor motor(D8, D9, D10, D11);   
+    int step_speed = 1000 ; // set default motor speed
+    int numstep = 1024 ;
+    motor.step(numstep/1,0,step_speed);
+}
 
     double h;
-    double *h_p = (double*)malloc(20*sizeof(double));
+    double *h_p = (double*)malloc(1*sizeof(double));
+    int heart_rate = 0;
+    int *beats_per_min = (int*)malloc(1*sizeof(int));
     
+    //Heartrate monitor
  void heartrate(){
-      
+    int number_of_beats = 0;
+    float j = 0.0;  
     h_p = &h;
+    beats_per_min = &heart_rate;
     while (1){
-    h = heart_rate2.read();
-    wait(0.1);
+    
+    buzzer = 0;
+    
+    h = heart_rate2.read();    
+    if(h>=1.0){
+        number_of_beats = number_of_beats + 1;
+        myled2 = 1;
+        buzzer = 1;
+        j = j + 0.3;
+        wait(0.3);
+        } 
+    else {
+        myled2 = 0;
+        j = j +  0.05;
+        wait(0.05);    
         }
+        //Every 10 sec calculate the heartrate
+    if(j > 10) {
+        heart_rate = number_of_beats * 6;
+        j = 0.0;
+        number_of_beats = 0;
+        }
+    else {
+            
+        }
+            
+        
+        
+    }
 }
+    double t;
+    double *t_p = (double*)malloc(1*sizeof(double));
 
 void temp(){
+    
+    t_p = &t;
     //------------------------------------------------------------------------//
         //This section is using the external temperatur sensor "Grove temperatur sensor v1.2
         //float resistance;
         const int B = 4275;
-        //const int R0 = 10000;
+        const int R0 = 100000;
         //char temp[10];
-        
-        //reads the voltage from the analog temperatur sensor
-        double a = adc_temp.read();
         double R;
+        double temp_combined;
+        double a; 
+        a = adc_temp.read();
         R = 1023.0/a-1.0;
+        R = R0*R;
+        t = (a - 0.5)*100;
+        //t  = 1.0/(log(R/R0)/B+1/298.15)-273.15;
+        //reads the voltage from the analog temperatur sensor
+        for( int i = 1; i <= 5; i = i + 1 ) {
+            a = adc_temp.read();
+            temp_combined = temp_combined + a;
+            if(i == 5) {
+                temp_combined = temp_combined / 5;
+                R = 1023.0/temp_combined-1.0;     
+                t  = 1.0/(log(R)/B+1/298.15)-173.15;
+                temp_combined = 0;
+                i = 1;
+                
+                wait(1);
+            } else {
+                wait(1);
+                }
+        }
+        
+        
+        
         //R = R0*R;
-        double temperature;
+        
         //Calculate the temperature from the voltage
-        temperature  = 1.0/(log(R)/B+1/298.15)-173.15;
+        
         //temperature = ((a*1000)-500)/10;
         //sprintf(temp, "%f", a);
         //resistance=(float)(1023-a)*10000/a; //get the resistance of the sensor;
@@ -55,22 +123,14 @@
         char temp_sound[10];
         float noiselevel;
         noiselevel = b;
-        //if the sound sensor is detection any noise a LED is switched on and a message with the current sound level is displayed.
+        
         if (noiselevel != 0) {
 
             //do something with noiselevel!!
             
         }
-
-        //external_LCD.DisplayStringAt(0, LINE(1), (uint8_t *)"This is just a test", CENTER_MODE);
         wait(0.1);
     }
-    
-    
-    
-//void write_to_stocket(){
-    
- //   }
 
 /*void proximity(){
     double prox_read;
--- a/main.cpp	Mon Dec 11 09:46:58 2017 +0000
+++ b/main.cpp	Mon Jun 18 13:16:23 2018 +0000
@@ -12,7 +12,6 @@
 #include "lwip/dhcp.h"
 #include "eth_arch.h"
 #include "lwip/tcpip.h"
-
 #include "NTPClient.h"
 #include <stdio.h>
 
@@ -20,26 +19,60 @@
 //Initialization of varius connections and interfaces
 
 
-DigitalOut myled2(D4);
+
 
 
-int number_clicks;
 LCD_DISCO_F746NG lcd;
 EthernetInterface eth;
-Thread t1, t2;
-
+Thread t1, t2, t3, t4, t5, t6;
+NTPClient ntp;
+TCPSocketConnection *sock;
 //declare external funktions and pointers
 extern void heartrate();
 extern double *h_p;
+extern double *hum;
+extern double *t_p;
+extern int *beats_per_min;
+extern void smotor();
+extern void temp();
+extern void humidity();
+//extern void sending();
 //extern void proximity();
-NTPClient ntp;
+
+char ip_add[30];
+
+
+
+/*void sending(message)
+{
+
+    sock = new TCPSocketConnection();
+    sock->connect("192.168.8.115", 12345);
+    sock->set_blocking(false, (int) (60 * 1000));
+    //std::string s = std::to_string(message);
+    //char s = '0'+message;
+    //const char *cstr = s.c_str();
+    sock->send(message, 127);
+
+}*/
 
 
 int main()
 {
-   
+    /*double humid;
+    while(1){
+        t5.start(humidity);
+        char humidity_levels[31];
+         humid = *hum;
+        sprintf(humidity_levels, "%.40f", humid);
 
-    
+        lcd.DisplayStringAt(-10, LINE(8), (uint8_t *)humidity_levels, CENTER_MODE);
+
+        wait(0.5);
+        }*/
+    //t3.start(smotor);
+
+
     //First we check if the ethernet interface was able to initialize
     if(eth.init()!=0) {
 
@@ -47,7 +80,7 @@
         sprintf(eth_init, "EthernetInterface Initialize Error");
         lcd.DisplayStringAt(0, LINE(10), (uint8_t *)eth_init, CENTER_MODE);
         while (1) {
-            wait(10);
+            wait(5);
         }
     }
     //check if the ethernet intercace is connected to a network.
@@ -60,19 +93,23 @@
         sprintf(mac_add, "Mac: %s", eth.getMACAddress());
         lcd.DisplayStringAt(0, LINE(10), (uint8_t *)mac_add, CENTER_MODE);
         while (1) {
-            wait(10);
+            wait(5);
         }
     }
-    char ip_add[30];
+    /*
+
     char net_mask[30];
     char gate_Way[30];
     char dhcp_status[30];
-
+    */
 
     /*Display the varius information achived from the dhcp server,
     It is also possible to set all these values manually,
     if no dhcp server is available
     */
+
+    //used for troupleshooting with the network configuration
+    /*
     sprintf(ip_add, "IP Address is %s", eth.getIPAddress());
     sprintf(net_mask, "NetMask is %s", eth.getNetworkMask());
     sprintf(gate_Way, "Gateway Address is %s", eth.getGateway());
@@ -81,90 +118,122 @@
     lcd.DisplayStringAt(0, LINE(4), (uint8_t *)net_mask, CENTER_MODE);
     lcd.DisplayStringAt(0, LINE(6), (uint8_t *)gate_Way, CENTER_MODE);
     lcd.DisplayStringAt(0, LINE(8), (uint8_t *)dhcp_status, CENTER_MODE);
-    wait(5);
-    lcd.Clear(LCD_COLOR_GREEN);
-    if (ntp.setTime("1.dk.pool.ntp.org") == 0)
-            {
-            char ntp_message [50];
-            time_t ctTime;
-            ctTime = time(NULL);
-            sprintf(ntp_message, "%s \r\n", ctime(&ctTime));
-            //sprintf(ntp_message, "Time is set to : %s \r\n", ctime(&ctTime));
-            lcd.DisplayStringAt(-10, LINE(8), (uint8_t *)ntp_message, CENTER_MODE);
-            
-            }
-        else
-        {
-        
-            lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"Error getting time", CENTER_MODE);
-        }
-    wait(2);
+    lcd.Clear(LCD_COLOR_GREEN); */
+    
+    
+    //Connects to a danish NTP server to syncronise time and date. 
+    if (ntp.setTime("1.dk.pool.ntp.org") == 0) {
+        char ntp_message [50];
+        time_t ctTime;
+        ctTime = time(NULL);
+        sprintf(ntp_message, "%s \r\n", ctime(&ctTime));
+        //sprintf(ntp_message, "Time is set to : %s \r\n", ctime(&ctTime));
+        lcd.DisplayStringAt(-10, LINE(8), (uint8_t *)ntp_message, CENTER_MODE);
+
+    } else {
+
+        lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"Error getting time", CENTER_MODE);
+    }
+    wait(1);
     lcd.Clear(LCD_COLOR_GREEN);
 
 
 
     //This is the main tasks included in the programm
-    Button b1(300, 3, 70, 40, "Hjerterytme", Font16);
-    Button b2(200, 3, 70, 40, "Lyd og temp", Font16);
-    lcd.DisplayStringAt(0, LINE(10), (uint8_t *)ip_add, CENTER_MODE);
+    sprintf(ip_add, "IP Address is %s", eth.getIPAddress());
+    //Creates a endless loop that works as the "main menu"
     while(1) {
-       
-        
-       
-        
-       
-       
-        
+        Button b1(300, 3, 70, 40, "Hjerterytme", Font16);
+        Button b2(50, 3, 70, 40, "Lyd og temp", Font16);
+        Button b3(150, 3, 70, 40, "Send", Font16);
+        lcd.DisplayStringAt(0, LINE(10), (uint8_t *)ip_add, CENTER_MODE);
 
         lcd.SetTextColor(LCD_COLOR_BLACK);
-        lcd.SetBackColor(LCD_COLOR_WHITE);
-        //print raw text to the LCD display
         lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Commence Initial Testing...", CENTER_MODE);
-        //shows the number of times the button was clicked
-       
-        lcd.DisplayStringAt(0, LINE(6), (uint8_t *) "Christoffer Bisander", CENTER_MODE);
-       
-       
-//t1.start(testing);
-//t1.detach();
-//input_functions fact;
-        //t1.start(proximity);
-        //t1.join();
-
-
-        
 
 
-
+        lcd.DisplayStringAt(0, LINE(6), (uint8_t *) "Christoffer Bisander", CENTER_MODE);
 
-        //Check if the button was clicked and then increments the number_clicks with one
-        if (b1.Touched()) { // if user touched "MODE" panel then return
+        if (b1.Touched()) { // switch to the heart rate monitor when b1 is clicked
             lcd.SetTextColor(LCD_COLOR_RED);
-            
+            //Spawn a thread that start monitoring the heartrate
             t2.start(heartrate);
-        lcd.Clear(LCD_COLOR_GREEN);
-        char heart_rate_current[50];
-        double hr;
-        while(1) {
-            hr = *h_p;
+            lcd.Clear(LCD_COLOR_GREEN);
+            char heart_rate_current[50];
+            char beats_per_min_final[10];
+            double hr;
+            int bpm;
+            Button stop_hr(400, 3, 70, 40, "Exit", Font16);
+            //from the pointer assigned in the hearrate function we constantly read the new vallues from the heartrate monitor. 
+            while(1) {
+                hr = *h_p;
+                bpm = *beats_per_min;
+                sprintf(heart_rate_current, "hjerterytme: %.8f", hr);
+                sprintf(beats_per_min_final, "Din puls er: %i", bpm);
+                //sending(bpm);
+                lcd.DisplayStringAt(0, LINE(10), (uint8_t *)beats_per_min_final, CENTER_MODE);
+                lcd.DisplayStringAt(0, LINE(14), (uint8_t *)heart_rate_current, CENTER_MODE);
+                //if the exit button is pressed the thread is terminated and we return to the main menu.
+                if (stop_hr.Touched()) {
+                    t2.terminate();
+                    break;
+                } else {
 
-//t2.start(input_functions::heartrate, fact);
-            sprintf(heart_rate_current, "hjerterytme: %f", hr);
-            lcd.DisplayStringAt(0, LINE(14), (uint8_t *)heart_rate_current, CENTER_MODE);
-            wait(0.1);
-        }
-        t2.join();
+                }
+                wait(0.1);
+            }
 
-            
-            wait(0.5);
+            t2.join();
             lcd.Clear(LCD_COLOR_BLACK);
         }
+        //Switch to the temperature monitoring mode 
+        if(b2.Touched()) {
+            lcd.SetTextColor(LCD_COLOR_RED);
+            //Spawn a thread that constantly monitor the temperature, and calculates the avarage temperature over a 5 sec period. 
+            t2.start(temp);
+            lcd.Clear(LCD_COLOR_GREEN);
+            char current_temp[20];
+            double tp;
+            Button stop_hr(400, 3, 70, 40, "Exit", Font16);
+            while(1) {
+                tp = *t_p;
+                //If the temperatur reached a certain limit, the motor start the idea here would be that it could be used as a climate system etc. 
+                if(tp>22.0) {
+                    t4.start(smotor);
+                } else {
+                }
+
+                sprintf(current_temp, "temp: %.8f", tp);
+                lcd.DisplayStringAt(0, LINE(14), (uint8_t *)current_temp, CENTER_MODE);
+                if (stop_hr.Touched()) {
+                    t2.terminate();
+                    break;
+                } else {
+
+                }
+                wait(0.5);
+            }
+
+            t2.join();
+            lcd.Clear(LCD_COLOR_BLACK);
+        }
+        //This is primarly a proof of concept it sends af message to a TCP socket running on a different machine. 
+        if(b3.Touched()) {
+            lcd.SetTextColor(LCD_COLOR_RED);
+            sock = new TCPSocketConnection();
+            sock->connect("10.130.17.131", 12345);
+            sock->set_blocking(false, (int) (60 * 1000));
+            //std::string utf8 = u8"This is just a test";
+
+            //message = "this is a test";
+
+            sock->send("This is a test", 30);
+            delete sock;
+            //TCPSocketConnection().connect("192.168.8.115", 12345);
+            //sending();
+            //Send some data
         
-        //----------------------------------------------------------------------------//
-        //analog sound sensor.
-        //read the voltage from the sound sensor
-        
-
+        }
     }
 
 }
--- a/mbed-rtos.lib	Mon Dec 11 09:46:58 2017 +0000
+++ b/mbed-rtos.lib	Mon Jun 18 13:16:23 2018 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
+http://mbed.org/users/mbed_official/code/mbed-rtos/#5713cbbdb706
--- a/mbed.bld	Mon Dec 11 09:46:58 2017 +0000
+++ b/mbed.bld	Mon Jun 18 13:16:23 2018 +0000
@@ -1,1 +1,1 @@
-https://mbed.org/users/mbed_official/code/mbed/builds/0f02307a0877
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/e7ca05fa8600
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sMotor.lib	Mon Jun 18 13:16:23 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/XtaticO/code/sMotor/#4b3b9e047ce3
--- a/socket_com.cpp	Mon Dec 11 09:46:58 2017 +0000
+++ b/socket_com.cpp	Mon Jun 18 13:16:23 2018 +0000
@@ -1,10 +1,11 @@
-#include "mbed.h"
+/*#include "mbed.h"
 #include "rtos.h"
 #include "LCD_DISCO_F746NG.h"
 #include "TextLCD.h"
 #include "TCPSocketConnection.h"
 
 LCD_DISCO_F746NG lcd_socket;
+void sending();
 
 void sending()
 {
@@ -22,8 +23,8 @@
     }
 
     serv_addr.sin_family = AF_INET;
-    serv_addr.sin_port = htons(8889);
-    serv_addr.sin_addr.s_addr = inet_addr("10.130.56.27");
+    serv_addr.sin_port = htons(8993);
+    serv_addr.sin_addr.s_addr = inet_addr("192.168.8.107");
 
     if(lwip_connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0) {
         sprintf(connect_error, "\n Error : Connect Failed \n");
@@ -44,4 +45,4 @@
         printf("\n Read Error \n");
     }
 
-}
\ No newline at end of file
+}*/
\ No newline at end of file