-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
306 lines (258 loc) · 9.44 KB
/
main.cpp
File metadata and controls
306 lines (258 loc) · 9.44 KB
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
/*********************************************************************
# Create a simple Node with Raspberry Pi 5
#
# Author : Phien Nguyen (Mark)
# Date : 10 Aug 2025
*********************************************************************/
#include <iostream>
#include <thread>
#include <chrono>
#include <ctime>
#include "hal/led_controller.h"
#include "hal/bh1750.h"
#include "hal/ili9341.h"
#include "hal/dht11.h"
#include "mqtt/mqtt_client.h"
LedController *led;
BH1750 *light_sensor;
Dht11Sensor *dht11;
ILI9341 *ili_lcd;
MQTT_Client *mqtt_node;
void myMessageHandler(const string& topic, const string& msg);
void thread_display_on_lcd(void);
void thread_send_to_mqtt(void);
int main(void)
{
// ************* Test LED *************
// LedController *led_0;
// int led_channel = 0;
// led_0 = new LedController(led_channel);
// led_0->set_pwm_period(1000000);
// led_0->set_pwm_duty(0);
// led_0->set_enable(true);
// for (int i = 0; i < 1000000; i += 100000)
// {
// led_0->set_pwm_duty(i);
// sleep(1);
// }
// led_0->set_enable(false);
// ************* Test BH1750 *************
// BH1750 *bh1750_sens = new BH1750();
// bh1750_sens->init();
// for (int i = 0; i < 10; i++)
// {
// int lux = bh1750_sens->read_light_level();
// if (lux < 0)
// cout << "Could not read bh1750" << endl;
// else
// cout << "Light level:: " << lux << endl;
// sleep(1);
// }
// ************* Test GPIO *************
// GPIOCtrl *dc_pin_ctrl = new GPIOCtrl(18, "dc-pin");
// GPIOCtrl *rs_pin_ctrl = new GPIOCtrl(23, "rs-pin");
// dc_pin_ctrl->set_dir_output();
// rs_pin_ctrl->set_dir_output();
// while (1) {
// dc_pin_ctrl->set_value(1);
// rs_pin_ctrl->set_value(1);
// cout << "Onnnn" << endl;
// sleep(2);
// dc_pin_ctrl->set_value(0);
// rs_pin_ctrl->set_value(0);
// cout << "Offf" << endl;
// sleep(2);
// }
// ************* Test ILI9341 *************
// ILI9341 *ili9341_lcd = new ILI9341(18, 23, 0, 320, 240, 0);
// ili9341_lcd->init_LCD();
// sleep(2);
// cout << "Filllll" << endl;
// ili9341_lcd->fill_screen(ILI9341_BLACK);
// ili9341_lcd->write_string(0, 120, "Temperature: ", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
// ili9341_lcd->write_string(0, 150, "Humid: ", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
// ili9341_lcd->write_string(0, 180, "Light level: ", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
// ili9341_lcd->write_string(0, 210, "LED status: ", font_11x18, ILI9341_RED, ILI9341_BLACK);
// ili9341_lcd->write_string(200, 120, "degree C", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
// ili9341_lcd->write_string(180, 150, "%", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
// ili9341_lcd->write_string(225, 180, "Lux", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
// while (1) {
// cout << "LCD" << endl;
// sleep(1);
// }
// ************* Test DHT11 *************
// Dht11Sensor *dht11_sensor = new Dht11Sensor(4);
// while (1)
// {
// float temp, humid;
// int ret = dht11_sensor->read(temp, humid);
// if (ret < 0)
// cout << "Could not read DHT11:: " << ret << endl;
// else {
// cout << "Temperature: " << temp << ", Humid: " << humid << endl;
// }
// sleep(3);
// }
// ************** Test MQTT *******************
// const string SERVER_ADDRESS = "ssl://xxx.s1.eu.hivemq.cloud:8883";
// const string CLIENT_ID = "pi5_client";
// const string USERNAME = "smartnode";
// const string PASSWORD = "Smartnode123";
// const string TOPIC_PUBLISH = "weather";
// const string TOPIC_SUBSCRIBE = "hiveMQ";
// MQTT_Client *mqtt_client = new MQTT_Client(SERVER_ADDRESS, CLIENT_ID, USERNAME, PASSWORD);
// mqtt_client->setOnMessage(myMessageHandler);
// if (mqtt_client->connect()) {
// mqtt_client->subscribe(TOPIC_SUBSCRIBE, 1);
// mqtt_client->publish(TOPIC_PUBLISH, 1, "Hello from Raspberry Pi 5!");
// } else {
// cerr << "MQTT connect failed!" << endl;
// return 1;
// }
// while (true) {
// this_thread::sleep_for(chrono::seconds(1));
// }
// ************** Main code **************
cout << "Simple node from Pi 5" << endl;
// Init LED
led = new LedController(0);
led->set_pwm_period(1000000);
led->set_pwm_duty(0);
led->set_enable(true);
// Init light sensor
light_sensor = new BH1750();
light_sensor->init();
// Init DHT11
dht11 = new Dht11Sensor(4);
// Init ILI9341
ili_lcd = new ILI9341(18, 23, 0, 320, 240, 0);
ili_lcd->init_LCD();
ili_lcd->fill_screen(ILI9341_BLACK);
ili_lcd->write_string(0, 120, "Temperature: ", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
ili_lcd->write_string(0, 150, "Humid: ", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
ili_lcd->write_string(0, 180, "Light level: ", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
ili_lcd->write_string(0, 210, "LED status: ", font_11x18, ILI9341_RED, ILI9341_BLACK);
ili_lcd->write_string(200, 120, "degree C", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
ili_lcd->write_string(180, 150, "%", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
ili_lcd->write_string(225, 180, "Lux", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
// Init MQTT
const string SERVER_ADDRESS = "ssl://xxx.s1.eu.hivemq.cloud:8883";
const string CLIENT_ID = "pi5_client";
const string USERNAME = "smartnode";
const string PASSWORD = "Smartnode123";
// const string TOPIC_PUBLISH = "weather";
const string TOPIC_SUBSCRIBE = "hiveMQ";
mqtt_node = new MQTT_Client(SERVER_ADDRESS, CLIENT_ID, USERNAME, PASSWORD);
mqtt_node->setOnMessage(myMessageHandler);
if (mqtt_node->connect()) {
mqtt_node->subscribe(TOPIC_SUBSCRIBE, 1);
} else {
cerr << "MQTT connect failed!" << endl;
return 1;
}
// Create thread
thread tid_1(thread_display_on_lcd);
thread tid_2(thread_send_to_mqtt);
tid_1.join();
tid_2.join();
return 0;
}
void myMessageHandler(const string& topic, const string& msg)
{
cout << "MQTT - " << topic << ", Received data: " << msg << endl;
}
void lcd_update_temperature(float value)
{
char buf[16];
sprintf(buf, "%.1f", value);
ili_lcd->write_string(150, 120, " ", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
ili_lcd->write_string(150, 120, buf, font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
}
void lcd_update_humid(float value)
{
char buf[16];
sprintf(buf, "%d", (int)value);
ili_lcd->write_string(150, 150, " ", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
ili_lcd->write_string(150, 150, buf, font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
}
void lcd_update_light_level(int value)
{
char buf[16];
sprintf(buf, "%d", (int)value);
ili_lcd->write_string(150, 180, " ", font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
ili_lcd->write_string(150, 180, buf, font_11x18, ILI9341_MAGENTA, ILI9341_BLACK);
}
void lcd_update_led_status(bool status)
{
char buf[16];
if (status)
sprintf(buf, "On");
else
sprintf(buf, "Off");
ili_lcd->write_string(150, 210, " ", font_11x18, ILI9341_RED, ILI9341_BLACK);
ili_lcd->write_string(150, 210, buf, font_11x18, ILI9341_RED, ILI9341_BLACK);
}
void lcd_update_time(char* str)
{
ili_lcd->write_string(220, 0, " ", font_11x18, ILI9341_WHITE, ILI9341_BLACK);
ili_lcd->write_string(220, 0, str, font_11x18, ILI9341_WHITE, ILI9341_BLACK);
}
int cur_light_level = 0;
float cur_temp = 0, cur_humid = 0;
bool LED_status = false;
void thread_display_on_lcd(void)
{
while (1)
{
// Get light sensor
cur_light_level = light_sensor->read_light_level();
if (cur_light_level > 100) {
led->set_pwm_duty(0);
LED_status = false;
}
else {
led->set_pwm_duty(500000);
LED_status = true;
}
lcd_update_light_level(cur_light_level);
lcd_update_led_status(LED_status);
// Get dht11
int ret = dht11->read(cur_temp, cur_humid);
if (ret >= 0) {
lcd_update_temperature(cur_temp);
lcd_update_humid(cur_humid);
}
// Time
auto now = chrono::system_clock::now();
time_t t = chrono::system_clock::to_time_t(now);
tm local_time = *localtime(&t);
int hour = local_time.tm_hour;
int minute = local_time.tm_min;
int second = local_time.tm_sec;
char time[16];
sprintf(time, "%02d:%02d:%02d", hour, minute, second);
lcd_update_time(time);
sleep(1);
}
}
void thread_send_to_mqtt(void)
{
const string TOPIC_PUBLISH = "weather";
sleep(3);
while (1)
{
// Time
auto now = chrono::system_clock::now();
time_t t = chrono::system_clock::to_time_t(now);
tm local_time = *localtime(&t);
int hour = local_time.tm_hour;
int minute = local_time.tm_min;
int second = local_time.tm_sec;
char data[100];
sprintf(data, "%02d:%02d:%02d - Light level: %d lx, Temperature: %.1f degree C, Humid: %d%%",
hour, minute, second, cur_light_level, cur_temp, (int)cur_humid);
// cout << "MQTT:" << data << endl;
mqtt_node->publish(TOPIC_PUBLISH, 1, data);
sleep(10);
}
}