Version 8, working version with Alix, sams and ollies code. Displays time, date and sensor info onto terminal, LCD and networking, and saves onto SD card.

Dependencies:   BMP280 ELEC350-Practicals-FZ429 TextLCD BME280 ntp-client

Committer:
Alix955
Date:
Mon Dec 31 19:20:22 2018 +0000
Revision:
12:4c7eaac8ceef
Parent:
11:42b0c567cc8c
Version 8, integration of Alix & Sams work with older(?) version of ollies. Displays time, date and all sensor information onto LCD, Terminal and Networking, and saves sensor info to SD card.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Alix955 9:f5eae5211225 1 #ifndef _NETWORK_
Alix955 9:f5eae5211225 2
Alix955 9:f5eae5211225 3 #define _NETWORK_
Alix955 9:f5eae5211225 4
Alix955 9:f5eae5211225 5 #include "mbed.h"
Alix955 9:f5eae5211225 6
Alix955 9:f5eae5211225 7 #include "FATFileSystem.h"
Alix955 9:f5eae5211225 8
Alix955 9:f5eae5211225 9 #include "sample_hardware.hpp"
Alix955 9:f5eae5211225 10
Alix955 9:f5eae5211225 11 #include "EthernetInterface.h"
Alix955 9:f5eae5211225 12
Alix955 9:f5eae5211225 13 #include "TCPServer.h"
Alix955 9:f5eae5211225 14
Alix955 9:f5eae5211225 15 //#include "messageStruct.hpp"
Alix955 9:f5eae5211225 16
Alix955 10:eea19f8e6122 17 //#include "LCD.hpp"
Alix955 10:eea19f8e6122 18
Alix955 9:f5eae5211225 19 #include "TCPSocket.h"
Alix955 9:f5eae5211225 20
Alix955 9:f5eae5211225 21 #include <iostream>
Alix955 9:f5eae5211225 22
Alix955 9:f5eae5211225 23 #include <string>
Alix955 9:f5eae5211225 24
Alix955 9:f5eae5211225 25 #include "ntp-client/NTPClient.h"
Alix955 9:f5eae5211225 26
Alix955 9:f5eae5211225 27
Alix955 9:f5eae5211225 28
Alix955 9:f5eae5211225 29
Alix955 11:42b0c567cc8c 30
Alix955 9:f5eae5211225 31
Alix955 11:42b0c567cc8c 32 #define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
Alix955 9:f5eae5211225 33 #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
Alix955 10:eea19f8e6122 34 #define HTTP_MESSAGE_BODY1 "" \
Alix955 10:eea19f8e6122 35 "<html>" "\r\n" \
Alix955 10:eea19f8e6122 36 " <body style=\"display:flex;text-align:center\">" "\r\n" \
Alix955 11:42b0c567cc8c 37 " <div id=\"Sensor Data\" name=\"Sensor Date\" style=\"margin:auto\">" "\r\n" \
Alix955 11:42b0c567cc8c 38 " <h1> <b>Sensor Data</b> </h1>" "\r\n" \
Alix955 9:f5eae5211225 39 " <p>The LDR value is "
Alix955 9:f5eae5211225 40
Alix955 10:eea19f8e6122 41 #define HTTP_MESSAGE_BODY2 "" \
Alix955 11:42b0c567cc8c 42 " Lux </p>" "\r\n" \
Alix955 11:42b0c567cc8c 43 " <div style=\"margin:auto\">" "\r\n" \
Alix955 11:42b0c567cc8c 44 " <p>The Temperature value is "
Alix955 11:42b0c567cc8c 45
Alix955 11:42b0c567cc8c 46 #define HTTP_MESSAGE_BODY3 "" \
Alix955 11:42b0c567cc8c 47 " Degrees</p>" "\r\n" \
Alix955 11:42b0c567cc8c 48 " <div style=\"margin:auto\">" "\r\n" \
Alix955 11:42b0c567cc8c 49 " <p>The Pressure value is "
Alix955 11:42b0c567cc8c 50
Alix955 11:42b0c567cc8c 51 #define HTTP_MESSAGE_BODY4 "" \
Alix955 11:42b0c567cc8c 52 " mbar </p>" "\r\n" \
Alix955 11:42b0c567cc8c 53 " <div style=\"margin:auto\">" "\r\n" \
Alix955 11:42b0c567cc8c 54 " <p>The current Time is "
Alix955 11:42b0c567cc8c 55
Alix955 11:42b0c567cc8c 56 #define HTTP_MESSAGE_BODY5 "" \
Alix955 11:42b0c567cc8c 57 " </p>" "\r\n" \
Alix955 10:eea19f8e6122 58 " </div>" "\r\n" \
Alix955 10:eea19f8e6122 59 " </body>" "\r\n" \
Alix955 9:f5eae5211225 60 "</html>"
Alix955 9:f5eae5211225 61
Alix955 11:42b0c567cc8c 62
Alix955 9:f5eae5211225 63
Alix955 11:42b0c567cc8c 64
Alix955 9:f5eae5211225 65
Alix955 11:42b0c567cc8c 66
Alix955 10:eea19f8e6122 67 #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \
Alix955 10:eea19f8e6122 68 HTTP_HEADER_FIELDS "\r\n" \
Alix955 10:eea19f8e6122 69 "\r\n" \
Alix955 10:eea19f8e6122 70 HTTP_MESSAGE_BODY "\r\n"
Alix955 9:f5eae5211225 71
Alix955 9:f5eae5211225 72
Alix955 9:f5eae5211225 73
Alix955 10:eea19f8e6122 74 #define IP "10.0.0.10"
Alix955 9:f5eae5211225 75
Alix955 10:eea19f8e6122 76 #define NETMASK "255.0.0.0"
Alix955 9:f5eae5211225 77
Alix955 10:eea19f8e6122 78 #define GATEWAY "10.0.0.2"
Alix955 9:f5eae5211225 79
Alix955 9:f5eae5211225 80 EthernetInterface eth;
Alix955 9:f5eae5211225 81
Alix955 9:f5eae5211225 82 NTPClient ntp(&eth);
Alix955 9:f5eae5211225 83
Alix955 9:f5eae5211225 84
Alix955 9:f5eae5211225 85
Alix955 9:f5eae5211225 86
Alix955 9:f5eae5211225 87 class Network
Alix955 9:f5eae5211225 88 {
Alix955 10:eea19f8e6122 89 //friend class LCD_Data;
Alix955 9:f5eae5211225 90 private:
Alix955 9:f5eae5211225 91 float temp; //current temperature of sensor
Alix955 9:f5eae5211225 92 float pressure; //current pressure of sensor
Alix955 9:f5eae5211225 93 float fLDR; //current light level from LDR
Alix955 10:eea19f8e6122 94 string time;
Alix955 9:f5eae5211225 95
Alix955 9:f5eae5211225 96 void update_temp(double t) //use this function to update the current temperature value
Alix955 9:f5eae5211225 97 {
Alix955 12:4c7eaac8ceef 98 temp = t;
Alix955 9:f5eae5211225 99 }
Alix955 9:f5eae5211225 100
Alix955 9:f5eae5211225 101 void update_pressure(double p) //use this function to update the current pressure value
Alix955 9:f5eae5211225 102 {
Alix955 12:4c7eaac8ceef 103 pressure = p;
Alix955 9:f5eae5211225 104 }
Alix955 9:f5eae5211225 105
Alix955 9:f5eae5211225 106 void update_LDR(double L)
Alix955 9:f5eae5211225 107 {
Alix955 12:4c7eaac8ceef 108 fLDR = L;
Alix955 9:f5eae5211225 109 }
Alix955 9:f5eae5211225 110
Alix955 9:f5eae5211225 111
Alix955 9:f5eae5211225 112 public:
Alix955 9:f5eae5211225 113 EventQueue Network_Queue;
Alix955 9:f5eae5211225 114
Alix955 9:f5eae5211225 115
Alix955 9:f5eae5211225 116
Alix955 9:f5eae5211225 117 Network(){ //constructor
Alix955 9:f5eae5211225 118
Alix955 9:f5eae5211225 119 //Configure an ethernet connection
Alix955 9:f5eae5211225 120 eth.set_network(IP, NETMASK, GATEWAY);
Alix955 9:f5eae5211225 121 eth.connect();
Alix955 9:f5eae5211225 122 }
Alix955 9:f5eae5211225 123
Alix955 9:f5eae5211225 124
Alix955 9:f5eae5211225 125 ~Network(){ //deconstructor
Alix955 9:f5eae5211225 126
Alix955 9:f5eae5211225 127 }
Alix955 9:f5eae5211225 128
Alix955 9:f5eae5211225 129
Alix955 9:f5eae5211225 130
Alix955 9:f5eae5211225 131
Alix955 9:f5eae5211225 132
Alix955 9:f5eae5211225 133 void update_sensor_info(sample_message msg) //updates all current sensor information, this is called by a ticker every 5 seconds to read from the mailbox
Alix955 9:f5eae5211225 134
Alix955 9:f5eae5211225 135 {
Alix955 9:f5eae5211225 136 update_temp(msg.temp); // Include message class passing of data
Alix955 9:f5eae5211225 137 update_pressure(msg.pressure);
Alix955 9:f5eae5211225 138 update_LDR(msg.ldr);
Alix955 9:f5eae5211225 139 }
Alix955 9:f5eae5211225 140
Alix955 9:f5eae5211225 141
Alix955 10:eea19f8e6122 142 void update_Time(string tm)
Alix955 10:eea19f8e6122 143 {
Alix955 10:eea19f8e6122 144 time = tm;
Alix955 10:eea19f8e6122 145 }
Alix955 9:f5eae5211225 146
Alix955 9:f5eae5211225 147
Alix955 9:f5eae5211225 148 void NetPush(){
Alix955 9:f5eae5211225 149
Alix955 9:f5eae5211225 150
Alix955 9:f5eae5211225 151 //Now setup a web server
Alix955 9:f5eae5211225 152
Alix955 9:f5eae5211225 153 TCPServer srv; //TCP/IP Server
Alix955 9:f5eae5211225 154 TCPSocket clt_sock; //Socket for communication
Alix955 9:f5eae5211225 155 SocketAddress clt_addr; //Address of incoming connection
Alix955 9:f5eae5211225 156
Alix955 9:f5eae5211225 157
Alix955 9:f5eae5211225 158
Alix955 9:f5eae5211225 159
Alix955 9:f5eae5211225 160 /* Open the server on ethernet stack */
Alix955 9:f5eae5211225 161 srv.open(&eth);
Alix955 9:f5eae5211225 162
Alix955 9:f5eae5211225 163
Alix955 9:f5eae5211225 164 /* Bind the HTTP port (TCP 80) to the server */
Alix955 9:f5eae5211225 165 srv.bind(eth.get_ip_address(), 80);
Alix955 9:f5eae5211225 166
Alix955 9:f5eae5211225 167
Alix955 9:f5eae5211225 168 /* Can handle 5 simultaneous connections */
Alix955 9:f5eae5211225 169 srv.listen(5);
Alix955 9:f5eae5211225 170
Alix955 9:f5eae5211225 171 //Block and wait on an incoming connection
Alix955 9:f5eae5211225 172
Alix955 9:f5eae5211225 173 srv.accept(&clt_sock, &clt_addr);
Alix955 9:f5eae5211225 174
Alix955 12:4c7eaac8ceef 175 //printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
Alix955 9:f5eae5211225 176
Alix955 9:f5eae5211225 177
Alix955 9:f5eae5211225 178
Alix955 9:f5eae5211225 179 //Uses a C++ string to make it easier to concatinate
Alix955 9:f5eae5211225 180
Alix955 9:f5eae5211225 181 string response;
Alix955 9:f5eae5211225 182
Alix955 9:f5eae5211225 183
Alix955 9:f5eae5211225 184
Alix955 9:f5eae5211225 185 //This is a C string
Alix955 9:f5eae5211225 186
Alix955 9:f5eae5211225 187 char ldr_str[64];
Alix955 9:f5eae5211225 188
Alix955 9:f5eae5211225 189 char temp_str[64];
Alix955 9:f5eae5211225 190
Alix955 9:f5eae5211225 191 char pressure_str[64];
Alix955 9:f5eae5211225 192
Alix955 12:4c7eaac8ceef 193 //printf("%s", time);
Alix955 9:f5eae5211225 194
Alix955 9:f5eae5211225 195 //Convert to a C String
Alix955 9:f5eae5211225 196
Alix955 9:f5eae5211225 197 sprintf(ldr_str, "%5.3f", fLDR );
Alix955 9:f5eae5211225 198
Alix955 9:f5eae5211225 199 sprintf(temp_str, "%5.3f", temp );
Alix955 9:f5eae5211225 200
Alix955 9:f5eae5211225 201 sprintf(pressure_str, "%5.3f", pressure );
Alix955 9:f5eae5211225 202
Alix955 9:f5eae5211225 203
Alix955 9:f5eae5211225 204
Alix955 9:f5eae5211225 205
Alix955 9:f5eae5211225 206 //Build the C++ string response
Alix955 9:f5eae5211225 207
Alix955 9:f5eae5211225 208 response = HTTP_MESSAGE_BODY1;
Alix955 9:f5eae5211225 209
Alix955 9:f5eae5211225 210 response += ldr_str;
Alix955 9:f5eae5211225 211
Alix955 9:f5eae5211225 212 response += HTTP_MESSAGE_BODY2;
Alix955 9:f5eae5211225 213
Alix955 11:42b0c567cc8c 214 response += temp_str;
Alix955 9:f5eae5211225 215
Alix955 11:42b0c567cc8c 216 response += HTTP_MESSAGE_BODY3;
Alix955 9:f5eae5211225 217
Alix955 11:42b0c567cc8c 218 response += pressure_str;
Alix955 9:f5eae5211225 219
Alix955 9:f5eae5211225 220 response += HTTP_MESSAGE_BODY4;
Alix955 9:f5eae5211225 221
Alix955 11:42b0c567cc8c 222 response += time;
Alix955 10:eea19f8e6122 223
Alix955 11:42b0c567cc8c 224 response += HTTP_MESSAGE_BODY5;
Alix955 11:42b0c567cc8c 225
Alix955 9:f5eae5211225 226
Alix955 9:f5eae5211225 227
Alix955 9:f5eae5211225 228
Alix955 9:f5eae5211225 229
Alix955 9:f5eae5211225 230
Alix955 9:f5eae5211225 231 //Send static HTML response (as a C string)
Alix955 9:f5eae5211225 232
Alix955 12:4c7eaac8ceef 233 // clt_sock.send(response.c_str(), response.size()+6);
Alix955 12:4c7eaac8ceef 234 clt_sock.send(response.c_str(), response.size());
Alix955 9:f5eae5211225 235
Alix955 9:f5eae5211225 236
Alix955 9:f5eae5211225 237
Alix955 9:f5eae5211225 238
Alix955 9:f5eae5211225 239
Alix955 9:f5eae5211225 240 }
Alix955 9:f5eae5211225 241
Alix955 9:f5eae5211225 242 };
Alix955 9:f5eae5211225 243
Alix955 9:f5eae5211225 244
Alix955 9:f5eae5211225 245
Alix955 9:f5eae5211225 246
Alix955 9:f5eae5211225 247
Alix955 9:f5eae5211225 248 Network m_oNet;
Alix955 9:f5eae5211225 249
Alix955 9:f5eae5211225 250 #endif