Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. //#include "Ubidots.h" //Library for uploading to Ubidots graphing service
  2. #include "Adafruit_Sensor.h" //Libraries for reading sensors
  3. #include "Adafruit_BME280.h"
  4. #include "math.h"
  5. #include "blynk.h"
  6.  
  7. #define BME_SCK D4
  8. #define BME_MISO D3
  9. #define BME_MOSI D2
  10. #define BME_CS D5 //These are not needed as we're using i2c for BME not SPI
  11.  
  12. #define do_wind FALSE //Change to TRUE when wind sensor is connected
  13.  
  14. #define SEALEVELPRESSURE_HPA (1013.25)
  15.  
  16. #define tempoffset -0.9F //BME temp sensor is always high by this amount
  17. #define TTS 1000 //Time till screen update in ms
  18. #define TTU 120 //Time till Ubidots update in TTS intervals
  19. #define TTW 120 //Time till wind update
  20.  
  21. #define TOKEN "ix9hooYdMoBXnxoCNP65gUTNKjBG1S"  // Ubidots stuff
  22.  
  23.  
  24. char auth[] = "8157361d2bf349e88d49b58c605aef00";
  25.  
  26. Adafruit_BME280 bme; // initialize BME sensor over I2C
  27.  
  28.  
  29. float timer1 = TTU; // How often to send sensor updates to Ubidots
  30. float timer2 = TTW + 60;
  31. int screentime = (TTS / 1000);
  32.  
  33. // diameter of anemometer
  34. float diameter = 2.75; //inches from center pin to middle of cup
  35. float mph;
  36. int lighton = 0;
  37.  
  38. // read RPM
  39. int half_revolutions = 0;
  40. int avgrevs = 0;
  41. int revs = 0;
  42. float kph = 0;
  43. float kphavg = 0;
  44. int kphgust = 0;
  45. double windchill = 0;
  46. double humidex = 0;
  47. int ttime = 0;
  48. unsigned long lastmillis = 0;
  49. unsigned long windmillis = 0;
  50. unsigned long millis1 = 0;
  51. unsigned long millis2 = 0;
  52. unsigned long sleeptimer = 0;
  53. float deltatime = 1;
  54. float deltatime2 = 1;
  55. double pres1, temp1, hum1, abshum1, humavg, tempavg, presavg;
  56. float deltacount1 = 0;
  57. float deltacount2 = 0;
  58. bool ubisend = FALSE;
  59. bool sleepnow = FALSE;
  60. unsigned hours1 = 0;
  61. char publishString[40];
  62.  
  63. void setup() { //This is where all Arduinos store the on-bootup code
  64.  
  65.     bme.begin();
  66.   Blynk.begin(auth);
  67. Time.format(Time.now(), "%I:%M:%S%p");
  68. bme.setSampling(Adafruit_BME280::MODE_FORCED,
  69.                 Adafruit_BME280::SAMPLING_X1, // temperature
  70.                 Adafruit_BME280::SAMPLING_X1, // pressure
  71.                 Adafruit_BME280::SAMPLING_X1, // humidity
  72.                 Adafruit_BME280::FILTER_OFF   );
  73. }
  74.  
  75. void loop() { //This is where all Arduinos store their "do this all the time" code
  76.  
  77.  
  78.   if (millis() - lastmillis >= TTS)
  79.   {
  80.     deltatime = (millis() - lastmillis) / 1000;
  81.     deltacount1 += 1;
  82.     deltacount2 += 1;
  83.     lastmillis = millis(); // Update lastmillis
  84.       bme.takeForcedMeasurement();
  85.     pres1 += (bme.readPressure() / 100.0F);
  86.     temp1 += (bme.readTemperature() + tempoffset);
  87.     hum1 += bme.readHumidity();
  88.  
  89.  
  90.  
  91.     revs = half_revolutions * 2;
  92.     revs = revs / deltatime;
  93.     if (revs > kphgust) {kphgust = revs;}
  94.     half_revolutions = 0; // Restart the RPM counter
  95.     kph += revs;
  96.  
  97.   }
  98.  
  99.   if ((millis() - millis2) >= (timer2*1000))
  100.   {
  101.     deltatime2 = (millis() - millis2) / 1000;
  102.  
  103.     presavg = pres1/deltacount2;
  104.     Blynk.virtualWrite(V2, presavg);
  105.     Particle.publish("Pressure", String::format("%3.2f", presavg), 60, PRIVATE);
  106.     pres1 = 0;
  107.     deltacount2 = 0;
  108.   }
  109.  
  110.   if ((millis() - millis1) >= (TTU*1000))
  111.   {
  112.       millis1 = millis();
  113.       humavg = hum1/deltacount1;
  114.       tempavg = temp1/deltacount1;
  115.  
  116.       abshum1 = (6.112 * pow(2.71828, ((17.67 * tempavg)/(tempavg+243.5))) * humavg * 2.1674)/(273.15 + tempavg);
  117.       Blynk.virtualWrite(V0, tempavg);
  118.       Blynk.virtualWrite(V1, humavg);
  119.  
  120.       Blynk.virtualWrite(V3, abshum1);
  121.       Particle.publish("Temperature", String::format("%3.2f", tempavg), 60, PRIVATE);
  122.       //Particle.publish("Humidity", String::format("%3.2f", humavg), 60, PRIVATE);
  123.       Particle.publish("AbsHumidity", String::format("%3.2f", abshum1), 60, PRIVATE);
  124.       publish_uptime();
  125.  
  126.  
  127.  
  128.       temp1 = 0;
  129.       hum1 = 0;
  130.       deltacount1 = 0;
  131.   }
  132.  
  133.  
  134.  
  135.  
  136.   Blynk.run();
  137.  
  138.   if (millis() >= 4294960296) {hours1 += 1193;}
  139. }
  140.  
  141.  
  142.  
  143. void publish_uptime()
  144. {
  145.      unsigned long now = millis();
  146.     // now is in milliseconds
  147.      float sec = now/1000;
  148.      float hours = hours1 + (sec/3600);
  149.     //sprintf(publishString,"%u:%u:%u",hours,min,sec);
  150.     sprintf(publishString,"%f",hours);
  151.     Particle.publish("Uptime hours", publishString, 60, PRIVATE);
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement