-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Saving successive positions to database
- Loading branch information
1 parent
f0bab35
commit ac3bdc3
Showing
5 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "StatusMessage.h" | ||
|
||
StatusMessage::StatusMessage() {} | ||
|
||
void StatusMessage::toJson(char *buffer, size_t bufferSize) | ||
{ | ||
doc["connection"] = 400; | ||
serializeJson(doc, buffer, bufferSize); | ||
} | ||
|
||
const char *StatusMessage::getTopic() | ||
{ | ||
return "esp32/status"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef STATUS_MESSAGE_H | ||
#define STATUS_MESSAGE_H | ||
|
||
#include "MqttMessage.h" | ||
|
||
class StatusMessage : public MqttMessage | ||
{ | ||
public: | ||
StatusMessage(); | ||
void toJson(char *buffer, size_t bufferSize) override; | ||
const char *getTopic() override; | ||
|
||
private: | ||
StaticJsonDocument<200> doc; | ||
}; | ||
|
||
#endif // STATUS_MESSAGE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const AWS = require('aws-sdk'); | ||
const mqtt = require('mqtt'); | ||
|
||
// Configure AWS SDK | ||
AWS.config.update({ region: 'eu-west-2' }); | ||
const dynamoDB = new AWS.DynamoDB.DocumentClient(); | ||
|
||
// Configure MQTT client | ||
const client = mqtt.connect('mqtt://18.132.10.124'); // Use your broker IP address | ||
|
||
// Global variable to store the updated NB value | ||
let globalNB = 0; | ||
|
||
client.on('connect', () => { | ||
console.log('Connected to MQTT broker'); | ||
client.subscribe(['esp32/mapping', 'esp32/status'], (err) => { | ||
if (err) { | ||
console.error('Failed to subscribe to topics', err); | ||
} else { | ||
console.log('Subscribed to topics'); | ||
} | ||
}); | ||
}); | ||
|
||
client.on('message', async (topic, message) => { | ||
if (topic === 'esp32/mapping') { | ||
const data = JSON.parse(message.toString()); | ||
|
||
const params = { | ||
TableName: 'Mapping', | ||
Item: { | ||
RunNb: globalNB, // Set the appropriate RunNb from globalNB | ||
Timestamp: data.timestamp, // Ensure case matches DynamoDB schema | ||
x: data.x, | ||
y: data.y, | ||
orientation: data.orientation | ||
}, | ||
}; | ||
|
||
try { | ||
await dynamoDB.put(params).promise(); | ||
console.log('Data inserted successfully'); | ||
} catch (error) { | ||
console.error('Could not insert data', error); | ||
} | ||
} else if (topic === 'esp32/status') { | ||
const data = JSON.parse(message.toString()); | ||
console.log('Received message on esp32/status!', data); | ||
|
||
// Retrieve the current NB value from DynamoDB | ||
const getNBParams = { | ||
TableName: 'Status', | ||
Key: { | ||
'Id': 1 // Assuming Id 1 holds the NB value | ||
} | ||
}; | ||
|
||
try { | ||
const nbResult = await dynamoDB.get(getNBParams).promise(); | ||
let currentNB = 0; | ||
|
||
if (nbResult.Item && nbResult.Item.NB !== undefined) { | ||
currentNB = nbResult.Item.NB; | ||
} | ||
|
||
// Update NB by adding 1 | ||
globalNB = currentNB + 1; | ||
console.log('Updated NB:', globalNB); | ||
|
||
// Save the new NB value back to DynamoDB | ||
const updateNBParams = { | ||
TableName: 'Status', | ||
Key: { | ||
'Id': 1 // Assuming Id 1 holds the NB value | ||
}, | ||
UpdateExpression: 'set NB = :newNB', | ||
ExpressionAttributeValues: { | ||
':newNB': globalNB | ||
} | ||
}; | ||
|
||
await dynamoDB.update(updateNBParams).promise(); | ||
console.log('NB value updated in DynamoDB'); | ||
|
||
} catch (error) { | ||
console.error('Error retrieving or updating NB value:', error); | ||
} | ||
} | ||
}); | ||
|
||
client.on('error', (error) => { | ||
console.error('MQTT Client Error:', error); | ||
}); |