Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #define temperaturePinOut A7
- #define precipitationPinOut D1
- #define pressurePinOut D0
- #define thermistorPinIn A5
- #define NUMSAMPLES 30
- #define thermistorResistor 100000
- #define thermistorResistance 100000
- #define thermistorBVal 3950
- #define thermistorTemp 25
- #define tTempPinOut DAC1
- int refresh; //Refresh time in seconds. Min is about 86 seconds (1000 API calls per day are allowed by forecast.io).
- double lastRefresh = 0; //Unix time of last refresh
- int i;
- int samples[NUMSAMPLES];
- String storedString;
- void setup() {
- pinMode(temperaturePinOut, OUTPUT);
- pinMode(precipitationPinOut, OUTPUT);
- pinMode(tTempPinOut, OUTPUT);
- pinMode(pressurePinOut, OUTPUT);
- pinMode(thermistorPinIn, INPUT);
- Serial.begin(9600);
- // Particle.subscribe("hook-response/john_weather_scraped", gotWeatherData, MY_DEVICES);
- Particle.subscribe("hook-response/John_Weather_Scraped", gotWeatherData, MY_DEVICES);
- // Particle.subscribe("hook-response/John_Weather_Scraped_Current", gotWeatherDataCurrent, MY_DEVICES);
- for(int i=0; i<10; i++) {
- Serial.println("waiting " + String(10-i) + " seconds before we publish");
- delay(500);
- }
- Time.zone(-5); //Set time zone as central - note that this won't adjust for daylight savings.
- Particle.publish("John_Weather_Scraped");
- lastRefresh = Time.now();
- }
- void loop() {
- // Serial.print("Difference: ");
- // Serial.println(Time.now() - lastRefresh);
- // Serial.print("Refresh interval: ");
- // Serial.println(refresh);
- // Serial.print("Logic: ");
- // Serial.println((Time.now() - lastRefresh) > refresh);
- if (Time.hour() < 5 || Time.hour() > 23) { //API calls are limited so maximize their use during the normal waking hours
- refresh = 300; //5 Minutes
- }
- else {
- refresh = 90; //1.5 minutes
- }
- if ((Time.now() - lastRefresh) > 3600) { //Display an error code if webhook data has not been received within the past 60 minutes
- digitalWrite(temperaturePinOut, LOW);
- delay(500);
- digitalWrite(temperaturePinOut, HIGH);
- delay(500);
- digitalWrite(temperaturePinOut, LOW);
- delay(500);
- digitalWrite(temperaturePinOut, HIGH);
- delay(500);
- digitalWrite(temperaturePinOut, LOW);
- delay(500);
- digitalWrite(temperaturePinOut, HIGH);
- delay(500);
- digitalWrite(temperaturePinOut, LOW);
- }
- else if ((Time.now() - lastRefresh) > refresh) {
- Particle.publish("John_Weather_Scraped");
- lastRefresh = Time.now();
- }
- float thermistorVal;
- // take N samples in a row, with a slight delay
- for (i=0 ; i< NUMSAMPLES; i++) {
- samples[i] = analogRead(thermistorPinIn);
- delay(10);
- }
- // average all the samples out
- thermistorVal = 0;
- for (i=0; i< NUMSAMPLES; i++) {
- thermistorVal += samples[i];
- }
- thermistorVal /= NUMSAMPLES;
- // Serial.print("Average analog reading ");
- // Serial.println(thermistorVal);
- // convert the value to resistance
- thermistorVal = 4096 / thermistorVal - 1;
- thermistorVal = thermistorResistor / thermistorVal;
- // Serial.print("Thermistor resistance ");
- // Serial.println(thermistorVal);
- float steinhart;
- steinhart = thermistorVal / thermistorResistance; // (R/Ro)
- steinhart = log(steinhart); // ln(R/Ro)
- steinhart /= thermistorBVal; // 1/B * ln(R/Ro)
- steinhart += 1.0 / (thermistorTemp + 273.15); // + (1/To)
- steinhart = 1.0 / steinhart; // Invert
- steinhart -= 273.15; // convert to C
- // Serial.print("Temperature ");
- // Serial.print(steinhart);
- // Serial.println(" *C");
- // Serial.print("constrain mapped ");
- // Serial.println(constrain(fmap(steinhart,18.0,26.0,0.0,2047.0),0,2047));
- analogWrite(tTempPinOut,constrain(fmap(steinhart,16.0,26.0,0.0,2048.0),0,2048));
- // analogWrite(tTempPinOut,constrain(fmap(16.0,16.0,26.0,0.0,2048.0),0,2048));
- // delay(1000);
- }
- // This function will get called when weather data comes in
- void gotWeatherData(const char *name, const char *data) {
- String incomingRawData = String(data);
- // Serial.println(incomingRawData);
- char strBuffer[125] = "";
- incomingRawData.toCharArray(strBuffer,125);
- float temperatureFloat = atof(strtok(strBuffer,"~"));
- float precipitationFloat = atof(strtok(NULL,"~"));
- float pressureFloat = atof(strtok(NULL,"~"));
- if (temperatureFloat != NULL) {
- // Serial.println("The temp is: " + tempStr + String(" *F"));
- // Serial.println((tempStr.toFloat()-32)*5/9);
- // Serial.println(map((tempStr.toFloat()-32)*5/9,0,30,0,4095));
- analogWrite(temperaturePinOut,constrain(fmap((temperatureFloat-32)*5/9,-20.0,30.0,0.0,255.0),0,255));
- // analogWrite(tempPinOut,constrain(fmap(-20.0,-20.0,30.0,0.0,255.0),0,255));
- // analogWrite(tempPinOut,0);
- }
- if (precipitationFloat != NULL) {
- analogWrite(precipitationPinOut,constrain(fmap(precipitationFloat,0.0,1.0,0.0,255.0),0,255));
- // analogWrite(precPinOut,constrain(fmap(0.25,0.0,1.0,0.0,255.0),0,255));
- // analogWrite(precPinOut,255);
- }
- if (pressureFloat != NULL) {
- analogWrite(pressurePinOut,constrain(fmap(pressureFloat,990.0,1040.0,0.0,255.0),0,255));
- // analogWrite(arbPinOut,constrain(fmap(1040.0,990.0,1040.0,0.0,255.0),0,255));
- // analogWrite(arbPinOut,0);
- }
- }
- float fmap(float x, float in_min, float in_max, float out_min, float out_max)
- {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement