JairoDanielMT's picture
Upload 4 files
aff7a76 verified
raw
history blame
1.21 kB
from fastapi import FastAPI
import sqlite3
import uvicorn
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(
title="API para Arduino Sensor Data",
description="API para almacenar datos de sensores de Arduino en una base de datos SQLite",
version="1.2.1",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
def read_root():
return {"message": "Welcome to the Arduino sensor data API"}
@app.post("/sensor_data")
async def create_sensor_data(
timestamp: str,
humedad_suelo: float,
luz: float,
turbidez: float,
voltaje: float,
estado: str,
):
conn = sqlite3.connect("arduino_data.db")
cursor = conn.cursor()
cursor.execute(
"INSERT INTO sensor_data (timestamp, humedad_suelo, luz, turbidez, voltaje, estado) VALUES (?, ?, ?, ?, ?, ?)",
(timestamp, humedad_suelo, luz, turbidez, voltaje, estado),
)
conn.commit()
conn.close()
return {"message": "Data inserted successfully"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)