This shows you the differences between two versions of the page.
— |
iothings:laboratoare:2025_code:lab3_2 [2025/10/11 13:18] (current) dan.tudose created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <code C main.cpp> | ||
+ | #include <Arduino.h> | ||
+ | #include <WiFi.h> | ||
+ | #include <MQTT.h> // 256dpi MQTT library | ||
+ | #include <ArduinoJson.h> | ||
+ | #include <Adafruit_NeoPixel.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 | ||
+ | //////////////////////////////////////////// | ||
+ | |||
+ | #define NEOPIXEL_PIN 3 | ||
+ | Adafruit_NeoPixel pixel(1, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); | ||
+ | |||
+ | WiFiClient net; | ||
+ | MQTTClient mqtt(1024); // 1KB message buffer | ||
+ | |||
+ | // ---------- Wi-Fi ---------- | ||
+ | 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()); | ||
+ | } | ||
+ | |||
+ | // ---------- MQTT message handler (256dpi signature) ---------- | ||
+ | void onMessage(String &topic, String &payload) { | ||
+ | Serial.printf("MQTT msg on %s: %s\n", topic.c_str(), payload.c_str()); | ||
+ | |||
+ | // Create a dynamic document on the stack with a defined capacity | ||
+ | JsonDocument doc; | ||
+ | DeserializationError err = deserializeJson(doc, payload); | ||
+ | if (err) { | ||
+ | Serial.printf("JSON parse error: %s\n", err.c_str()); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | int r = doc["r"] | 0; | ||
+ | int g = doc["g"] | 0; | ||
+ | int b = doc["b"] | 0; | ||
+ | int brightness = doc["brightness"] | 50; | ||
+ | |||
+ | pixel.setPixelColor(0, pixel.Color(r, g, b)); | ||
+ | pixel.setBrightness(brightness); | ||
+ | pixel.show(); | ||
+ | |||
+ | // Acknowledge | ||
+ | String ackTopic = String(BASE_TOPIC) + "/led/ack"; | ||
+ | JsonDocument ack; // also replace StaticJsonDocument here | ||
+ | ack["ok"] = true; | ||
+ | ack["r"] = r; | ||
+ | ack["g"] = g; | ||
+ | ack["b"] = b; | ||
+ | ack["brightness"] = brightness; | ||
+ | |||
+ | char buf[96]; | ||
+ | size_t n = serializeJson(ack, buf, sizeof(buf)); | ||
+ | mqtt.publish(ackTopic, String(buf, n)); | ||
+ | } | ||
+ | |||
+ | |||
+ | // ---------- MQTT connect / subscribe ---------- | ||
+ | void ensureMQTT() { | ||
+ | if (mqtt.connected()) return; | ||
+ | |||
+ | String clientId = "sparrow-c6-led-" + String((uint32_t)ESP.getEfuseMac(), HEX); | ||
+ | |||
+ | Serial.println("MQTT connecting..."); | ||
+ | while (!mqtt.connect(clientId.c_str())) { | ||
+ | Serial.print("."); | ||
+ | delay(1000); | ||
+ | } | ||
+ | Serial.println("\nMQTT connected"); | ||
+ | |||
+ | String sub = String(BASE_TOPIC) + "/led"; | ||
+ | mqtt.subscribe(sub); | ||
+ | Serial.printf("Subscribed: %s\n", sub.c_str()); | ||
+ | } | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(115200); | ||
+ | delay(200); | ||
+ | |||
+ | pixel.begin(); | ||
+ | pixel.clear(); | ||
+ | pixel.show(); | ||
+ | |||
+ | // Broker + transport setup and callback | ||
+ | mqtt.begin(MQTT_HOST, MQTT_PORT, net); | ||
+ | mqtt.onMessage(onMessage); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | ensureWiFi(); | ||
+ | ensureMQTT(); | ||
+ | |||
+ | // Process incoming packets and keepalive | ||
+ | mqtt.loop(); | ||
+ | delay(10); | ||
+ | } | ||
+ | |||
+ | </code> |