Skip to content

Commit

Permalink
Firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
davivc committed Mar 26, 2024
1 parent 222749a commit 850724b
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export AWS_REGION=us-east-2
1 change: 1 addition & 0 deletions backend/.env-copy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export AWS_REGION=us-east-2
11 changes: 11 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Dockerfile
FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
Binary file added backend/__pycache__/app.cpython-38.pyc
Binary file not shown.
26 changes: 26 additions & 0 deletions backend/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from flask import Flask, request, jsonify
import boto3

app = Flask(__name__)

session = boto3.Session(
region_name='us-west-2'
)

dynamodb = session.resource('dynamodb', endpoint_url='http://dynamodb:8000')
table = dynamodb.Table('BME280Data')

@app.route('/bme280', methods=['POST'])
def post_bme280_data():
data = request.get_json()
table.put_item(Item=data)
return jsonify({'message': 'Data received'}), 200

@app.route('/rain_check', methods=['GET'])
def get_rain_check():
# You need to implement the logic for checking if it's going to rain
is_going_to_rain = False
return jsonify({'is_going_to_rain': is_going_to_rain}), 200

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
25 changes: 25 additions & 0 deletions backend/create_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import boto3

dynamodb = boto3.resource('dynamodb', endpoint_url='http://localhost:8000')

table = dynamodb.create_table(
TableName='BME280Data',
KeySchema=[
{
'AttributeName': 'date',
'KeyType': 'HASH' # Partition key
}
],
AttributeDefinitions=[
{
'AttributeName': 'date',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)

print("Table status:", table.table_status)
2 changes: 2 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Flask
boto3
47 changes: 47 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
version: "3.7"
services:
dynamodb:
image: amazon/dynamodb-local
stdin_open: true
tty: true
volumes:
- "dynamo_data:/home/dynamodblocal/data"
ports:
- "8000:8000"
environment:
- DEBUG=false
- AWS_DEFAULT_REGION=us_east-2
command: "-jar DynamoDBLocal.jar -sharedDb -dbPath /home/dynamodblocal/data/"
networks:
- ecs-local-network
user: root
dynamodb-admin:
image: aaronshaf/dynamodb-admin
ports:
- "8001:8001"
environment:
DYNAMO_ENDPOINT: "http://dynamodb:8000"
AWS_REGION: "us-west-2"
AWS_ACCESS_KEY_ID: local
AWS_SECRET_ACCESS_KEY: local
depends_on:
- dynamodb
networks:
- ecs-local-network
backend:
build: ./backend
ports:
- "5000:5000"
volumes:
- ./backend:/app
env_file:
- ./backend/.env
networks:
- ecs-local-network

volumes:
dynamo_data:

networks:
ecs-local-network:
external: true
113 changes: 113 additions & 0 deletions firmware/main.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#define SEALEVELPRESSURE_HPA (1013.25)

// Initialize BME280
#define BME_SDA 2
#define BME_SCL 3
Adafruit_BME280 bme; // I2C

// Sound Sensor Pin
#define SOUND_SENSOR_PIN A0

// Initialize the LCD. The address (0x27) and size (16, 2) may vary for your display
LiquidCrystal_I2C lcd(0x27, 16, 2);

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

unsigned long lastPostTime = 0;

void setup() {
Serial.begin(9600);
delay(100);

// 1. Check BME280 sensor
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}

// 2. Connect to WiFi
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}

Serial.println("Connected to WiFi");

// 3. Initialize sound sensor
pinMode(SOUND_SENSOR_PIN, INPUT);


// 4. Initialize display
lcd.init();
lcd.backlight();
}

void loop() {
float temperature = bme.readTemperature();
float humidity = bme.readHumidity();
float pressure = bme.readPressure() / 100.0F;

// Display the BME280 data on the display
displayData(temperature, humidity, pressure);

// If a clapping sound is detected, speak the current temperature, humidity, and pressure
// ...

unsigned long currentTime = millis();
if (currentTime - lastPostTime > 60000) {
lastPostTime = currentTime;

// Post BME280 data to the endpoint
postData(temperature, humidity, pressure);
// Regularly consume an endpoint to check if it's going to rain
checkRain();
}
}

void displayData(float temperature, float humidity, float pressure) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");

lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(humidity);
lcd.print("%");

// Due to space constraints, you might need to cycle the pressure reading separately or display it based on an event
}

void postData(float temperature, float humidity, float pressure) {
HTTPClient http;
http.begin("YOUR_ENDPOINT");
http.addHeader("Content-Type", "application/json");
String postData = String("{\"temperature\":") + temperature + ",\"humidity\":" + humidity + ",\"pressure\":" + pressure + "}";
int httpCode = http.POST(postData);
http.end();
}

void checkRain() {
HTTPClient http;
http.begin("YOUR_RAIN_CHECK_ENDPOINT");
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
if (payload == "true") {
// Speak that the rain is coming
// ...
}
}
http.end();
}

0 comments on commit 850724b

Please sign in to comment.