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:
Thu Dec 13 15:46:07 2018 +0000
Revision:
11:42b0c567cc8c
Parent:
10:eea19f8e6122
Child:
12:4c7eaac8ceef
Newest version of alix & sam

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 9:f5eae5211225 98 temp = 5;
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 9:f5eae5211225 103 pressure = 4;
Alix955 9:f5eae5211225 104 }
Alix955 9:f5eae5211225 105
Alix955 9:f5eae5211225 106 void update_LDR(double L)
Alix955 9:f5eae5211225 107 {
Alix955 9:f5eae5211225 108 fLDR = 3;
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
Alix955 9:f5eae5211225 126 ~Network(){ //deconstructor
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
Alix955 9:f5eae5211225 134 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 135
Alix955 9:f5eae5211225 136 {
Alix955 9:f5eae5211225 137 update_temp(msg.temp); // Include message class passing of data
Alix955 9:f5eae5211225 138 update_pressure(msg.pressure);
Alix955 9:f5eae5211225 139 update_LDR(msg.ldr);
Alix955 9:f5eae5211225 140 }
Alix955 9:f5eae5211225 141
Alix955 9:f5eae5211225 142
Alix955 10:eea19f8e6122 143 void update_Time(string tm)
Alix955 10:eea19f8e6122 144 {
Alix955 10:eea19f8e6122 145 time = tm;
Alix955 10:eea19f8e6122 146 }
Alix955 9:f5eae5211225 147
Alix955 9:f5eae5211225 148
Alix955 9:f5eae5211225 149 void NetPush(){
Alix955 9:f5eae5211225 150
Alix955 9:f5eae5211225 151
Alix955 9:f5eae5211225 152 //Now setup a web server
Alix955 9:f5eae5211225 153
Alix955 9:f5eae5211225 154 TCPServer srv; //TCP/IP Server
Alix955 9:f5eae5211225 155 TCPSocket clt_sock; //Socket for communication
Alix955 9:f5eae5211225 156 SocketAddress clt_addr; //Address of incoming connection
Alix955 9:f5eae5211225 157
Alix955 9:f5eae5211225 158
Alix955 9:f5eae5211225 159
Alix955 9:f5eae5211225 160
Alix955 9:f5eae5211225 161 /* Open the server on ethernet stack */
Alix955 9:f5eae5211225 162 srv.open(&eth);
Alix955 9:f5eae5211225 163
Alix955 9:f5eae5211225 164
Alix955 9:f5eae5211225 165 /* Bind the HTTP port (TCP 80) to the server */
Alix955 9:f5eae5211225 166 srv.bind(eth.get_ip_address(), 80);
Alix955 9:f5eae5211225 167
Alix955 9:f5eae5211225 168
Alix955 9:f5eae5211225 169 /* Can handle 5 simultaneous connections */
Alix955 9:f5eae5211225 170 srv.listen(5);
Alix955 9:f5eae5211225 171
Alix955 9:f5eae5211225 172 //Block and wait on an incoming connection
Alix955 9:f5eae5211225 173
Alix955 9:f5eae5211225 174 srv.accept(&clt_sock, &clt_addr);
Alix955 9:f5eae5211225 175
Alix955 9:f5eae5211225 176 printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
Alix955 9:f5eae5211225 177
Alix955 9:f5eae5211225 178
Alix955 9:f5eae5211225 179
Alix955 9:f5eae5211225 180 //Uses a C++ string to make it easier to concatinate
Alix955 9:f5eae5211225 181
Alix955 9:f5eae5211225 182 string response;
Alix955 9:f5eae5211225 183
Alix955 9:f5eae5211225 184
Alix955 9:f5eae5211225 185
Alix955 9:f5eae5211225 186 //This is a C string
Alix955 9:f5eae5211225 187
Alix955 9:f5eae5211225 188 char ldr_str[64];
Alix955 9:f5eae5211225 189
Alix955 9:f5eae5211225 190 char temp_str[64];
Alix955 9:f5eae5211225 191
Alix955 9:f5eae5211225 192 char pressure_str[64];
Alix955 9:f5eae5211225 193
Alix955 9:f5eae5211225 194
Alix955 9:f5eae5211225 195
Alix955 9:f5eae5211225 196
Alix955 9:f5eae5211225 197
Alix955 9:f5eae5211225 198 //Convert to a C String
Alix955 9:f5eae5211225 199
Alix955 9:f5eae5211225 200 sprintf(ldr_str, "%5.3f", fLDR );
Alix955 9:f5eae5211225 201
Alix955 9:f5eae5211225 202 sprintf(temp_str, "%5.3f", temp );
Alix955 9:f5eae5211225 203
Alix955 9:f5eae5211225 204 sprintf(pressure_str, "%5.3f", pressure );
Alix955 9:f5eae5211225 205
Alix955 9:f5eae5211225 206
Alix955 9:f5eae5211225 207
Alix955 9:f5eae5211225 208
Alix955 9:f5eae5211225 209 //Build the C++ string response
Alix955 9:f5eae5211225 210
Alix955 9:f5eae5211225 211 response = HTTP_MESSAGE_BODY1;
Alix955 9:f5eae5211225 212
Alix955 9:f5eae5211225 213 response += ldr_str;
Alix955 9:f5eae5211225 214
Alix955 9:f5eae5211225 215 response += HTTP_MESSAGE_BODY2;
Alix955 9:f5eae5211225 216
Alix955 11:42b0c567cc8c 217 response += temp_str;
Alix955 9:f5eae5211225 218
Alix955 11:42b0c567cc8c 219 response += HTTP_MESSAGE_BODY3;
Alix955 9:f5eae5211225 220
Alix955 11:42b0c567cc8c 221 response += pressure_str;
Alix955 9:f5eae5211225 222
Alix955 9:f5eae5211225 223 response += HTTP_MESSAGE_BODY4;
Alix955 9:f5eae5211225 224
Alix955 11:42b0c567cc8c 225 response += time;
Alix955 10:eea19f8e6122 226
Alix955 11:42b0c567cc8c 227 response += HTTP_MESSAGE_BODY5;
Alix955 11:42b0c567cc8c 228
Alix955 9:f5eae5211225 229
Alix955 9:f5eae5211225 230
Alix955 9:f5eae5211225 231
Alix955 9:f5eae5211225 232
Alix955 9:f5eae5211225 233
Alix955 9:f5eae5211225 234 //Send static HTML response (as a C string)
Alix955 9:f5eae5211225 235
Alix955 9:f5eae5211225 236 clt_sock.send(response.c_str(), response.size()+6);
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
Alix955 9:f5eae5211225 249
Alix955 9:f5eae5211225 250 Network m_oNet;
Alix955 9:f5eae5211225 251
Alix955 9:f5eae5211225 252 #endif