I'm pretty new to esp32 and I'm trying to combine two codes in one, they are simple codes from projects that I found online, the first one uses PIR sensor to send message to telegram each time motion is detected, second code uses RFID to open door lock. When I tried directly merging the two codes together the esp32 went crazy on serial monitor and nothing worked even the WIFI failed to connect.
Then I tried creating task for each one using freeRTOS and I did it like this:
task1: for Wifi connection running on CORE 0 //wifi function is taken from sketch 1
task2: for RFID and door lock running on CORE 1 //taken from sketch 2
task3: for motion detection and sending telegram message running on CORE 0 //taken from sketch 1
Again esp32 went crazy when I opened serial monitor and it gave me this error:" Guru Meditation Error: Core 1 panic'ed (IllegalInstruction). Exception was unhandled. "
I have no clue why is this happening, does it really require RTOS to combine the two sketches and run them at the same time, or a simple delays and if statements would solve the problem? I need some help please.
Sketch 1: Motion detection from enter link description here
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/telegram-esp32-motion-detection-arduino/
Project created using Brian Lough's Universal Telegram Bot Library: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
*/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// Initialize Telegram BOT
#define BOTtoken "XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "XXXXXXXXXX"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
const int motionSensor = 27; // PIR Motion Sensor
bool motionDetected = false;
// Indicates when motion is detected
void IRAM_ATTR detectsMovement() {
//Serial.println("MOTION DETECTED!!!");
motionDetected = true;
}
void setup() {
Serial.begin(115200);
// PIR Motion Sensor mode INPUT_PULLUP
pinMode(motionSensor, INPUT_PULLUP);
// Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
bot.sendMessage(CHAT_ID, "Bot started up", "");
}
void loop() {
if(motionDetected){
bot.sendMessage(CHAT_ID, "Motion detected!!", "");
Serial.println("Motion Detected");
motionDetected = false;
}
}
Sketch 2: RFID door lock from enter link description here
/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-rfid-nfc-door-lock-system
*/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 5 // ESP32 pin GIOP5
#define RST_PIN 27 // ESP32 pin GIOP27
#define RELAY_PIN 22 // ESP32 pin GIOP22 connects to relay
MFRC522 rfid(SS_PIN, RST_PIN);
byte keyTagUID[4] = {0xFF, 0xFF, 0xFF, 0xFF};
void setup() {
Serial.begin(9600);
SPI.begin(); // init SPI bus
rfid.PCD_Init(); // init MFRC522
pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output.
digitalWrite(RELAY_PIN, HIGH); // lock the door
Serial.println("Tap an RFID/NFC tag on the RFID-RC522 reader");
}
void loop() {
if (rfid.PICC_IsNewCardPresent()) { // new tag is available
if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
if (rfid.uid.uidByte[0] == keyTagUID[0] &&
rfid.uid.uidByte[1] == keyTagUID[1] &&
rfid.uid.uidByte[2] == keyTagUID[2] &&
rfid.uid.uidByte[3] == keyTagUID[3] ) {
Serial.println("Access is granted");
digitalWrite(RELAY_PIN, LOW); // unlock the door for 2 seconds
delay(2000);
digitalWrite(RELAY_PIN, HIGH); // lock the door
}
else
{
Serial.print("Access denied, UID:");
for (int i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();
}
rfid.PICC_HaltA(); // halt PICC
rfid.PCD_StopCrypto1(); // stop encryption on PCD
}
}
}
1 Answer 1
No, you don't need an RTOS to do something simple like:
if (motionDetected)
open_the_door ();
You basically have two loops:
void loop ()
{
if (motionDetected)
send_a_message_to_open_the_door ();
}
And:
void loop ()
{
if (message_received_to_open_the_door)
open_the_door();
}
So, you combine them into a single loop:
void loop ()
{
if (motionDetected)
open_the_door();
}
I'll leave you to make the actual code changes.