Skip to content

Commit

Permalink
AWS secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
davivc committed May 6, 2024
1 parent f1efe27 commit aa1b118
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 124 deletions.
4 changes: 3 additions & 1 deletion backend/.env
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export AWS_REGION=us-east-2
export AWS_REGION='us-east-2'
export AWS_ACCESS_KEY_ID='DUMMYIDEXAMPLE'
export AWS_SECRET_ACCESS_KEY='DUMMYEXAMPLEKEY'
Binary file modified backend/__pycache__/app.cpython-38.pyc
Binary file not shown.
35 changes: 21 additions & 14 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,32 @@
import boto3

app = Flask(__name__)
app.debug = True

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

dynamodb = session.resource('dynamodb', endpoint_url='http://dynamodb:8000')
table = dynamodb.Table('BME280Data')
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'])
@app.route("/bme280", methods=["GET", "POST"])
def bme280_data():
if request.method == "POST":
data = request.get_json()
table.put_item(Item=data)
return jsonify({"message": "Data received"}), 200

response = table.scan()
data = response["Items"]
return jsonify(data), 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
return jsonify({"is_going_to_rain": is_going_to_rain}), 200


if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: "3.7"
name: arduino_weather
services:
dynamodb:
image: amazon/dynamodb-local
Expand Down
218 changes: 218 additions & 0 deletions firmware/firmware.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
// #include <WiFiClient.h>

#define SEALEVELPRESSURE_HPA (1013.25)

// LED pins
int redPin = D6;
int greenPin = D7;
int bluePin = D8;

// Initialize BME280
Adafruit_BME280 bme; // I2C

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

const char* ssid = "ai_dinamica";
const char* password = "ai_dinamica";

unsigned long lastPostTime = 0;

WiFiClientSecure client;

void setup() {
Serial.println("Starting Arduino...");
Serial.begin(9600);
while (!Serial) {
delay(10);
}

// Set RGB LED pins as output
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

// 1. Initialize display
lightOn("blue");
Serial.println("Initializing display...");
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hello, there!");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(1000);

// 2. Check BME280 sensor
if (!bme.begin(0x76)) {
clearLCDLine(1);
lcd.print("Could not find BME280 sensor, check wiring!");
Serial.println("Could not find BME280 sensor, check wiring!");
lightOn("red");
while (1);
}

clearLCDLine(1);
lcd.setCursor(0, 1);
lcd.print("WiFi Setup");
delay(500);

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

while (WiFi.status() != WL_CONNECTED) {
lightOn("red");
delay(500);
clearLCDLine(1);
lcd.setCursor(0, 1);
loopMessage("Connecting to WiFi... ", 10);
Serial.println("Connecting to WiFi...");
}

clearLCDLine(1);
lcd.setCursor(0, 1);
lcd.print("Connected!");
Serial.println("Connected to WiFi");
lightOn("green");

delay(5000);
lightOn("blue");
}

void loop() {

unsigned long currentTime = millis();
if (currentTime - lastPostTime > 10000 or lastPostTime == 0) {
lastPostTime = currentTime;

// Check measurements
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);

// Post BME280 data to the endpoint
// postData(temperature, humidity, pressure);
}
}

void displayData(float t, float h, float p) {
lcd.clear();

lcd.setCursor(0, 0);
lcd.print(t);
lcd.setCursor(5, 0);
lcd.print("C");

lcd.setCursor(10, 0);
lcd.print(h);
lcd.setCursor(15, 0);
lcd.print("%");

lcd.setCursor(0, 1);
lcd.print(p);
lcd.setCursor(7, 1);
lcd.print("hPa");
}

void loopMessage(String phrase, int loop) {
int len = phrase.length();
int displayWidth = 16; // The number of columns in the LCD

for (int offset = 0; offset < 20; offset++) { // Infinite loop
// Calculate the start and end indices for the substring
int startIdx = offset % len;
int endIdx = startIdx + displayWidth;
String toDisplay;

if (endIdx <= len) {
// No wrap-around needed
toDisplay = phrase.substring(startIdx, endIdx);
} else {
// Wrap around the end of the phrase to the beginning
toDisplay = phrase.substring(startIdx, len) + phrase.substring(0, endIdx % len);
}

// Set the cursor position
lcd.setCursor(0, 1); // Assuming you want to display it on the second row

// Print the substring on LCD and Serial
lcd.print(toDisplay);

// Delay to control the speed of scrolling
delay(500); // Adjust delay as needed

// Clear the part of the LCD where the text is displayed to prevent ghosting
clearLCDLine(1);
}
}

// 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();
// }

void clearLCDLine(int line) {
// Clear the line
lcd.setCursor(0, line); // Set cursor to the beginning of the first line
for(int i = 0; i < lcdCols; i++) {
lcd.print(" "); // Overwrite the line with spaces
}
}

void lightOn(String color) {
if (color == "red") {
setColor(255, 0, 0);
}
else if (color == "green") {
setColor(0, 255, 0);
}
else if (color == "blue") {
setColor(0, 0, 255);
}
else if (color == "yellow") {
setColor(255, 235, 0);
}
}

void lightOff() {
setColor(0, 0, 0);
}

// Function to set color
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

Loading

0 comments on commit aa1b118

Please sign in to comment.