Advertisement
Guest User

Untitled

a guest
Jul 1st, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. /***************************************************************************
  2. This is a library for the BME280 humidity, temperature & pressure sensor
  3.  
  4. Designed specifically to work with the Adafruit BME280 Breakout
  5. ----> http://www.adafruit.com/products/2650
  6.  
  7. These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  8. to interface. The device's I2C address is either 0x76 or 0x77.
  9.  
  10. Adafruit invests time and resources providing this open source code,
  11. please support Adafruit andopen-source hardware by purchasing products
  12. from Adafruit!
  13.  
  14. Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  15. BSD license, all text above must be included in any redistribution
  16. ***************************************************************************/
  17.  
  18. #include <Wire.h>
  19. #include <SPI.h>
  20. #include <Adafruit_Sensor.h>
  21. #include <Adafruit_BME280.h>
  22. #include <Time.h>
  23. #include <Timezone.h>
  24.  
  25. #define BME_SCK 13
  26. #define BME_MISO 12
  27. #define BME_MOSI 11
  28. #define BME_CS 9
  29.  
  30. Adafruit_BME280 bme(BME_CS); // hardware SPI
  31.  
  32. #include <RF24.h>
  33.  
  34. #define CE_PIN 8
  35. #define CSN_PIN 10
  36. const byte slaveAddress[5] = {'R','x','A','A','A'};
  37. RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
  38.  
  39. struct package
  40. {
  41. int temperature ;
  42. int pressure;
  43. int humidity ;
  44. float rain;
  45. int rf_hh;
  46. int rf_mm;
  47. int rf_dd;
  48. int rf_mo;
  49. int rf_yy;
  50. };
  51. typedef struct package Package;
  52. Package data;
  53.  
  54.  
  55. unsigned long delayTime;
  56.  
  57. #include <Adafruit_GPS.h>
  58. #include <SoftwareSerial.h>
  59.  
  60. SoftwareSerial mySerial(5, 4);
  61.  
  62. Adafruit_GPS GPS(&mySerial);
  63.  
  64. #define GPSECHO false
  65.  
  66. boolean usingInterrupt = false;
  67.  
  68. TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; // Central European Summer Time
  69. TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; // Central European Standard Time
  70. Timezone CE(CEST, CET);
  71.  
  72. const byte interruptPin = 3;
  73. volatile byte buckettips = 0;
  74. long debouncing_time = 500; //Debouncing Time in Milliseconds
  75. volatile unsigned long last_micros;
  76. int Res = 0;
  77.  
  78. void setup() {
  79.  
  80. Serial.begin(115200);
  81. Serial.println(F("BME280 test"));
  82.  
  83. bool status;
  84.  
  85.  
  86. status = bme.begin();
  87. if (!status) {
  88. Serial.println("Could not find a valid BME280 sensor, check wiring!");
  89. while (1);
  90. }
  91.  
  92. Serial.println();
  93. pinMode(3, INPUT);
  94. attachInterrupt(digitalPinToInterrupt(interruptPin), bucketTipped, FALLING);
  95.  
  96. GPS.begin(9600);
  97.  
  98.  
  99. GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  100. GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
  101. GPS.sendCommand(PGCMD_ANTENNA);
  102.  
  103. useInterrupt(true);
  104.  
  105. mySerial.println(PMTK_Q_RELEASE);
  106.  
  107. radio.begin();
  108. radio.setDataRate( RF24_250KBPS );
  109. radio.openWritingPipe(slaveAddress);
  110.  
  111. delayTime = 1000;
  112.  
  113. delay(1000);
  114.  
  115.  
  116. }
  117.  
  118. // Interrupt is called once a millisecond, looks for any new GPS data, and stores it
  119. SIGNAL(TIMER0_COMPA_vect) {
  120. char c = GPS.read();
  121. // if you want to debug, this is a good time to do it!
  122. #ifdef UDR0
  123. if (GPSECHO)
  124. if (c) UDR0 = c;
  125. // writing direct to UDR0 is much much faster than Serial.print
  126. // but only one character can be written at a time.
  127. #endif
  128. }
  129.  
  130. void useInterrupt(boolean v) {
  131. if (v) {
  132. // Timer0 is already used for millis() - we'll just interrupt somewhere
  133. // in the middle and call the "Compare A" function above
  134. OCR0A = 0xAF;
  135. TIMSK0 |= _BV(OCIE0A);
  136. usingInterrupt = true;
  137. } else {
  138. // do not call the interrupt function COMPA anymore
  139. TIMSK0 &= ~_BV(OCIE0A);
  140. usingInterrupt = false;
  141. }
  142. }
  143.  
  144. uint32_t timer = millis();
  145.  
  146. void loop() {
  147.  
  148. printValues();
  149. radio.write(&data, sizeof(data));
  150. // Always use sizeof() as it gives the size as the number of bytes.
  151. // For example if dataToSend was an int sizeof() would correctly return 2
  152. resetrain();
  153. delay(delayTime);
  154. Serial.write(27); // ESC command
  155. Serial.print("[2J"); // clear screen command
  156. Serial.write(27);
  157. Serial.print("[H"); // cursor to home command
  158.  
  159. }
  160.  
  161.  
  162. void printValues() {
  163. uint8_t hh, mm, ss, dd, mo, yy;
  164. Serial.println("---Start---");
  165. Serial.print("Temperature = ");
  166. int temp_int = bme.readTemperature();
  167. Serial.print(temp_int);
  168. Serial.println(" *C");
  169. data.temperature = temp_int;
  170. Serial.print("Pressure = ");
  171. int pres_int = (bme.readPressure() / 100.0F);
  172. Serial.print(pres_int);
  173. Serial.println(" hPa");
  174. data.pressure = pres_int;
  175. Serial.print("Humidity = ");
  176. int hum_int = bme.readHumidity();
  177. Serial.print(hum_int);
  178. Serial.println(" %");
  179. Serial.println();
  180. data.humidity = hum_int;
  181. Serial.print("Bucket tipped ");
  182. Serial.print(buckettips);
  183. Serial.println(" times");
  184. data.rain = buckettips * 0.33;
  185. Serial.print(data.rain);
  186. Serial.println(" mm of rain");
  187.  
  188. // in case you are not using the interrupt above, you'll
  189. // need to 'hand query' the GPS, not suggested :(
  190. if (! usingInterrupt) {
  191. // read data from the GPS in the 'main loop'
  192. char c = GPS.read();
  193. // if you want to debug, this is a good time to do it!
  194. if (GPSECHO)
  195. if (c) Serial.print(c);
  196. }
  197.  
  198. // if a sentence is received, we can check the checksum, parse it...
  199. if (GPS.newNMEAreceived()) {
  200. // a tricky thing here is if we print the NMEA sentence, or data
  201. // we end up not listening and catching other sentences!
  202. // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
  203. //Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
  204.  
  205. if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
  206. return; // we can fail to parse a sentence in which case we should just wait for another
  207. }
  208.  
  209.  
  210.  
  211. Serial.print("\nTime: ");
  212. hh = GPS.hour;
  213. Serial.print(hh, DEC); Serial.print(':');
  214. mm = GPS.minute;
  215. Serial.print(mm, DEC); Serial.print(':');
  216. ss = GPS.seconds;
  217. Serial.println(ss, DEC);
  218. Serial.print("Date: ");
  219. dd = GPS.day;
  220. Serial.print(dd, DEC); Serial.print('/');
  221. mo = GPS.month;
  222. Serial.print(mo, DEC); Serial.print("-20");
  223. yy = GPS.year;
  224. Serial.println(yy, DEC);
  225. Serial.println();
  226.  
  227. setTime(hh, mm, ss, dd, mo, yy);
  228.  
  229. time_t utc = now();
  230. // local = myTZ.toLocal(utc, &tcr);
  231. printDateTime(CE, utc);
  232.  
  233. }
  234.  
  235. // given a Timezone object, UTC and a string description, convert and print local time with time zone
  236. void printDateTime(Timezone tz, time_t utc)
  237. {
  238. uint8_t rf_hh, rf_mm, rf_dd, rf_mo, rf_yy;
  239. char buf[40];
  240. char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
  241. TimeChangeRule *tcr; // pointer to the time change rule, use to get the TZ abbrev
  242.  
  243. time_t t = tz.toLocal(utc, &tcr);
  244. strcpy(m, monthShortStr(month(t)));
  245. sprintf(buf, "%.2d:%.2d:%.2d %d/%d-%d",
  246. hour(t), minute(t), second(t), day(t), month(t), year(t));
  247. Serial.println(buf);
  248. data.rf_hh = hour(t);
  249. data.rf_mm = minute(t);
  250. data.rf_dd = day(t);
  251. data.rf_mo = month(t);
  252. data.rf_yy = year(t);
  253. Serial.print("---End---");
  254.  
  255.  
  256. }
  257.  
  258. void resetrain(){
  259.  
  260. if(data.rf_dd == 1)
  261. {
  262. if(data.rf_hh == 0)
  263. {
  264. if(data.rf_mm == 0)
  265. {
  266. if(Res == 0)
  267. {
  268. buckettips = 0;
  269. Res = 1;
  270. }
  271. }
  272. }
  273. }
  274.  
  275. if(data.rf_dd == 1)
  276. {
  277. if(data.rf_hh == 0)
  278. {
  279. if(data.rf_mm == 1)
  280. {
  281. Res = 0;
  282. }
  283. }
  284. }
  285.  
  286. }
  287.  
  288. void bucketTipped() {
  289. if((long)(micros() - last_micros) >= debouncing_time * 1000) {
  290. buckettips++;
  291. last_micros = micros();
  292. }
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement