|
| 1 | +/** |
| 2 | + * IoT_Sensor_Gateway — Mode 1 (Polling / Main Loop) |
| 3 | + * Device: ESP8266 — IoT Gateway |
| 4 | + * |
| 5 | + * Responsibilities: |
| 6 | + * 1. Connect to Wi-Fi. |
| 7 | + * 2. Establish a TinyLink connection with the ATtiny88 via begin(). |
| 8 | + * 3. Every 60 seconds, send a TYPE_CMD request to the ATtiny88. |
| 9 | + * 4. When the ATtiny88 sends back a TYPE_DATA response, acknowledge it |
| 10 | + * with sendAck() and forward the temperature reading to the cloud. |
| 11 | + * |
| 12 | + * Update mode: link.update() called in loop() — portable, zero extra setup. |
| 13 | + * |
| 14 | + * Wiring: see ATtiny88_Sensor.ino |
| 15 | + */ |
| 16 | + |
| 17 | +#include <TinyLink.h> |
| 18 | +#include <adapters/TinyArduinoAdapter.h> |
| 19 | +#include <ESP8266WiFi.h> |
| 20 | +#include <ESP8266HTTPClient.h> |
| 21 | +#include "SharedData.h" |
| 22 | + |
| 23 | +using namespace tinylink; |
| 24 | + |
| 25 | +// --------------------------------------------------------------------------- |
| 26 | +// Configuration — update before uploading |
| 27 | +// --------------------------------------------------------------------------- |
| 28 | +const char* WIFI_SSID = "YourSSID"; |
| 29 | +const char* WIFI_PASS = "YourPassword"; |
| 30 | +const char* ENDPOINT_URL = "http://api.example.com/temperature"; |
| 31 | + |
| 32 | +const unsigned long REQUEST_INTERVAL_MS = 60000UL; // 60 s between requests |
| 33 | + |
| 34 | +// --------------------------------------------------------------------------- |
| 35 | + |
| 36 | +TinyArduinoAdapter adapter(Serial); |
| 37 | +TinyLink<SensorMessage, TinyArduinoAdapter> link(adapter); |
| 38 | + |
| 39 | +static uint8_t requestId = 0; |
| 40 | +static unsigned long lastRequest = 0; |
| 41 | + |
| 42 | +// --------------------------------------------------------------------------- |
| 43 | +// Send the temperature reading to the cloud endpoint |
| 44 | +// --------------------------------------------------------------------------- |
| 45 | +void postToCloud(const SensorMessage& data) { |
| 46 | + if (WiFi.status() != WL_CONNECTED) return; |
| 47 | + |
| 48 | + WiFiClient client; |
| 49 | + HTTPClient http; |
| 50 | + http.begin(client, ENDPOINT_URL); |
| 51 | + http.addHeader("Content-Type", "application/json"); |
| 52 | + |
| 53 | + char body[96]; |
| 54 | + snprintf(body, sizeof(body), |
| 55 | + "{\"id\":%u,\"temp\":%.2f,\"uptime\":%lu}", |
| 56 | + (unsigned)data.requestId, |
| 57 | + (double)data.temperature, |
| 58 | + (unsigned long)data.uptime); |
| 59 | + |
| 60 | + int code = http.POST(body); // blocks until response or timeout |
| 61 | + http.end(); |
| 62 | + (void)code; // log or handle error code as needed |
| 63 | +} |
| 64 | + |
| 65 | +// --------------------------------------------------------------------------- |
| 66 | +// Callback: fires when a TYPE_DATA response arrives from the ATtiny88 |
| 67 | +// --------------------------------------------------------------------------- |
| 68 | +void onSensorData(const SensorMessage& data) { |
| 69 | + // Step 1: ACK the response — releases the ATtiny88 from AWAITING_ACK. |
| 70 | + link.sendAck(); |
| 71 | + |
| 72 | + // Step 2: Forward the reading to the cloud. |
| 73 | + postToCloud(data); |
| 74 | +} |
| 75 | + |
| 76 | +// --------------------------------------------------------------------------- |
| 77 | + |
| 78 | +void setup() { |
| 79 | + Serial.begin(9600); |
| 80 | + |
| 81 | + WiFi.begin(WIFI_SSID, WIFI_PASS); |
| 82 | + while (WiFi.status() != WL_CONNECTED) { delay(100); } |
| 83 | + |
| 84 | + link.onDataReceived(onSensorData); |
| 85 | + link.begin(); // Initiate TinyLink handshake with the ATtiny88. |
| 86 | +} |
| 87 | + |
| 88 | +void loop() { |
| 89 | + link.update(); // Drive the TinyLink engine. |
| 90 | + |
| 91 | + // Every 60 s (once the link is up): request a sensor reading. |
| 92 | + if (link.connected() && |
| 93 | + (millis() - lastRequest >= REQUEST_INTERVAL_MS)) { |
| 94 | + |
| 95 | + SensorMessage req; |
| 96 | + req.requestId = requestId++; |
| 97 | + req.temperature = 0.0f; // not used in requests |
| 98 | + req.uptime = static_cast<uint32_t>(millis()); |
| 99 | + |
| 100 | + link.sendData(TYPE_CMD, req); // → ATtiny88 receives this as a Cmd |
| 101 | + lastRequest = millis(); |
| 102 | + } |
| 103 | +} |
0 commit comments