Melbourne specific temperature/humidity sensor

Dependencies:   EALib EthernetInterface SHTx mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "QSPIFileSystem.h"
00004 #include "SHTx/sht15.hpp"
00005  
00006  
00007 #define ECHO_SERVER_PORT   7
00008 #define on 0
00009 #define off 1
00010 #define DESIRED_SIZE_IN_MB  (1)
00011 
00012 // devices 
00013 Serial pc(USBTX, USBRX); // tx, rx
00014 DigitalOut led1(LED1);
00015 DigitalOut led2(LED2);
00016 DigitalOut led3(LED3);
00017 DigitalOut led4(LED4);
00018 DigitalIn ipReset(p23,PullUp);
00019 QSPIFileSystem qspifs("qspi");
00020 SHTx::SHT15 sensor(p32, p31);
00021 EthernetInterface eth;
00022 TCPSocketServer server;
00023 TCPSocketConnection client;
00024 Ticker oneSecondTick;
00025 DigitalOut hostQueryLED(p24);
00026 DigitalOut timingPulse(p25);
00027 
00028 // variables
00029 char ipStr[20];
00030 char subnetStr[20];
00031 char gatewayStr[20]; 
00032 char inBuff[256];
00033 char outBuff[256];
00034 float currTemperature = 25.4;
00035 float currHumidity = 78.3;
00036 int flashLedCnt = 4;
00037 int cnt = 0;
00038 bool sensorCommsFault;
00039 int ipResetCount = 0;
00040  
00041 // procedure declarations
00042 void initSensor();
00043 void readSensor();
00044 void oneSecondTimer();
00045 bool readConfigFile();
00046 void writeConfigFile();
00047 void processRcvBuffer(char *inBuff);
00048 
00049  
00050  
00051 int main() 
00052 {
00053     char *macAddr;
00054     //char *hello = "Hello\n";
00055     
00056     // get mac address and print it to PC
00057     //macAddr = eth.getMACAddress();
00058     //pc.printf("MAC address %s\n", macAddr);
00059     
00060     // check if file system already formatted
00061     if (!qspifs.isformatted()) 
00062     {        pc.printf("Format qspi file system\n");
00063         qspifs.format(DESIRED_SIZE_IN_MB);
00064     }
00065  
00066     // try to read config file which contains the IP address, subnet and gateway
00067 /*    if (readConfigFile())
00068     {
00069         pc.printf("Config file ethernet details\n %s\n %s\n %s\n", ipStr, subnetStr, gatewayStr);
00070         // set IP address from config file    
00071         eth.init(ipStr, subnetStr, gatewayStr);
00072     }
00073     else
00074     {
00075         pc.printf("default ethernet address\n");
00076         // set to a default IP address, subnet and gateway
00077         strcpy(ipStr, "10.39.123.123");
00078         strcpy(subnetStr, "255.255.0.0");
00079         strcpy(gatewayStr,"10.39.1.1");
00080 
00081         // initialise the IP stack to these defaults
00082         eth.init(ipStr, subnetStr, gatewayStr); 
00083         
00084         // save default settings as working config
00085         writeConfigFile();
00086     }
00087 */
00088 
00089     eth.init(); //Use DHCP
00090     eth.connect();
00091 
00092     // get mac address and print it to PC
00093     macAddr = eth.getMACAddress();
00094     pc.printf("MAC address %s\n", macAddr);
00095 
00096     pc.printf("IP Address is %s\n", eth.getIPAddress());
00097 
00098     // turn off all the leds on the chip
00099     led1 = 1;
00100     led2 = 1;
00101     led3 = 0;
00102     led4 = 0;
00103   
00104     // bind to the required port      
00105     server.bind(ECHO_SERVER_PORT);
00106     
00107     // put into listen mode
00108     server.listen();
00109     
00110     // initialise the sensor
00111     initSensor();
00112     
00113     // start the one second tick that does periodic tasks such as read the sensor and toggle the activity LED
00114     oneSecondTick.attach(&oneSecondTimer, 1);
00115     
00116     // do forever
00117     while (1)
00118     {
00119         // configure stack ready to accept a client connection        
00120         server.accept(client);
00121  
00122         // wait for a connection request from the client (receipt of a packet)
00123         client.set_blocking(true, 0);
00124         
00125         // get received packet from client
00126         int n = client.receive(inBuff, sizeof(inBuff));
00127 
00128         // process the request from the client
00129         processRcvBuffer(inBuff);
00130         
00131         // close the client connection        
00132         client.close();
00133 
00134         // pulse LED 4 to show ethernet activity
00135         hostQueryLED = 0;
00136         wait(0.1);
00137         hostQueryLED = 1;
00138 
00139         //pc.printf("Sensor update\n");
00140         //pc.printf("Temp %5.1f, Humidity %5.1f\n", currTemperature, currHumidity );
00141     }}
00142 
00143 
00144 
00145 void initSensor()
00146 {
00147     
00148     // reset sensor just to check if it can be communicated with (returns true)
00149     if (sensor.reset())
00150     {
00151         sensorCommsFault = false;
00152         pc.printf("Sensor reset\n");
00153         
00154         // set temperature in celcius
00155         sensor.setScale(false);
00156 
00157         // optimise speed of sensor
00158         if (!sensor.setOTPReload(false))
00159         {
00160             sensorCommsFault = true;
00161             pc.printf("Set OTP reload\n");
00162         }
00163                     
00164         // set resolution to high (14 bits for temperature, 12 bits for humidity)
00165         if (!sensor.setResolution(false))
00166         {
00167             sensorCommsFault = true;
00168             pc.printf("Set resolution\n");
00169         }
00170     }
00171     else
00172     {
00173         sensorCommsFault = true;
00174         pc.printf("Sensor comms fault\n");
00175     }
00176 }
00177     
00178 
00179 void readSensor() 
00180 {
00181     bool goodRead = false;
00182     
00183     if (!sensorCommsFault)
00184     {
00185         timingPulse = 1;
00186         
00187         goodRead = sensor.update();
00188     
00189         timingPulse = 0;
00190 
00191         // trigger a measurement
00192         if (goodRead == true)
00193         {
00194             // get temperature in celcius
00195             currTemperature = sensor.getTemperature();
00196         
00197             // get relative humidity
00198             currHumidity = sensor.getHumidity();
00199         }
00200         else
00201         {
00202             currTemperature = -1.0F;
00203                 
00204             currHumidity = -1.0F;
00205         }    
00206             
00207     }
00208     else            // return error values
00209     {    
00210         currTemperature = -1.0F;
00211             
00212         currHumidity = -1.0F;
00213     }        
00214 }
00215 
00216 
00217 
00218 void oneSecondTimer()
00219 {
00220     
00221     // if end of pulse sequence and LED still lit
00222     if (flashLedCnt == 0 && led4 == 1)
00223     {
00224         // turn off LED
00225         led4 = 0;
00226     }
00227         
00228     // if location LED still requires pulsing
00229     if (flashLedCnt > 0)
00230     {
00231         // toggle the LED indicator
00232         led4 = flashLedCnt & 1;
00233         
00234         // decrement the count
00235         flashLedCnt--;
00236     }
00237 
00238     // check if user pushbutton pressed (low)
00239     if (ipReset == 0)
00240     {
00241         // increment the counter
00242         ipResetCount++;
00243 
00244         // turn on a LED for seconds 1 to 4
00245         switch (ipResetCount)
00246         {
00247             case 1:
00248                 led1 = 0;
00249                 break;
00250             case 2:
00251                 led2 = 0;
00252                 break;
00253             case 3:
00254                 led3 = 1;
00255                 break;
00256             case 4:
00257                 led4 = 1;
00258                 break;
00259             case 5:
00260                 // turn off all the leds on the chip to indicate change of IP address
00261                 led1 = 1;
00262                 led2 = 1;
00263                 led3 = 0;
00264                 led4 = 0;
00265                 break;
00266             default: ;
00267         }
00268         
00269         // if button pressed for 5 seconds
00270         if (ipResetCount >= 5)
00271         {
00272             // initialise the IP address back to the default value
00273             strcpy(ipStr, "10.39.123.123");
00274             strcpy(subnetStr, "255.255.0.0");
00275             strcpy(gatewayStr,"10.39.1.1");
00276             
00277             // save to config file
00278             writeConfigFile();
00279         }
00280     }
00281     else
00282     {
00283         // reset the counter
00284         ipResetCount = 0;
00285 
00286         // turn off all the leds on the chip
00287         led1 = 1;
00288         led2 = 1;
00289         led3 = 0;
00290         led4 = 0;
00291     }
00292 }
00293 
00294 
00295 bool readConfigFile()
00296 {
00297     int digits;
00298     int octet1, octet2, octet3, octet4;
00299     bool ipOk = false;
00300     bool subnetOk = false;
00301     bool gatewayOk = false;
00302 
00303     // open file pointer to config data    
00304     FILE *fp = fopen("/qspi/config.dat", "r");
00305 
00306     // if open successful
00307     if (fp != NULL)
00308     {
00309         // read 20 byte blocks for each parameter
00310         fread(ipStr, 20, 1, fp);
00311         fread(subnetStr, 20, 1, fp); 
00312         fread(gatewayStr, 20, 1, fp); 
00313         
00314         // close the file pointer
00315         fclose(fp);
00316 
00317         // break the IP address down to individual octets and return number of octets read
00318         digits = sscanf(ipStr, "%d.%d.%d.%d", &octet1, &octet2, &octet3, &octet4);
00319 
00320         // check if 4 octets and all within range
00321         if (digits == 4 && octet1 >=0 && octet1 < 256 && octet2 >=0 && octet2 < 256 && octet3 >=0 && octet3 < 256 && octet4 >=0 && octet4 < 256)
00322             ipOk = true;
00323 
00324         // break the subnet down to individual octets and return number of octets read
00325         digits = sscanf(subnetStr, "%d.%d.%d.%d", &octet1, &octet2, &octet3, &octet4);
00326         
00327         // check if 4 octets and all within range
00328         if (digits == 4 && octet1 >=0 && octet1 < 256 && octet2 >=0 && octet2 < 256 && octet3 >=0 && octet3 < 256 && octet4 >=0 && octet4 < 256)
00329             subnetOk = true;
00330             
00331         // break the gateway down to individual octets and return number of octets read
00332         digits = sscanf(gatewayStr, "%d.%d.%d.%d", &octet1, &octet2, &octet3, &octet4);
00333         
00334         // check if 4 octets and all within range
00335         if (digits == 4 && octet1 >=0 && octet1 < 256 && octet2 >=0 && octet2 < 256 && octet3 >=0 && octet3 < 256 && octet4 >=0 && octet4 < 256)
00336             gatewayOk = true;
00337             
00338         // if IP address, subnet and gateway valid
00339         if (ipOk && subnetOk && gatewayOk)
00340             return true;
00341         else
00342             return false;
00343     }
00344     return false;
00345 }
00346 
00347 
00348 
00349 void writeConfigFile()
00350 {
00351 
00352     // open file pointer to config data file
00353     FILE *fp = fopen("/qspi/config.dat", "w");
00354 
00355     // if valid handle returned
00356     if (fp != NULL)
00357     {
00358         // write the IP address, subnet and gateway as 3 twenty byte blocks
00359         fwrite(ipStr, 20, 1, fp);
00360         fwrite(subnetStr, 20, 1, fp); 
00361         fwrite(gatewayStr, 20, 1, fp); 
00362 
00363         // close the file handle
00364         fclose(fp);
00365     }
00366 }
00367 
00368 
00369     
00370     
00371 
00372                 
00373 void processRcvBuffer(char *inBuff)
00374 {
00375     int digits;
00376     int octet1, octet2, octet3, octet4;
00377     bool ipOk = false;
00378     bool subnetOk = false;
00379     bool gatewayOk = false;
00380     char *cPtr = outBuff;
00381     
00382     switch (inBuff[0])
00383     {
00384         case 'r':                 // return temperature and humidity readings
00385             
00386             readSensor();
00387             
00388             // get temperature and humidity in a string            
00389             sprintf( cPtr, "%5.1f\n%5.1f\n", currTemperature, currHumidity );
00390 
00391             // send readings back to client
00392             client.send_all(outBuff, strlen(outBuff));       
00393             break;
00394         case 'i':                 // could be id or ip
00395             switch (inBuff[1])
00396             {
00397                 case 'd':                         // flash a led to identify this device
00398                     flashLedCnt = 10;
00399                     break;
00400                 case 'p':                         // set the IP address to that contained in this packet
00401                     // split packet up into seperate strings - ip add, subnet and gateway
00402                     sscanf(inBuff, "ip %s\n%s\n%s", ipStr, subnetStr, gatewayStr);
00403                     
00404                     //pc.printf("ip %s\nSubnet %s\ngateway %s\n", ipStr, subnetStr, gatewayStr);
00405                     
00406                     // check if ip address valid
00407                     digits = sscanf(ipStr, "%d.%d.%d.%d", &octet1, &octet2, &octet3, &octet4);
00408         
00409                     if (digits == 4 && octet1 >=0 && octet1 < 256 && octet2 >=0 && octet2 < 256 && octet3 >=0 && octet3 < 256 && octet4 >=0 && octet4 < 256)
00410                         ipOk = true;
00411 
00412                     //pc.printf("ip %d.%d.%d.%d\n", octet1, octet2, octet3, octet4 );
00413                     
00414                     // check if subnet valid
00415                     digits = sscanf(subnetStr, "%d.%d.%d.%d", &octet1, &octet2, &octet3, &octet4);
00416         
00417                     //pc.printf("subnet %d.%d.%d.%d\n", octet1, octet2, octet3, octet4 );
00418 
00419                     if (digits == 4 && octet1 >=0 && octet1 < 256 && octet2 >=0 && octet2 < 256 && octet3 >=0 && octet3 < 256 && octet4 >=0 && octet4 < 256)
00420                         subnetOk = true;
00421                     
00422                     // check if gateway valid
00423                     digits = sscanf(gatewayStr, "%d.%d.%d.%d", &octet1, &octet2, &octet3, &octet4);
00424         
00425                     //pc.printf("gateway %d.%d.%d.%d\n", octet1, octet2, octet3, octet4 );
00426 
00427                     if (digits == 4 && octet1 >=0 && octet1 < 256 && octet2 >=0 && octet2 < 256 && octet3 >=0 && octet3 < 256 && octet4 >=0 && octet4 < 256)
00428                         gatewayOk = true;
00429 
00430                     // if all valid
00431                     if (ipOk && subnetOk && gatewayOk)
00432                     {
00433                         //pc.printf("Changing IP settings\n");
00434                         
00435                         // save to config file
00436                         writeConfigFile();
00437                     }
00438                     break;
00439                 default: ;
00440             }
00441             break;
00442         default: 
00443             break;
00444     }
00445 }                    
00446