Created
December 4, 2025 12:46
-
-
Save adenine/200fe8f21eb89a5f7740fe5b93b9751f to your computer and use it in GitHub Desktop.
Arduino R4 MQTT Code Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Code with relay and MQTT control | |
| #include <ArduinoMqttClient.h> | |
| #include <WiFiNINA.h> | |
| #include "arduino_secrets.h" | |
| #include <Arduino_JSON.h> | |
| #include <assert.h> | |
| const char input[] = "{\"result\":true,\"count\":42,\"foo\":\"bar\"}"; | |
| //const char input[] = "{\"result\":true,\"count\":42,\"foo\":\"bar\"}"; | |
| ///////please enter your sensitive data in the Secret tab/arduino_secrets.h | |
| char ssid[] = SECRET_SSID; // your network SSID (name) | |
| char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) | |
| WiFiClient wifiClient; | |
| MqttClient mqttClient(wifiClient); | |
| const char broker[] = "10.35.30.52"; | |
| int port = 1883; | |
| const char topic[] = "real_unique_topic"; | |
| const char topic2[] = "real_unique_topic_2"; | |
| const char topic3[] = "real_unique_topic_3"; | |
| // π’ NEW: Topic for receiving lamp control commands | |
| const char lampControlTopic[] = "lamp/control"; | |
| //set interval for sending messages (milliseconds) | |
| const long interval = 8000; | |
| unsigned long previousMillis = 0; | |
| int count = 0; | |
| const int relayPin = 7; | |
| bool lampState = false; | |
| void setup() { | |
| //Initialize serial and wait for port to open: | |
| Serial.begin(9600); | |
| // attempt to connect to Wifi network: | |
| Serial.print("Attempting to connect to WPA SSID: "); | |
| Serial.println(ssid); | |
| while (WiFi.begin(ssid, pass) != WL_CONNECTED) { | |
| // failed, retry | |
| Serial.print("."); | |
| delay(5000); | |
| } | |
| Serial.println("You're connected to the network"); | |
| Serial.println(); | |
| Serial.print("Attempting to connect to the MQTT broker: "); | |
| Serial.println(broker); | |
| if (!mqttClient.connect(broker, port)) { | |
| Serial.print("MQTT connection failed! Error code = "); | |
| Serial.println(mqttClient.connectError()); | |
| while (1) | |
| ; | |
| } | |
| Serial.println("You're connected to the MQTT broker!"); | |
| Serial.println(); | |
| // send message, the Print interface can be used to set the message contents | |
| mqttClient.beginMessage("Yuxispace"); | |
| char name[] = "IOTLamp,"; | |
| char desc[] = "Lamp connected to the interwebs,"; | |
| char pubs[] = "Switch,"; | |
| char subs[] = "Power"; | |
| // char SendString[] = (name + "," + desc + "," + pubs + "," + subs) | |
| mqttClient.print(name); | |
| mqttClient.print(desc); | |
| mqttClient.print(pubs); | |
| mqttClient.print(subs); | |
| mqttClient.endMessage(); | |
| Serial.print("Subscribing to topic: "); | |
| Serial.println(lampControlTopic); | |
| mqttClient.subscribe(lampControlTopic); | |
| mqttClient.onMessage(onMqttMessage); | |
| pinMode(relayPin, OUTPUT); | |
| digitalWrite(relayPin, LOW); // Start with relay OFF | |
| // Code for Relay ends | |
| } | |
| void onMqttMessage(int messageSize) { | |
| String message = ""; | |
| // Read the incoming message | |
| while (mqttClient.available()) { | |
| message += (char)mqttClient.read(); | |
| } | |
| Serial.print("Received message on topic: "); | |
| Serial.println(lampControlTopic); | |
| Serial.print("Message: "); | |
| Serial.println(message); | |
| JSONVar myObject = JSON.parse(message); | |
| Serial.print(myObject["msg"]); | |
| String message2 = myObject["msg"]; | |
| // Serial.println(JSON.typeof(myObject)); // prints: object | |
| lampState = !lampState; | |
| digitalWrite(relayPin, lampState ? HIGH : LOW); | |
| Serial.println(lampState ? "π‘ Lamp toggled ON" : "π Lamp toggled OFF"); | |
| // Control lamp based on message content | |
| if (message2 == "ON" || message2 == "on" || message2 == "1") { | |
| digitalWrite(relayPin, HIGH); | |
| lampState = true; | |
| Serial.println("π‘ Lamp turned ON"); | |
| } else if (message2 == "OFF" || message2 == "off" || message2 == "0") { | |
| digitalWrite(relayPin, LOW); | |
| lampState = false; | |
| Serial.println("π Lamp turned OFF"); | |
| } else if (message2 == "true" || message2 == "True") { | |
| lampState = !lampState; | |
| digitalWrite(relayPin, lampState ? HIGH : LOW); | |
| Serial.println(lampState ? "π‘ Lamp toggled ON" : "π Lamp toggled OFF"); | |
| } else { | |
| Serial.println("β οΈ Unknown command. Use: ON, OFF, or TOGGLE"); | |
| } | |
| } | |
| void loop() { | |
| // call poll() regularly to allow the library to send MQTT keep alive which | |
| // avoids being disconnected by the broker | |
| mqttClient.poll(); | |
| unsigned long currentMillis = millis(); | |
| if (currentMillis - previousMillis >= interval) { | |
| // save the last time a message was sent | |
| previousMillis = currentMillis; | |
| //record random value from A0, A1 and A2 | |
| int Rvalue = analogRead(A0); | |
| int Rvalue2 = analogRead(A1); | |
| int Rvalue3 = analogRead(A2); | |
| Serial.print("Sending message to topic: "); | |
| Serial.println(topic); | |
| Serial.println(Rvalue); | |
| Serial.print("Sending message to topic: "); | |
| Serial.println(topic2); | |
| Serial.println(Rvalue2); | |
| Serial.print("Sending message to topic: "); | |
| Serial.println(topic3); | |
| Serial.println(Rvalue3); | |
| // send message, the Print interface can be used to set the message contents | |
| mqttClient.beginMessage(topic); | |
| mqttClient.print(Rvalue); | |
| mqttClient.endMessage(); | |
| mqttClient.beginMessage(topic2); | |
| mqttClient.print(Rvalue2); | |
| mqttClient.endMessage(); | |
| mqttClient.beginMessage(topic3); | |
| mqttClient.print(Rvalue3); | |
| mqttClient.endMessage(); | |
| Serial.println(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment