This shows you the differences between two versions of the page.
iothings:laboratoare:2025:lab3_code1 [2025/10/11 12:26] dan.tudose created |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | <code C main.cpp> | ||
- | #include <Arduino.h> | ||
- | #include <WiFi.h> | ||
- | #include <MQTT.h> // 256dpi MQTT library | ||
- | |||
- | //////////////// 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 | ||
- | //////////////////////////////////////////// | ||
- | |||
- | WiFiClient net; | ||
- | MQTTClient mqtt(1024); // 1KB message buffer | ||
- | |||
- | unsigned long lastPub = 0; | ||
- | |||
- | void connectWiFi() { | ||
- | 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(500); | ||
- | Serial.print("."); | ||
- | } | ||
- | Serial.printf("\nWiFi OK, IP: %s\n", WiFi.localIP().toString().c_str()); | ||
- | } | ||
- | |||
- | void messageReceived(String &topic, String &payload) { | ||
- | Serial.printf("Message on %s: %s\n", topic.c_str(), payload.c_str()); | ||
- | } | ||
- | |||
- | void connectMQTT() { | ||
- | // Build a real String, then pass c_str() | ||
- | String clientId = "esp32-client-" + String((uint32_t)ESP.getEfuseMac(), HEX); | ||
- | |||
- | Serial.println("MQTT connecting..."); | ||
- | while (!mqtt.connect(clientId.c_str())) { | ||
- | Serial.print("."); | ||
- | delay(1000); | ||
- | } | ||
- | Serial.println("\nMQTT connected!"); | ||
- | |||
- | // Optional subscription | ||
- | String subTopic = String(BASE_TOPIC) + "/#"; | ||
- | mqtt.subscribe(subTopic); | ||
- | Serial.printf("Subscribed to %s\n", subTopic.c_str()); | ||
- | } | ||
- | |||
- | void setup() { | ||
- | Serial.begin(115200); | ||
- | delay(500); | ||
- | |||
- | connectWiFi(); | ||
- | |||
- | mqtt.begin(MQTT_HOST, MQTT_PORT, net); | ||
- | mqtt.onMessage(messageReceived); | ||
- | |||
- | // Optional: last will so brokers/clients know if we drop | ||
- | String willTopic = String(BASE_TOPIC) + "/status"; | ||
- | mqtt.setWill(willTopic.c_str(), "offline", true, 1); | ||
- | |||
- | connectMQTT(); | ||
- | |||
- | // Publish "online" once connected | ||
- | mqtt.publish(willTopic, "online", true, 1); | ||
- | } | ||
- | |||
- | void loop() { | ||
- | connectWiFi(); // ensure WiFi | ||
- | if (!mqtt.connected()) connectMQTT(); | ||
- | mqtt.loop(); // non-blocking | ||
- | delay(10); | ||
- | |||
- | // Publish every 5 seconds | ||
- | if (millis() - lastPub > 5000) { | ||
- | lastPub = millis(); | ||
- | String topic = String(BASE_TOPIC) + "/heartbeat"; | ||
- | String payload = String("{\"uptime_ms\":") + millis() + "}"; | ||
- | bool ok = mqtt.publish(topic, payload); | ||
- | Serial.printf("Publish %s => %s (%s)\n", | ||
- | topic.c_str(), payload.c_str(), ok ? "OK" : "FAIL"); | ||
- | } | ||
- | } | ||
- | </code> |