Un autre exemple avec du mqtt in et du mqtt out…
C’est avec de l’ethernet filaire. dans les déclarations au début sont à changer
Là, l’interface c’est un clavier à code (wiegand) et un détecture de mouvement mais, la logique de la partie pub/sub est la même:
#include <SPI.h>
#include <Ethernet.h>
#include <Wiegand.h>
#define SERRURE 4 /* PIN sur lequel est connecté le relai */
#define LUMIERE 9 /* PIN sur lequel la lumière du clavier est connectée */
#define pirPIN 6
int pirState = LOW;
int pirVal = LOW;
String wgPINCODE ="";
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x0E, 0xBE, 0xFE, 0x32, 0x12, 0x99 };
#include <PubSubClient.h>
long stayon = 500;
void callback(char* topic, byte* payload, unsigned int length);
const char* mqtt_server = "192.168.x.t";
const char* mqtt_input = "porte/input";
const char* mqtt_output = "porte/output";
IPAddress ipEth(192, 168, x, y);
IPAddress ipDNS(192, 168, x, z);
IPAddress subnet(255, 255, 255, 0);
void callback(char* topic, byte* payload, unsigned int length);
WIEGAND wg;
EthernetClient ethClient;
PubSubClient client(mqtt_server, 1883, callback, ethClient);
long lastMsg = 0;
char message_buff[100];
void setup() {
Serial.begin(115200);
setup_eth();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
wg.begin(2,3); /* vert et blanc sur sont 2 et 3 */
pinMode(SERRURE, OUTPUT);
pinMode(LUMIERE, OUTPUT);
pinMode(pirPIN, INPUT);
digitalWrite(SERRURE, LOW);
digitalWrite(LUMIERE, HIGH);
}
void setup_eth() {
/*
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
*/
Ethernet.begin(mac, ipEth, ipDNS);
Serial.println(Ethernet.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0';
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
String message((char*)payload);
if (message == "open") {
digitalWrite(SERRURE, HIGH);
digitalWrite(LUMIERE, LOW);
delay(stayon);
digitalWrite(SERRURE, LOW);
digitalWrite(LUMIERE, HIGH);
}
if (message == "confirm") {
digitalWrite(LUMIERE, LOW);
delay(stayon);
digitalWrite(LUMIERE, HIGH);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("GarageWiegand")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.subscribe(mqtt_input);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
pirVal = digitalRead(pirPIN);
if (pirVal == HIGH) { // check if the input is HIGH
// Serial.println("pirVal High!");
delay(150);
if (pirState == LOW) {
Serial.println("Motion detected!");
client.publish(mqtt_output,"moveon");
// We only want to print on the output change, not state
pirState = HIGH;
}
}
else {
// Serial.println("pirVal low!");
if (pirState == HIGH) {
Serial.println("Motion stopped!");
client.publish(mqtt_output,"moveoff");
pirState = LOW;
}
}
if(wg.available()) {
if ( wg.getWiegandType() == 8 || wg.getWiegandType() == 4 )
{
unsigned long wgCODE = wg.getCode();
if (wgCODE == 13 )
{
char wgCodeChar[16];
// sprintf(wgCodeChar, "%s", wgPINCODE);
wgPINCODE.toCharArray(wgCodeChar, 16);
client.publish(mqtt_output,wgCodeChar);
Serial.println(wgPINCODE);
Serial.println(wgCodeChar);
wgPINCODE = "";
}
else if ( wgCODE == 27 )
{
wgPINCODE = "";
}
else
{
wgPINCODE+= String(wgCODE);
if (wgPINCODE.length() > 15 )
wgPINCODE = "";
}
}
else
{
unsigned long wgCode = wg.getCode();
char wgCodeChar[16];
sprintf(wgCodeChar,"%lu",wgCode);
Serial.println(wgCodeChar);
Serial.println(wg.getCode());
client.publish(mqtt_output,wgCodeChar);
}
}
client.loop();
}