Christoffer Bisander / Mbed 2 deprecated Data_collection_tcpip

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "F746_GUI.hpp"
00003 #include "LCD_DISCO_F746NG.h"
00004 #include "TextLCD.h"
00005 #include <string>
00006 #include <math.h>
00007 #include "EthernetInterface.h"
00008 #include "rtos.h"
00009 #include "lwip/inet.h"
00010 #include "lwip/netif.h"
00011 #include "netif/etharp.h"
00012 #include "lwip/dhcp.h"
00013 #include "eth_arch.h"
00014 #include "lwip/tcpip.h"
00015 #include "NTPClient.h"
00016 #include <stdio.h>
00017 
00018 
00019 //Initialization of varius connections and interfaces
00020 
00021 
00022 
00023 
00024 
00025 LCD_DISCO_F746NG lcd;
00026 EthernetInterface eth;
00027 Thread t1, t2, t3, t4, t5, t6;
00028 NTPClient ntp;
00029 TCPSocketConnection *sock;
00030 //declare external funktions and pointers
00031 extern void heartrate();
00032 extern double *h_p;
00033 extern double *hum;
00034 extern double *t_p;
00035 extern int *beats_per_min;
00036 extern void smotor();
00037 extern void temp();
00038 extern void humidity();
00039 //extern void sending();
00040 //extern void proximity();
00041 
00042 char ip_add[30];
00043 
00044 
00045 
00046 /*void sending(message)
00047 {
00048 
00049     sock = new TCPSocketConnection();
00050     sock->connect("192.168.8.115", 12345);
00051     sock->set_blocking(false, (int) (60 * 1000));
00052     //std::string s = std::to_string(message);
00053     //char s = '0'+message;
00054     //const char *cstr = s.c_str();
00055     sock->send(message, 127);
00056 
00057 }*/
00058 
00059 
00060 int main()
00061 {
00062     /*double humid;
00063     while(1){
00064         t5.start(humidity);
00065         char humidity_levels[31];
00066          humid = *hum;
00067         sprintf(humidity_levels, "%.40f", humid);
00068 
00069         lcd.DisplayStringAt(-10, LINE(8), (uint8_t *)humidity_levels, CENTER_MODE);
00070 
00071         wait(0.5);
00072         }*/
00073     //t3.start(smotor);
00074 
00075 
00076     //First we check if the ethernet interface was able to initialize
00077     if(eth.init()!=0) {
00078 
00079         char eth_init [40];
00080         sprintf(eth_init, "EthernetInterface Initialize Error");
00081         lcd.DisplayStringAt(0, LINE(10), (uint8_t *)eth_init, CENTER_MODE);
00082         while (1) {
00083             wait(5);
00084         }
00085     }
00086     //check if the ethernet intercace is connected to a network.
00087 
00088     if(eth.connect()!=0) {
00089         char eth_conn[30];
00090         sprintf(eth_conn, "EthernetInterface Connect Error");
00091         lcd.DisplayStringAt(0, LINE(8), (uint8_t *)eth_conn, CENTER_MODE);
00092         char mac_add[15];
00093         sprintf(mac_add, "Mac: %s", eth.getMACAddress());
00094         lcd.DisplayStringAt(0, LINE(10), (uint8_t *)mac_add, CENTER_MODE);
00095         while (1) {
00096             wait(5);
00097         }
00098     }
00099     /*
00100 
00101     char net_mask[30];
00102     char gate_Way[30];
00103     char dhcp_status[30];
00104     */
00105 
00106     /*Display the varius information achived from the dhcp server,
00107     It is also possible to set all these values manually,
00108     if no dhcp server is available
00109     */
00110 
00111     //used for troupleshooting with the network configuration
00112     /*
00113     sprintf(ip_add, "IP Address is %s", eth.getIPAddress());
00114     sprintf(net_mask, "NetMask is %s", eth.getNetworkMask());
00115     sprintf(gate_Way, "Gateway Address is %s", eth.getGateway());
00116     sprintf(dhcp_status, "Ethernet Setup OK");
00117     lcd.DisplayStringAt(0, LINE(2), (uint8_t *)ip_add, CENTER_MODE);
00118     lcd.DisplayStringAt(0, LINE(4), (uint8_t *)net_mask, CENTER_MODE);
00119     lcd.DisplayStringAt(0, LINE(6), (uint8_t *)gate_Way, CENTER_MODE);
00120     lcd.DisplayStringAt(0, LINE(8), (uint8_t *)dhcp_status, CENTER_MODE);
00121     lcd.Clear(LCD_COLOR_GREEN); */
00122     
00123     
00124     //Connects to a danish NTP server to syncronise time and date. 
00125     if (ntp.setTime("1.dk.pool.ntp.org") == 0) {
00126         char ntp_message [50];
00127         time_t ctTime;
00128         ctTime = time(NULL);
00129         sprintf(ntp_message, "%s \r\n", ctime(&ctTime));
00130         //sprintf(ntp_message, "Time is set to : %s \r\n", ctime(&ctTime));
00131         lcd.DisplayStringAt(-10, LINE(8), (uint8_t *)ntp_message, CENTER_MODE);
00132 
00133     } else {
00134 
00135         lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"Error getting time", CENTER_MODE);
00136     }
00137     wait(1);
00138     lcd.Clear(LCD_COLOR_GREEN);
00139 
00140 
00141 
00142     //This is the main tasks included in the programm
00143     sprintf(ip_add, "IP Address is %s", eth.getIPAddress());
00144     //Creates a endless loop that works as the "main menu"
00145     while(1) {
00146         Button b1(300, 3, 70, 40, "Hjerterytme", Font16);
00147         Button b2(50, 3, 70, 40, "Lyd og temp", Font16);
00148         Button b3(150, 3, 70, 40, "Send", Font16);
00149         lcd.DisplayStringAt(0, LINE(10), (uint8_t *)ip_add, CENTER_MODE);
00150 
00151         lcd.SetTextColor(LCD_COLOR_BLACK);
00152         lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Commence Initial Testing...", CENTER_MODE);
00153 
00154 
00155         lcd.DisplayStringAt(0, LINE(6), (uint8_t *) "Christoffer Bisander", CENTER_MODE);
00156 
00157         if (b1.Touched()) { // switch to the heart rate monitor when b1 is clicked
00158             lcd.SetTextColor(LCD_COLOR_RED);
00159             //Spawn a thread that start monitoring the heartrate
00160             t2.start(heartrate);
00161             lcd.Clear(LCD_COLOR_GREEN);
00162             char heart_rate_current[50];
00163             char beats_per_min_final[10];
00164             double hr;
00165             int bpm;
00166             Button stop_hr(400, 3, 70, 40, "Exit", Font16);
00167             //from the pointer assigned in the hearrate function we constantly read the new vallues from the heartrate monitor. 
00168             while(1) {
00169                 hr = *h_p;
00170                 bpm = *beats_per_min;
00171                 sprintf(heart_rate_current, "hjerterytme: %.8f", hr);
00172                 sprintf(beats_per_min_final, "Din puls er: %i", bpm);
00173                 //sending(bpm);
00174                 lcd.DisplayStringAt(0, LINE(10), (uint8_t *)beats_per_min_final, CENTER_MODE);
00175                 lcd.DisplayStringAt(0, LINE(14), (uint8_t *)heart_rate_current, CENTER_MODE);
00176                 //if the exit button is pressed the thread is terminated and we return to the main menu.
00177                 if (stop_hr.Touched()) {
00178                     t2.terminate();
00179                     break;
00180                 } else {
00181 
00182                 }
00183                 wait(0.1);
00184             }
00185 
00186             t2.join();
00187             lcd.Clear(LCD_COLOR_BLACK);
00188         }
00189         //Switch to the temperature monitoring mode 
00190         if(b2.Touched()) {
00191             lcd.SetTextColor(LCD_COLOR_RED);
00192             //Spawn a thread that constantly monitor the temperature, and calculates the avarage temperature over a 5 sec period. 
00193             t2.start(temp);
00194             lcd.Clear(LCD_COLOR_GREEN);
00195             char current_temp[20];
00196             double tp;
00197             Button stop_hr(400, 3, 70, 40, "Exit", Font16);
00198             while(1) {
00199                 tp = *t_p;
00200                 //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. 
00201                 if(tp>22.0) {
00202                     t4.start(smotor);
00203                 } else {
00204                 }
00205 
00206                 sprintf(current_temp, "temp: %.8f", tp);
00207                 lcd.DisplayStringAt(0, LINE(14), (uint8_t *)current_temp, CENTER_MODE);
00208                 if (stop_hr.Touched()) {
00209                     t2.terminate();
00210                     break;
00211                 } else {
00212 
00213                 }
00214                 wait(0.5);
00215             }
00216 
00217             t2.join();
00218             lcd.Clear(LCD_COLOR_BLACK);
00219         }
00220         //This is primarly a proof of concept it sends af message to a TCP socket running on a different machine. 
00221         if(b3.Touched()) {
00222             lcd.SetTextColor(LCD_COLOR_RED);
00223             sock = new TCPSocketConnection();
00224             sock->connect("10.130.17.131", 12345);
00225             sock->set_blocking(false, (int) (60 * 1000));
00226             //std::string utf8 = u8"This is just a test";
00227 
00228             //message = "this is a test";
00229 
00230             sock->send("This is a test", 30);
00231             delete sock;
00232             //TCPSocketConnection().connect("192.168.8.115", 12345);
00233             //sending();
00234             //Send some data
00235         
00236         }
00237     }
00238 
00239 }
00240 
00241 
00242 
00243 
00244