Weird behavior when using NodeMCU V3 with RF522 - arduino

I'm attempting to use the RF522 with my NodeMCU v3 using the SPI interface. I was able to hook the RF522 to my Pi Zero and get it to scan both the chip that came with it as well as the NFC of my phone so I'm fairly confident the hardware is good.
Here's what I've found so far (connections are documented in code below):
If I hook everything up, I cannot upload my Arduino sketch
(espcomm_upload_mem failed) and I cannot run a script that is on the
NodeMCU.
If I disconnect the 3v power to the RF522, I can upload the script and run it.
If I leave the 3v power disconnect, the program fails the self test: mfrc522.PCD_PerformSelfTest()
If I plug the 3v power line in after the NodeMCU boots but before the self test, it passes the self test and runs!
I never find a card (repeats "No new card..." forever). Tried existing chip and phone but nothing was found.
I'm not sure why having the power installed keeps everything else from starting and I have no idea why I can't read a card when the self-test passes! Does anyone else have experience getting the RF522 and NodeMCU to work together?
Code:
#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <PubSubClient.h>
#include <SPI.h>
#include <MFRC522.h>
/*
*RST GPIO15 -- D8
*SDA(SS) GPIO2 -- D4
*MOSI GPIO13 -- D7
*MISO GPIO12 -- D6
*SCK GPIO14 -- D5
*GND GND
*3,3V 3,3V
*/
#define SS_PIN 2 // D4 -- SDA-PIN for RC522
#define RST_PIN 15 // D8 -- RST-PIN for RC522
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
WiFiClient espClient;
PubSubClient mqtt(espClient);
const char* MQTT_user = "rfid.local";
const char* MQTT_server = "mqtt.local";
const char* topic_base = "home/rfid/";
void setup() {
Serial.begin(115200);
Serial.println("Lets get started!!");
WiFiManager wifiManager;
wifiManager.setTimeout(180);
if(!wifiManager.autoConnect("RFID", "RFID")) {
Serial.println("Failed to connect and hit timeout");
delay(3000);
ESP.reset();
delay(5000);
}
Serial.println("Connected...yippie!");
mqtt.setServer(MQTT_server, 1883);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
if (mfrc522.PCD_PerformSelfTest())
Serial.println("RF522 Passed self test!");
else {
Serial.println("RF522 Failed self test!");
delay(3000);
ESP.reset();
delay(5000);
}
Serial.println("Waiting for someone to scan...");
}
void reconnectMQTT() {
// Loop until we're reconnected
while (!mqtt.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqtt.connect(MQTT_user, MQTT_user, MQTT_user)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(mqtt.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
// Make sure we are still connected!
if (!mqtt.connected()) {
reconnectMQTT();
}
mqtt.loop();
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
Serial.println("No new card...");
delay(1000);
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
Serial.println("Can't read card...");
delay(1000);
return;
}
// Dump debug info about the card. PICC_HaltA() is automatically called.
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
//mqtt.publish(topic_base...);
}

The problem was that I needed to call mfrc522.PCD_Init(); again after performing the self test and version dump. Once I did this, everything worked as expected!

Related

Arduino ESP32-Wroom-32D to XBee Via BlueTooth

I have a few questions as to why my input isn't behaving correctly. I have 2 functioning scripts I've used:
ESP32 to Xbee using UART2, I open a serial connection to Xbee3 and type "+++" or AT Commands "ATID" and I am able to view to xbee's response via serial monitoring, works fine
Samsung Phone Serial App to ESP32 using Bluetooth, I'm also able to connect to the phone and type commands from phone to terminal and terminal to phone, works fine.
code for item 1:
//Arduino IDE
//ESP32 to Xbee UART2
//Select your modem:
#define TINY_GSM_MODEM_XBEE
// Set serial for debug console (to the Serial Monitor, speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
HardwareSerial SerialAT(2);
#define TINY_GSM_DEBUG SerialMon
void setup() {
// put your setup code here, to run once:
// Set console baud rate
SerialMon.begin(115200);
SerialAT.begin(9600);
delay(1000);
SerialAT.print("+++"); while (SerialAT.available()) {SerialMon.write(SerialAT.read());}
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
// Access AT commands from Serial Monitor
SerialMon.println(F("***********************************************************"));
SerialMon.println(F(" You can now send AT commands"));
SerialMon.println(F(" Enter \"AT\" (without quotes), and you should see \"OK\""));
SerialMon.println(F(" If it doesn't work, select \"Both NL & CR\" in Serial Monitor"));
SerialMon.println(F("***********************************************************"));
while(true) {
if (SerialAT.available()) {
delay(1000);
SerialAT.println("ATID");
SerialMon.write(SerialAT.read());
}
if (SerialMon.available()) {
SerialAT.write(SerialMon.read());
}
delay(0);
}
}
Code for item 2:
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
#define TINY_GSM_MODEM_XBEE
#define SerialMon Serial
HardwareSerial SerialAT(2);
#define TINY_GSM_DEBUG SerialMon
void setup() {
SerialMon.begin(115200);
SerialAT.begin(9600);
SerialBT.begin("ESP32testBLMfalse"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
// Access AT commands from Serial Monitor
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(1000);
}
My issue was when I tried to combine these. See, I wanted to know if it was possible to open a Bluetooth connection with the ESP32 and send the AT commands from the phone app to the XBEE connected to it on UART2. This is what I'm currently doing. The command looks like it sends fine, but my serial output on reads:
"
The device started, now you can pair it with bluetooth!
10:44:04.546-> OK????????????????????????????????????????????????????????????????????????????????????
"
not sure if it's due to the bit rate, let me know if anyone can assist.
Thank you
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
#define TINY_GSM_MODEM_XBEE
#define SerialMon Serial
HardwareSerial SerialAT(2);
#define TINY_GSM_DEBUG SerialMon
void setup() {
SerialMon.begin(115200);
SerialAT.begin(9600);
SerialBT.begin("ESP32testBLMfalse"); //Bluetooth device name
delay(1000);
SerialAT.print("+++"); while (SerialAT.available()) {SerialMon.write(SerialAT.read());}
delay(1000);
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
// Access AT commands from Serial Monitor
if (SerialBT.available()) {
delay(1000);
Serial.write(SerialAT.read()); //send phone input to xbee
}
if (SerialMon.available()) {
SerialAT.write(SerialBT.read()); //send xbee output to phone
SerialAT.write(SerialMon.read()); //send xbee output to serial monitor
}
delay(1000);
}
I was expecting
Samsung phone [input] --BlueTooth--> ESP32 --UART2-> Xbee
Xbee [output] --UART2--> ESP32 serial monitor --BLueTooth-->Samsung phone

How does my ESP8266/NodeMCU communicate with my GSM Module SIM800L?

so I want to connect my SIM800L GSM Module to my NodeMCU. I bought a LM2596 DC-DC voltage regulator to convert the output voltage to ~4V for the SIM800L. Input voltage for the regulator is 9V/1A.
Everything is connected: RX from GSM to D5 (GPIO14), TX to D6 (GPIO12) and RST to D7 (GPIO13). The GPIOs are deliberately chosen, since I've read online, that the RX/TX Pins on the NodeMCU are used internally. I tried matching RX/TX pins before, but it's the same result (that's why I am certain, it has to be my code that is defect). A SIM card is also inserted in the GSM Module (with PIN).
I'm coding on the Arduino IDE and I'm using the GSMSim and SoftwareSerial libraries for connecting to the GSM Module. I've tried the example sketches from the GSMSim to communicate to the GSM Module, but I'm not getting any answer on my serial monitor.
I also tried manually sending commands to the GSM Module.
#include <GSMSim.h>
#include <SoftwareSerial.h>
// You can use any Serial interface. I recommended HardwareSerial. Please use the library with highiest baudrate.
// In examples, i used HardwareSerial. You can change it anymore.
#define RX 14
#define TX 12
#define RESET 13
SoftwareSerial serial(RX, TX);
GSMSim gsmModule(serial, RESET);
void setup() {
serial.begin(115200); // If you dont change module baudrate, it comes with auto baudrate.
while(!serial) ;
Serial.begin(115200); // Serial for debug...
while(!Serial) ;
Serial.println("");
Serial.println("serial begin");
// Init module...
gsmModule.init(); // use for reseting module. Use it if you dont have any valid reason.
delay(100);
gsmModule.sendATCommand("AT+GMR");
if(serial.available()) Serial.println(serial.readString());
Serial.println("Sent AT");
}
OUTPUT:
serial.begin
Sent AT
Another example with the modified GSMSim_HTTP sketch:
#include <GSMSim.h>
#include <SoftwareSerial.h>
// You can use any Serial interface. I recommended HardwareSerial. Please use the library with highiest baudrate.
// In examples, i used HardwareSerial. You can change it anymore.
#define RX 14
#define TX 12
#define RESET 13
SoftwareSerial serial(RX, TX);
GSMSim http(serial, RESET);
static volatile int num = 0;
void setup() {
serial.begin(115200); // If you dont change module baudrate, it comes with auto baudrate.
while(!serial) {
; // wait for module for connect.
}
Serial.begin(115200); // Serial for debug...
// Init module...
http.init(); // use for init module. Use it if you dont have any valid reason.
Serial.print("Set Phone Function... ");
Serial.println(http.setPhoneFunc(1));
//delay(1000);
Serial.print("is Module Registered to Network?... ");
Serial.println(http.isRegistered());
//delay(1000);
Serial.print("Signal Quality... ");
Serial.println(http.signalQuality());
//delay(1000);
Serial.print("Operator Name... ");
Serial.println(http.operatorNameFromSim());
//delay(1000);
//Serial.print("GPRS Init... ");
//Serial.println(http.gprsInit("internet")); // Its optional. You can set apn, user and password with this method. Default APN: "internet" Default USER: "" Default PWD: ""
//delay(1000);
Serial.print("Connect GPRS... ");
Serial.println(http.connect());
//delay(1000);
Serial.print("Get IP Address... ");
Serial.println(http.getIP());
delay(1000);
Serial.print("Get... ");
Serial.println(http.get("sera.erdemarslan.com/test.php"));
delay(1000);
Serial.print("Get with SSL and read returned data... ");
Serial.println(http.getWithSSL("erdemarslan.com/test.php", true));
delay(1000);
Serial.print("Post... ");
Serial.println(http.post("sera.erdemarslan.com/test.php", "name=Erdem&surname=Arslan", "application/x-www-form-urlencoded"));
delay(1000);
Serial.print("Post with SSL and read returned data... ");
Serial.println(http.post("erdemarslan.com/test.php", "name=Erdem&surname=Arslan", "application/x-www-form-urlencoded", true));
delay(1000);
Serial.print("Close GPRS... ");
Serial.println(http.closeConn());
//delay(1000);
// For other methods please look at readme.txt file.
}
void loop() {
// Use your Serial interface...
if(serial.available()) {
String buffer = "";
buffer = Serial1.readString();
num = num + 1;
Serial.print(num);
Serial.print(". ");
Serial.println(buffer);
}
// put your main code here, to run repeatedly:
}
OUTPUT:
serial.begin
Set Phone Function... 0
is Module Registered to Network?... 0
Signal Quality... 99
Operator Name... NOT CONNECTED
Connect GPRS... 0
Get IP Address...
Get module date time... ERROR:NOT_GET_DATETIME
Set timezone and time server... 0
Sync date time from server... AT_COMMAND_ERROR
Get module date time after sycn... ERROR:NOT_GET_DATETIME
Close GPRS... 0
I hope it's something obvious, since I am not familiar with AT commands at all.
Thanks!

Communication between Arduino Nano and HM-10 BLE controller is not working

I want to check if communication is working between my SerialMonitor in Arduino IDE and BLE controller.
I typed command AT to my SerialMonitor and it suppose to return OK response but nothing happened.
This is scheme what I used:
Code:
#include <SoftwareSerial.h>
SoftwareSerial bleSerial(2, 3); // RX, TX
void setup() {
//initialize serial port for logs
Serial.begin(9600);
while (!Serial) {
}
bleSerial.begin(9600);
}
void loop() {
if (bleSerial.available()) {
Serial.write(bleSerial.read());
}
if (Serial.available()) {
bleSerial.write(Serial.read());
}
}
UPDATE:
Changed values for SoftwareSerial bleSerial(3, 2); // RX, TX still doesnt work.
UPDATE2:
I've tried switching pins and code, nothing works. I should at least see HM-10 controller in my bluetooth devices on my Android phone, but I cant see anything.
UPDATE3:
I've used code from this Stackoverflow post and its working fine. I can finally see controller in my bluetooth devices on my Android phone also It returned name MLT-BT05 after AT+NAME? command. Looks like you have to read message per char and put delay 10ms between chars, otherwise it will not be possible to read message from BLE controller. That was the only problem.
You should connect RX-TX and TX-RX (not RX-RX and TX-TX like your graphic shows) so change the cables and the code from
SoftwareSerial bleSerial(2, 3); // RX, TX
to
SoftwareSerial bleSerial(3, 2); // RX, TX
Connect according to this graphic (incl voltage divider)
Abd use the following sketch to test (read comments for details):
// SerialIn_SerialOut_HM-10_01
//
// Uses hardware serial to talk to the host computer and AltSoftSerial for communication with the bluetooth module
//
// What ever is entered in the serial monitor is sent to the connected device
// Anything received from the connected device is copied to the serial monitor
// Does not send line endings to the HM-10
//
// Pins
// BT VCC to Arduino 5V out.
// BT GND to GND
// Arduino D8 (SS RX) - BT TX no need voltage divider
// Arduino D9 (SS TX) - BT RX through a voltage divider (5v to 3.3v)
//
#include <SoftwareSerial.h>
SoftwareSerial BTserial;
char c=' ';
bool NL = true;
void setup()
{
Serial.begin(9600);
Serial.print("Sketch: "); Serial.println(__FILE__);
Serial.print("Uploaded: "); Serial.println(__DATE__);
Serial.println(" ");
BTserial.begin(9600);
Serial.println("BTserial started at 9600");
}
void loop()
{
// Read from the Bluetooth module and send to the Arduino Serial Monitor
if (BTserial.available())
{
c = BTserial.read();
Serial.write(c);
}
// Read from the Serial Monitor and send to the Bluetooth module
if (Serial.available())
{
c = Serial.read();
if (c!=10 & c!=13 )
{
BTserial.write(c);
}
// Echo the user input to the main window. The ">" character indicates the user entered text.
if (NL) { Serial.print("\r\n>"); NL = false; }
Serial.write(c);
if (c==10) { NL = true; }
}
}

How can I connect an ESP32 to an A6 GSM module?

I'm really in need of help with this problem and as most commonly advised, I have researched and researched and researched for days and I can't figure out what's wrong...
With that being said, I'm working on a project that uses an ESP-32S (pinout) and an A6 GSM module. (pinout) I'm attempting to get them connected to have the A6 send data to ThingSpeak... My problem is that I can't get it to work... I have the exact same configuration between the two using an ESP8266 and it connects and works but with the ESP32S it just doesn't seem to work...
Below is the code I have used to connect the 32 to the A6 and everything says success and pass but it won't change any ThingSpeak values using any methods...
#define TINY_GSM_MODEM_A6
#include "TinyGsmClient.h"
#include "ThingSpeak.h"
#define SerialMon Serial
HardwareSerial SerialAT(1);
bool modemConeted;
// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[] = "wireless.twilio.com";
const char user[] = "";
const char pass[] = "";
bool dataSent = false;
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
void setup(){
SerialMon.begin(9600);
delay(1000);
gsmModSetup();
}
void gsmModSetup() {
// Set GSM module baud rate
SerialAT.begin(9600, SERIAL_8N1, 27, 26, false); //27 and 26 are the pins on the ESP32S connected to the U_Rxd/U_Txd pins of A6
delay(3000);
SerialMon.println("Initializing modem...");
modem.init();
delay(3000);
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.println(modemInfo);
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
SerialMon.print("Connecting to ");
SerialMon.print(apn);
if (!modem.gprsConnect(apn, user, pass)) {
SerialMon.println(" fail");
delay(5000);
return;
}
SerialMon.println(" OK");
}
void loop(){
}
I'm using a Twilo Sim card that's loaded with data (as said above it works with esp8266 so not the SIM and not the board) The only part of this code that doesn't pass is the part that says GPRS connect. it never says pass or fail. It just sits for like five minutes then continues.
After this, I try and post to ThingSpeak and it gives ThingSpeak ERROR-307 which means failed to connect to ThingSpeak...
I have tried 2 different ways
The what I find the easy and normal way
ThingSpeak.setField(1, temp);
ThingSpeak.setField(2, h);
ThingSpeak.setField(3, p);
ThingSpeak.setField(4, pt);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
and the more complicated way of connecting to the server then sends a GET command with all the values put together with strings.
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + WriteAPIKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(thingSpeakData.length());
client.print("\n\n");
client.print(thingSpeakData);
}
client.stop();
Yet neither work and I don't know what to do... My A6 is connected to the 32 using GPIO26 & 27 they're connected to the A6 U_Rxd/U_Txd pins. All boards share common ground as they should.
Any more guides to read or code to change or libraries to try would be extremely helpful as my days of digging has yielded nothing but frustration as one attempt after another fails. the readers of this really are my last hope in solving this problem that's hindering my farther progression.
(Any questions about anything on my end, please ask. -Thank you)
The enclose documentation shows ESP32S pins for HW Serial 0 RX/TX on pin GIPO01/GIPO03, HW Serial 1 RX/TX on pin GIPO09/GIPO10 Given you have attached no oter HW at the moment, why not try with the basic config with serial1Read about this: https://github.com/G6EJD/ESP32-Using-Hardware-Serial-Ports and use the latest core ESP32 package (as of today 23.3.2020 is 1.04, previous releases had issues with serial. Do notforget to setup the serial a given in the article its different to esp8266 (!) so much about code compability. If you use the code on both platforms, you have to work with
#ifdef ESP32
//configure serial for esp32
#elif ESP8266
//configure serial for esp8266
#else
#error "Hardware not supported"
#endif
Here a small test routine for just the serial communication. If you type something in the input line of serial monitor it should be mirrored in the output window
#include <HardwareSerial.h>
void setup() {
Serial.begin(9600);
// set the data rate for the HardwareSerial port
Serial2.begin(9600);
}
void loop() {
if (Serial2.available()) {
Serial.write(Serial2.read());
}
if (Serial.available()) {
Serial2.write(Serial.read());
}
}
You can then expand the serial2 to your settings. If it fails it may be dueto: U1UXD is unused and can be used for your projects. Some boards use this port for SPI Flash access though - so try Serial3.

ESP8266 not detectable

It's my first time trying out the esp8266 on the arduino uno, using the ITEADLIB_Arduino_WeeESP8266 library. However I am not able to get anything done, not even wifi.getversion().
Here's the Serial Monitor output
setup begin
FW Version:
to station err
Join AP failure
setup end
(forever loop)
And here's the code
#include "ESP8266.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2); /* RX:D3, TX:D2 */
ESP8266 wifi(mySerial);
#define SSID "AndroidAP"
#define PASSWORD "12345678"
void setup(void)
{
}
void loop(void)
{
Serial.begin(9600);
Serial.print("setup begin\r\n");
Serial.print("FW Version: ");
Serial.println(wifi.getVersion().c_str());
if (wifi.setOprToStation()) {
Serial.print("to station ok\r\n");
} else {
Serial.print("to station err\r\n");
}
if (wifi.joinAP(SSID, PASSWORD)) {
Serial.print("Join AP success\r\n");
Serial.print("IP: ");
Serial.println(wifi.getLocalIP().c_str());
} else {
Serial.print("Join AP failure\r\n");
}
Serial.print("setup end\r\n");
Serial.println("");
delay(10000);
}
I followed the instruction on the github readme, and the led on the esp8266 is on the whole time, so I guess it is not a wiring issue. Is it possible that the 8266 is dead?
You may try this Serial.begin(115200);

Resources