Skip to content

Commit a3c99fc

Browse files
committed
Firmware source file for the QTemp32 demo application.
1 parent 8a7bd18 commit a3c99fc

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

demo/qtemp32/firmware/firmware.ino

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* This file is part of QLBase (https://github.com/nthnn/QLBase).
3+
* Copyright 2024 - Nathanne Isip
4+
*
5+
* Permission is hereby granted, free of charge,
6+
* to any person obtaining a copy of this software
7+
* and associated documentation files (the “Software”),
8+
* to deal in the Software without restriction,
9+
* including without limitation the rights to use, copy,
10+
* modify, merge, publish, distribute, sublicense, and/or
11+
* sell copies of the Software, and to permit persons to
12+
* whom the Software is furnished to do so, subject to
13+
* the following conditions:
14+
*
15+
* The above copyright notice and this permission notice
16+
* shall be included in all copies or substantial portions
17+
* of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF
20+
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
21+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
22+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
23+
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
24+
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
27+
* OR OTHER DEALINGS IN THE SOFTWARE.
28+
*/
29+
30+
#include <ArduinoJson.h>
31+
#include <base64.h>
32+
#include <DFRobot_DHT11.h>
33+
#include <dynaconfig.h>
34+
#include <HTTPClient.h>
35+
#include <WiFi.h>
36+
37+
#define DHT11_PIN 5
38+
39+
// These are the localized version of the app
40+
// key and ID on the development environment.
41+
const char* serverUrl = "http://192.168.100.122/qlbase/api/index.php?action=track_create_live_timestamp";
42+
const char* apiKey = "qba_8925a3c887_5f663b70";
43+
const char* appId = "3871-c096-107c-94a0";
44+
45+
DFRobot_DHT11 DHT;
46+
String anonId, userId;
47+
48+
String generateRandomString() {
49+
const char characters[] = "abcdefghijklmnopqrstuvwxyz";
50+
const int charactersLength = sizeof(characters) - 1;
51+
52+
String result = "";
53+
for(int i = 0; i < 8; i++)
54+
result += characters[random(0, charactersLength)];
55+
return result;
56+
}
57+
58+
void setup() {
59+
Serial.begin(115200);
60+
61+
DynaConfig dynaConfig("QTemp32");
62+
dynaConfig.checkWiFiConfig();
63+
64+
WiFi.mode(WIFI_STA);
65+
WiFi.begin(
66+
dynaConfig.getConfigSSID(),
67+
dynaConfig.getConfigPasskey()
68+
);
69+
dynaConfig.close();
70+
71+
Serial.println("Connecting");
72+
while(WiFi.status() != WL_CONNECTED) {
73+
Serial.print(".");
74+
delay(100);
75+
}
76+
77+
Serial.println("\nConnected to the WiFi network");
78+
Serial.print("Local ESP32 IP: ");
79+
Serial.println(WiFi.localIP());
80+
81+
anonId = generateRandomString(),
82+
userId = generateRandomString();
83+
}
84+
85+
void loop() {
86+
String tracker = generateRandomString();
87+
DHT.read(DHT11_PIN);
88+
89+
DynamicJsonDocument jsonData(256);
90+
jsonData["temp"] = DHT.temperature;
91+
jsonData["humid"] = DHT.humidity;
92+
93+
String dataString;
94+
serializeJson(jsonData, dataString);
95+
96+
DynamicJsonDocument jsonDoc(256);
97+
jsonDoc["tracker"] = tracker;
98+
jsonDoc["anon_id"] = anonId;
99+
jsonDoc["user_id"] = userId;
100+
jsonDoc["event"] = "temprecevt";
101+
jsonDoc["payload"] = base64::encode(dataString);
102+
103+
String requestBody;
104+
serializeJson(jsonDoc, requestBody);
105+
106+
if(WiFi.status() == WL_CONNECTED) {
107+
HTTPClient http;
108+
http.begin(serverUrl);
109+
http.addHeader("Content-Type", "application/json");
110+
http.addHeader("QLBase-API-Key", apiKey);
111+
http.addHeader("QLBase-App-ID", appId);
112+
113+
int httpResponseCode = http.POST(requestBody);
114+
if(httpResponseCode > 0) {
115+
Serial.printf("HTTP Response code: %d\n", httpResponseCode);
116+
117+
String response = http.getString();
118+
Serial.println("Response:");
119+
Serial.println(response);
120+
}
121+
else Serial.printf(
122+
"Error in sending POST: %s\n",
123+
http.errorToString(httpResponseCode).c_str()
124+
);
125+
126+
http.end();
127+
}
128+
129+
delay(3000);
130+
}

0 commit comments

Comments
 (0)