Installation Guide¶
This guide walks you through a complete installation of GreenCop, from infrastructure setup to deploying your first sensor.
Installation Options¶
Choose your installation method:
Best for testing and development. Uses Docker Compose for easy setup.
Time Required: 15 minutes Requirements: Docker, Docker Compose, Node.js
Production deployment with full cloud infrastructure.
Time Required: 30-45 minutes Requirements: GCP account, Terraform, Docker
Backend in cloud, frontend and sensors local for development.
Time Required: 20 minutes Requirements: GCP account, Docker
Prerequisites¶
Required Software¶
Install these tools before proceeding:
- Git: Download
- Docker: Download
- Docker Compose: Usually included with Docker Desktop
- Node.js (v18+): Download
- Python (3.12+): Download
For Cloud Deployment¶
- Google Cloud SDK: Install gcloud
- Terraform: Download
- GCP Account: Sign up
For Hardware Deployment¶
- ESP32 Development Board
- USB Cable (for flashing)
- MicroPython Firmware: Download
- esptool.py:
pip install esptool
Local Installation¶
Step 1: Clone Repository¶
Step 2: Set Up Backend¶
Navigate to the customers service:
Create environment file:
Edit .env with your settings:
# Database Configuration
DB_URL=postgresql://greencop:greencop@db:5432/greencop
# JWT Secret
SECRET_KEY=change-this-to-a-secure-random-string
# Optional: Google Cloud (leave empty for local dev)
GOOGLE_CLOUD_PROJECT=
Security
For production, generate a secure SECRET_KEY:
Start the database and API:
Wait for services to start (about 30 seconds), then run migrations:
Verify the API is running:
Expected response:
Step 3: Set Up Frontend¶
In a new terminal, navigate to the frontend:
Install dependencies:
Create environment file:
Start the development server:
The frontend will be available at http://localhost:5173.
Step 4: Verify Installation¶
Open your browser and go to:
You should see the GreenCop login page. Try registering a new account to verify everything works!
Cloud Installation¶
Step 1: Configure GCP¶
Set up your Google Cloud Project:
# Login to Google Cloud
gcloud auth login
# Create a new project (or use existing)
gcloud projects create greencop-prod --name="GreenCop Production"
# Set the project
gcloud config set project greencop-prod
# Enable required APIs
gcloud services enable \
compute.googleapis.com \
run.googleapis.com \
sql-component.googleapis.com \
sqladmin.googleapis.com \
pubsub.googleapis.com \
cloudfunctions.googleapis.com \
bigquery.googleapis.com \
cloudresourcemanager.googleapis.com
Step 2: Configure Terraform¶
Navigate to the Terraform directory:
Create a terraform.tfvars file:
project_id = "greencop-prod"
region = "us-central1"
# Database settings
db_password = "change-this-secure-password"
db_user = "greencop"
db_name = "greencop"
# Service account
service_account_email = "greencop-sa@greencop-prod.iam.gserviceaccount.com"
Initialize Terraform:
Step 3: Deploy Infrastructure¶
Review the plan:
Apply the configuration:
Type yes when prompted. This will create:
- Cloud SQL (PostgreSQL) database
- Pub/Sub topics (
dataandalerts) - Cloud Functions (data ingestion, alert detection)
- BigQuery dataset and table
- Cloud Run service for the API
- Required IAM roles and permissions
Deployment Time
Infrastructure deployment takes about 10-15 minutes. Cloud SQL instance creation is the slowest step.
Step 4: Deploy Cloud Functions¶
Package and deploy the data ingestion function:
cd ../../services/pubsub_bq_bridge
zip -r function_source.zip main.py requirements.txt
gcloud functions deploy pubsub-bq-bridge \
--runtime python312 \
--trigger-topic data \
--entry-point process_sensor_data \
--set-env-vars PROJECT_ID=greencop-prod,DATASET_ID=sensor_data,TABLE_ID=readings
Package and deploy the alert detection function:
cd ../alerts
zip -r function_source.zip main.py requirements.txt
gcloud functions deploy alert-detection \
--runtime python312 \
--trigger-topic data \
--entry-point detect_alerts \
--set-env-vars PROJECT_ID=greencop-prod,ALERT_TOPIC=alerts,MAX_ALLOWED_TEMP=50.0,MAX_ALLOWED_HUMIDITY=50.0
Step 5: Deploy Backend API¶
Build and push Docker image:
cd ../../services/customers
# Configure Docker for GCP
gcloud auth configure-docker
# Build image
docker build -t gcr.io/greencop-prod/customers-api:latest .
# Push to Google Container Registry
docker push gcr.io/greencop-prod/customers-api:latest
Deploy to Cloud Run:
gcloud run deploy customers-api \
--image gcr.io/greencop-prod/customers-api:latest \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--set-env-vars DB_URL="postgresql://greencop:your-password@/greencop?host=/cloudsql/greencop-prod:us-central1:greencop-db" \
--set-env-vars SECRET_KEY="your-secure-secret-key"
Step 6: Run Database Migrations¶
Connect to Cloud SQL and run migrations:
# Start Cloud SQL Proxy
cloud_sql_proxy -instances=greencop-prod:us-central1:greencop-db=tcp:5432 &
# Run migrations
alembic upgrade head
Step 7: Deploy Frontend¶
Build the frontend:
cd ../../web/frontend
# Set API URL to Cloud Run endpoint
echo "VITE_API_BASE_URL=https://customers-api-xxxxx-uc.a.run.app" > .env
# Build
npm run build
Deploy to your preferred hosting (Firebase Hosting, Netlify, Vercel, etc.):
Hybrid Installation¶
Backend in Cloud¶
Follow Cloud Installation steps 1-6 to deploy backend infrastructure.
Frontend Local¶
cd web/frontend
npm install
# Point to cloud API
echo "VITE_API_BASE_URL=https://customers-api-xxxxx-uc.a.run.app" > .env
npm run dev
This setup is ideal for frontend development while using production backend.
Hardware Setup¶
Install MicroPython on ESP32¶
Download MicroPython firmware:
Erase flash and install MicroPython:
esptool.py --port /dev/ttyUSB0 erase_flash
esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 ESP32_GENERIC-20240222-v1.22.2.bin
Finding Your Port
- Linux/Mac: Usually
/dev/ttyUSB0or/dev/ttyACM0 - Windows: Usually
COM3orCOM4 - Run
ls /dev/tty*(Linux/Mac) or check Device Manager (Windows)
Configure Sensor¶
Copy sensor code to your computer:
Edit config.py:
WIFI_SSID = "your-wifi-network"
WIFI_PASSWORD = "your-wifi-password"
GATEWAY_URL = "http://your-gateway-ip:8000" # Or cloud endpoint
Upload files to ESP32:
# Using ampy (install: pip install adafruit-ampy)
ampy --port /dev/ttyUSB0 put config.py
ampy --port /dev/ttyUSB0 put main.py
Reboot the ESP32. The green LED should blink when publishing data!
Verification¶
Check Backend Health¶
Check Frontend¶
Open browser to http://localhost:5173 (or your cloud URL).
Check Sensor¶
Monitor serial output:
You should see:
Connecting to WiFi...
WiFi connected!
Registering sensor...
Publishing reading: {"temperature": 25.3, "humidity": 45.2}
Check Data Pipeline¶
Query BigQuery:
bq query --use_legacy_sql=false '
SELECT * FROM `greencop-prod.sensor_data.readings`
ORDER BY timestamp DESC
LIMIT 10
'
Troubleshooting¶
Database Connection Failed¶
Symptom: API won't start, database connection errors
Solutions:
-
Verify PostgreSQL is running:
-
Check connection string in
.env -
Ensure migrations ran successfully:
Cloud Function Deployment Failed¶
Symptom: gcloud functions deploy fails
Solutions:
-
Verify APIs are enabled:
-
Check function logs:
-
Ensure service account has proper permissions
ESP32 Won't Connect¶
Symptom: Sensor can't connect to WiFi or gateway
Solutions:
- Verify WiFi credentials in
config.py - Check gateway URL is reachable
- Monitor serial output for error messages
- Ensure ESP32 has power (some USB ports don't provide enough)
Frontend Build Errors¶
Symptom: npm run build fails
Solutions:
-
Clear node_modules and reinstall:
-
Verify Node.js version:
Next Steps¶
Installation complete! Now you can:
- Register your first account
- Set up server rooms
- Configure sensors
- Explore the API
- Understand the architecture
Gateway Service Installation¶
Go Gateway Setup¶
The gateway service is required for ESP32 sensors to communicate with the cloud.
Prerequisites: - Go 1.21 or higher - Same network as ESP32 sensors
Installation:
What Happens:
1. Gateway starts HTTP server on port 8080
2. Broadcasts mDNS service greencop-gateway.local
3. ESP32 sensors auto-discover and register
4. Data flows to Google Cloud automatically
Verification:
Auto-Registration Flow¶
sequenceDiagram
participant ESP32
participant Gateway as Go Gateway
participant mDNS
participant Cloud as GCP Pub/Sub
Gateway->>mDNS: Broadcast greencop-gateway.local
ESP32->>mDNS: Resolve greencop-gateway.local
mDNS-->>ESP32: Gateway IP (e.g., 192.168.1.100)
ESP32->>Gateway: POST /api/v1/register {node_id, ip}
Gateway-->>ESP32: 201 Created
Note over ESP32,Gateway: Registration complete!
loop Every 30 seconds
ESP32->>Gateway: POST /api/v1/message {temp, humidity}
Gateway->>Cloud: Publish to Pub/Sub
ESP32->>Gateway: POST /api/v1/heartbeat
end
Zero Configuration: No IP addresses to configure, no DNS setup needed!