I am trying to make a device to replace an old paper-based "Sign In - Sign Out" timesheet with a combination of an RC522 RFID module and an ESP32 dev board.
I have managed to make it work with a D1 Mini via Wifi however since I am not allowed to use Wifi I had to pick another board with GPRS connection -> TTGO T-Call ESP32 Sim800L.
I was able to connect the T-Call to the network and then my website to send HTTP Post to it. I was able to connect and RC522 to the board and read the cards as well...
Separately both worked quite well however as soon as I try to put the two to together it will not finish the POST request.
It connects to the network and my website just fine:
Initializing modem...
AT
⸮AT
⸮AT
AT
OK
AT&W
AT&W
OK
AT+CFUN=0
AT+CFUN=0
OK
AT+CFUN=1,1
AT+CFUN=1,1
OK
AT
⸮AT
⸮AT
OK
ATE0
ATE0
OK
AT+CMEE=0
OK
AT+CLTS=1
OK
AT+CBATCHK=1
OK
AT+CPIN?
ERROR
AT+CPIN?
RDY
+CFUN: 1
ERROR
AT+CPIN?
+CPIN: READY
+CPIN: READY
OK
AT+CPIN?
+CPIN: READY
OK
AT+CPIN="1210"
ERROR
Connecting to APN: data.ukAT+CIPSHUT
SHUT OK
AT+CGATT=0
OK
AT+SAPBR=3,1,"Contype","GPRS"
OK
AT+SAPBR=3,1,"APN","data.uk"
OK
AT+SAPBR=3,1,"USER","user"
OK
AT+SAPBR=3,1,"PWD","one2one"
OK
AT+CGDCONT=1,"IP","data.uk"
OK
AT+CGACT=1,1
Call Ready
SMS Ready
OK
AT+SAPBR=1,1
OK
AT+SAPBR=2,1
+SAPBR: 1,1,"10.178.141.45"
OK
AT+CGATT=1
OK
AT+CIPMUX=1
OK
AT+CIPQSEND=1
OK
AT+CIPRXGET=1
OK
AT+CSTT="data.uk","user","one2one"
OK
AT+CIICR
OK
AT+CIFSR;E0
10.178.141.45
OK
AT+CDNSCFG="8.8.8.8","8.8.4.4"
OK
OK
Connecting to *****.co.ukAT+CIPCLOSE=0,1
ERROR
AT+CIPSSL=0
OK
AT+CIPSTART=0,"TCP","*****.co.uk",80
OK
0, CONNECT OK
OK
Connecting to *****.co.uk
OK
But as soon as it detects a card and tries to perform an HTTP request is goes like:
Performing HTTP POST request...
AT+CIPSEND=0,43
AT+CIPSEND=0,21
AT+CIPSEND=0,17
AT+CIPSEND=0,2
AT+CIPSEND=0,49
AT+CIPSEND=0,2
AT+CIPSEND=0,16
AT+CIPSEND=0,2
AT+CIPSEND=0,2
AT+CIPSEND=0,11
AT+CIPSEND=0,2
AT+CIPRXGET=4,0
AT+CIPSTATUS=0
It is driving me crazy because I have tested both of these codes and they both worked perfectly individually. Only thing I can think of is that they are somehow "interferring" with each other because the order of loading (initializing) modules is off or they might need some delays between them... not sure.
Here is my full combined code, if anybody has any idea to solve this issue I would really apprecitiate it!
//GPRS credentials
const char apn[] = "data.uk";
const char gprsUser[] = "user";
const char gprsPass[] = "one2one";
// SIM card PIN
const char simPIN[] = "1210";
// Server details
const char server[] = "*****";
String resource = "*****/check.php";
const int port = 80;
// TTGO T-Call pins
#define MODEM_RST 5
#define MODEM_PWKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26
#define I2C_SDA 21
#define I2C_SCL 22
#define SS_PIN 15
#define RST_PIN 33
#define LED_PIN 12
#define SPK_PIN 13
// Set serial for debug console (to Serial Monitor, default speed 115200)
// Set serial for AT commands (to SIM800 module)
#define SerialAT Serial1
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
// Define the serial console for debug prints, if needed
#define DUMP_AT_COMMANDS
#include <TinyGsmClient.h>
#include <SPI.h>
#include <MFRC522.h>
bool addRfid, readyOK, loadingVar = false;
unsigned long chipID, lastID, timeout1;
MFRC522 mfrc522(SS_PIN, RST_PIN);
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, Serial);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
// TinyGSM Client for Internet connection
TinyGsmClient client(modem);
bool httpPost(long rid) {
String rfid = String(rid);
if(rfid == "911560") {
addRfid = true;
Serial.println("addRfid: True");
digitalWrite(LED_PIN, HIGH);
} else {
if(addRfid == true) {
resource = "******/add.php";
addRfid = false;
digitalWrite(LED_PIN, LOW);
} else {
resource = "*****/check.php";
}
Serial.println(" OK");
// Making an HTTP POST request
Serial.println("Performing HTTP POST request...");
String httpRequestData = "rfid=" + rfid;
client.print(String("POST ") + resource + " HTTP/1.1\r\n");
client.print(String("Host: ") + server + "\r\n");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded\r\n");
client.print("Content-Length: ");
client.println(httpRequestData.length());
client.println(httpRequestData);
unsigned long timeout1 = millis();
while (client.connected() && millis() - timeout1 < 10000L) {
Serial.println(" Connected and timeout ok");
// Print available data (HTTP response from server)
while (client.available()) {
char c = client.read();
Serial.print(c);
timeout1 = millis();
Serial.println(" Available");
}
}
Serial.println();
return true;
}
}
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(SPK_PIN, OUTPUT);
// Start Serial
Serial.begin(115200);
// Set modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH);
// Set GSM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
// Restart SIM800 module, it takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
modem.restart();
// use modem.init() 'restart' if you don't need the complete restart
// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
modem.simUnlock(simPIN);
}
// Establishing GPRS connection
Serial.print("Connecting to APN: ");
Serial.print(apn);
if (modem.gprsConnect(apn, gprsUser, gprsPass)) {
Serial.println(" OK");
Serial.print("Connecting to ");
Serial.print(server);
if (client.connect(server, port)) {
Serial.println(" OK");
readyOK = true;
} else {
Serial.println(" fail");
}
Serial.print("Connecting to ");
Serial.print(server);
// Start SPI and initializing MFRC
SPI.begin();
mfrc522.PCD_Init();
delay(10);
mfrc522.PCD_DumpVersionToSerial();
digitalWrite(LED_PIN, HIGH); delay(150);
digitalWrite(LED_PIN, LOW); delay(150);
digitalWrite(LED_PIN, HIGH); delay(150);
digitalWrite(LED_PIN, LOW); delay(10);
Serial.println("Card reader is ready.");
} else {
Serial.println(" fail");
}
}
void loop() {
if (readyOK) {
if (mfrc522.PICC_IsNewCardPresent()){
digitalWrite(SPK_PIN, HIGH); digitalWrite(LED_PIN, HIGH); delay(100);
digitalWrite(SPK_PIN, LOW); digitalWrite(LED_PIN, LOW); delay(100);
digitalWrite(SPK_PIN, HIGH); digitalWrite(LED_PIN, HIGH); delay(100);
digitalWrite(SPK_PIN, LOW); digitalWrite(LED_PIN, LOW); delay(100);
chipID = 0;
mfrc522.PICC_ReadCardSerial();
for (byte i = 0; i < mfrc522.uid.size; i++){
chipID=((chipID+mfrc522.uid.uidByte[i])*10);
}
Serial.print("Card's ID: ");
Serial.println(chipID);
httpPost(chipID);
} else { //new card
Serial.println(" Waiting for card...");
}
}
else {
Serial.println(" Connection lost");
}
digitalWrite(LED_PIN, HIGH);
delay(1000);
}
PS.: I have tried to give as much informations as I could however I would gladly provide more if needed.
I have tried to separate the functions like "reading an RFID" or "performing an HTTP request" and they both worked individually.