Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.24 KB | None | 0 0
  1. #include <TFT_HX8357.h> // Hardware-specific library
  2. #include <Wire.h>
  3. #include <SPI.h>
  4. #include <Adafruit_Sensor.h>
  5. #include <Adafruit_BME280.h>
  6. #define BME_SCK 52
  7. #define BME_MISO 50
  8. #define BME_MOSI 51
  9. #define BME_CS 9
  10.  
  11. Adafruit_BME280 bme(BME_CS); // hardware SPI
  12.  
  13. #include <RF24.h>
  14. #define CE_PIN 7
  15. #define CSN_PIN 8
  16. const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
  17. RF24 radio(CE_PIN, CSN_PIN);
  18.  
  19. TFT_HX8357 tft = TFT_HX8357(); // Invoke custom library
  20.  
  21. int previousRemoteTemperature = 0;
  22. int previousRemoteHumidity = 0;
  23. int previousRemotePressure = 0;
  24. float previousRemoteRain = 0.00;
  25. int previousRemoteHours = 0;
  26. int previousRemoteMinutes = 0;
  27. int previousRemoteDay = 0;
  28. int previousRemoteMonth = 0;
  29. int previousRemoteYear = 0;
  30. int previousIndoorTemperature = 0;
  31. int previousIndoorHumidity = 0;
  32. int previousIndoorPressure = 0;
  33.  
  34. int remoteTemperature = 0;
  35. int remoteHumidity = 0;
  36. int remotePressure = 0;
  37. float remoteRain = 0.00;
  38. int remoteHours = 0;
  39. int remoteMinutes = 0;
  40. int remoteDay = 0;
  41. int remoteMonth = 0;
  42. int remoteYear = 0;
  43. int indoorTemperature = 0;
  44. int indoorHumidity = 0;
  45. int indoorPressure = 0;
  46.  
  47. struct package
  48. {
  49. int temperature;
  50. int pressure;
  51. int humidity;
  52. float rain;
  53. int rf_hh;
  54. int rf_mm;
  55. int rf_dd;
  56. int rf_mo;
  57. int rf_yy;
  58. };
  59.  
  60. typedef struct package Package;
  61. Package data;
  62.  
  63. void setup()
  64. {
  65. pinMode(53, OUTPUT);
  66. Serial.begin(115200);
  67. Serial.println(F("Start"));
  68. Serial.println(F("BME280 test"));
  69.  
  70. bool status;
  71.  
  72. status = bme.begin();
  73. if (!status) {
  74. Serial.println("Could not find a valid BME280 sensor, check wiring!");
  75. while (1);
  76. }
  77.  
  78. Serial.println();
  79.  
  80. tft.init();
  81. tft.setRotation(0);
  82. tft.fillScreen(TFT_BLACK);
  83. tft.setTextFont(1); // Select font 1 which is the Adafruit GLCD font
  84. delay(100);
  85.  
  86. startWirelessCommunication();
  87.  
  88. printUI();
  89.  
  90. delay(1000);
  91.  
  92. }
  93.  
  94. void loop()
  95. {
  96.  
  97. checkForWirelessData();
  98. updating();
  99.  
  100.  
  101. }
  102.  
  103. void startWirelessCommunication()
  104. {
  105. Serial.println(F("Start Wireless Comm"));
  106. radio.begin();
  107. radio.setDataRate( RF24_250KBPS ) ;
  108. radio.openReadingPipe(1, thisSlaveAddress);
  109. radio.startListening();
  110. delay(100);
  111. }
  112.  
  113. void checkForWirelessData()
  114. {
  115.  
  116. if ( radio.available())
  117. {
  118. while (radio.available())
  119. {
  120. radio.read( &data, sizeof(data) );
  121. remoteTemperature = data.temperature;
  122. remotePressure = data.pressure;
  123. remoteHumidity = data.humidity;
  124. remoteRain = data.rain;
  125. remoteHours = data.rf_hh;
  126. remoteMinutes = data.rf_mm;
  127. remoteDay = data.rf_dd;
  128. remoteMonth = data.rf_mo;
  129. remoteYear = data.rf_yy;
  130. }
  131. Serial.print("\nRadio Package:");
  132. Serial.print("\n");
  133. Serial.println(data.temperature);
  134. Serial.println(data.pressure);
  135. Serial.println(data.humidity);
  136. Serial.println(data.rain);
  137. Serial.println(data.rf_hh);
  138. Serial.println(data.rf_mm);
  139. Serial.println(data.rf_dd);
  140. Serial.println(data.rf_mo);
  141. Serial.println(data.rf_yy);
  142. }
  143. }
  144.  
  145. void updating()
  146. {
  147.  
  148. const unsigned long oneMinutes = 1 * 30 * 1000UL;
  149. static unsigned long lastSampleTime = 0 - oneMinutes; // initialize such that a reading is due the first time through loop()
  150.  
  151. unsigned long now = millis();
  152. if (now - lastSampleTime >= oneMinutes)
  153. {
  154. lastSampleTime += oneMinutes;
  155.  
  156. readSensor();
  157. printIndoorTemperature();
  158. printIndoorPressure();
  159. printIndoorHumidity();
  160. printRemoteTemperature();
  161. printRemotePressure();
  162. printRemoteHumidity();
  163. printRemoteHours();
  164. printRemoteMinutes();
  165. printRemoteDay();
  166. printRemoteMonth();
  167. printRemoteYear();
  168. printRemoteRain();
  169.  
  170. }
  171.  
  172. }
  173.  
  174. void readSensor()
  175. {
  176. previousIndoorTemperature = indoorTemperature;
  177. previousIndoorPressure = indoorPressure;
  178. previousIndoorHumidity = indoorHumidity;
  179.  
  180. indoorTemperature = bme.readTemperature();
  181. indoorPressure = (bme.readPressure() / 100.0F);
  182. indoorHumidity = bme.readHumidity();
  183. }
  184.  
  185.  
  186. void printIndoorTemperature()
  187. {
  188.  
  189. if(indoorTemperature != previousIndoorTemperature)
  190. {
  191.  
  192. tft.fillRect(115, 145, 100, 30, TFT_BLACK);
  193. tft.setCursor(115,145);
  194. tft.setTextColor(TFT_CYAN);
  195. tft.setTextSize(4);
  196. tft.print(indoorTemperature-2);
  197. Serial.print("Temp. Inside: ");
  198. Serial.println(indoorTemperature);
  199.  
  200. previousIndoorTemperature = indoorTemperature;
  201.  
  202. }
  203. }
  204.  
  205. void printIndoorPressure()
  206. {
  207.  
  208. if(indoorPressure != previousIndoorPressure)
  209. {
  210.  
  211. tft.fillRect(115, 235, 100, 30, TFT_BLACK);
  212. tft.setCursor(115,235);
  213. tft.setTextColor(TFT_CYAN);
  214. tft.setTextSize(4);
  215. tft.print(indoorPressure);
  216. Serial.print("Pressure Inside: ");
  217. Serial.println(indoorPressure);
  218.  
  219. previousIndoorPressure = indoorPressure;
  220.  
  221. }
  222. }
  223.  
  224. void printIndoorHumidity()
  225. {
  226. if(indoorHumidity != previousIndoorHumidity)
  227. {
  228.  
  229. tft.fillRect(115, 190, 100, 30, TFT_BLACK);
  230. tft.setCursor(115,190);
  231. tft.setTextColor(TFT_CYAN);
  232. tft.setTextSize(4);
  233. tft.print(indoorHumidity);
  234. Serial.print("Humidity Inside: ");
  235. Serial.println(indoorHumidity);
  236.  
  237. previousIndoorHumidity = indoorHumidity;
  238.  
  239. }
  240. }
  241.  
  242. void printRemoteTemperature()
  243. {
  244.  
  245. if (remoteTemperature != previousRemoteTemperature)
  246. {
  247.  
  248. tft.fillRect(130, 344, 100, 30, TFT_BLACK);
  249. tft.setCursor(130, 344);
  250. tft.setTextColor(TFT_GREEN);
  251. tft.setTextSize(3);
  252. tft.print(remoteTemperature-2);
  253. Serial.print("Temp. Outside: ");
  254. Serial.println(remoteTemperature);
  255.  
  256. previousRemoteTemperature = remoteTemperature;
  257.  
  258. }
  259. }
  260.  
  261. void printRemotePressure()
  262. {
  263.  
  264. if (remotePressure != previousRemotePressure)
  265. {
  266.  
  267. tft.fillRect(130, 409, 100, 30, TFT_BLACK);
  268. tft.setCursor(130, 409);
  269. tft.setTextColor(TFT_GREEN);
  270. tft.setTextSize(3);
  271. tft.print(remotePressure);
  272. Serial.print("Pressure Outside: ");
  273. Serial.println(remotePressure);
  274.  
  275. previousRemotePressure = remotePressure;
  276.  
  277. }
  278. }
  279.  
  280. void printRemoteHumidity()
  281. {
  282.  
  283. if (remoteHumidity != previousRemoteHumidity)
  284. {
  285.  
  286. tft.fillRect(130, 374, 100, 30, TFT_BLACK);
  287. tft.setCursor(130, 374);
  288. tft.setTextColor(TFT_GREEN);
  289. tft.setTextSize(3);
  290. tft.print(remoteHumidity);
  291. Serial.print("Humidity Outside: ");
  292. Serial.println(remoteHumidity);
  293.  
  294. previousRemoteHumidity = remoteHumidity;
  295.  
  296. }
  297. }
  298.  
  299. void printRemoteHours()
  300. {
  301.  
  302. if (remoteHours != previousRemoteHours && remoteHours < 24)
  303. {
  304.  
  305. int i = remoteHours;
  306. char buf[30];
  307. memset (buf, 0, sizeof(buf));
  308. snprintf(buf, sizeof(buf)-1, "%02d", i);
  309.  
  310. tft.fillRect(224, 25, 35, 30, TFT_BLACK);
  311. tft.setCursor(224, 25);
  312. tft.setTextColor(TFT_WHITE);
  313. tft.setTextSize(3);
  314. tft.print(buf);
  315.  
  316. Serial.print("Hours: ");
  317. Serial.println(remoteHours);
  318.  
  319. previousRemoteHours = remoteHours;
  320.  
  321. }
  322.  
  323. }
  324.  
  325. void printRemoteMinutes()
  326. {
  327.  
  328. if (remoteMinutes != previousRemoteMinutes && remoteMinutes < 60)
  329. {
  330.  
  331. int i = remoteMinutes;
  332. char buf[30];
  333. memset (buf, 0, sizeof(buf));
  334. snprintf(buf, sizeof(buf)-1, "%02d", i);
  335.  
  336. tft.fillRect(270, 25, 35, 30, TFT_BLACK);
  337. tft.setCursor(270, 25);
  338. tft.setTextColor(TFT_WHITE);
  339. tft.setTextSize(3);
  340. tft.print(buf);
  341.  
  342. Serial.print("Minutes: ");
  343. Serial.println(remoteMinutes);
  344.  
  345. previousRemoteMinutes = remoteMinutes;
  346.  
  347. }
  348. }
  349.  
  350. void printRemoteDay()
  351. {
  352.  
  353. if (remoteDay != previousRemoteDay && remoteDay < 32)
  354. {
  355.  
  356. int i = remoteDay;
  357. char buf[30];
  358. memset (buf, 0, sizeof(buf));
  359. snprintf(buf, sizeof(buf)-1, "%02d", i);
  360.  
  361. tft.fillRect(20, 25, 35, 30, TFT_BLACK);
  362. tft.setCursor(20, 25);
  363. tft.setTextColor(TFT_WHITE);
  364. tft.setTextSize(3);
  365. tft.print(buf);
  366.  
  367. Serial.print("Day: ");
  368. Serial.println(remoteDay);
  369.  
  370. previousRemoteDay = remoteDay;
  371.  
  372. }
  373. }
  374.  
  375. void printRemoteMonth()
  376. {
  377.  
  378. if (remoteMonth != previousRemoteMonth && remoteMonth < 13)
  379. {
  380.  
  381. int i = remoteMonth;
  382. char buf[30];
  383. memset (buf, 0, sizeof(buf));
  384. snprintf(buf, sizeof(buf)-1, "%02d", i);
  385.  
  386. tft.fillRect(77, 25, 35, 30, TFT_BLACK);
  387. tft.setCursor(77, 25);
  388. tft.setTextColor(TFT_WHITE);
  389. tft.setTextSize(3);
  390. tft.print(buf);
  391.  
  392. Serial.print("Month: ");
  393. Serial.println(remoteMonth);
  394.  
  395. previousRemoteMonth = remoteMonth;
  396. }
  397.  
  398. }
  399.  
  400. void printRemoteYear()
  401. {
  402.  
  403. if (remoteYear != previousRemoteYear && remoteYear < 10000)
  404. {
  405.  
  406. tft.fillRect(134, 25, 70, 30, TFT_BLACK);
  407. tft.setCursor(134, 25);
  408. tft.setTextColor(TFT_WHITE);
  409. tft.setTextSize(3);
  410. tft.print(remoteYear);
  411.  
  412. Serial.print("Year: ");
  413. Serial.println(remoteYear);
  414.  
  415. previousRemoteYear = remoteYear;
  416.  
  417. }
  418. }
  419.  
  420. void printRemoteRain()
  421. {
  422.  
  423. if (remoteRain == 0.00 && previousRemoteRain == 0.00)
  424. {
  425.  
  426. tft.fillRect(130, 439, 100, 30, TFT_BLACK);
  427. tft.setCursor(130, 439);
  428. tft.setTextColor(TFT_GREEN);
  429. tft.setTextSize(3);
  430. tft.print(0.00);
  431.  
  432. }
  433.  
  434. if (remoteRain != previousRemoteRain)
  435. {
  436.  
  437. tft.fillRect(130, 439, 100, 30, TFT_BLACK);
  438. tft.setCursor(130, 439);
  439. tft.setTextColor(TFT_GREEN);
  440. tft.setTextSize(3);
  441. tft.print(remoteRain);
  442.  
  443. Serial.print("Rain: ");
  444. Serial.print(remoteRain);
  445. Serial.println(" mm");
  446.  
  447. previousRemoteRain = remoteRain;
  448.  
  449. }
  450. }
  451.  
  452. void printUI()
  453. {
  454.  
  455. tft.drawRoundRect(5, 5, 310, 71, 5, TFT_WHITE);
  456. tft.drawRoundRect(6, 6, 310, 71, 5, TFT_WHITE);
  457.  
  458. tft.drawRoundRect(5, 90, 310, 185, 5, TFT_WHITE);
  459. tft.drawRoundRect(6, 91, 310, 185, 5, TFT_WHITE);
  460.  
  461. tft.drawRoundRect(5, 289, 310, 185, 5, TFT_WHITE);
  462. tft.drawRoundRect(6, 290, 310, 185, 5, TFT_WHITE);
  463.  
  464. tft.fillRect(77, 90, 155, 40, TFT_CYAN);
  465. tft.fillRect(77, 289, 155, 40, TFT_GREEN);
  466.  
  467. tft.setCursor(58, 25);
  468. tft.setTextColor(TFT_WHITE);
  469. tft.setTextSize(3);
  470. tft.print("/");
  471.  
  472. tft.setCursor(115, 25);
  473. tft.setTextColor(TFT_WHITE);
  474. tft.setTextSize(3);
  475. tft.print("-");
  476.  
  477. tft.setCursor(257, 25);
  478. tft.setTextColor(TFT_WHITE);
  479. tft.setTextSize(3);
  480. tft.print(":");
  481.  
  482. tft.setCursor(115, 100);
  483. tft.setTextColor(TFT_BLACK);
  484. tft.setTextSize(3);
  485. tft.print("INDE");
  486.  
  487. tft.setCursor(125, 299);
  488. tft.setTextColor(TFT_BLACK);
  489. tft.setTextSize(3);
  490. tft.print("UDE");
  491.  
  492. tft.setCursor(230, 190);
  493. tft.setTextColor(TFT_CYAN);
  494. tft.setTextSize(4);
  495. tft.print("%");
  496.  
  497. tft.setCursor(230, 374);
  498. tft.setTextColor(TFT_GREEN);
  499. tft.setTextSize(3);
  500. tft.print("%");
  501.  
  502. tft.setCursor(230, 145);
  503. tft.setTextColor(TFT_CYAN);
  504. tft.setTextSize(2);
  505. tft.print("o");
  506.  
  507. tft.setCursor(245, 145);
  508. tft.setTextColor(TFT_CYAN);
  509. tft.setTextSize(4);
  510. tft.print("C");
  511.  
  512. tft.setCursor(230, 344);
  513. tft.setTextColor(TFT_GREEN);
  514. tft.setTextSize(1);
  515. tft.print("o");
  516.  
  517. tft.setCursor(245, 344);
  518. tft.setTextColor(TFT_GREEN);
  519. tft.setTextSize(3);
  520. tft.print("C");
  521.  
  522. tft.setCursor(230, 409);
  523. tft.setTextColor(TFT_GREEN);
  524. tft.setTextSize(3);
  525. tft.print("hPa");
  526.  
  527. tft.setCursor(230, 439);
  528. tft.setTextColor(TFT_GREEN);
  529. tft.setTextSize(3);
  530. tft.print("mm");
  531.  
  532. tft.setCursor(230, 235);
  533. tft.setTextColor(TFT_CYAN);
  534. tft.setTextSize(4);
  535. tft.print("hPa");
  536. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement