I am working with inductive proximity sensor and bolt locks.
In the void loop, I have setup the code in a way that when the proximity sensor detects the metal, bolt lock is on(locked) and when no metal its off(unlocked).
Also, I want to control my bolt locks from mqtt. I have setup a json for that and when i send commands from my laptop it changes the state for boltlock for a second and goes back to loop where proximity sensor conditions are written.
Is there a way to hold the bolt lock state until proximity sensor status is changed?
Here is my code.
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <string.h>
#include <ArduinoJson.h>
// proxy sensor code
int proxy0 = 21;
int state0 = LOW;
// proxy sensor code end
//Bolt lock code
int lock1=7;
int lock2=27;
//Bolt lock code end
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 61);
const char* server = "192.168.1.3";
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
StaticJsonDocument<258> pub_json;
StaticJsonDocument<500> sub_json;
void callback(char* topic, byte* payload, unsigned int length) {
//strlcpy(name, doc["name"] | "default", sizeof(name));
Serial.println("Enter callback");
Serial.println(length);
deserializeJson(sub_json, payload, length);
int sub_array = sub_json["status"].size();
String extinsion_id = sub_json["extinsion"];
String door_state=String(sub_json["status"][0]["door"]);
String lock_state=String(sub_json["status"][0]["lock"]);
// for controlling bolt locks from mqtt
int p =(char)payload[0]-'0';
if(p==0)
{
digitalWrite(lock1,LOW);
digitalWrite(lock2,LOW);
Serial.println("Lock is closed ");
delay(5000);
}
if(p==1)
{
digitalWrite(lock1,HIGH);
digitalWrite(lock2,HIGH);
Serial.println("Lock is open");
delay(5000);
}
Serial.println();
Serial.print("extinsion number");Serial.println(extinsion_id);
Serial.print("door_state");Serial.println(door_state);
Serial.print("lock_state");Serial.println(lock_state);
}
void setup() {
// Open serial communications and wait for port to open:
Ethernet.init(17);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Ethernet.begin(mac, ip);
// Allow the hardware to sort itself out
delay(1500);
mqttClient.setServer("192.168.1.3", 1883);
mqttClient.setCallback(callback);
if (mqttClient.connect("arduino-1")) {
// connection succeeded
Serial.println("Connected ");
boolean r = mqttClient.subscribe("/bob/status");
Serial.println("subscribe ");
Serial.println(r);
}
else {
Serial.println("Connection failed ");
}
// for bolt locks
pinMode(lock1,OUTPUT);
pinMode(lock2, OUTPUT);
}
void loop() {
int val = digitalRead(proxy0);
if (val != state0) {
state0 = val;
}
if ( state0 == LOW ) {
char bufferout[128];
int b =serializeJson(pub_json, bufferout);
JsonArray array = pub_json.to<JsonArray>();
pub_json.add("Not detected");
digitalWrite(lock1,HIGH);
digitalWrite(lock2,HIGH);
pub_json.add("Lock");
boolean rc = mqttClient.publish("BOB", bufferout);
Serial.println(bufferout);
}
else {
char bufferout[128];
int b =serializeJson(pub_json, bufferout);
JsonArray array = pub_json.to<JsonArray>();
pub_json.add("Detected");
digitalWrite(lock1,LOW);
digitalWrite(lock2,LOW);
pub_json.add("Lock");
boolean rc = mqttClient.publish("BOB" , bufferout);
Serial.println(bufferout);
}
delay(1000);
mqttClient.loop();
delay(1000);
}