Advertisement
MatusisDrake

Particle Photon Weather Station

Jan 9th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.88 KB | None | 0 0
  1. #include <math.h>
  2.  
  3. #define temperaturePinOut A7
  4. #define precipitationPinOut D1
  5. #define pressurePinOut D0
  6.  
  7. #define thermistorPinIn A5
  8.  
  9. #define NUMSAMPLES 30
  10. #define thermistorResistor 100000
  11. #define thermistorResistance 100000
  12. #define thermistorBVal 3950
  13. #define thermistorTemp 25
  14.  
  15. #define tTempPinOut DAC1
  16.  
  17. int refresh;  //Refresh time in seconds. Min is about 86 seconds (1000 API calls per day are allowed by forecast.io).
  18. double lastRefresh      = 0;    //Unix time of last refresh
  19.  
  20. int i;
  21.  
  22. int samples[NUMSAMPLES];
  23.  
  24. String storedString;
  25.  
  26. void setup() {
  27.  
  28.     pinMode(temperaturePinOut,             OUTPUT);
  29.     pinMode(precipitationPinOut,             OUTPUT);
  30.     pinMode(tTempPinOut,            OUTPUT);
  31.     pinMode(pressurePinOut,              OUTPUT);
  32.    
  33.     pinMode(thermistorPinIn,        INPUT);
  34.  
  35.     Serial.begin(9600);
  36.  
  37.     // Particle.subscribe("hook-response/john_weather_scraped", gotWeatherData, MY_DEVICES);
  38.     Particle.subscribe("hook-response/John_Weather_Scraped", gotWeatherData, MY_DEVICES);
  39.     // Particle.subscribe("hook-response/John_Weather_Scraped_Current", gotWeatherDataCurrent, MY_DEVICES);
  40.  
  41.     for(int i=0; i<10; i++) {
  42.         Serial.println("waiting " + String(10-i) + " seconds before we publish");
  43.         delay(500);
  44.     }
  45.    
  46.     Time.zone(-5); //Set time zone as central - note that this won't adjust for daylight savings.
  47.    
  48.     Particle.publish("John_Weather_Scraped");
  49.    
  50.     lastRefresh = Time.now();
  51.  
  52. }
  53.  
  54. void loop() {
  55.    
  56.     // Serial.print("Difference: ");
  57.     // Serial.println(Time.now() - lastRefresh);
  58.     // Serial.print("Refresh interval: ");
  59.     // Serial.println(refresh);
  60.     // Serial.print("Logic: ");
  61.     // Serial.println((Time.now() - lastRefresh) > refresh);
  62.  
  63.     if (Time.hour() < 5 || Time.hour() > 23) { //API calls are limited so maximize their use during the normal waking hours
  64.         refresh = 300; //5 Minutes
  65.     }
  66.     else {
  67.         refresh = 90; //1.5 minutes
  68.     }
  69.    
  70.     if ((Time.now() - lastRefresh) > 3600) { //Display an error code if webhook data has not been received within the past 60 minutes
  71.         digitalWrite(temperaturePinOut, LOW);
  72.         delay(500);
  73.         digitalWrite(temperaturePinOut, HIGH);
  74.         delay(500);
  75.         digitalWrite(temperaturePinOut, LOW);
  76.         delay(500);
  77.         digitalWrite(temperaturePinOut, HIGH);
  78.         delay(500);
  79.         digitalWrite(temperaturePinOut, LOW);
  80.         delay(500);
  81.         digitalWrite(temperaturePinOut, HIGH);
  82.         delay(500);
  83.         digitalWrite(temperaturePinOut, LOW);
  84.     }
  85.     else if ((Time.now() - lastRefresh) > refresh) {
  86.         Particle.publish("John_Weather_Scraped");
  87.         lastRefresh = Time.now();
  88.     }
  89.    
  90.     float thermistorVal;
  91.    
  92.     // take N samples in a row, with a slight delay
  93.     for (i=0   ; i< NUMSAMPLES; i++) {
  94.         samples[i] = analogRead(thermistorPinIn);
  95.         delay(10);
  96.     }
  97.  
  98.     // average all the samples out
  99.     thermistorVal = 0;
  100.     for (i=0; i< NUMSAMPLES; i++) {
  101.         thermistorVal += samples[i];
  102.     }
  103.     thermistorVal /= NUMSAMPLES;
  104.  
  105.     //   Serial.print("Average analog reading ");
  106.     //   Serial.println(thermistorVal);
  107.  
  108.     // convert the value to resistance
  109.     thermistorVal = 4096 / thermistorVal - 1;
  110.     thermistorVal = thermistorResistor / thermistorVal;
  111.     // Serial.print("Thermistor resistance ");
  112.     // Serial.println(thermistorVal);
  113.  
  114.     float steinhart;
  115.     steinhart = thermistorVal / thermistorResistance;     // (R/Ro)
  116.     steinhart = log(steinhart);                  // ln(R/Ro)
  117.     steinhart /= thermistorBVal;                   // 1/B * ln(R/Ro)
  118.     steinhart += 1.0 / (thermistorTemp + 273.15); // + (1/To)
  119.     steinhart = 1.0 / steinhart;                 // Invert
  120.     steinhart -= 273.15;                         // convert to C
  121.  
  122.     // Serial.print("Temperature ");
  123.     // Serial.print(steinhart);
  124.     // Serial.println(" *C");
  125.    
  126.     // Serial.print("constrain mapped ");
  127.     // Serial.println(constrain(fmap(steinhart,18.0,26.0,0.0,2047.0),0,2047));
  128.  
  129.     analogWrite(tTempPinOut,constrain(fmap(steinhart,16.0,26.0,0.0,2048.0),0,2048));
  130.     // analogWrite(tTempPinOut,constrain(fmap(16.0,16.0,26.0,0.0,2048.0),0,2048));
  131.  
  132.     // delay(1000);
  133.  
  134. }
  135.  
  136. // This function will get called when weather data comes in
  137. void gotWeatherData(const char *name, const char *data) {
  138.     String incomingRawData = String(data);
  139.    
  140.     // Serial.println(incomingRawData);
  141.    
  142.     char strBuffer[125] = "";
  143.    
  144.     incomingRawData.toCharArray(strBuffer,125);
  145.    
  146.     float temperatureFloat = atof(strtok(strBuffer,"~"));
  147.     float precipitationFloat = atof(strtok(NULL,"~"));
  148.     float pressureFloat = atof(strtok(NULL,"~"));
  149.  
  150.     if (temperatureFloat != NULL) {
  151.         // Serial.println("The temp is: " + tempStr + String(" *F"));
  152.         // Serial.println((tempStr.toFloat()-32)*5/9);
  153.         // Serial.println(map((tempStr.toFloat()-32)*5/9,0,30,0,4095));
  154.        
  155.         analogWrite(temperaturePinOut,constrain(fmap((temperatureFloat-32)*5/9,-20.0,30.0,0.0,255.0),0,255));
  156.         // analogWrite(tempPinOut,constrain(fmap(-20.0,-20.0,30.0,0.0,255.0),0,255));
  157.         // analogWrite(tempPinOut,0);
  158.     }
  159.  
  160.     if (precipitationFloat != NULL) {
  161.         analogWrite(precipitationPinOut,constrain(fmap(precipitationFloat,0.0,1.0,0.0,255.0),0,255));
  162.         // analogWrite(precPinOut,constrain(fmap(0.25,0.0,1.0,0.0,255.0),0,255));
  163.         // analogWrite(precPinOut,255);
  164.     }
  165.    
  166.     if (pressureFloat != NULL) {
  167.         analogWrite(pressurePinOut,constrain(fmap(pressureFloat,990.0,1040.0,0.0,255.0),0,255));
  168.         // analogWrite(arbPinOut,constrain(fmap(1040.0,990.0,1040.0,0.0,255.0),0,255));
  169.         // analogWrite(arbPinOut,0);
  170.     }
  171. }
  172.  
  173. float fmap(float x, float in_min, float in_max, float out_min, float out_max)
  174. {
  175.   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement