-
Notifications
You must be signed in to change notification settings - Fork 5
/
Modbus.ino
624 lines (510 loc) · 17.6 KB
/
Modbus.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
* Application note: Thermostat with MODBUS via RS485 for ArduiTouch ESP
* Version 1.0
* Copyright (C) 2018 Hartmut Wendt www.zihatec.de
*
* (based on sources of https://github.com/angeloc/simplemodbusng)
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*______Import Libraries_______*/
#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <XPT2046_Touchscreen.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Fonts/FreeSansBold12pt7b.h>
#include <Fonts/FreeSansBold24pt7b.h>
#include <SimpleModbusSlave.h>
#include "usergraphics.h"
/*______End of Libraries_______*/
/*______Define LCD pins for ArduiTouch _______*/
#define TFT_CS D1
#define TFT_DC D2
#define TFT_LED 15
#define HAVE_TOUCHPAD
#define TOUCH_CS 0
#define TOUCH_IRQ 2
// #define touch_yellow_header // enable this line for TFTs with yellow header
/*_______End of defanitions______*/
/*______Assign pressure_______*/
#define ILI9341_ULTRA_DARKGREY 0x632C
#define MINPRESSURE 10
#define MAXPRESSURE 2000
/*_______Assigned______*/
/*____Calibrate TFT LCD_____*/
#define TS_MINX 370
#define TS_MINY 470
#define TS_MAXX 3700
#define TS_MAXY 3600
/*______End of Calibration______*/
/*____Modbus_____*/
#define BAUDRATE 9600
#define DEFAULT_ID 1
enum
{
// just add or remove registers
// The first register starts at address 0
ROOM_TEMP, // measured room temp from external sensor
SET_TEMP, // set-point temperature by user,
FAN_LEVEL, // level for ventilation (values 0 - 5)
BEEPER, // any value between 400 and 4000 will set the beeper with the given frequency for 100ms
DISP_ONOFF, // timer for display automatic off function (0 switch backlight off, >0 set timer for automatic off)
TOTAL_ERRORS,
// leave this one
TOTAL_REGS_SIZE
// total number of registers for function 3 and 16 share the same register array
};
/*______End of Modbus______*/
/*____Program specific constants_____*/
#define MAX_TEMPERATURE 28
#define MIN_TEMPERATURE 18
enum { PM_MAIN, PM_OPTION, PM_CLEANING}; // Program modes
enum { BOOT, COOLING, TEMP_OK, HEATING}; // Thermostat modes
/*______End of specific constants______*/
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
XPT2046_Touchscreen touch(TOUCH_CS, TOUCH_IRQ);
#define _debug
int X,Y;
uint8_t Thermostat_mode = BOOT;
uint8_t iFan_level = 0;
uint8_t iRoom_temperature = 21;
uint8_t iSet_temperature = 20;
uint8_t PMode = PM_MAIN; // program mode
uint8_t Modbus_ID = DEFAULT_ID; // ID / address for modbus
bool Touch_pressed = false;
uint8_t Timer_Cleaning=0;
unsigned int holdingRegs[TOTAL_REGS_SIZE]; // function 3 and 16 register array
void setup() {
#ifdef _debug
Serial.begin(9600);
#endif
// Init MODBUS registers
holdingRegs[ROOM_TEMP] = iRoom_temperature;
holdingRegs[SET_TEMP] = iSet_temperature;
holdingRegs[ROOM_TEMP] = iFan_level;
holdingRegs[BEEPER] = 0;
holdingRegs[DISP_ONOFF] = 255;
#ifndef _debug
/* parameters(long baudrate,
unsigned char ID,
unsigned char transmit enable pin,
unsigned int holding registers size,
unsigned char low latency)
The transmit enable pin is used in half duplex communication to activate a MAX485 or similar
to deactivate this mode use any value < 2 because 0 & 1 is reserved for Rx & Tx.
Low latency delays makes the implementation non-standard
but practically it works with all major modbus master implementations.
*/
modbus_configure(BAUDRATE, Modbus_ID, 0, TOTAL_REGS_SIZE, 0);
#endif
pinMode(TFT_LED, OUTPUT);
digitalWrite(TFT_LED, HIGH); // HIGH to Turn on;
//digitalWrite(TFT_LED, LOW); // LOW to Turn on with assembled T1 and R2
tft.begin();
touch.begin();
#ifdef _debug
Serial.print("tftx ="); Serial.print(tft.width()); Serial.print(" tfty ="); Serial.println(tft.height());
#endif
draw_main_screen();
//Sound
noTone(D0);
}
TS_Point p;
void loop() {
//TS_Point p = waitTouch();
//X = p.y; Y = p.x;
if (Touch_Event()== true) {
X = p.y; Y = p.x;
if (Touch_pressed == false) {
if (holdingRegs[DISP_ONOFF]) DetectButtons();
holdingRegs[DISP_ONOFF] = 255; // reset BL timer
}
Touch_pressed = true;
} else {
Touch_pressed = false;
}
//automatic display BL timeout
if (holdingRegs[DISP_ONOFF]) {
holdingRegs[DISP_ONOFF]--;
digitalWrite(TFT_LED, HIGH); // Backlight on
//digitalWrite(TFT_LED, LOW); // Backlight on - with assembled T1 and R2
} else {
digitalWrite(TFT_LED, LOW); // Backlight off
//digitalWrite(TFT_LED, HIGH); // Backlight off - with assembled T1 and R2
}
// screen cleaning
Cleaning_processing();
// Modbus
Modbus_processing();
delay(100);
}
/********************************************************************//**
* @brief detects a touch event and converts touch data
* @param[in] None
* @return boolean (true = touch pressed, false = touch unpressed)
*********************************************************************/
bool Touch_Event() {
p = touch.getPoint();
delay(1);
#ifdef touch_yellow_header
p.x = map(p.x, TS_MINX, TS_MAXX, 320, 0); // yellow header
#else
p.x = map(p.x, TS_MINX, TS_MAXX, 0, 320); // black header
#endif
p.y = map(p.y, TS_MINY, TS_MAXY, 240, 0);
if (p.z > MINPRESSURE) return true;
return false;
}
/********************************************************************//**
* @brief Processing for screen cleaning function
* @param[in] None
* @return None
*********************************************************************/
void Cleaning_processing()
{
// idle timer for screen cleaning
if (PMode == PM_CLEANING) {
if ((Timer_Cleaning % 10) == 0) {
tft.fillRect(0,0, 100, 60, ILI9341_BLACK);
tft.setCursor(10, 50);
tft.print(Timer_Cleaning / 10);
}
if (Timer_Cleaning) {
Timer_Cleaning--;
} else {
draw_option_screen();
PMode = PM_OPTION;
}
}
}
/********************************************************************//**
* @brief Processing for MODBUS function / checking all registers
* @param[in] None
* @return None
*********************************************************************/
void Modbus_processing()
{
holdingRegs[TOTAL_ERRORS] = modbus_update(holdingRegs);
// update of variables by Modbus
if (holdingRegs[ROOM_TEMP] != iRoom_temperature) {
if ((holdingRegs[ROOM_TEMP] > 50) || (holdingRegs[ROOM_TEMP] < 5)) {
holdingRegs[ROOM_TEMP] = iRoom_temperature;
} else {
iRoom_temperature = holdingRegs[ROOM_TEMP];
update_Room_temp();
update_circle_color();
}
}
if (holdingRegs[SET_TEMP] != iSet_temperature) {
if ((holdingRegs[SET_TEMP] > MAX_TEMPERATURE) || (holdingRegs[SET_TEMP] < MIN_TEMPERATURE)) {
holdingRegs[SET_TEMP] = iSet_temperature;
} else {
iSet_temperature = holdingRegs[SET_TEMP];
update_SET_temp();
update_circle_color();
}
}
if (holdingRegs[FAN_LEVEL] != iFan_level) {
if ((holdingRegs[FAN_LEVEL] > 5) || (holdingRegs[FAN_LEVEL] < 0)) {
holdingRegs[FAN_LEVEL] = iFan_level;
} else {
iFan_level = holdingRegs[FAN_LEVEL];
draw_fan_level(50,312,iFan_level);
}
}
if ((holdingRegs[BEEPER] > 500) && (holdingRegs[BEEPER] < 4000)) {
tone(D0,holdingRegs[BEEPER],100);
}
holdingRegs[BEEPER] = 0;
}
/********************************************************************//**
* @brief detecting pressed buttons with the given touchscreen values
* @param[in] None
* @return None
*********************************************************************/
void DetectButtons()
{
// in main program
if (PMode == PM_MAIN){
// button UP
if ((X>190) && (Y<50)) {
if (iSet_temperature < MAX_TEMPERATURE) iSet_temperature++;
tone(D0,2000,100);
holdingRegs[SET_TEMP] = iSet_temperature;
update_SET_temp();
update_circle_color();
}
// button DWN
if ((X>190) && (Y>200 && Y<250)) {
if (iSet_temperature > MIN_TEMPERATURE) iSet_temperature--;
tone(D0,2000,100);
holdingRegs[SET_TEMP] = iSet_temperature;
update_SET_temp();
update_circle_color();
}
// button FAN MAX
if ((X>180) && (Y>270)) {
tone(D0,2000,100);
if (iFan_level < 5) iFan_level++;
draw_fan_level(50,312,iFan_level);
holdingRegs[FAN_LEVEL] = iFan_level;
}
// button FAN MIN
if ((X<60) && (Y>270)) {
tone(D0,2000,100);
if (iFan_level > 0) iFan_level--;
draw_fan_level(50,312,iFan_level);
holdingRegs[FAN_LEVEL] = iFan_level;
}
// button gearwheel
if ((X<60) && (Y<50)) {
draw_option_screen();
PMode = PM_OPTION;
}
} else if (PMode == PM_OPTION){
// button -
if ((X<110) && (Y<75)) {
if (Modbus_ID > 0) Modbus_ID--;
update_Modbus_addr();
}
// button +
if ((X>130) && (Y<75)) {
if (Modbus_ID < 255) Modbus_ID++;
update_Modbus_addr();
}
// button screen cleaning
if ((Y>85) && (Y<155)) {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setFont(&FreeSansBold24pt7b);
PMode = PM_CLEANING;
Timer_Cleaning = 255;
}
// button OK
if (Y>265) {
Thermostat_mode = BOOT;
draw_main_screen();
modbus_configure(BAUDRATE, Modbus_ID, 0, TOTAL_REGS_SIZE, 0);
PMode = PM_MAIN;
}
}
}
/********************************************************************//**
* @brief Drawing of the main program screen
* @param[in] None
* @return None
*********************************************************************/
void draw_main_screen()
{
tft.fillScreen(ILI9341_BLACK);
// draw circles
update_circle_color();
// draw temperature up/dwn buttons
draw_up_down_button();
// draw icons
tft.drawRGBBitmap(10,290, fan_blue_24,24,24);
tft.drawRGBBitmap(200,282, fan_blue_32,32,32);
tft.drawRGBBitmap(10,10, wrench, 24,24);
// draw default fan level
draw_fan_level(50,312,iFan_level);
update_SET_temp();
}
/********************************************************************//**
* @brief Drawing of the screen for Options menu
* @param[in] None
* @return None
*********************************************************************/
void draw_option_screen()
{
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setFont(&FreeSansBold9pt7b);
// Modbus Address adjustment
tft.setCursor(10, 20);
tft.print("MODBUS address");
tft.setFont(&FreeSansBold24pt7b);
tft.setCursor(30, 65);
tft.print("-");
tft.setCursor(190, 65);
tft.print("+");
tft.drawLine(5,80,235,80, ILI9341_WHITE);
// Screen cleaning idle timer
tft.setFont(&FreeSansBold12pt7b);
tft.setCursor(26, 130);
tft.print("Screen cleaning");
tft.drawLine(5,160,235,160, ILI9341_WHITE);
// OK Button
tft.setFont(&FreeSansBold24pt7b);
tft.drawLine(5,260,235,260, ILI9341_WHITE);
tft.setCursor(90, 310);
tft.print("OK");
update_Modbus_addr();
}
/********************************************************************//**
* @brief update of the value for set temperature on the screen
* (in the big colored circle)
* @param[in] None
* @return None
*********************************************************************/
void update_SET_temp()
{
int16_t x1, y1;
uint16_t w, h;
String curValue = String(iSet_temperature);
int str_len = curValue.length() + 1;
char char_array[str_len];
curValue.toCharArray(char_array, str_len);
tft.fillRect(70, 96, 60, 50, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setFont(&FreeSansBold24pt7b);
tft.getTextBounds(char_array, 80, 130, &x1, &y1, &w, &h);
tft.setCursor(123 - w, 130);
tft.print(char_array);
}
/********************************************************************//**
* @brief update of the value for room temperature on the screen
* (in the small grey circle)
* @param[in] None
* @return None
*********************************************************************/
void update_Room_temp()
{
int16_t x1, y1;
uint16_t w, h;
String curValue = String(iRoom_temperature);
int str_len = curValue.length() + 1;
char char_array[str_len];
curValue.toCharArray(char_array, str_len);
tft.fillRect(36, 200, 30, 21, ILI9341_ULTRA_DARKGREY);
tft.setTextColor(ILI9341_WHITE, ILI9341_ULTRA_DARKGREY);
tft.setFont(&FreeSansBold12pt7b);
tft.getTextBounds(char_array, 40, 220, &x1, &y1, &w, &h);
tft.setCursor(61 - w, 220);
tft.print(char_array);
}
/********************************************************************//**
* @brief update of the color of the big circle according the
* difference between set and room temperature
* @param[in] None
* @return None
*********************************************************************/
void update_circle_color()
{
// HEATING
if ((iRoom_temperature < iSet_temperature) && (Thermostat_mode != HEATING)) {
Thermostat_mode = HEATING;
draw_circles();
}
// COOLING
if ((iRoom_temperature > iSet_temperature) && (Thermostat_mode != COOLING)) {
Thermostat_mode = COOLING;
draw_circles();
}
// Temperature ok
if ((iRoom_temperature == iSet_temperature) && (Thermostat_mode != TEMP_OK)) {
Thermostat_mode = TEMP_OK;
draw_circles();
}
}
/********************************************************************//**
* @brief update of the value for MODBUS ID in the options menu on
* the screen
* @param[in] None
* @return None
*********************************************************************/
void update_Modbus_addr()
{
tft.fillRect(110, 30, 60, 45, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setFont(&FreeSansBold24pt7b);
tft.setCursor(115, 65);
tft.print(Modbus_ID);
}
/********************************************************************//**
* @brief drawing of the circles in main screen including the value
* of room temperature
* @param[in] None
* @return None
*********************************************************************/
void draw_circles()
{
//draw big circle
unsigned char i;
if (iRoom_temperature < iSet_temperature) {
// heating - red
for(i=0; i < 10; i++) tft.drawCircle(120, 120, 80 + i, ILI9341_RED);
} else if (iRoom_temperature > iSet_temperature) {
// cooling - blue
for(i=0; i < 10; i++) tft.drawCircle(120, 120, 80 + i, ILI9341_BLUE);
} else {
// Temperature ok
for(i=0; i < 10; i++) tft.drawCircle(120, 120, 80 + i, ILI9341_GREEN);
}
//draw small
tft.fillCircle(60, 200, 40, ILI9341_ULTRA_DARKGREY);
//draw °C in big circle
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setFont(&FreeSansBold9pt7b);
tft.setCursor(130, 100);
tft.print("o");
tft.setFont(&FreeSansBold24pt7b);
tft.setCursor(140, 130);
tft.print("C");
// draw room and °C in small circle
tft.setTextColor(ILI9341_WHITE, ILI9341_ULTRA_DARKGREY);
tft.setFont(&FreeSansBold12pt7b);
tft.setCursor(75, 220);
tft.print("C");
tft.drawCircle(69,204, 2, ILI9341_WHITE);
tft.drawCircle(69,204, 3, ILI9341_WHITE);
tft.setFont(&FreeSansBold9pt7b);
tft.setCursor(35, 190);
tft.print("Room");
update_Room_temp();
}
/********************************************************************//**
* @brief drawing of the both buttons for setting temperature up
* and down
* @param[in] None
* @return None
*********************************************************************/
void draw_up_down_button()
{
//up button
tft.fillTriangle(215,10,230,30,200,30, ILI9341_WHITE);
//down button
tft.fillTriangle(215,230,230,210,200,210, ILI9341_WHITE);
}
/********************************************************************//**
* @brief drawing of the fan level in main screen
* @param[in] None
* @return None
*********************************************************************/
void draw_fan_level(uint16_t x0, uint16_t y0, uint8_t ilevel)
{
unsigned char i;
if (ilevel >= 5) ilevel = 5;
for(i=0; i < 5; i++) {
if (i < ilevel) {
tft.fillRect(x0 + (30*i), y0- 10 -(i*8), 20, 10 + (i*8), ILI9341_WHITE);
} else {
tft.fillRect(x0 + (30*i), y0- 10 -(i*8), 20, 10 + (i*8), ILI9341_BLACK);
tft.drawRect(x0 + (30*i), y0- 10 -(i*8), 20, 10 + (i*8), ILI9341_WHITE);
}
}
}