I am trying to send MFRC522 data to a C# application through a NodeMCU acting as an access point for the C# app. I have modified the code I got from this site.
#include ESP8266WiFi.h>
#include SPI.h>
#include MFRC522.h>
const char* AP_SSID ="IoT4143" ;
const char* AP_PASSWORD ="iot4143";
#define RST_PIN D3
#define SS_PIN D4
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
WiFiServer server(80);
WiFiClient client;
void wifiConnect() {
WiFi.disconnect();
WiFi.softAP(AP_SSID, AP_PASSWORD);
int watchDog = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
watchDog++;
if(watchDog == 60) {
WiFi.disconnect();
WiFi.printDiag(Serial);
watchDog = 0;
return;
}
}
server.begin();
}
void setup() {
Serial.begin(115200);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
wifiConnect();
}
void loop() {
//Are we connected to WiFi?
if(WiFi.status() != WL_CONNECTED) {
wifiConnect();
}
//Wait for a connection attempt
client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
int watchDog = 0;
while(!client.available()) {
delay(1);
if(watchDog == 10000) {
watchDog = 0;
return;
}
watchDog++;
}
sendData();
}
void sendData() {
// Read the first line of the request
String request = client.readStringUntil('\r');
client.flush();
// Match the request
while(request=="READSTOP") {
Serial.println("RFID READING STARTED..."); //Forward to C# App
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
// Dump debug info about the card; PICC_HaltA() is automatically called
client.print(mfrc522.uid));
}
Serial.println("RFID READING STOPPED"); //Stop server
delay(1);
client.stop();
}
I am very new to hardware/network programming. I don't know what will be possible errors in this code and whether it will work or not.
When I verified the code in the Arduino IDE, there was an error on this line of code:
client.print(mfrc522.uid));
error:= no matching function for call to 'WiFiClient::print(MFRC522::Uid&)'
When the connect button in the application is clicked the "READSTART" string is sent to NodeMCU and when disconnect is clicked the "READSTOP" string is sent.
Please help!
1 Answer 1
MFRC522::uid is a struct (see source code). There's no defined way to print it.
If you want to write the raw bytes over the TCP connection, you can just use
client.write((uint8_t*)mfrc522.uid.uidByte, mfrc522.uid.size);
If you want to write it as ASCII text, you can use the following:
void byte_to_str(char* buff, uint8_t val) { // convert an 8-bit byte to a string of 2 hexadecimal characters
buff[0] = nibble_to_hex(val >> 4);
buff[1] = nibble_to_hex(val);
}
char nibble_to_hex(uint8_t nibble) { // convert a 4-bit nibble to a hexadecimal character
nibble &= 0xF;
return nibble > 9 ? nibble - 10 + 'A' : nibble + '0';
}
void printHEX(Stream &s, uint8_t *data, size_t length) {
char *buffer = new char[length*2];
for (size_t i = 0; i < length; i++) {
byte_to_str(&buffer[i*2], data[i]);
}
s.write(buffer, length*2);
delete[] buffer;
}
Then use it like this:
printHEX(client, mfrc522.uid.uidByte, mfrc522.uid.size);
It loops over all bytes in the UID, converts them to ASCII (hexadecimal representation, so 2 characters per byte), and then writes the text over the TCP connection.
(Note that there are many other problems with your code as well, e.g. the includes are missing the opening '<'.)
-
Thank you @tttapa for your explanation, the includes missing '<' was purposly done other than that what could be the possible errors/problems you are taking abt????Venkatesh Shiga– Venkatesh Shiga2017年12月29日 13:29:33 +00:00Commented Dec 29, 2017 at 13:29
-
In file included from \libraries\ESP8266WiFi\src/ESP8266WiFi.h:39:0, from \typrojectTRY1.ino:1: src/WiFiClient.h: In instantiation of 'size_t WiFiClient::write(T&, size_t) [with T = unsigned char [10]; size_t = unsigned int]': \typrojectTRY1.ino:103:53: required from here libraries\ESP8266WiFi\src/WiFiClient.h:123:36: error: request for member 'available' in 'source', which is of non-class type 'unsigned char [10]' (part1/2)Venkatesh Shiga– Venkatesh Shiga2017年12月29日 13:46:05 +00:00Commented Dec 29, 2017 at 13:46
-
size_t left = source.available(); \libraries\ESP8266WiFi\src/WiFiClient.h:127:5: error: request for member 'read' in 'source', which is of non-class type 'unsigned char [10]' source.read(buffer.get(), will_send); Error compiling for board NodeMCU 1.0 (ESP-12E Module); part(2/2) now i am getting this error seriously i don't what it means i am a c# programmer.Venkatesh Shiga– Venkatesh Shiga2017年12月29日 13:46:41 +00:00Commented Dec 29, 2017 at 13:46
-
@VenkateshShiga : I updated my answer. You have to cast it to a
uint8_t*
.tttapa– tttapa2017年12月29日 14:29:21 +00:00Commented Dec 29, 2017 at 14:29 -
Thank you,i have done compiling there isn't any error...Venkatesh Shiga– Venkatesh Shiga2017年12月30日 04:25:31 +00:00Commented Dec 30, 2017 at 4:25
Explore related questions
See similar questions with these tags.
client.print(mfrc522.uid));
How many opening and closing parentheses do you count in that line? Hmm...?