balcony_weather_station/INTEGRATION_COMPLETE.md

176 lines
No EOL
5.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# TCP Client Integration Complete ✓
The `node1.c` file has been successfully updated to use the TCP client library to send sensor data to your Go backend server.
## Changes Made
### 1. node1.c
**Added:**
- WiFi and backend server configuration defines
- `wifi_init()` function to connect to WiFi
- TCP client initialization in `main()`
- Actual message sending in `cb_30` callback (replaced commented-out code)
- Error handling for WiFi/TCP failures
**Key Features:**
- WiFi connects on startup with 30-second timeout
- If WiFi fails, sensor readings continue but data isn't sent
- Every 5 minutes (10 readings × 10 seconds), sends JSON to your Go server
- Comms LED blinks on successful sends
- Prints status messages to serial output
### 2. CmakeLists.txt
**Added:**
- `tcp_client.c` to node1 executable sources
- Backend server configuration variables
- Compile definitions for `BACKEND_SERVER_IP` and `BACKEND_SERVER_PORT`
## Message Format
The Pico sends JSON messages to `192.168.1.153:8080`:
```json
{"temperature":23.50,"pressure":1013.25,"humidity":45.00,"pm1":10.00,"pm2_5":25.50,"pm10":50.00}
```
Your Go server will receive this in the `handleConnection` function and log it with structured logging.
## Building
```bash
cd build
cmake .. \
-DWIFI_SSID="your_ssid" \
-DWIFI_PASSWORD="your_password" \
-DMQTT_SERVER="your_mqtt_server" \
-DMQTT_USERNAME="mqtt_user" \
-DMQTT_PASSWORD="mqtt_pass" \
-DBACKEND_SERVER_IP="192.168.1.153" \
-DBACKEND_SERVER_PORT=8080
make node1
```
Or if you have a build script that sets environment variables, just run it.
## Testing
1. **Start the Go server:**
```bash
cd event_proxy
go run main.go
```
You should see:
```
{"time":"...","level":"INFO","msg":"Starting TCP server","address":"192.168.1.153:8080"}
```
2. **Flash the Pico W:**
```bash
# Use your flash.sh script or:
cp build/node1.uf2 /Volumes/RPI-RP2/
```
3. **Monitor the Pico:**
```bash
# Use your screen.sh script or:
screen /dev/tty.usbmodem* 115200
```
4. **Watch for connection messages:**
**On Pico serial output:**
```
Initializing WiFi...
Connecting to WiFi 'your_ssid'...
WiFi connected successfully
TCP client ready: 192.168.1.153:8080
Making BME280 Reading
cb_30: 0
...
cb_30: 9
Preparing data to send
Temperature: 23.50
Pressure: 1013.25
Humidity: 45.00
PM1: 10.00
PM2.5: 25.50
PM10: 50.00
Sending data to backend server...
tcp_client: connecting to 192.168.1.153 port 8080
tcp_client: connected, sending message
tcp_client: sent 108 bytes
tcp_client: message sent completely
tcp_client: send success
✓ Data sent successfully
COMMS LED BLINK COUNT: 10
```
**On Go server output:**
```json
{"time":"...","level":"INFO","msg":"New connection","remote_addr":"192.168.1.xxx:xxxxx"}
{"time":"...","level":"INFO","msg":"Received message","message":"{\"temperature\":23.50,\"pressure\":1013.25,\"humidity\":45.00,\"pm1\":10.00,\"pm2_5\":25.50,\"pm10\":50.00}\n","bytes":108}
{"time":"...","level":"INFO","msg":"Error reading from connection","error":"EOF"}
```
## Configuration
All configuration is in CMake variables or defines:
| Variable | Default | Description |
|----------|---------|-------------|
| `WIFI_SSID` | - | Your WiFi network name |
| `WIFI_PASSWORD` | - | Your WiFi password |
| `BACKEND_SERVER_IP` | `192.168.1.153` | Your Go server IP |
| `BACKEND_SERVER_PORT` | `8080` | Your Go server port |
| `LOOP_INTERVAL_MS` | `10000` | Time between readings (10 sec) |
## Timing
- **Reading interval:** 10 seconds (configured by `LOOP_INTERVAL_MS`)
- **Readings per batch:** 10
- **Send interval:** 100 seconds (~1.7 minutes)
- **Connection timeout:** 10 seconds
- **Watchdog timeout:** 60 seconds
- **Auto-restart:** 24 hours
## Troubleshooting
### WiFi not connecting
- Check SSID and password in build command
- Ensure WiFi is 2.4GHz (Pico W doesn't support 5GHz)
- Monitor serial output for detailed error messages
### TCP connection fails
- Verify Go server is running: `go run event_proxy/main.go`
- Check server IP is correct: `192.168.1.153`
- Ensure both devices are on same network
- Check firewall settings on server machine
### Data not arriving at server
- Verify `wifi_connected` is true in serial output
- Check for "✓ Data sent successfully" messages
- Look for connection logs in Go server output
- Increase timeout if network is slow (change 10000 to 20000)
## Next Steps
1. **Run it!** Flash the Pico and start the Go server
2. **Monitor both** to verify data flow
3. **Process the data** in your Go server (parse JSON, store to DB, forward to Home Assistant, etc.)
4. **Adjust timing** if needed by changing `LOOP_INTERVAL_MS`
## Files Reference
- `tcp_client.h` - TCP client header
- `tcp_client.c` - TCP client implementation
- `node1.c` - Main application (updated)
- `CmakeLists.txt` - Build configuration (updated)
- `TCP_CLIENT_README.md` - Detailed API documentation
- `event_proxy/main.go` - Your Go TCP server
## Success! 🎉
Your Pico W weather station now sends sensor data over WiFi to your Go backend server every ~1.7 minutes!