-
Notifications
You must be signed in to change notification settings - Fork 1
/
bluesense.ino
156 lines (130 loc) · 4.33 KB
/
bluesense.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
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <Arduino.h>
#include <PubSubClient.h>
#include <Arduino.h>
#include <WiFiClientSecure.h>
#include "Credentials.h"
// #include "Certificate.h"
#include "./src/services/sensors/Tds.h"
#include "./src/services/sensors/Ph.h"
#include "./src/services/logs/PostLog.h"
WiFiClientSecure wifiClient;
PubSubClient client(wifiClient);
float tdsVal = 0;
float phVal = 0;
float voltage = 0;
String macAddress;
const unsigned long interval = 60000 * 10; // Set the interval in milliseconds
const unsigned long intervalMqttPost = 1000; // Set the interval in milliseconds
unsigned long previousMillis = 0;
unsigned long previousMillisMqttPost = 0;
void setup() {
Serial.begin(115200);
setupTds();
setupPh();
setupPostLogToServer();
setupWifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
client.subscribe("esp32/dev");
}
void setupWifi() {
const char *ssid = "awikwok";
const char *password = "888#0N0d";
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed!\n");
// return;
}
macAddress = WiFi.macAddress();
macAddress.replace(":", "");
Serial.println(macAddress);
// connected = WiFi.waitForConnectResult() != WL_CONNECTED;
Serial.println(WiFi.localIP());
wifiClient.setInsecure();
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void reconnect() {
while (!client.connected()) {
if (client.connect(macAddress.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.println("failed with state ");
Serial.println(client.state());
}
}
}
void loop() {
// printSensorValue();
tdsLoop(&tdsVal);
loopPh(&phVal);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// postLogToServer(wifiClient, macAddress, tdsVal, phVal);
if (WiFi.status() == WL_CONNECTED) {
if (wifiClient) {
// create an HTTPClient instance
HTTPClient https;
// Initializing an HTTPS communication using the secure client
Serial.print("[HTTPS] begin...\n");
if (https.begin(wifiClient, serverName)) { // HTTPS
Serial.print("[HTTPS] POST LOG...\n");
// start connection and send HTTP header
https.addHeader("Content-Type", "application/json");
StaticJsonDocument<200> jsonDoc;
jsonDoc["device_id"] = macAddress;
jsonDoc["ph"] = phVal;
jsonDoc["tds"] = tdsVal;
String jsonString;
serializeJson(jsonDoc, jsonString);
int httpResponseCode = https.POST(jsonString);
// // httpCode will be negative on error
if (httpResponseCode > 0) {
Serial.printf("[HTTPS] POST... code: %d\n", httpResponseCode);
if (httpResponseCode == HTTP_CODE_OK || httpResponseCode == HTTP_CODE_MOVED_PERMANENTLY) {
// print server response payload
String payload = https.getString();
Serial.println(payload);
}
} else {
// Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpResponseCode).c_str());
}
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
https.end();
}
}
} else {
Serial.println("WiFi Disconnected");
}
previousMillis = currentMillis;
}
if (!client.connected()) {
reconnect();
} else {
if (currentMillis - previousMillisMqttPost >= intervalMqttPost) {
StaticJsonDocument<200> jsonDoc;
// JsonObject phObj = jsonDoc.createNestedObject("ph");
jsonDoc["ph"] = phVal;
jsonDoc["tds"] = tdsVal;
String jsonString;
serializeJson(jsonDoc, jsonString);
client.publish("esp32/dev", jsonString.c_str());
Serial.println(jsonString);
previousMillisMqttPost = currentMillis;
}
}
client.loop();
}