Coursework template

Dependencies:   BMP280 TextLCD BME280

Files at this revision

API Documentation at this revision

Comitter:
noutram
Date:
Sat Nov 24 09:35:51 2018 +0000
Commit message:
updated for 2018;

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
BME280.lib Show annotated file Show diff for this revision Revisions of this file
BMP280.lib Show annotated file Show diff for this revision Revisions of this file
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
README.md Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib 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-os.lib 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
sd-driver.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Sat Nov 24 09:35:51 2018 +0000
@@ -0,0 +1,4 @@
+.build
+.mbed
+projectfiles
+*.py*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BME280.lib	Sat Nov 24 09:35:51 2018 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/MACRUM/code/BME280/#c1f1647004c4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BMP280.lib	Sat Nov 24 09:35:51 2018 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/charly/code/BMP280/#d22ecbef9b90
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Networkbits.cpp	Sat Nov 24 09:35:51 2018 +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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Networkbits.hpp	Sat Nov 24 09:35:51 2018 +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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Sat Nov 24 09:35:51 2018 +0000
@@ -0,0 +1,57 @@
+# Getting started with Blinky on mbed OS
+
+This guide reviews the steps required to get Blinky working on an mbed OS platform.
+
+Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).
+
+## Import the example application
+
+From the command-line, import the example:
+
+```
+mbed import mbed-os-example-blinky
+cd mbed-os-example-blinky
+```
+
+### Now compile
+
+Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5:
+
+```
+mbed compile -m K64F -t ARM
+```
+
+Your PC may take a few minutes to compile your code. At the end, you see the following result:
+
+```
+[snip]
++----------------------------+-------+-------+------+
+| Module                     | .text | .data | .bss |
++----------------------------+-------+-------+------+
+| Misc                       | 13939 |    24 | 1372 |
+| core/hal                   | 16993 |    96 |  296 |
+| core/rtos                  |  7384 |    92 | 4204 |
+| features/FEATURE_IPV4      |    80 |     0 |  176 |
+| frameworks/greentea-client |  1830 |    60 |   44 |
+| frameworks/utest           |  2392 |   512 |  292 |
+| Subtotals                  | 42618 |   784 | 6384 |
++----------------------------+-------+-------+------+
+Allocated Heap: unknown
+Allocated Stack: unknown
+Total Static RAM memory (data + bss): 7168 bytes
+Total RAM memory (data + bss + heap + stack): 7168 bytes
+Total Flash memory (text + data + misc): 43402 bytes
+Image: .\.build\K64F\ARM\mbed-os-example-blinky.bin
+```
+
+### Program your board
+
+1. Connect your mbed device to the computer over USB.
+1. Copy the binary file to the mbed device.
+1. Press the reset button to start the program.
+
+The LED on your platform turns on and off.
+
+## Troubleshooting
+
+If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Sat Nov 24 09:35:51 2018 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/simon/code/TextLCD/#308d188a2d3a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Nov 24 09:35:51 2018 +0000
@@ -0,0 +1,82 @@
+#include "sample_hardware.hpp"
+#include "Networkbits.hpp"
+
+// This is a very short demo that demonstrates all the hardware used in the coursework.
+// You will need a network connection set up (covered elsewhere). The host PC should have the address 10.0.0.1
+
+//Threads
+Thread nwrkThread;
+ 
+
+int main() {
+    //Greeting
+    printf("Testing\n\n");    
+    
+    //Power on self test
+    post();
+    
+    //Initialise the SD card (this needs to move)
+    if ( sd.init() != 0) {
+        printf("Init failed \n");
+        lcd.cls();
+        lcd.printf("CANNOT INIT SD");        
+        errorCode(FATAL);
+    } 
+    
+    //Create a filing system for SD Card
+    FATFileSystem fs("sd", &sd);     
+
+    //Open to WRITE
+    FILE* fp = fopen("/sd/test.csv","a");
+    if (fp == NULL) {
+        error("Could not open file for write\n");
+        lcd.cls();
+        lcd.printf("CANNOT OPEN FILE\n\n");
+        errorCode(FATAL);
+    }
+    
+    //Last message before sampling begins
+    lcd.cls();
+    lcd.printf("READY\n\n");
+        
+        
+    //Press either switch to unmount
+    while ((SW1 == 0) && (SW2 == 0)) {
+        
+        //Base loop delay
+        wait(1.0);
+        
+        //Read environmental sensors
+        double temp = sensor.getTemperature();
+        double pressure = sensor.getPressure();
+        
+        //Write new data to LCD (not fast!)
+        lcd.cls();
+        lcd.printf("Temp   Pressure\n"); 
+        lcd.printf("%6.1f ",temp);
+        lcd.printf("%.2f\n",pressure);
+        
+        //Write to SD (potentially slow)
+        fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
+    }
+    
+    //Close File
+    fclose(fp);
+    
+    //Close down
+    sd.deinit();
+    printf("Unmounted...\n");
+    lcd.cls();
+    lcd.printf("Unmounted...\n\n");
+    
+    //Flash to indicate goodness
+    while(true) {
+        greenLED = 1;
+        wait(0.5);
+        greenLED = 0;
+        wait(0.1);    
+    }
+}
+
+
+    
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Sat Nov 24 09:35:51 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#2fd0c5cfbd83fce62da6308f9d64c0ab64e1f0d6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sample_hardware.cpp	Sat Nov 24 09:35:51 2018 +0000
@@ -0,0 +1,107 @@
+#include "mbed.h"
+#include "sample_hardware.hpp"
+#include "Networkbits.hpp"
+
+#define RED_DONE 1
+#define YELLOW_DONE 2
+
+//Digital outputs
+DigitalOut onBoardLED(LED1);
+DigitalOut redLED(PE_15);
+DigitalOut yellowLED(PB_10);
+DigitalOut greenLED(PB_11);
+
+//Inputs
+DigitalIn  onBoardSwitch(USER_BUTTON);
+DigitalIn  SW1(PE_12);
+DigitalIn  SW2(PE_14);
+//Serial pc(USBTX, USBRX);
+AnalogIn adcIn(PA_0);
+
+//Environmental Sensor driver
+#ifdef BME
+BME280 sensor(D14, D15);
+#else
+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() 
+{
+    //POWER ON TEST (POT)
+    puts("**********STARTING POWER ON SELF TEST (POST)**********");
+    
+    //Test LEDs
+    puts("ALL LEDs should be blinking");
+    for (unsigned int n=0; n<10; n++) {
+        redLED    = 1;
+        yellowLED = 1;
+        greenLED  = 1;
+        wait(0.05);
+        redLED    = 0;
+        yellowLED = 0;
+        greenLED  = 0;     
+        wait(0.05);         
+    } 
+    
+    //Output the switch states (hold them down to test)
+    printf("SW1: %d\tSW2: %d\n\r", SW1.read(), SW2.read());    
+    printf("USER: %d\n\r", onBoardSwitch.read()); 
+    
+    //Output the ADC
+    printf("ADC: %f\n\r", adcIn.read());
+    
+    //Read Sensors (I2C)
+    float temp = sensor.getTemperature();
+    float pressure = sensor.getPressure();
+    #ifdef BME
+    float humidity = sensor.getHumidity();
+    #endif
+   
+    //Display in PuTTY
+    printf("Temperature: %5.1f\n", temp);
+    printf("Pressure: %5.1f\n", pressure);
+    #ifdef BME
+    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**********");
+ 
+}
+
+void errorCode(ELEC350_ERROR_CODE err)
+{
+    switch (err) {
+      case OK:
+        greenLED = 1;
+        wait(1.0);
+        greenLED = 0;
+        return;                
+      case FATAL:
+        while(1) {
+            redLED = 1;
+            wait(0.1);
+            redLED = 0;
+            wait(0.1);                
+        }
+    };
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sample_hardware.hpp	Sat Nov 24 09:35:51 2018 +0000
@@ -0,0 +1,39 @@
+#ifndef __sample_hardware__
+#define __sample_hardware__
+
+//#define BME
+#ifdef BME
+#include "BME280.h"
+#else
+#include "BMP280.h"
+#endif
+#include "TextLCD.h"
+#include "SDBlockDevice.h"
+#include "FATFileSystem.h"
+
+enum ELEC350_ERROR_CODE {OK, FATAL};
+
+extern DigitalOut onBoardLED;
+extern DigitalOut redLED;
+extern DigitalOut yellowLED;
+extern DigitalOut greenLED;
+
+extern DigitalIn  onBoardSwitch;
+extern DigitalIn  SW1;
+extern DigitalIn  SW2;
+//extern Serial pc;
+extern AnalogIn adcIn;
+
+#ifdef BME
+extern BME280 sensor;
+#else
+extern BMP280 sensor;
+#endif
+
+extern TextLCD lcd;
+extern SDBlockDevice sd;
+
+extern void post();
+extern void errorCode(ELEC350_ERROR_CODE err);
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sd-driver.lib	Sat Nov 24 09:35:51 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/sd-driver/#ae7e7440054c9447f8255bdccbcc523b3f6dffe4