This shows you the differences between two versions of the page.
iothings:laboratoare:2025:lab3_code3 [2025/10/11 13:19] dan.tudose removed |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | <code C main.cpp> | ||
- | #include <Arduino.h> | ||
- | #include <WiFi.h> | ||
- | #include <Wire.h> | ||
- | #include <MQTT.h> | ||
- | #include <ArduinoJson.h> | ||
- | #include <Adafruit_BME680.h> | ||
- | //////////////// EDIT THESE //////////////// | ||
- | const char* WIFI_SSID = "YOUR_SSID"; | ||
- | const char* WIFI_PASSWORD = "YOUR_PASSWORD"; | ||
- | const char* MQTT_HOST = "test.mosquitto.org"; // or your lab broker | ||
- | const uint16_t MQTT_PORT = 1883; | ||
- | const char* BASE_TOPIC = "iot/studentname"; // change per student | ||
- | //////////////////////////////////////////// | ||
- | |||
- | // Sparrow I2C: SDA=21, SCL=22 | ||
- | #define SDA_PIN 21 | ||
- | #define SCL_PIN 22 | ||
- | Adafruit_BME680 bme; // I2C | ||
- | |||
- | WiFiClient net; | ||
- | MQTTClient mqtt(1024); // 1KB message buffer | ||
- | |||
- | void ensureWiFi() { | ||
- | if (WiFi.status() == WL_CONNECTED) return; | ||
- | WiFi.mode(WIFI_STA); | ||
- | WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | ||
- | Serial.print("WiFi connecting"); | ||
- | while (WiFi.status() != WL_CONNECTED) { | ||
- | delay(400); | ||
- | Serial.print("."); | ||
- | } | ||
- | Serial.printf("\nIP: %s\n", WiFi.localIP().toString().c_str()); | ||
- | } | ||
- | |||
- | void ensureMQTT() { | ||
- | if (mqtt.connected()) return; | ||
- | |||
- | // Optional Last Will so dashboards see offline state | ||
- | String willTopic = String(BASE_TOPIC) + "/bme688/status"; | ||
- | mqtt.setWill(willTopic.c_str(), "offline", true, 1); | ||
- | |||
- | String cid = String("sparrow-c6-sense-") + String((uint32_t)ESP.getEfuseMac(), HEX); | ||
- | Serial.println("MQTT connecting..."); | ||
- | while (!mqtt.connect(cid.c_str())) { | ||
- | Serial.print("."); | ||
- | delay(1000); | ||
- | } | ||
- | Serial.println("\nMQTT connected"); | ||
- | |||
- | // Publish "online" status retained | ||
- | mqtt.publish(willTopic, "online", true, 1); | ||
- | } | ||
- | |||
- | unsigned long lastPub = 0; | ||
- | |||
- | void setup() { | ||
- | Serial.begin(115200); | ||
- | delay(200); | ||
- | |||
- | Wire.begin(SDA_PIN, SCL_PIN); | ||
- | |||
- | // MQTT broker + transport | ||
- | mqtt.begin(MQTT_HOST, MQTT_PORT, net); | ||
- | |||
- | if (!bme.begin(0x76)) { // Sparrow uses 0x76 | ||
- | Serial.println("BME688 not found!"); | ||
- | for(;;) { delay(1000); } | ||
- | } | ||
- | // Reasonable oversampling; heater off for simplicity | ||
- | bme.setTemperatureOversampling(BME680_OS_8X); | ||
- | bme.setHumidityOversampling(BME680_OS_2X); | ||
- | bme.setPressureOversampling(BME680_OS_4X); | ||
- | bme.setIIRFilterSize(BME680_FILTER_SIZE_3); | ||
- | bme.setGasHeater(0, 0); // off | ||
- | } | ||
- | |||
- | void loop() { | ||
- | ensureWiFi(); | ||
- | ensureMQTT(); | ||
- | |||
- | mqtt.loop(); // process incoming/keepalive | ||
- | delay(10); | ||
- | |||
- | if (millis() - lastPub > 10000) { | ||
- | lastPub = millis(); | ||
- | |||
- | if (!bme.performReading()) { | ||
- | Serial.println("BME read failed"); | ||
- | return; | ||
- | } | ||
- | |||
- | // Build JSON (ArduinoJson v7 style, no deprecated StaticJsonDocument) | ||
- | JsonDocument doc; | ||
- | doc["ts"] = (uint32_t)(millis() / 1000); | ||
- | doc["temp_c"] = bme.temperature; | ||
- | doc["hum_pct"] = bme.humidity; | ||
- | doc["press_hpa"] = bme.pressure / 100.0; | ||
- | doc["gas_ohm"] = bme.gas_resistance; | ||
- | |||
- | String payload; | ||
- | serializeJson(doc, payload); | ||
- | |||
- | String topic = String(BASE_TOPIC) + "/bme688"; | ||
- | bool ok = mqtt.publish(topic, payload); // QoS0, non-retained | ||
- | Serial.printf("Pub %s => %s (%s)\n", | ||
- | topic.c_str(), payload.c_str(), ok ? "OK" : "FAIL"); | ||
- | } | ||
- | } | ||
- | |||
- | </code> |