My teacher just gave me a NFC tag v1.0 for a project. I have to use my phone to unlock a door.
I searched on internet and I just found this code:
#include "NfcTag.h"
#include <Wire.h>
NfcTag nfcTag;
int led = 5;
bool flag = false;
bool preFlag = false;
void setup(){
Serial.begin(9600);
pinMode(led,OUTPUT);
nfcTag.init();
}
void loop(){
flag = nfcTag.readByte(EEPROM_I2C_LENGTH-1) == 0xff?true:false;
if(flag != preFlag){
Serial.println("get remote NFC control signal!");
if(flag == true){
Serial.println("led will light up!");
digitalWrite(led,HIGH);
}else{
Serial.println("led will turn dark!");
digitalWrite(led,LOW);
}
preFlag = flag;
}
delay(5*1000);
}
This come from their wiki: http://wiki.seeed.cc/Grove-NFC_Tag/
I can connect with the phone and use it to change blocks. The problem is that when I try it, the LED just doesn't work at all. I tried the LED and he is working, I also tried the Digital pin on the Arduino UNO with the LED and it works as well. Also, the monitor doesn't show any off those printLn.
I used this way to connect them:
SCL - RX
SDA - TX
GND - GND
5V - VCC
I also used a code to debug, but no matter what address I use to readByte, I will always get the number 127 even after using the phone to change it with the app.
#include "NfcTag.h"
#include <Wire.h>
NfcTag nfcTag;
bool flag = false;
bool preFlag = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("starting sequence");
nfcTag.init();
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("checking for nfc");
//flag = nfcTag.readByte(EEPROM_I2C_LENGTH-1) == 0xff?true:false;
//Serial1.println(flag);
Serial.println(nfcTag.getICNumber());
Serial.println(nfcTag.getAFI());
Serial.println(nfcTag.getRFU());
Serial.println(nfcTag.getMemoryVolume());
Serial.println("result:");
Serial.println(nfcTag.readByte(EEPROM_I2C_LENGTH-1)); //I tried changeing manualy the value, but the output still 127.
delay(2000);
}
Output:
checking for nfc
127
127
32639
8355711
result:
127
If I could put the LED working, I have already a transistor with a power converter on the exit of the pin5, so the door can be unlocked.
By the way, I don't know why sometimes the NFC doesn't work and after some tries it start working again.
Any help will be appreciate :) Thanks.
The correct connection for the I2C pins is:
SCL - PC5 (Analog A5)
SDA - PC4 (Analog A4)
This part supports Uart communications as you wired it. The link you provided for the part you are using only support I2C and will not work as you wired it.
Related
I'm trying to send data from Arduino UNO to NodeMCU via UART.
What I want to do is that when Arduino UNO sends "on" String to the NodeMCU, NodeMCU lights up its builtin LED, when "off" - it turns off.
I send data from Arduino UNO via standard Serial.println (). On the NodeMCU I use the SoftwareSerial library. I assigned rx and tx to pins D7 and D8 accordingly. Serial ports on Arduino(standard) and NodeMCU(SoftwareSerial) are set at 9600 baud rate.
Standard Serial port (USB) of NodeMCU is set to 115200.
I send the string that I receive from the Arduino to the standard serial port of the node (connected to usb)
The question is:
On the standard port of NodeMCU, which I view through the Arduino IDE, messages coming from the arduino are displayed, and displayed correctly (those that were sent), but the NodeMCU does not want to accept them in conditional statements and light up my LED. Why?
At the same time, when I remap the virtual UART to its original pins (connected to USB, GPIO3 and GPIO1, and send the same messages via usb through the COM port view in the Arduino IDE, the LED turns on and off as I programmed it.
Do you have any ideas why this is happening?
By the way, I do not lower voltage coming from Arduino RX and TX pins from 5V to 3.3V, but since messages are recived coorectly, I don't think that this is causing a problem.
Here's my code:
Arduino:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("on");
delay(1000);
Serial.println("off");
delay(1000);
}
NodeMCU:
#include <SoftwareSerial.h>
SoftwareSerial s(D7,D8);//rx,tx
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
s.begin(9600);
pinMode(D4,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
String str = s.readStringUntil('\n');
Serial.println(str);
if(str == "on"){
digitalWrite(D4, HIGH);
}
if(str=="off"){
digitalWrite(D4, LOW);
}
}
Screenshot of COM4:
Screenshot of COM4:
UPD: I tried using sending 1 or 0 as int value via Serial.write() and s.read() and it works, maybe the prolem is in String type somehow
You Use İt Maybe Work
#include <SoftwareSerial.h>
String text = "";
SoftwareSerial s(D7,D8);//rx,tx
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
s.begin(9600);
pinMode(D4,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
String str = s.readStringUntil('\n');
text = str
Serial.println(text);
if(text == "on"){
digitalWrite(D4, HIGH);
}
if(text =="off"){
digitalWrite(D4, LOW);
}
}
I am following to the official tutorial and trying to get my ESP32S give some output.
Official tutorial
I can do everything but I am not getting any output. My OS is Ubuntu 22.04 LTS, the code I try to launch:
#include <Arduino.h>
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Hello world!");
delay(1000);
}
This is what I got at the monitor (NO FRACKING "HELLO WORLD!" MESSAGE).
I have tried to execute blink program with no success either!
The Program:
#include <Arduino.h>
#define LED 2
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED, HIGH);
Serial.println("LED is on");
delay(1000);
digitalWrite(LED, LOW);
Serial.println("LED is off");
delay(1000);
}
Output for both programs with empty log files. Nothing in them!
And LED doesn't blink.
This is my platformio.ini file:
[env:wemos_d1_mini32]
platform = espressif32
board = wemos_d1_mini32
framework = arduino
lib_deps = kitesurfer1404/WS2812FX#^1.4.1
monitor_speed = 9600
monitor_filters = esp32_exception_decoder, default, log2file, colorize
debug_tool = olimex-arm-usb-ocd-h
Device is connected, I checked it.
I mean I have no Idea why this is not working!
In platformio.ini file add 2 lines and it will work:
[env:wemos_d1_mini32]
platform = espressif32
board = wemos_d1_mini32
framework = arduino
lib_deps = kitesurfer1404/WS2812FX#^1.4.1
monitor_speed = 9600
monitor_filters = esp32_exception_decoder, default, log2file, colorize
debug_tool = olimex-arm-usb-ocd-h
monitor_dtr = 0
monitor_rts = 0
Something with these dtr and rts pins are not ok so you just disable them and the software will be working.
debug_tool = olimex-arm-usb-ocd-h
Looking at this line at platformio.ini, I am assuming that you have a debugger connected. In that case, is platformIO identifying the right serial port?
Identify your Serial port of the ESP32 and add this line with your COM port number like the following to your platformio.ini -
upload_port = COM3
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.
I am working on my senior project for school, and part of what I need to do is use an HM-19 Bluetooth 5.0 module to connect to another Bluetooth 5.0 module and establish a master slave connection.
I can do that just fine, but when I include the code needed for my ultrasonic sensor to do scan, my commands to my HM-19 don't return anything and I can't do any of the basic functions such as finding connections. I have tested it with and without the ultrasonic sensor code and the problem occurs when I use the sensor portion of the code.
TO BE CLEAR, all I am trying to do is just have my Bluetooth 5.0 chip connect to another and do normal commands while also inputting into my serial monitor a distance when I put my hand in front. THIS IS JUST A TEST, once I get that done I will move to what I really want to do.
IT'S JUST A STARTING POINT IN A PROJECT. I have a function call for my sensor and my bluetooth chip in the void loop, that's all that is in there.
I just want to know how to fix this issue. How can I scan with my ultrasonic sensor and send commands to my Bluetooth module? Any help would be greatly appreciated.
[Here are the results when the sensor is commented][1] and [here are the unsuccessful results that results in an infinite loop where I can't get to the portion of the code that returns what the chip says][2]. Lastly, although most of the links include stuff for the HM-10, the commands are basically the same for the HM-19. I'm adding more because stack overflow won't let me edit this post until there are more characters or something. I hope you have a good day/evening person reading this.
Here is my code:
// 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 <AltSoftSerial.h>
AltSoftSerial BTserial;
// https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
char c=' ';
boolean NL = true;
const int trigPin = 9;
const int echoPin = 10;
float duration, distance;
boolean wait_your_turn = false; //My attempt to make sure the sensor and the Bluetooth module don't interfere with each other
//if I'm sending data from the serial monitor to the bluetooth module and vice versa it switches to true and the bluetooth module
//does its thing, so the sensor doesn't get in the way.
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
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()
{
Bluetooth();
Sensor();
}
void Sensor(){
if((wait_your_turn == true))
{}
else
{
Serial.println("Scanning for stuff.");
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
if(distance <= 20)
{
Serial.println(distance);
delay(500);
}
}
}
void Bluetooth()
{
if (Serial.available())
{
if(wait_your_turn == false)
Serial.println("Serial is available");
wait_your_turn = true;
while(Serial.available()>0)
c = Serial.read();
Serial.write(c);
if(c!=10&c!=13)
BTserial.print(c);
}
if (BTserial.available())
{
// Serial.print("We are at the bluetooth portion.");
while(BTserial.available())
c = BTserial.read();
Serial.print(c);
wait_your_turn = false;
}
}
[1]: https://i.stack.imgur.com/Dn4i0.png
[2]: https://i.stack.imgur.com/s9Ifv.png
Sorry, I forgot about this question. I figured it out. What I did was have 1 Arduino control the Ultrasonic sensor and send a character to the other Arduino when something was in range of the sensor. The other Arduino would then read the character and based on the character send it would perform an action. Thank you everyone who commented and have a great rest of your days.
I have the ESP8266 connected to my Arduino Uno. With a blank sketch I can use Serial Monitor to connect it to my wifi network using these commands
AT+IPR=9600
AT+CWMODE=1
AT+CWJAP="SSID_HERE",""
It get's an ip and everything. But now I want my sketch to just do this using this code
#include <SoftwareSerial.h>
#define SSID "SSID_HERE"
void setup(){
Serial.begin(9600);
Serial.setTimeout(5000);
delay(1000);
}
boolean connectWiFi()
{
// connect
Serial.println("AT+CWMODE=1");
Serial.println("AT+CWJAP=\"SSID_HERE\",\"\"");
delay(2000);
if(Serial.find("OK"))
{
Serial.println("AT+CIFSR");
Serial.flush();
delay(1000);
return true;
}
else
{
// Can not connect to the WiFi.
return false;
}
}
But it doesn't work.. The Serial.println shows up in the Serial Monitor, but the ESP8266 doesn't seem to respond. What am I missing?
AT -commands ends with carriage return, so you need to add '\r' to every command you print.
In your code lines looks like:
Serial.println("AT+CWMODE=1\r");
Serial.println("AT+CWJAP=\"SSID_HERE\",\"\"\r");
Serial.println("AT+CIFSR\r");
Reference: https://en.wikibooks.org/wiki/Serial_Programming/Modems_and_AT_Commands/Special_Commands_and_Character_Sequences
The problem here is that you are trying to use pins 0 & 1 for the serial comms, well its part of the problem.. Because the arduino uses serial as well, it for me is only really good to use pins 0 & 1 for serial when i've grounded the reset pin on the arduino. This turns the arduino into a dummy device.
You can use something like software serial and two different pins instead, this way you will not interfere with the hardware serial of the arduino.
Also just to note, the below example will barely work.. For some it will for others it wont.. The problem here is that software serial does not really work / run at 115200..
You can change baud rate via AT+UART_DEF=19200,8,1,0,0 which will also disable flow control, then use software serial with a different speed mySerial.begin(19200)
Using Serial.println("TEXT") will send the line returns for you, so no need to add them unless you use Serial.print("TEXT\r\n")
DO NOT USE: AT+IPR= as this will brick it and require a reflash
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 10); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
pinMode(11, INPUT);
pinMode(10, OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("ARDUINO: Starting");
mySerial.begin(115200);
Serial.println("ARDUINO: Sending AT Command");
mySerial.println("AT");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}