Demo of the sample LCD class, BMP280 Sensor and network with power on self test. Requires a network connectionb
Dependencies: BMP280 TextLCD BME280
Revision 6:9a95a39d1cce, committed 2018-11-24
- Comitter:
- noutram
- Date:
- Sat Nov 24 08:44:15 2018 +0000
- Parent:
- 5:5fd848b23b3e
- Commit message:
- simplify;
Changed in this revision
--- a/ELEC350-Hardware.lib Sat Nov 24 08:20:16 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://os.mbed.com/teams/University-of-Plymouth-Stage-2-and-3/code/ELEC350-Coursework-2017/#df979097cc71
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Networkbits.cpp Sat Nov 24 08:44:15 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(ð); + + /* 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 08:44:15 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/sample_hardware.cpp Sat Nov 24 08:44:15 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 08:44:15 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