Skip to content

Commit

Permalink
Saving successive positions to database
Browse files Browse the repository at this point in the history
  • Loading branch information
sarachehab committed May 23, 2024
1 parent f0bab35 commit ac3bdc3
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions esp32/lib/WifiSetup/MqttSetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ void MqttSetup::connect(unsigned long timeout)
delay(500);
}
}

// Send a status message to update run number on server
StatusMessage statusMessage;
publishMessage(statusMessage);
}

void MqttSetup::loop()
Expand Down
1 change: 1 addition & 0 deletions esp32/lib/WifiSetup/MqttSetup.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "MqttMessage.h"
#include "BatteryMessage.h"
#include "MappingMessage.h"
#include "StatusMessage.h"

class MqttSetup
{
Expand Down
14 changes: 14 additions & 0 deletions esp32/lib/WifiSetup/StatusMessage.cpp
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";
}
17 changes: 17 additions & 0 deletions esp32/lib/WifiSetup/StatusMessage.h
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
93 changes: 93 additions & 0 deletions server/write_database.js
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);
});

0 comments on commit ac3bdc3

Please sign in to comment.