Advertisement
Guest User

Untitled

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