================================================================================
  Simple Question SQ3: Minimal CoAP Server & Client (aiocoap)
================================================================================

FULL QUESTION:
SQ3. Minimal CoAP Test: Implement a minimal asynchronous CoAP Server on UDP port 5683 and a standalone CoAP Client that fetches a JSON payload via GET request.
Use this simpler program to verify that aiocoap is working on UDP port 5683 without socket binding issues before running larger CoAP applications.

SHORT QUESTION:
Minimal CoAP Server & Client Verification Pair.

================================================================================
README & SETUP INSTRUCTIONS:
================================================================================
# SQ3: Minimal CoAP Server & Client Verification

## Purpose
Test asynchronous CoAP communication using `aiocoap` on local port 5683.

## Requirements
```bash
pip install aiocoap
```

## How to Run
1. Open Terminal 1:
   ```bash
   python server.py
   ```
2. Open Terminal 2:
   ```bash
   python client.py
   ```

================================================================================
SOURCE CODE FILES:
================================================================================

1. server.py
--------------------------------------------------------------------------------
import asyncio
import json
import aiocoap
import aiocoap.resource as resource

class SimpleResource(resource.Resource):

    def __init__(self):
        super().__init__()

    async def render_get(self, request):
        payload = json.dumps({
            "status": "OK",
            "protocol": "CoAP",
            "temperature": 27.5,
            "humidity": 60.0
        })
        print(f"[CoAP Server] Served GET request: {payload}")
        return aiocoap.Message(payload=payload.encode("utf-8"), content_format=50)

async def main():
    root = resource.Site()
    root.add_resource(["data"], SimpleResource())
    await aiocoap.Context.create_server_context(root, bind=("127.0.0.1", 5683))
    print("CoAP Server listening on coap://127.0.0.1:5683/data")
    await asyncio.get_running_loop().create_future()

if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("\nCoAP Server stopped.")
--------------------------------------------------------------------------------

2. client.py
--------------------------------------------------------------------------------
import asyncio
import json
import aiocoap

async def main():
    context = await aiocoap.Context.create_client_context()
    request = aiocoap.Message(code=aiocoap.GET, uri="coap://127.0.0.1:5683/data")
    print("Sending CoAP GET request to coap://127.0.0.1:5683/data...")
    try:
        response = await context.request(request).response
        data = json.loads(response.payload.decode("utf-8"))
        print("\n[SUCCESS] Received Response from CoAP Server:")
        for k, v in data.items():
            print(f"  {k}: {v}")
    except Exception as e:
        print(f"[ERROR] Failed to query CoAP Server: {e}")

if __name__ == "__main__":
    asyncio.run(main())
--------------------------------------------------------------------------------
