Lab3 - Pressure, temperature, and humidity sensors displayed on a webpage.

Dependencies:   EthernetNetIf NTPClient_NetServices GPS mbed HTTPServer SDFileSystem

Files at this revision

API Documentation at this revision

Comitter:
kadams6
Date:
Tue Oct 05 19:34:40 2010 +0000
Commit message:

Changed in this revision

EthernetNetIf.lib Show annotated file Show diff for this revision Revisions of this file
FATFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
GPS.lib Show annotated file Show diff for this revision Revisions of this file
HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
HTTPServer.lib Show annotated file Show diff for this revision Revisions of this file
NTPClient.lib Show annotated file Show diff for this revision Revisions of this file
PachubeClient.cpp Show annotated file Show diff for this revision Revisions of this file
PachubeClient.h Show annotated file Show diff for this revision Revisions of this file
SCP1000.h Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.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
main.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r dfd0841721d5 EthernetNetIf.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetNetIf.lib	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
diff -r 000000000000 -r dfd0841721d5 FATFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FATFileSystem.lib	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_unsupported/code/fatfilesystem/
\ No newline at end of file
diff -r 000000000000 -r dfd0841721d5 GPS.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPS.lib	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/GPS/#15611c7938a3
diff -r 000000000000 -r dfd0841721d5 HTTPClient.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/HTTPClient/#d97a4fc01c86
diff -r 000000000000 -r dfd0841721d5 HTTPServer.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPServer.lib	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/HTTPServer/#d753966e4d97
diff -r 000000000000 -r dfd0841721d5 NTPClient.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NTPClient.lib	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/NTPClient/#7c3f1199256a
diff -r 000000000000 -r dfd0841721d5 PachubeClient.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PachubeClient.cpp	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,32 @@
+#include "PachubeClient.h"
+
+PachubeClient::PachubeClient(const string& apiKey) : _client(), _csvContent("text/csv") {
+    _client.setRequestHeader("X-PachubeApiKey", apiKey);
+}
+
+PachubeClient::~PachubeClient() {
+}
+
+// put csv method to feed
+void PachubeClient::PutCsv(const string& environmentID, const string& data) {
+    _csvContent.set(data);
+    string uri = "http://api.pachube.com/v1/feeds/" + environmentID + ".csv?_method=put";
+    _result = _client.post(uri.c_str(), _csvContent, NULL);
+    _response = _client.getHTTPResponseCode();
+}
+
+// put csv method to datastream
+void PachubeClient::PutCsv(const string& environmentID, const string& datastreamID, const string& data) {
+    _csvContent.set(data);
+    string uri = "http://api.pachube.com/v1/feeds/" + environmentID + "/datastreams/" + datastreamID + ".csv?_method=put";
+    _result = _client.post(uri.c_str(), _csvContent, NULL);
+    _response = _client.getHTTPResponseCode();
+}
+
+// http result and response
+HTTPResult PachubeClient::Result() {
+    return _result;
+}
+int PachubeClient::Response() {
+    return _response;
+}
diff -r 000000000000 -r dfd0841721d5 PachubeClient.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PachubeClient.h	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,35 @@
+#ifndef PACHUBECLIENT_H_
+#define PACHUBECLIENT_H_
+
+#include <mbed.h>
+#include <HTTPClient.h>
+
+class PachubeClient {
+
+public:
+    // constructor and destructor
+    PachubeClient(const string& apiKey);
+    virtual ~PachubeClient();
+
+    // put csv method to feed
+    void PutCsv(const string& environmentID, const string& data);
+
+    // put csv method to datastream
+    void PutCsv(const string& environmentID, const string& datastreamID, const string& data);
+
+    // http result and response
+    HTTPResult Result();
+    int Response();
+            
+private:
+    // http client and data
+    HTTPClient _client;
+    HTTPText _csvContent;
+    
+    // http result and response
+    HTTPResult _result;
+    int _response;
+    
+};
+
+#endif // PACHUBECLIENT_H_
diff -r 000000000000 -r dfd0841721d5 SCP1000.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SCP1000.h	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,84 @@
+#ifndef _SCP1000_H
+#define _SCP1000_H
+
+#include "mbed.h"
+
+class SCP1000 {
+    public:
+        SCP1000(PinName mosi, PinName miso, PinName sclk, PinName cs)
+            : m_spi(mosi, miso, sclk)
+            , m_cs(cs) {
+            m_cs=1;
+            m_spi.frequency(500000); // the fastest of the sensor
+            m_spi.format(8, 0); // duda son dos palabras de 8 bits? 
+            wait(0.5);
+            //------------------------------------------------
+            // pc.printf("RESET\r\n");
+            write_register(0x06,0x01);
+            wait(0.5);
+
+            // pc.printf("Initialize High Resolution Constant Reading Mode\r\n");
+            write_register(0x03,0x0A);
+            wait(0.5);
+        };
+        
+        ~SCP1000() { /* empty */ };
+        
+        unsigned long readPressure() {
+            unsigned long pressure_msb = read_register(PRESSURE);
+            pressure_msb &= 0x07;
+            unsigned long pressure_lsb = read_register16(PRESSURE_LSB);
+            unsigned long pressure = ((pressure_msb<<16)| pressure_lsb);
+            pressure /= 4;
+            
+            return pressure;
+        };
+        
+        float readTemperature() {
+            float temp_in = read_register16(TEMP);
+            temp_in /= 20;
+            return temp_in;
+        };
+        
+        
+    private:
+        static const char PRESSURE = 0x1F;   //Pressure 3 MSB
+        static const char PRESSURE_LSB = 0x20; //Pressure 16 LSB
+        static const char TEMP = 0x21;       //16 bit temp
+        SPI m_spi;
+        DigitalOut m_cs;
+        
+        char read_register(char register_name) {
+            register_name <<=2;
+            register_name &= 0xFC;
+            m_cs=0; //Select SPI device
+            m_spi.write(register_name); //Send register location
+            char register_value=m_spi.write(0x00);
+            m_cs=1;
+            return register_value;  
+        };
+
+        void write_register(char register_name, char register_value) {
+            register_name <<= 2;
+            register_name |= 0x02; //le estamos diciendo que escriba
+            m_cs=0; //Select SPI device
+            m_spi.write(register_name); //Send register location
+            m_spi.write(register_value); //Send value to record into register
+            m_cs=1;
+        }; 
+
+        float read_register16(char register_name) {   
+            register_name <<= 2;
+            register_name &= 0xFC; //Read command
+            m_cs=0; //Select SPI Device
+            m_spi.write(register_name); //Write byte to device
+            int in_byte1 = m_spi.write(0x00);    
+            int in_byte2 = m_spi.write(0x00);
+            m_cs=1;
+            float in_word= (in_byte1<<=8) | (in_byte2);   
+            return(in_word);
+        };
+
+};
+
+#endif // _SCP1000_H
\ No newline at end of file
diff -r 000000000000 -r dfd0841721d5 SDFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/SDFileSystem/#b1ddfc9a9b25
diff -r 000000000000 -r dfd0841721d5 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,200 @@
+#include "mbed.h"
+
+//#define SD_FS
+
+#include "EthernetNetIf.h"
+#include "HTTPServer.h"
+#include "HTTPClient.h"
+#ifdef SD_FS
+#include "SDFileSystem.h"
+#endif
+#include "GPS.h"
+#include "NTPClient.h"
+
+#include "SCP1000.h"
+#include "PachubeClient.h"
+#include "main.h"
+
+/***********
+ *   I/O   *
+ ***********/
+DigitalOut listenStatus(LED1);          // Blink when HTTP server is listening
+DigitalOut ipStatus(LED2);              // Set when IP address acquired
+DigitalOut pollStatus(LED3);            // Reserved
+DigitalOut errorStatus(LED4);           // Blink when error condition occurs; read other LEDs for code.
+
+AnalogIn humiditySensor(p17);           // 
+//AnalogIn tempSensor(p16);               // Temp sensor.
+
+SCP1000 scp1000(p5,p6,p7,p8);           // Temp & pressure.
+
+
+GPS gps(p9, p10);
+
+#ifdef SD_FS
+SDFileSystem sd(p5, p6, p7, p8, "fs");  // the pinout on the mbed Cool Components workshop board
+#else
+LocalFileSystem local("fs");
+#endif
+
+NTPClient ntp;
+PachubeClient pachube("633b3cb4e8ee8ba4a4f6c4f65df283360e0ed3d2ca572c249e532632eb5e19cd");
+
+
+void postFeed(float pressure, float humidity, float temperature) {  
+    char data[256] = {0};
+
+    // feed has two datastreams in this example, values are comma separated
+    sprintf("%5.3f,%5.3f,%5.3f", data, temperature, pressure, humidity);
+
+    pachube.PutCsv("10587", data);
+
+    if (pachube.Result() != HTTP_OK || pachube.Response() != 200) {
+        ledError(ERR_UPFEED);
+    } else {
+        errorStatus = !errorStatus;
+    }
+}
+
+
+/**
+ * Blink the error LED and place the error code on the other LEDs.
+ * WARNING: Never returns!
+ *
+ */
+void ledError(ErrorCode code) {
+    listenStatus = code & 1;
+    ipStatus = (code >> 1) & 1;
+    pollStatus = (code >> 2) & 1;
+
+    while (true) {
+        errorStatus = !errorStatus;
+        wait(BLINK_RATE);
+    }
+}
+
+/**
+ * Read sensor data and write it to file.
+ */
+void pollSensors() {
+    int locked = 0;
+    float temp = 0;
+    //float tempB = 0;
+    
+    float lat = 0;
+    float lng = 0;
+    float pressure = 0;
+    float humidity = 0;
+   
+    char * timeStr;
+    time_t ctTime;
+    
+    pollStatus = !pollStatus;
+    
+    FILE* fp = fopen("/fs/sensors.htm", "w");
+    if (fp == NULL) {
+        ledError(ERR_FILE);
+    }
+
+    // Temp
+    // (SensorValue/4) - 50
+    //temp = tempSensor * 160 - 50; // Analog to deg C
+    //temp = 1.8 * temp + 32; // deg C to F.
+    
+    temp = scp1000.readTemperature();
+    temp = 1.8 * temp + 32; // deg C to F
+    
+    // Pressure
+    unsigned long ulPressure = scp1000.readPressure();
+    pressure = ((float) ulPressure) / 1000.0; // Pa to kPa
+    
+    // Humidity
+    // RH (%) = (SensorValue x 0.1906) - 40.2
+    
+    // This might be wrong.
+    humidity = (1000 * humiditySensor * 0.1906) - 40.2;
+    
+    ctTime = time(NULL);
+    ctTime -= 60 * 60 * 4; // Account for timezone offset.
+    timeStr = ctime(&ctTime);
+    
+    int i = 0;
+    while (timeStr[i] != '\0') {
+        if (timeStr[i] == '\n') {
+            timeStr[i] = ' ';
+        }
+        i++;
+    } 
+    
+
+    // True if GPS is locked.
+    //if (gps.sample()) {
+        //locked = 1;
+        //lat = gps.latitude;
+        //lng = gps.longitude;
+    //}
+    
+    fprintf(fp, "{\n\t\"gpsLocked\": %d,\n", locked);
+    fprintf(fp, "\t\"latitude\": %f,\n", lat);
+    fprintf(fp, "\t\"longitude\": %f,\n", lng);
+    fprintf(fp, "\t\"temperature\": %4.2f,\n", temp);
+    fprintf(fp, "\t\"pressure\": %5.2f,\n", pressure);
+    fprintf(fp, "\t\"time\": \"%s ET\",\n", timeStr);
+    fprintf(fp, "\t\"humidity\": %5.2f\n}", humidity);
+    
+    //postFeed(pressure, humidity, temp);
+    
+    fclose(fp);
+}
+
+/**
+ * Flip listening status LED.
+ */
+void flipLed() {
+    listenStatus = !listenStatus;
+}
+
+/**
+ * Entry point
+ */
+int main() {
+    EthernetNetIf eth;
+    HTTPServer server;
+    
+    ipStatus = 0;
+    listenStatus = 0;
+    pollStatus = 0;
+
+    EthernetErr ethErr = eth.setup();
+    if(ethErr) {
+        ledError(ERR_ETHERNET);
+    }
+    
+    Host ntpServer(IpAddr(), 123, "nist1.columbiacountyga.gov");
+    ntp.setTime(ntpServer);
+    
+    ipStatus = 1;
+    
+    // Web root directory
+    FSHandler::mount("/fs", "/");
+  
+    // Bind the filesystem handler for all requests        
+    server.addHandler<FSHandler>("/"); // "/" matches every URL
+    server.bind(80); // listen port
+    
+    Ticker ledTicker;
+    ledTicker.attach(&flipLed, BLINK_RATE);
+    
+    pollSensors();
+    
+    Ticker pollTicker;
+    pollTicker.attach(&pollSensors, SENSOR_POLL_RATE);
+    
+    // Listen indefinitely
+    while(true)
+    {
+        Net::poll();
+    }
+  
+    return 0;
+}
diff -r 000000000000 -r dfd0841721d5 main.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,18 @@
+#ifndef __MAIN_H
+#define __MAIN_H
+
+typedef enum {
+    ERR_ETHERNET = 0, // Error setting up ethernet interface
+    ERR_FILE,         // Error reading or writing to the sensor data file
+    ERR_UPFEED        // Error while uploading sensor feed
+} ErrorCode;
+
+void ledError(ErrorCode);
+void pollSensors();
+void flipLed();
+void postFeed(float pressure, float humidity, float temperature);
+
+#define BLINK_RATE          0.25
+#define SENSOR_POLL_RATE    30
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r dfd0841721d5 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Oct 05 19:34:40 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e