Сбор информации о погодных условиях

Dependencies:   RF24 USBDevice mbed

Files at this revision

API Documentation at this revision

Comitter:
pro100kot14
Date:
Wed Oct 28 20:49:13 2015 +0000
Parent:
2:ad2653bcf93f
Child:
4:7cd67d988145
Commit message:
Added the class that implements the data transmission through the module nRF24L01;; Running waiting for requests and responses to them

Changed in this revision

Illumination.h Show annotated file Show diff for this revision Revisions of this file
Photoresistor.cpp Show annotated file Show diff for this revision Revisions of this file
RequestsCodes.h Show annotated file Show diff for this revision Revisions of this file
WirelessListener.cpp Show annotated file Show diff for this revision Revisions of this file
WirelessListener.h 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
--- a/Illumination.h	Thu Oct 22 20:31:06 2015 +0000
+++ b/Illumination.h	Wed Oct 28 20:49:13 2015 +0000
@@ -2,7 +2,7 @@
 #define ILLUMINATION_H
 
 enum Illumination{
-    dark, veryCloudy, cloudy, clear, verySunny
+    DARK, VERY_CLOUDLY, CLOUDLY, CLEAR, VERY_SUNNY
 };
 
 #endif
\ No newline at end of file
--- a/Photoresistor.cpp	Thu Oct 22 20:31:06 2015 +0000
+++ b/Photoresistor.cpp	Wed Oct 28 20:49:13 2015 +0000
@@ -9,12 +9,12 @@
     
     if(realV > 0){
         double resistance = (10.0 * 3.3) / realV - 10.0; //resistance in kOhms.
-        if(resistance >= 1000.0) return dark;
-        if(resistance >= 19.0) return veryCloudy; //approximately <10 lx
-        if(resistance >= 0.7) return cloudy; //approximately <5000 lx
-        if(resistance >= 0.25) return clear; //approximately <10000 lx
-        return verySunny; //approximately >10000 lx
+        if(resistance >= 1000.0) return DARK;
+        if(resistance >= 19.0) return VERY_CLOUDLY; //approximately <10 lx
+        if(resistance >= 0.7) return CLOUDLY; //approximately <5000 lx
+        if(resistance >= 0.25) return CLEAR; //approximately <10000 lx
+        return VERY_SUNNY; //approximately >10000 lx
     }else{
-            return dark; //resistance == +inf. No light 
+            return DARK; //resistance == +inf. No light 
         }
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RequestsCodes.h	Wed Oct 28 20:49:13 2015 +0000
@@ -0,0 +1,10 @@
+#ifndef REQUESTSCODES_H
+#define REQUESTSCODES_H
+
+enum RequestsCodes{
+    SHADE_TEMPERATURE = 101,
+    LIGHT_TEMPERATURE,
+    LIGHT,
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WirelessListener.cpp	Wed Oct 28 20:49:13 2015 +0000
@@ -0,0 +1,74 @@
+#include "WirelessListener.h"
+
+WirelessListener::WirelessListener(uint64_t readPipe, uint64_t writePipe){
+    this->readPipe = readPipe;
+    this->writePipe = writePipe;
+    
+    radio = new RF24(P1_22, P1_21, P1_20, P1_25, P1_24);
+    wirelessInit();
+    
+    termTmp36 = new ThermometerTmp36(AnalogIn(A0));
+    term503 = new Thermistor(AnalogIn(A1), 0.001995, 0.00007997, 0.0000003863);
+    //term503->setError(-5000);
+    photores = new Photoresistor(AnalogIn(A2));
+    
+    
+}
+
+WirelessListener::~WirelessListener(){
+    delete(radio);
+    delete(termTmp36);
+    delete(term503);
+    delete(photores);
+}
+
+void WirelessListener::wirelessInit(){
+    radio->begin();
+    radio->setAutoAck(true);
+    radio->enableAckPayload();
+    radio->powerUp();
+    radio->setPALevel(RF24_PA_HIGH);
+    radio->setDataRate(RF24_2MBPS);
+    radio->setRetries(15,15); // Retry 15 times with 4000us delay between retries.
+    radio->openReadingPipe(1,readPipe);
+    radio->openWritingPipe(writePipe);   
+}
+
+void WirelessListener::startListening(){
+    radio->startListening();
+    //--------------------------------
+    //платформозависимая от базы часть
+        short request;
+        short response;
+    //--------------------------------
+    while(1){
+        if(radio->available()){
+        #ifdef DEBUG
+            pc.printf("available()\r\n");
+        #endif
+        radio->read(&request, sizeof(request));
+        #ifdef DEBUG
+            pc.printf("request = %d\r\n", request);
+        #endif
+        switch(request){
+        case SHADE_TEMPERATURE:
+            response = termTmp36->getTemperature()*100.0;
+            break;
+        case LIGHT_TEMPERATURE:
+            response = term503->getTemperature()*100.0;
+            break;   
+        case LIGHT:
+            response = photores->getIllumination();
+            break;
+        default:
+            response = 0;
+        }
+        #ifdef DEBUG
+        pc.printf("response = %d\r\n", response);
+        #endif
+        radio->stopListening();
+        radio->write(&response, sizeof(response));
+        radio->startListening();
+        }
+    }    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WirelessListener.h	Wed Oct 28 20:49:13 2015 +0000
@@ -0,0 +1,58 @@
+#ifndef WIRELESSLISTENER_H
+#define WIRELESSLISTENER_H
+
+#include "mbed.h"
+#include "RF24.h"
+#include "RequestsCodes.h"
+#include "ThermometerTmp36.h"
+#include "Thermistor.h"
+#include "Photoresistor.h"
+
+//#define DEBUG
+
+#ifdef DEBUG
+#include "USBSerial.h"
+#endif
+
+/**
+* Encapsulates inside accept requests, processing (reading sensor data) and transmission back
+*/
+class WirelessListener{  
+public:
+    /**
+    * The request handler
+    * @param readPipe Pipe identifier for receiving
+    * @param writePipe Pipe identifier for transmission 
+    */
+    WirelessListener(uint64_t readPipe, uint64_t writePipe);
+    
+    ~WirelessListener();
+   
+    /**
+    * Start receiving and processing requests
+    */    
+    void startListening();
+    
+    
+private:
+#ifdef DEBUG
+    USBSerial pc;
+#endif
+    //Initialization of the transmitter
+    void wirelessInit();
+    
+    //Receiving pipe
+    uint64_t readPipe;
+    //Transmission pipe
+    uint64_t writePipe;
+    //Transmitter
+    RF24 *radio;
+    
+    //Sensors
+    ThermometerTmp36 *termTmp36;
+    Thermistor *term503;
+    Photoresistor *photores;
+    
+};
+
+#endif
\ No newline at end of file
--- a/main.cpp	Thu Oct 22 20:31:06 2015 +0000
+++ b/main.cpp	Wed Oct 28 20:49:13 2015 +0000
@@ -1,27 +1,9 @@
-#include "mbed.h"
-#include "USBSerial.h"
-#include "ThermometerTmp36.h"
-#include "Thermistor.h"
-#include "Photoresistor.h"
+#include "WirelessListener.h"
 
-USBSerial pc;
-AnalogIn   tmp36(A0);
-AnalogIn   thermist(A2);
-AnalogIn   photoresist(A4);
-char lightLevels[][12]={
-    "Dark", 
-    "Very cloudy",
-    "Cloudy", 
-    "Clear", 
-    "Very sunny"
-    };
-int main() {
-    ThermometerTmp36 termTmp36(tmp36);
-    Thermistor term503(thermist, 0.001995, 0.00007997, 0.0000003863);
-    Photoresistor light(photoresist);
-    term503.setError(-5000);
-    while(1) {
-        pc.printf("TMP36: %f degC\tTermistor: %f degC\t%s\r\n", termTmp36.getTemperature(), term503.getTemperature(), lightLevels[light.getIllumination()]);
-        wait(1);
-    }
-}
+int main(){
+    WirelessListener listener(0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL);
+    
+    listener.startListening();
+    
+    return 0;    
+}
\ No newline at end of file