Skip to content

Commit 0b213f8

Browse files
committed
http
1 parent ef96ffd commit 0b213f8

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

ESP/src/main.cpp

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ String espID;
2626
void connectToWifi();
2727
void setUpTime();
2828

29-
String getValuesJSON(float temperature, float humidity, int timestamp, String espID);
29+
String getValuesJSON(float temperature, float humidity, String espID);
3030

3131
String readReading();
3232

@@ -48,25 +48,42 @@ void setup()
4848
else
4949
{
5050
File file = SPIFFS.open("/espID.txt", "r");
51-
if (!file)
52-
{
53-
Serial.println("Failed to open file for reading");
5451

55-
file = SPIFFS.open("/espID.txt", "w");
52+
while (espID == "")
53+
{
5654
if (!file)
5755
{
58-
Serial.println("Failed to open file for writing");
56+
Serial.println("Failed to open file for reading");
57+
58+
file = SPIFFS.open("/espID.txt", "w");
59+
if (!file)
60+
{
61+
Serial.println("Failed to open file for writing");
62+
}
63+
else
64+
{
65+
espID = String(random(1000, 9999));
66+
file.print(espID);
67+
}
5968
}
60-
else
61-
{
62-
espID = String(random(1000, 9999));
63-
Serial.println("Esp ID: " + espID);
6469

65-
file.print(espID);
70+
espID = file.readString();
71+
if (espID == "")
72+
{
73+
file.close();
74+
file = SPIFFS.open("/espID.txt", "w");
75+
if (!file)
76+
{
77+
Serial.println("Failed to open file for writing");
78+
}
79+
else
80+
{
81+
espID = String(random(1000, 9999));
82+
file.print(espID);
83+
}
6684
}
6785
}
6886

69-
espID = file.readString();
7087
Serial.println("Esp ID: " + espID);
7188

7289
file.close();
@@ -112,7 +129,7 @@ void connectToWifi()
112129

113130
String readReading()
114131
{
115-
return getValuesJSON(dht.readTemperature(), dht.readHumidity(), time(&now), espID);
132+
return getValuesJSON(dht.readTemperature(), dht.readHumidity(), espID);
116133
}
117134

118135
void sendInfo()
@@ -122,7 +139,7 @@ void sendInfo()
122139

123140
String values;
124141
serializeJson(status, values);
125-
142+
126143
Serial.println("making POST request");
127144

128145
String path = "/addSensor";
@@ -159,7 +176,7 @@ void sendReading(String value)
159176
}
160177

161178
// Send values to web page
162-
String getValuesJSON(float temperature, float humidity, int timestamp = 0, String espID = "")
179+
String getValuesJSON(float temperature, float humidity, String espID = "")
163180
{
164181
if (isnan(temperature) || isnan(humidity))
165182
{
@@ -170,7 +187,6 @@ String getValuesJSON(float temperature, float humidity, int timestamp = 0, Strin
170187
// Return values in JSON
171188
reading["temperature"] = temperature;
172189
reading["humidity"] = humidity;
173-
reading["timestamp"] = timestamp;
174190
reading["espID"] = espID;
175191

176192
String values;
@@ -197,6 +213,7 @@ void setUpTime()
197213
wait_time++;
198214
if (wait_time > max_wait_time)
199215
{
216+
Serial.println("");
200217
Serial.println("Failed to obtain time. Check your network connection.");
201218
break;
202219
}

services/api/server.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ await fastify.register(fastifySwagger, {
2525
version: "0.1.0",
2626
},
2727
},
28+
allowedHosts: ['http://vazypi.local:9051'],
2829
});
2930

3031
await fastify.register(fastifySwaggerUI, {
@@ -108,6 +109,7 @@ fastify.post(
108109
const newReading = { espID, temperature, humidity, timestamp: new Date(timestampNew) };
109110
const result = await readings.insertOne(newReading);
110111
reply.send(result);
112+
return;
111113
}
112114
);
113115

@@ -169,9 +171,11 @@ fastify.get(
169171
});
170172

171173
reply.send(result);
174+
return;
172175
} catch (error) {
173176
console.error("Error querying MongoDB:", error);
174177
reply.code(500).send({ error: "Failed to fetch readings." });
178+
return;
175179
}
176180
}
177181
);
@@ -213,10 +217,12 @@ fastify.post(
213217
const existingSensor = await sensors.findOne({ ip, espID });
214218
if (existingSensor) {
215219
reply.code(400).send({ error: "Sensor already exists." });
220+
return;
216221
}
217222

218223
const result = await sensors.insertOne(newSensor);
219224
reply.send(result);
225+
return;
220226
}
221227
);
222228

@@ -260,6 +266,7 @@ fastify.get(
260266

261267
console.log(result);
262268
reply.send(result);
269+
return;
263270
}
264271
);
265272

@@ -311,12 +318,15 @@ fastify.patch(
311318

312319
if (result.modifiedCount === 0) {
313320
reply.code(404).send({ error: "Sensor not found or no changes made" });
321+
return;
314322
} else {
315323
reply.send({ message: "Sensor updated successfully" });
324+
return;
316325
}
317326
} catch (error) {
318327
console.error("Error updating sensor:", error);
319328
reply.code(500).send({ error: "Internal server error" });
329+
return;
320330
}
321331
}
322332
);

services/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
- action: rebuild
1616
path: ./web-nuxt
1717
environment:
18-
- NUXT_PUBLIC_API_BASE_URL=https://api:3000
18+
- NUXT_PUBLIC_API_BASE_URL=http://api:3000
1919
db:
2020
build: ./db
2121
container_name: climate-db

0 commit comments

Comments
 (0)