Library of hardware declarations and utility functions for the ELEC350/1 Practicals and Coursework

Dependents:   Task618-mbedos-F429ZI Task621-mbedos_FZ429ZI Task622-mbedos-FZ429ZI Task632-mbedos-FZ429 ... more

Files at this revision

API Documentation at this revision

Comitter:
noutram
Date:
Thu Dec 07 15:28:16 2017 +0000
Parent:
7:d0e445a97c60
Commit message:
Library for ELEC350 and ELEC351 2017

Changed in this revision

Networkbits.cpp Show annotated file Show diff for this revision Revisions of this file
Networkbits.hpp Show annotated file Show diff for this revision Revisions of this file
sample_hardware.cpp Show annotated file Show diff for this revision Revisions of this file
sample_hardware.hpp Show annotated file Show diff for this revision Revisions of this file
diff -r d0e445a97c60 -r df979097cc71 Networkbits.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Networkbits.cpp	Thu Dec 07 15:28:16 2017 +0000
@@ -0,0 +1,68 @@
+#include "sample_hardware.hpp"
+#include "Networkbits.hpp"
+
+//Network thread - responsible for listening for connectinos and responding with updated tempature values
+void networktest()
+{
+    //This only runs when BOTH switches are pressed down
+    if ((SW1 == 0) || (SW2 == 0)) return;
+    
+    lcd.cls();
+    lcd.printf("Basic HTTP server example\n");
+    
+    //Configure an ethernet connection
+    EthernetInterface eth;
+    eth.set_network(IP, NETMASK, GATEWAY);
+    eth.connect();
+    lcd.printf("The target IP address is '%s'\n", eth.get_ip_address());
+    
+    //Now setup a web server
+    TCPServer srv;           //TCP/IP Server
+    TCPSocket clt_sock;      //Socket for communication
+    SocketAddress clt_addr;  //Address of incoming connection
+    
+    /* Open the server on ethernet stack */
+    srv.open(&eth);
+    
+    /* Bind the HTTP port (TCP 80) to the server */
+    srv.bind(eth.get_ip_address(), 80);
+    
+    /* Can handle 5 simultaneous connections */
+    srv.listen(5);
+    
+    //KEEP RESPONDING WHILE THE SWITCHES ARE PRESSED
+    while ((SW1 == 1) && (SW2 == 1)) {
+        using namespace std;
+        lcd.cls();
+        lcd.printf("Open 10.0.0.1 in a browser\n");
+        
+        //Block and wait on an incoming connection
+        srv.accept(&clt_sock, &clt_addr);
+        printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
+        
+        //Uses a C++ string to make it easier to concatinate
+        string response;
+        //This is a C string
+        char ldr_str[64];
+        
+        //Read the temperature value - note that getParameters() is thread safe
+        float temp = sensor.getTemperature();
+        
+        //Convert to a C String
+        sprintf(ldr_str, "%5.3f", temp );
+        printf("LDR: %5.3f\n\r", temp);
+        
+        //Build the C++ string response
+        response = HTTP_MESSAGE_BODY1;
+        response += ldr_str;
+        response += HTTP_MESSAGE_BODY2;
+        
+        //Send static HTML response (as a C string)
+        clt_sock.send(response.c_str(), response.size()+6);    
+    }
+    
+    printf("Release BOTH switches\n");
+    lcd.printf("Release BOTH switches\n");
+    while ((SW1 != 0) && (SW2 != 0));
+    wait(0.5); //debounce
+}
\ No newline at end of file
diff -r d0e445a97c60 -r df979097cc71 Networkbits.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Networkbits.hpp	Thu Dec 07 15:28:16 2017 +0000
@@ -0,0 +1,38 @@
+#ifndef MBED_NETWORKBITS_H
+#define MBED_NETWORKBITS_H
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "TCPServer.h"
+#include "TCPSocket.h"
+#include <iostream>
+#include <string> 
+
+
+#define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
+#define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
+#define HTTP_MESSAGE_BODY1 ""                                    \
+"<html>" "\r\n"                                                  \
+"  <body style=\"display:flex;text-align:center\">" "\r\n"       \
+"    <div style=\"margin:auto\">" "\r\n"                         \
+"      <h1>Hello World</h1>" "\r\n"                              \
+"      <p>The temperature is "                                     
+
+#define HTTP_MESSAGE_BODY2 ""                                    \
+       "</p>" "\r\n"                                             \
+"    </div>" "\r\n"                                              \
+"  </body>" "\r\n"                                               \
+"</html>"
+
+#define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n"   \
+                      HTTP_HEADER_FIELDS "\r\n" \
+                      "\r\n"                    \
+                      HTTP_MESSAGE_BODY "\r\n"
+
+#define IP        "10.0.0.10"
+#define NETMASK   "255.0.0.0"
+#define GATEWAY   "10.0.0.1"
+
+extern void networktest();
+
+#endif
\ No newline at end of file
diff -r d0e445a97c60 -r df979097cc71 sample_hardware.cpp
--- a/sample_hardware.cpp	Wed Dec 06 15:57:58 2017 +0000
+++ b/sample_hardware.cpp	Thu Dec 07 15:28:16 2017 +0000
@@ -1,5 +1,6 @@
 #include "mbed.h"
 #include "sample_hardware.hpp"
+#include "Networkbits.hpp"
 
 #define RED_DONE 1
 #define YELLOW_DONE 2
@@ -24,6 +25,15 @@
 BMP280 sensor(D14, D15);
 #endif
 
+//LCD Driver (provided via mbed repository)
+//RS D9
+//E  D8
+//D7,6,4,2 are the 4 bit for d4-7
+TextLCD lcd(D9, D8, D7, D6, D4, D2); // rs, e, d4-d7
+
+//SD Card
+SDBlockDevice sd(PB_5, D12, D13, D10); // mosi, miso, sclk, cs 
+
 //POWER ON SELF TEST
 void post() 
 {
@@ -64,6 +74,16 @@
     printf("Pressure: %5.1f\n", humidity);
     #endif
     
+    //Display on LCD
+    redLED = 1;
+    lcd.cls();
+    lcd.printf("LCD TEST...");
+    wait(0.5);
+    redLED = 0;
+    
+    //Network test (if BOTH switches are held down)
+    networktest();
+    
     puts("**********POST END**********");
  
 }
diff -r d0e445a97c60 -r df979097cc71 sample_hardware.hpp
--- a/sample_hardware.hpp	Wed Dec 06 15:57:58 2017 +0000
+++ b/sample_hardware.hpp	Thu Dec 07 15:28:16 2017 +0000
@@ -7,6 +7,9 @@
 #else
 #include "BMP280.h"
 #endif
+#include "TextLCD.h"
+#include "SDBlockDevice.h"
+#include "FATFileSystem.h"
 
 enum ELEC350_ERROR_CODE {OK, FATAL};
 
@@ -27,6 +30,9 @@
 extern BMP280 sensor;
 #endif
 
+extern TextLCD lcd;
+extern SDBlockDevice sd;
+
 extern void post();
 extern void errorCode(ELEC350_ERROR_CODE err);