Greetings...
I'm doing a small project to read an RFID card and send the data via API on the ESP8266. When the dip switch is activated for the ATmega328P, I can read the card data on the serial monitor. When the dip switch is switched to the ESP8266, I can connect to the WiFi and the data that I send via the serial monitor is captured and sent via API.
However, when both chips are activated in the dip switch in mode 1 and 2 (ATmega328P+ESP8266 simultaneously), it seems that nothing works and they don't communicate with each other. I've already rewritten the code in several ways but without success.
I use UART communication and the SCL and SDA output on the PN532 and it is connected to port 2,3.
code atmega
#include <SoftwareSerial.h>
#include <PN532_SWHSU.h>
#include <PN532.h>
SoftwareSerial SWSerial( 3, 2 ); // RX, TX
#define RX_PIN 10
#define TX_PIN 11
// Cria o objeto SoftwareSerial
SoftwareSerial espSerial(RX_PIN, TX_PIN);
PN532_SWHSU pn532swhsu( SWSerial );
PN532 nfc( pn532swhsu );
String stagId = "None", dispTag = "None";
byte nuidPICC[4];
void setup(void)
{
SWSerial.begin(115200);
espSerial.begin(9600);
Serial.begin(9600);
Serial.println("Hello Maker!");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata)
{
Serial.print("Didn't Find PN53x Module");
while (1); // Halt
}
// Got valid data, print it out!
Serial.print("Found chip PN5");
Serial.println((versiondata >> 24) & 0xFF, HEX);
Serial.print("Firmware ver. ");
Serial.print((versiondata >> 16) & 0xFF, DEC);
Serial.print('.');
Serial.println((versiondata >> 8) & 0xFF, DEC);
// Configure board to read RFID tags
nfc.SAMConfig();
}
void loop()
{
readNFC();
// Lê qualquer resposta do ESP8266 e envia para o monitor serial
if (espSerial.available()) {
Serial.write(espSerial.read());
}
// Lê qualquer entrada do monitor serial e envia para o ESP8266
if (Serial.available()) {
espSerial.write(Serial.read());
}
delay(1000); // Aguarda um segundo antes de enviar a próxima mensagem
}
void readNFC()
{
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success)
{
Serial.print("UID Length: ");
Serial.print(uidLength, DEC);
Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i = 0; i < uidLength; i++)
{
nuidPICC[i] = uid[i];
Serial.print(" "); Serial.print(uid[i], DEC);
}
Serial.println();
stagId = tagToString(nuidPICC);
Serial.print(F("stagId is : "));
stagId.trim();
Serial.println(stagId.c_str());
//Serial.write(stagId.c_str());
espSerial.println(stagId.c_str());
delay(1000); // 1 second halt
}
else
{
// PN532 probably timed out waiting for a card
Serial.println("Timed out! Waiting for a card...");
}
}
String tagToString(byte id[4])
{
String tagId = "";
for (byte i = 0; i < 4; i++)
{
if (i < 3) tagId += String(id[i]) + "_";
else tagId += String(id[i]);
}
return tagId;
}
esp8266
/*
This sketch establishes a TCP connection to a "quote of the day" service.
It sends a "hello" message, and then prints received data.
*/
#include <ESP8266Firebase.h>
Firebase firebase("https://teste/");
#include <ESP8266WiFi.h>
#ifndef STASSID
#define STASSID "NET"
#define STAPSK "pass"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
//CONFIGURAÇÕES DO SERVIDOR
int HTTP_PORT = 80;
String HTTP_METHOD = "GET"; // or "POST"
char HOST_NAME[] = "192.168.3.9"; // hostname of web server:
String PATH_NAME = "/api.php";
void setup() {
Serial.begin(9600);
Serial1.begin(9600); // Comunicação serial com o ATmega328P
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
firebase.setString("Test/question", "Is it really working?");
firebase.setString("Test/answer", "Yes!");
}
void loop() {
static bool wait = false;
String IncomingString = "";
boolean StringReady = false;
//aguarda o envio da IDtag
//while ((Serial.available()) & (StringReady == false)){
if (Serial.available() > 0) {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
//IncomingString = Serial.readString();
//IncomingString = Serial.readStringUntil('\n');
IncomingString = Serial.readString();
firebase.setString("Test/message", IncomingString);
//if (!IncomingString.isEmpty()) { // bail if we got an empty line
StringReady = true;
Serial.println("valor de incoming " + IncomingString);
//}
Serial.print("connecting to ");
Serial.print(HOST_NAME);
Serial.print(':');
Serial.println(HTTP_PORT);
Serial.println(IncomingString);
//strcat(host, IncomingString); // Concatenate str2 to result
// Use WiFiClient class to create TCP connections
WiFiClient client;
//if (!client.connect(host + "TAGID=" + IncomingString, port)) {
if (!client.connect(HOST_NAME, HTTP_PORT)) {
Serial.println("connection failed");
delay(5000);
return;
}
// This will send a string to the server
Serial.println("sending data to server");
if (client.connected()) {
String queryString = String("?TAGID=") + String(IncomingString);
firebase.setString("Test/message", queryString);
Serial.println("GET " + PATH_NAME + queryString + " HTTP/1.1");
client.println("GET " + PATH_NAME + queryString);
//client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println(); // end HTTP request header
}
// wait for data to be available
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
delay(60000);
return;
}
}
// Read all the lines of the reply from server and print them to Serial
Serial.println("receiving from remote server");
// not testing 'client.connected()' since we do not need to send data here
while (client.available()) {
char ch = static_cast<char>(client.read());
Serial.print(ch);
}
// Close the connection
Serial.println();
Serial.println("closing connection");
client.stop();
}
StringReady = false;
}
