Skip to content

Commit

Permalink
Create ArduinoMqttClientLamp.ino
Browse files Browse the repository at this point in the history
  • Loading branch information
tigoe committed Nov 8, 2024
1 parent 3e25a19 commit 856c6a1
Showing 1 changed file with 163 additions and 0 deletions.
163 changes: 163 additions & 0 deletions arduino-clients/ArduinoMqttClientLamp/ArduinoMqttClientLamp.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
MQTT Client Lamp sender/receiver
This sketch demonstrates an MQTT client that connects to a broker, subscribes to a topic,
and both listens for messages on that topic and sends messages to it, a random number between 0 and 255.
When the client receives a message, it parses it, and PWMs an LED on pin 6.
This sketch uses https://public.cloud.shiftr.io as the MQTT broker, but others will work as well.
See https://tigoe.github.io/mqtt-examples/#broker-client-settings for connection details.
Libraries used:
* http://librarymanager/All#WiFiNINA or
* http://librarymanager/All#WiFi101
* http://librarymanager/All#WiFiS3
* http://librarymanager/All#ArduinoMqttClient
the arduino_secrets.h file:
#define SECRET_SSID "" // network name
#define SECRET_PASS "" // network password
#define SECRET_MQTT_USER "public" // broker username
#define SECRET_MQTT_PASS "public" // broker password
created 11 June 2020
updated 7 Nov 2024
by Tom Igoe
*/

#include <WiFiNINA.h> // use this for Nano 33 IoT, MKR1010, Uno WiFi
// #include <WiFi101.h> // use this for MKR1000
// #include <WiFiS3.h> // use this for Uno R4 WiFi
// #include <ESP8266WiFi.h> // use this for ESP8266-based boards
#include <ArduinoMqttClient.h>
#include "arduino_secrets.h"

// initialize WiFi connection as SSL:
WiFiClient wifi;
MqttClient mqttClient(wifi);

// details for MQTT client:
char broker[] = "yoursite.com";
int port = 1883;
char topic[] = "lamp/brightness";
String clientID = "lightClient-";

// last time the client sent a message, in ms:
long lastSensorReading = 0;
int threshold = 4;

void setup() {
// initialize serial:
Serial.begin(9600);
// wait for serial monitor to open:
if (!Serial) delay(3000);
pinMode(6, OUTPUT);
// connect to WiFi:
connectToNetwork();
// make the clientID unique by adding the last three digits of the MAC address:
byte mac[6];
WiFi.macAddress(mac);
for (int i = 0; i < 3; i++) {
clientID += String(mac[i], HEX);
}
// set the credentials for the MQTT client:
mqttClient.setId(clientID);
// if needed, login to the broker with a username and password:
mqttClient.setUsernamePassword(SECRET_MQTT_USER, SECRET_MQTT_PASS);
}

void loop() {
// if you disconnected from the network, reconnect:
if (WiFi.status() != WL_CONNECTED) {
connectToNetwork();
// skip the rest of the loop until you are connected:
return;
}

// if not connected to the broker, try to connect:
if (!mqttClient.connected()) {
Serial.println("attempting to connect to broker");
connectToBroker();
}
// poll for new messages from the broker:
mqttClient.poll();
// get an analog reading, divide by 4 to set range to 0-255:

int sensorReading = analogRead(A0) / 4;

// once every interval, send a message:
if (abs(sensorReading - lastSensorReading) > threshold) {
if (mqttClient.connected()) {
// start a new message on the topic:
mqttClient.beginMessage("lamp/knob");
// print the body of the message:
mqttClient.print(sensorReading);
// send the message:
mqttClient.endMessage();
// send a serial notification:
Serial.print("published a message: ");
Serial.println(sensorReading);
// save reading for next time:
lastSensorReading = sensorReading;
}
}
}

boolean connectToBroker() {
// if the MQTT client is not connected:
if (!mqttClient.connect(broker, port)) {
// print out the error message:
Serial.print("MOTT connection failed. Error no: ");
Serial.println(mqttClient.connectError());
// return that you're not connected:
return false;
}

// set the message receive callback:
mqttClient.onMessage(onMqttMessage);
// subscribe to a topic:
Serial.print("Subscribing to topic: ");
Serial.println(topic);
mqttClient.subscribe(topic);

// once you're connected, you
// return that you're connected:
return true;
}

void onMqttMessage(int messageSize) {
// we received a message, print out the topic and contents
Serial.println("Received a message with topic ");
Serial.print(mqttClient.messageTopic());
Serial.print(", length ");
Serial.print(messageSize);
Serial.println(" bytes:");
String incoming = "";
// use the Stream interface to print the contents
while (mqttClient.available()) {
incoming += (char)mqttClient.read();
}
// convert the incoming string to an int so you can use it:
int result = incoming.toInt();
// use the result to dim the builtin LED:
if (result > 0) {
analogWrite(6, result);
}
// print the result:
Serial.println(result);
delay(100);
}

void connectToNetwork() {
// try to connect to the network:
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Attempting to connect to: " + String(SECRET_SSID));
//Connect to WPA / WPA2 network:
WiFi.begin(SECRET_SSID, SECRET_PASS);
delay(2000);
}

// print IP address once connected:
Serial.print("Connected. My IP address: ");
Serial.println(WiFi.localIP());
}

0 comments on commit 856c6a1

Please sign in to comment.