· 5 years ago · Nov 03, 2020, 09:50 PM
1#include <Arduino.h>
2
3#include <Wire.h>
4#include <LiquidCrystal_I2C.h>
5#include <DS3231.h>
6#include <OneWire.h>
7#include <DallasTemperature.h>
8#include <SoftwareSerial.h>
9#include <SPI.h>
10#include <nRF24L01.h>
11#include <RF24.h>
12#define ONE_WIRE_BUS 8
13#define BACKLIGHT_PIN 3
14LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7);
15//NewSoftSerial mySerial = NewSoftSerial(2, 3);
16SoftwareSerial virtualSerial(2, 3);
17DS3231 rtc(SDA, SCL);
18int readcounter = 0;
19OneWire oneWire(ONE_WIRE_BUS);
20DallasTemperature sensors(& oneWire);
21float temperatura_wewnetrzna;
22float temperatura_zewnetrzna_bmp;
23float temp_bmp;
24float dane[4];
25RF24 radio(9, 10); // CE, CSN
26
27float temp_wewn, temp_zewn, wilg, cisnienie;
28
29
30int licznik_ekranow = 1;
31int ekran_plus = 7;
32int ekran_minus = 5;
33int resetuj = 6;
34
35boolean obecny_plus = LOW;
36boolean ostatni_plus = LOW;
37boolean ostatni_minus = LOW;
38boolean obecny_minus = LOW;
39
40String apiKey = "UIP3W43H4ZY7R0JR"; //Write API key Thingspeak
41
42String ssid = "sciszone"; // Nazwa WiFi
43String haslo = "B2h89whpb88o"; // Hasło
44
45boolean DEBUG = true;
46const uint64_t paczka = 0x12345678;
47
48void showResponse(int waitTime) {
49 long t = millis();
50 char c;
51 while (t + waitTime > millis()) {
52 if (virtualSerial.available()) {
53 c = virtualSerial.read();
54 if (DEBUG) Serial.print(c);
55 }
56 }
57}
58
59//===============================================================
60void setup() {
61 DEBUG = true;
62 lcd.begin(20, 4); // initialize the lcd
63 lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
64 lcd.setBacklight(HIGH);
65 virtualSerial.begin(9600);
66 Serial.begin(9600);
67
68 radio.begin();
69 radio.openReadingPipe(1, paczka);
70 radio.setPALevel(RF24_PA_MIN);
71 radio.startListening();
72
73 sensors.begin();
74 rtc.begin();
75 //rtc.setDOW(SUNDAY); // Set Day-of-Week to SUNDAY
76 //rtc.setTime(17, 50, 0); // Set the time to 12:00:00 (24hr format)
77 //rtc.setDate(1, 11, 2020); // Set the date to January 1st, 2014
78
79 pinMode(ekran_minus, INPUT_PULLUP);
80 pinMode(ekran_plus, INPUT_PULLUP);
81 pinMode(resetuj, INPUT_PULLUP);
82
83boolean obecny_plus = LOW;
84boolean ostatni_plus = LOW;
85boolean ostatni_minus = LOW;
86boolean obecny_minus = LOW;
87
88//virtualSerial.println("AT+CIOBAUD=9600");
89//showResponse(1000);
90
91virtualSerial.println("AT+RST");
92showResponse(1000);
93
94virtualSerial.println("AT+CWMODE=1"); // set esp8266 as client
95 showResponse(1500);
96
97 virtualSerial.println("AT+CWJAP=\"" + ssid + "\",\"" + haslo + "\""); // set your home router SSID and password
98 showResponse(5000);
99
100 Serial.println("Setup completed");
101}
102 boolean debounce(boolean ostatni, int pin) {
103 boolean obecny = digitalRead(pin);
104 if (ostatni != obecny) {
105 delay(5);
106 obecny = digitalRead(pin);
107 }
108 return obecny;
109}
110//============================================================
111void loop(void){
112 readcounter++;
113 //float temperatura_wewnetrzna, temperatura_zewnetrzna, cisnienie, wilgotnosc;
114 float (temp_wewn, temp_zewn, wilg, cisnienie, temp_bmp); //temp_bmp;
115 //temp_wewnetrzna = sensors.getTempCByIndex(0);
116
117 sensors.requestTemperatures();
118 temp_wewn = sensors.getTempCByIndex(0);
119
120 if (radio.available()) {
121 bool done = false;
122 while (!done) {
123 radio.read(dane, sizeof(dane));
124 }
125 temp_zewn=dane[0];
126 temp_bmp=dane[2];
127 wilg = dane[1];
128 cisnienie = dane[3];
129
130 }
131
132
133 Serial.print(dane[0]);
134Serial.print(" ");
135Serial.println(temp_zewn);
136Serial.print(dane[1]);
137Serial.print(" ");
138Serial.print(wilg);
139
140
141 if (readcounter == 300){
142
143 lcd.clear();
144 lcd.setCursor(5, 0);
145 lcd.print("WYSYLANIE");
146 lcd.setCursor(7, 1);
147 lcd.print("DANYCH");
148 lcd.setCursor(9, 2);
149 lcd.print("NA");
150 lcd.setCursor(7, 3);
151 lcd.print("SERWER!");
152
153 thingSpeakWrite(temp_wewn, temp_zewn, wilg, cisnienie); //cisnienie);
154
155 readcounter = 0;
156 lcd.clear();
157 }
158
159//////////////////////PRZYCISKI/////////////////////////////
160 obecny_plus = debounce(ostatni_plus, ekran_plus); //Debounce for Up button
161 obecny_minus = debounce(ostatni_minus, ekran_minus); //Debounce for Down button
162
163 //Page Up
164 if (ostatni_plus == LOW && obecny_plus == HIGH) { //When up button is pressed
165 lcd.clear(); //When page is changed, lcd clear to print new page
166 if (licznik_ekranow < 3) { //Page counter never higher than 3(total of pages)
167 licznik_ekranow = licznik_ekranow + 1; //Page up
168
169 } else {
170 licznik_ekranow = 3;
171 }
172 }
173
174 ostatni_plus = obecny_plus;
175
176 //Page Down
177 if (ostatni_minus == LOW && obecny_minus == HIGH) { //When down button is pressed
178 lcd.clear(); //When page is changed, lcd clear to print new page
179 if (licznik_ekranow > 1) { //Page counter never lower than 1 (total of pages)
180 licznik_ekranow = licznik_ekranow - 1; //Page down
181
182 } else {
183 licznik_ekranow = 1;
184 }
185 }
186
187 ostatni_minus = obecny_minus;
188 //////////////////////////////////////////////////////////////
189
190
191switch (licznik_ekranow) {
192
193 case 1:
194 { //Design of home page 1
195
196
197 lcd.setCursor(3, 2);
198 //lcd.write((uint8_t)1);
199 lcd.print("T zewn: ");
200 lcd.print(temp_zewn);
201
202 //lcd.write((uint8_t)0);
203 // lcd.write(0);
204 lcd.setCursor(3, 3);
205 //lcd.write((uint8_t)1);
206 lcd.print(" Wewn: ");
207 lcd.print(temperatura_wewnetrzna);
208
209 // lcd.write((uint8_t)0);
210 //lcd.write(0);
211 }
212 break;
213
214 case 2:
215 { //Design of page 2
216
217
218 lcd.setCursor(0, 2);
219 lcd.write("Wilgotnosc ");
220 //lcd.write((uint8_t)2);
221 lcd.print(" ");
222 lcd.print(dane[1]);
223 lcd.print(" %");
224 lcd.setCursor(0, 3);
225 lcd.print(" Cisn ");
226 lcd.print(dane[3]);
227 lcd.print(" hPa");
228 }
229 break;
230
231
232 case 3:
233 { //Design of page 3
234 lcd.setCursor(2, 2);
235 lcd.print("Temp BMP");
236 lcd.setCursor(7, 3);
237 lcd.print(dane[2]);
238 lcd.print(" C");
239
240
241 }
242 break;
243}
244
245
246
247}
248
249void thingSpeakWrite(float value1, float value2, float value3, float value4) {
250 String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection
251 cmd += "184.106.153.149"; // api.thingspeak.com
252 cmd += "\",80";
253 virtualSerial.println(cmd);
254 if (DEBUG) Serial.println(cmd);
255// if (virtualSerial.find("Error")) {
256// if (DEBUG) Serial.println("AT+CIPSTART error");
257// return false;
258// }
259
260 String getStr = "GET /update?api_key="; // prepare GET string
261 getStr += apiKey;
262
263 getStr += "&field1=";
264 getStr += String(value1);
265 getStr += "&field2=";
266 getStr += String(value2);
267 getStr += "&field3=";
268 getStr += String(value3);
269 getStr += "&field4=";
270 getStr += String(value4);
271 // ...
272 getStr += "\r\n\r\n";
273
274 // send data length
275 cmd = "AT+CIPSEND=";
276
277 cmd += String(getStr.length());
278 virtualSerial.println(cmd);
279 if (DEBUG) Serial.println(cmd);
280
281 delay(200);
282 if (virtualSerial.find(">")) {
283 virtualSerial.print(getStr);
284 if (DEBUG) Serial.print(getStr);
285 } else {
286 virtualSerial.println("AT+CIPCLOSE");
287 // alert user
288 if (DEBUG) Serial.println("AT+CIPCLOSE");
289 return false;
290 }
291 return true;
292}