Rune
(Rune Kalvik Berg)
April 21, 2018, 3:01pm
35
Sounds correct, but without the RFID part. Just the random letters and numbers.
vpomax
(Vpomax)
April 21, 2018, 5:25pm
36
WOW!!! it works fine!!!
it was more easy than I thinked
Thank you Rune for support… you have been very helpful and patient
I will use it in alternative to device_tracker becouse sometimes device_tracker do not recognize smartphone when we return at home, so I need a fast way to disarm it
2 Likes
mistralst
(mistralst)
June 20, 2018, 9:09pm
37
Thanks for the great contribution to the original post, it’s more than great!
I have this now setup, but have 2 questions:
The blue light is working for me (the one that turns ON when it reads a tag). I know you disabled the red&green leds on purpose, but could you describe exactly why? And how could I enable them again by using your code?
I don’t understand the part on your code that says “code: youralarmpassword”. What is exactly that alarm for? In my setup I just have the following in configuration.yaml and seems to be more than enough:
alarm_control_panel:
platform: manual
name: Home Alarm
pending_time: 0
trigger_time: 1 # Workaround to prevent being in the triggered state
Thanks a lot!
Rune
(Rune Kalvik Berg)
June 21, 2018, 2:32pm
38
Hi. All the LEDs should be working with the sketch, but without the blinking. I use Manual Alarm Panel with a password (optional), since you dont have password you dont need that line (Manual Alarm Panel ).
mistralst
(mistralst)
June 21, 2018, 7:19pm
39
Rune:
sketch
Hum… strange only the blue one is working with me.
Anyway, what sketch are you refering to?
Thanks.
Rune
(Rune Kalvik Berg)
June 21, 2018, 7:39pm
40
Hi
As you can see below in the sketch I have assigned D0 , D1 and D2 to the LEDs.
Rune:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include "MFRC522.h"
/* wiring the MFRC522 to ESP8266 (ESP-12)
RST = GPIO4
SDA(SS) = GPIO2
MOSI = GPIO13
MISO = GPIO12
SCK = GPIO14
GND = GND
3.3V = 3.3V
RGB LED Setup
Red LED = D1
Ground = GND
Gr. LED = D2
Blue LED = D0
*/
#define RST_PIN D3 // RST-PIN GPIO4
#define SS_PIN D8 // SDA-PIN GPIO2
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
// Wifi Connection details
const char* ssid = "xxxxx";
const char* password = "xxxxxx";
const char* mqtt_server = "xxxxxxx";
const char* mqtt_user = "xxxxxxx";
const char* mqtt_password = "xxxxxxxx";
const char* clientID = "RFID";
const char* rfid_topic = "home/rfid";
const int redPin = D1;
const int greenPin = D2;
const int idPin = D0;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
void setup() {
Serial.begin(115200);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
pinMode(redPin, OUTPUT); // Red LED
pinMode(redPin, HIGH);
pinMode(greenPin, OUTPUT); // Green :LED
pinMode(greenPin, HIGH);
pinMode(idPin, OUTPUT); // ID :LED
pinMode(idPin, HIGH);
delay(2);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
// Connect to Wifi
void setup_wifi() {
//Turn off Access Point
WiFi.mode(WIFI_STA);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
// Check for incoming messages
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("");
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
char s[20];
sprintf(s, "%s", payload);
if ( (strcmp(topic,"home/alarm")==0))
{
payload[length] = '\0';
String sp = String((char*)payload);
// Alarm is off
if (sp == "disarmed")
{
Serial.println();
Serial.print("Alarm is set to Disarmed");
Serial.println();
off_both_led();
}
// Alarm is Armed Home
else if (sp == "armed_home")
{
Serial.println();
Serial.print("Alarm is set to Armed Home");
Serial.println();
on_green_led();
}
// Alarm is arming
else if (sp == "pending")
{
Serial.println();
Serial.print("Alarm set to Pending");
Serial.println();
on_both_led();
}
// Alarm is Armed Away
else if (sp == "armed_away")
{
Serial.println();
Serial.print("Alarm set to Armed Away");
Serial.println();
on_green_led();
}
// Alarm is Triggered
else if (sp == "triggered")
{
Serial.println();
Serial.print("Alarm is triggered!!");
Serial.println();
on_red_led();
}
}}
/* interpret the ascii digits in[0] and in[1] as hex
* notation and convert to an integer 0..255.
*/
int hex8(byte *in)
{
char c, h;
c = (char)in[0];
if (c <= '9' && c >= '0') { c -= '0'; }
else if (c <= 'f' && c >= 'a') { c -= ('a' - 0x0a); }
else if (c <= 'F' && c >= 'A') { c -= ('A' - 0x0a); }
else return(-1);
h = c;
c = (char)in[1];
if (c <= '9' && c >= '0') { c -= '0'; }
else if (c <= 'f' && c >= 'a') { c -= ('a' - 0x0a); }
else if (c <= 'F' && c >= 'A') { c -= ('A' - 0x0a); }
else return(-1);
return ( h<<4 | c);
}
// Reconnect to wifi if connection lost
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientID, mqtt_user, mqtt_password)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("home/rfid", "connected");
// ... and resubscribe
client.subscribe("home/alarm");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// Main functions
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
delay(50);
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
delay(50);
return;
}
// Show some details of the PICC (that is: the tag/card)
Serial.println("");
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
digitalWrite(idPin, HIGH);
delay(500);
digitalWrite(idPin, LOW);
delay(500);
// Send data to MQTT
String rfidUid = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
rfidUid += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
rfidUid += String(mfrc522.uid.uidByte[i], HEX);
}
const char* id = rfidUid.c_str();
client.publish("home/rfid", id);
delay(3000);
}
// LED Loop
void off_both_led(){
Serial.println("Turning off all led");
digitalWrite(greenPin, LOW);
digitalWrite(redPin, LOW);
}
void flash_green_led(){
Serial.println("Flashing Green LED");
digitalWrite(greenPin, LOW);
// Flash 15 times:
for(int i = 0; i < 14; i++)
{
digitalWrite(greenPin, HIGH);
delay(1000);
digitalWrite(greenPin, LOW);
delay(500);
}}
void flash_red_led(){
Serial.println("Flashing Red LED");
digitalWrite(redPin, LOW);
// Flash 20 times:
for(int i = 0; i < 19; i++)
{
digitalWrite(redPin, HIGH);
delay(1000);
digitalWrite(redPin, LOW);
delay(500);
}}
void on_green_led(){
Serial.println("Turning on green led");
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
}
void on_red_led(){
Serial.println("Turning on red led");
digitalWrite(greenPin, LOW);
digitalWrite(redPin, HIGH);
}
void on_both_led(){
Serial.println("Turning on both led");
digitalWrite(greenPin, HIGH);
digitalWrite(redPin, HIGH);
}
// Helper routine to dump a byte array as hex values to Serial
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
mistralst
(mistralst)
June 26, 2018, 6:40pm
41
It’s possible I have to include some resistors on the circuit?
Rune
(Rune Kalvik Berg)
June 26, 2018, 7:00pm
42
The LEDs are changed upon new states from the alarm via Mqtt and if a RFID signal is triggered. Are you sure that the esp receives any state change from HA?
mistralst
(mistralst)
June 27, 2018, 9:33pm
43
I think the problem is just what you said: MQTT not publishing or ESP not receiving the states.
On HA logbook/diary seems fine:
I just don’t understand what does this do:
service: logbook.log
data_template:
name: >-
{{ MQTT }}
message: RFID 1 -> Alarm off!
How can I check if MQTT to ESP is being send?
Rune
(Rune Kalvik Berg)
June 27, 2018, 9:53pm
44
Hi. If you have not given the alarm any name (which is optional) it should be ha_alarm. In your /dev-state you should find a entity like: alarm_control_panel.ha_alarm. This is your entity to put in the automation.
mistralst
(mistralst)
June 27, 2018, 9:57pm
45
Rune:
ha_alarm
Sorry, I just edited my previous post while you were replying. I understood and fine my alarm, it’s called:
alarm_control_panel.home_alarm
Can you kindly check my previous post after I edited?
mistralst
(mistralst)
June 27, 2018, 10:03pm
46
I think I found the issue… ESP is waiting for state change on MQTT: “home/alarm”, but in my HA config I have nothing about “home/alarm”, all I have is: “home/rfid”.
What config and where should I add regarding MQTT “home/alarm”?
Rune
(Rune Kalvik Berg)
June 27, 2018, 10:17pm
47
Hi, yes you are lacking the MQTT part of the alarm, so now it is just the manual alarm without MQTT.
It should be something like:
alarm_control_panel:
- platform: manual_mqtt
state_topic: home/alarm
command_topic: home/alarm/set
mistralst
(mistralst)
June 29, 2018, 5:51am
48
Thanks @Rune , that totally fixed the issue.
1 Like
mistralst
(mistralst)
August 10, 2018, 9:24am
50
Hi Rune,
I just realized, that after some time running, the device stops reading RFID tags.
While the node MCU keeps pinging (you can ping the IP address of the device and it is up), if you approach a RFID tag to the reader it does nothing (that is: no blue light which means is not reading/processing the NFC). But the green light is still ON, which means the device is “kind of working”.
Any thoughts? Thanks a lot.
Rune
(Rune Kalvik Berg)
August 10, 2018, 9:34am
51
Hi, Does the light still change based on alarm status changes?
mistralst
(mistralst)
August 10, 2018, 9:56am
52
Yes. If I change the alarm status from Home Assistant panel the light still change.
Can you confirm if you had any issues like this? And what exact model of Node MCU are you using? I have some Weemos to try with.
1 Like
Rune
(Rune Kalvik Berg)
August 10, 2018, 10:05am
53
Hi, sounds like the RFID sensor have freezed on a error. I have not been running mine for more then a couple of weeks before removing it again (since I got my device tracker running as it should). I will connect mine again and see whats happens. I use these boards NodeMCU , but I have also ordered some Wemos to try them out. I will connect for a week and let you know. I also put the code on github in case someone else needs it Github
1 Like
mistralst
(mistralst)
August 10, 2018, 10:11am
54
That’s great Rune, please let me know if you are able to reproduce the issue after some time.
P.S.: I’ll check the code you have in your GitHub to make sure is the same as mine.
1 Like
Rune
(Rune Kalvik Berg)
August 20, 2018, 5:34am
55
Hi, its still going strong. No issue yet
EDIT: 2 weeks now and it is still working fine, since I dont use it anymore I will now turn it off.
1 Like