Advertisement
Guest User

Untitled

a guest
Nov 11th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #include "SFE_TSL2561.h"
  2. #include "dht.h"
  3.  
  4.  
  5. #define DHTPIN D4
  6. #define SOUNDPIN A0
  7. #define PHPIN A1
  8. #define FLOATHIGHPIN A2
  9. #define FLOATLOWPIN A3
  10. #define DHTTYPE DHT11
  11.  
  12.  
  13.  
  14. // Initialize sensor classes
  15. DHT dht(DHTPIN, DHTTYPE);
  16. SFE_TSL2561 tsl = SFE_TSL2561();
  17.  
  18. // Initialize variables
  19. char eventinfo[64];
  20. unsigned int ms;
  21. int publishdelay = 60 * 1000;
  22.  
  23.  
  24. // Initialize PH Sensor
  25. void InitializePHSensor(){
  26.     pinMode(PHPIN, INPUT);
  27. }
  28.  
  29. // Initialize Tank Monitor
  30. void InitializeTankLevel(){
  31.     pinMode(FLOATLOWPIN, INPUT);
  32.     pinMode(FLOATHIGHPIN, INPUT);
  33. }
  34.  
  35. // Initialize DHT
  36. void InitializeDHT(){
  37.     dht.begin();
  38. }
  39.  
  40. // Initialize TSL2561
  41. void InitializeTSL2561(){
  42.     unsigned char id;
  43.    
  44.     tsl.begin();
  45.    
  46.     tsl.getID(id);
  47.     tsl.setTiming(0, 2, ms);
  48.     tsl.setPowerUp();
  49. }
  50.  
  51. // Initialize Sound Sensor
  52. void InitializeSoundSensor(){
  53.     pinMode(SOUNDPIN, INPUT);
  54. }
  55.  
  56. // Publish PH info
  57. void PublishPHInfo(){
  58.      int sensorValue = analogRead(PHPIN);      
  59.      int  pH = (0.0178 * sensorValue - 1.889);    
  60.      
  61.     sprintf(eventinfo, "pH=%.2f|pH", pH);
  62.    
  63.     Publish(eventinfo);
  64. }
  65.  
  66. // Publish Temperature, Humidity, Dew Point
  67. void PublishDHTInfo(){
  68.     float h = dht.readHumidity();
  69.     float t = dht.readTemperature();
  70.     float d = dht.dewPoint(t, h);
  71.  
  72.     sprintf(eventinfo, "Temperature=%.2f|�C,Humidity=%.2f|%%,Dew Point=%.2f|�C", t, h, d);
  73.    
  74.     Publish(eventinfo);
  75. }
  76.  
  77. // Publish  Tank Level
  78. void PublishTankLevelInfo(){
  79.  
  80.     int levelHigh = LOW;
  81.     int levelLow = LOW;
  82.  
  83.     levelHigh = digitalRead(FLOATHIGHPIN);
  84.     levelLow = digitalRead(FLOATLOWPIN);
  85.  
  86. if(levelHigh == LOW){
  87.         sprintf(eventinfo, "Tank Level= (HalfFull)|");
  88.     }
  89.     else if(levelLow == LOW){
  90.         sprintf(eventinfo, "Tank Level= (Fill Now!)|");
  91.     }
  92.     else{
  93.         sprintf(eventinfo, "Tank Level= (Full)|");
  94.     }
  95.    
  96.     Publish(eventinfo);
  97. }
  98.  
  99. // Publish  luminosity
  100. void PublishTSL2561Info(){
  101.     unsigned int data1;
  102.     unsigned int data2;
  103.     bool gain;
  104.  
  105.     tsl.getData(data1, data2);
  106.    
  107.     double lux;
  108.     tsl.getLux(gain,ms,data1,data2,lux);
  109.    
  110.     sprintf(eventinfo, "Lux=%.2f|Lux", lux);
  111.    
  112.     Publish(eventinfo);
  113. }
  114.  
  115. // Publish sound info
  116. void PublishSoundInfo(){
  117.     // map analog reading (0-4095) to (0-100)
  118.     int level = map(analogRead(SOUNDPIN), 0, 4095, 0, 100);
  119.    
  120.     // Just some arbitrary values
  121.     if(level > 60){
  122.         sprintf(eventinfo, "Sound Level=%d (High)|", level);
  123.     }
  124.     else if(level > 30){
  125.         sprintf(eventinfo, "Sound Level=%d (Medium)|", level);
  126.     }
  127.     else{
  128.         sprintf(eventinfo, "Sound Level=%d (Low)|", level);
  129.     }
  130.    
  131.     Publish(eventinfo);
  132. }
  133.  
  134. // Publush event
  135. void Publish(char* szEventInfo){
  136.     Spark.publish("Current Conditions", szEventInfo);
  137. }
  138.  
  139. // Initialize applicaiton
  140. void InitializeApplication(){
  141.     Serial.begin(9600);
  142.     pinMode(D7, OUTPUT);
  143. }
  144.  
  145. // Blink LED and wait for some time
  146. void BlinkLED(){
  147.     digitalWrite(D7, HIGH);  
  148.     delay(500);
  149.     digitalWrite(D7, LOW);  
  150.     delay(500);
  151.    
  152.     delay(publishdelay);
  153. }
  154.  
  155.  
  156.  
  157. void setup(){
  158.  
  159.     InitializeApplication();
  160.    
  161.     Serial.println("Initializing sensors...");
  162.        
  163.     InitializePHSensor();
  164.     InitializeDHT();
  165.     InitializeTSL2561();
  166.     InitializeSoundSensor();
  167.     InitializeTankLevel();
  168.  
  169. }
  170.  
  171. void loop()
  172. {
  173.     // Publish events. Wait for 2 second between publishes
  174.     PublishDHTInfo();
  175.     delay(2000);
  176.     PublishPHInfo();
  177.     delay(2000);
  178.     PublishTankLevelInfo();
  179.     delay(2000);
  180.     PublishTSL2561Info();
  181.     delay(2000);
  182.     PublishSoundInfo();
  183.     delay(2000);
  184.    
  185.     BlinkLED();  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement