{"id":3725,"date":"2017-09-24T21:56:03","date_gmt":"2017-09-24T17:56:03","guid":{"rendered":"https:\/\/nayarweb.com\/blog\/?p=3725"},"modified":"2017-09-24T22:05:31","modified_gmt":"2017-09-24T18:05:31","slug":"rfid-and-lora-on-arduino","status":"publish","type":"post","link":"https:\/\/nayarweb.com\/blog\/2017\/rfid-and-lora-on-arduino\/","title":{"rendered":"RFID and LoRa on Arduino"},"content":{"rendered":"<p>Last time I got a <a href=\"https:\/\/nayarweb.com\/blog\/2017\/hands-on-with-lora-network-iot\/\"><span style=\"color: #ff9900;\">LoRa Server and LoRa Client to communicate to each other<\/span><\/a>. It was about time to give them something to talk about i.e. start exchanging sensor data rather than just &#8220;Hello World&#8221;.<\/p>\n<p><strong>RFID tags<\/strong><\/p>\n<p>I plugged in my RFID reader which I bought like a years ago. My Arduino UNO was getting scrambled data on its serial. I tried connecting the RFID reader alone. The Arduino detected the device but whenever I&#8217;d place an RF tag on it, no data would be read.<\/p>\n<figure id=\"attachment_3731\" aria-describedby=\"caption-attachment-3731\" style=\"width: 229px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3731 size-medium\" src=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/09\/wp-1506269375838179989099-e1506270205595-229x300.jpg\" alt=\"\" width=\"229\" height=\"300\" srcset=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/09\/wp-1506269375838179989099-e1506270205595-229x300.jpg 229w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/09\/wp-1506269375838179989099-e1506270205595-768x1007.jpg 768w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/09\/wp-1506269375838179989099-e1506270205595-781x1024.jpg 781w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/09\/wp-1506269375838179989099-e1506270205595-1272x1668.jpg 1272w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/09\/wp-1506269375838179989099-e1506270205595.jpg 1710w\" sizes=\"auto, (max-width: 229px) 100vw, 229px\" \/><figcaption id=\"caption-attachment-3731\" class=\"wp-caption-text\">RFID-RC522<\/figcaption><\/figure>\n<p>The forums suggested to solder the pins on the board for better connection but it didn&#8217;t work in my case. Most probably my device was faulty.<\/p>\n<p>Fortunately my friend Damien from <a href=\"https:\/\/nayarweb.com\/dmelec\"><span style=\"color: #ff9900;\">DM Electronics<\/span><\/a>\u00a0had the device is his stock which he sold to me. I&#8217;d highly recommend you to buy your electronic devices from there if you&#8217;re in Mauritius. He&#8217;s an awesome guy who has a deep understanding about how the devices actually operate on the inside.<\/p>\n<p>https:\/\/www.youtube.com\/watch?v=23aMjljCLZI<\/p>\n<p>I followed the tutorial above with Damien&#8217;s RC522 and it worked. But this is only half of my problems. I still need to put the LoRa shield back on.<\/p>\n<p><strong>Making LoRa and RFID reader work together<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3728\" src=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/09\/IMG_20170923_221414-1024x576.jpg\" alt=\"\" width=\"640\" height=\"360\" srcset=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/09\/IMG_20170923_221414-1024x576.jpg 1024w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/09\/IMG_20170923_221414-300x169.jpg 300w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/09\/IMG_20170923_221414-768x432.jpg 768w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2017\/09\/IMG_20170923_221414-1272x716.jpg 1272w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>The LoRa shield and RC5222 use\u00a0<em>Serial Peripheral Interface (SPI)<\/em> to communicate with the Arduino. SPI was new to me since I was familiar with 3 pin sensors (Ground, 3.3V and Data). SPI consists of:<\/p>\n<ol>\n<li>3.3V: Provides power. Easy<\/li>\n<li>Ground: Well, ground<\/li>\n<li>RST: Reset: Still no idea what it does. Can you tell me in the comments?<\/li>\n<li>SS: Slave Select: It sends a signal when the device shall listen and transmit data.<\/li>\n<li>MOSI: Master Out Slave In: The line which is used to receive data from the Arduino<\/li>\n<li>MISO: Master In Slave Out: The line which is used to send data to the Arduino.<\/li>\n<\/ol>\n<p>Since the SS line exists, it should be possible to mute the RFID reader while LoRa is transmitting\/receiving data. The\u00a0MFRC522 library allows the user to set a custom SS and RST pin.<\/p>\n<blockquote>\n<pre>#define RST_PIN 6\r\n#define SS_PIN 7\r\n\r\nMFRC522 mfrc522(SS_PIN, RST_PIN);<\/pre>\n<\/blockquote>\n<p>The default 9,10 pins will be used by LoRa and 6,7 pins will be used by RFID Reader. I tried manually setting turning of LoRa shield by setting the SS pin high without success<\/p>\n<blockquote>\n<pre>pinMode(10, OUTPUT);      \/\/ sets the digital pin as output\r\ndigitalWrite(10, HIGH);<\/pre>\n<\/blockquote>\n<p>Reading the source code of the SPI library (&#8220;SPI.h&#8221;), I came across the method `end()`. The RH_RF95 library also has a `sleep()` method. My final code looks like this:<\/p>\n<pre>void setup() {\r\n  Serial.begin(9600); \r\n}\r\n\r\nvoid loop() {\r\n  readCard();\r\n  delay(5000);\r\n}\r\n\r\nvoid readCard() {\r\n  SPI.begin();\r\n  mfrc522.PCD_Init();  \r\n  ...\r\n  SPI.end();\r\n  lora(datafromcard);\r\n}\r\n\r\nvoid lora(String data) {\r\n  rf95.init()\r\n  ...\r\n  rf95.send(data.c_str(), data.length());\r\n  ...\r\n  rf95.sleep();\r\n}\r\n<\/pre>\n<p>And there you go. LoRa and RFID working together.<\/p>\n<p><iframe loading=\"lazy\" title=\"Arduino RFID with LoRa IoT\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/mAKe8H0Cmck?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time I got a LoRa Server and LoRa Client to communicate to each other. It was about time to give them something to talk about i.e. start exchanging sensor data rather than just &#8220;Hello World&#8221;. RFID tags I plugged in my RFID reader which I bought like a years ago. My Arduino UNO was &hellip; <a href=\"https:\/\/nayarweb.com\/blog\/2017\/rfid-and-lora-on-arduino\/\" class=\"continue-reading\">Continue reading <span class=\"screen-reader-text\">RFID and LoRa on Arduino<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[210],"tags":[234,232],"class_list":["post-3725","post","type-post","status-publish","format-standard","hentry","category-technology","tag-arduino","tag-internet-of-things-iot"],"_links":{"self":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/3725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/comments?post=3725"}],"version-history":[{"count":16,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/3725\/revisions"}],"predecessor-version":[{"id":3745,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/3725\/revisions\/3745"}],"wp:attachment":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/media?parent=3725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/categories?post=3725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/tags?post=3725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}