The problem is if i want to combine post and get data together. Post code cannot get data.length and always 0 value. So i cannot post data to website.
This is my code. Can you help me?
#if 1
#include <SPI.h>
#include <PN532_SPI.h>
#include "PN532.h"
PN532_SPI pn532spi(SPI, 10);
PN532 nfc(pn532spi);
#elif 0
#include <PN532_HSU.h>
#include <PN532.h>
PN532_HSU pn532hsu(Serial);
PN532 nfc(pn532hsu);
#else
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
#endif
#include "Wire.h"
#include <LiquidCrystal_I2C.h>
int relay = 7; //deklarasi pin relay (7)
String ssid = "tethering"; // Nama SSID Akses point
String password = "password"; // Password akses point
String data;
String server = "www.myweb.com"; // domain
String uri = "/nfcpost.php";// file php di server
int reRequest = 0;
int doorlock = 5; // Deklarasi pin relay
int sameData = 0;
// Modulo I2C display no endereco 0x27
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
byte zero = 0x00;
byte data1, data2, data3, data4, data5, data6, data7;
String dat1, dat2, dat3, dat4, dat5, dat6, dat7;
String id;
byte readCard[20];
String temp_id;
byte byteArray[20];
int sending = 0;
int sent = 0;
int gett = 0;
void setup() {
// Serial.begin(115200); //deklarasi proses komunikasi serial
Serial.begin(115200);
delay(200);
pinMode(doorlock, OUTPUT);
delay(100);
digitalWrite(doorlock, LOW);
nfc.begin();
lcd.begin (16, 2);
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
// Serial.print("Didn't find PN53x board");
lcd.setCursor(0, 0);
lcd.print("NFC Reader");
lcd.setCursor(0, 1);
lcd.print("Tidak ditemukan");
delay(500);
// lcd.clear();
while (1); // halt
}
// Got ok data, print it out!
//Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX);
lcd.setCursor(0, 0);
lcd.print("NFC Reader setup");
delay(500);
// lcd.clear();//Serial.print("Firmware ver. "); Serial.print((versiondata >> 16) & 0xFF, DEC);
lcd.print((versiondata >> 16) & 0xFF, DEC);
delay(500);
lcd.clear();// Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
reset();
connectWifi();
delay(2000); // tunda waktu 2 detik supaya stabil
}
void loop() {
readNFC();
if (sent == 1) {
httppost ();
sent = 0;
}
if (gett == 1) {
httpget();
gett = 0;
id = "";
}
// if (sameData == 1) {
// httpget();
// lcd.setCursor(0, 0);
// lcd.print("Data sama");
// delay(5000);
// }
}
void readNFC() {
lcd.setCursor(0, 0);
lcd.print("Tempelkan");
lcd.setCursor(0, 1);
lcd.print("NFC anda");
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)
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success) {
// Serial.println("Found a card!");
lcd.setCursor(0, 0);
lcd.print("NFC terdeteksi=");
delay(500);
// Serial.print("UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
// Serial.println("UID Value: ");
// char x[20];
// String s;
for (uint8_t i = 0; i < uidLength; i++)
{
// Serial.print(" Data ke "); Serial.print(i);
// Serial.print(" 0x"); Serial.println(uid[i]);
if (i == 0) data1 = uid[i];
if (i == 1) data2 = uid[i];
if (i == 2) data3 = uid[i];
if (i == 3) data4 = uid[i];
if (i == 4) data5 = uid[i];
if (i == 5) data6 = uid[i];
if (i == 6) data7 = uid[i];
readCard[i] = uid[i];
temp_id = String(readCard[i], DEC);
id += temp_id;
}
lcd.setCursor(0, 1);
lcd.print(id);
delay(500);
lcd.clear();
// Serial.println("");
// Wait 1 second before continuing
// if ((data1 == 83) && (data2 == 231) == (data3 == 163) && (data4 == 148) && (data5 == 0) && (data6 == 154) && (data7 == 0)) digitalWrite(13, HIGH);
// delay(1000);
data = "nfc=" + id + "";
// data = "nfc=123";
// Serial.print("Mengirim id=");
// Serial.println(id);
lcd.setCursor(0, 0);
lcd.print("Mengirim id=");
delay(500);
lcd.setCursor(0, 1);
lcd.print(id);
delay(500);
lcd.clear();
// httppost ();
// if(sameData==1){
// httpget();
// }
//Serial.println(data1);
//Serial.println(data2);
//Serial.println(data3);
//Serial.println(data4);
sent = 1;
}
else
{
// PN532 probably timed out waiting for a card
// Serial.println("Timed out waiting for a card");
digitalWrite(13, LOW);
}
}
void reset() {
resetAgain:
Serial.println("AT+RST"); // AT Command untuk reset Serial28266
delay(1000);
if (Serial.find("OK") )// Serial.println("Module Reset");
int zer = 0;
else {
goto resetAgain;
}
}
//Proses menghubungkan ke akses point
void connectWifi() {
lcd.setCursor(0, 0);
lcd.print("Mengubungkan");
lcd.setCursor(0, 1);
lcd.print("ke server...");
delay(2000);
lcd.clear();
String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";
Serial.println(cmd);
delay(4000);
if (Serial.find("OK")) {
// Serial.println("Connected!");
lcd.setCursor(0, 0);
lcd.print("Terhubung");
lcd.setCursor(0, 1);
lcd.print("ke server");
delay(2000);
lcd.clear();
}
else {
connectWifi();
//Serial.println("Cannot connect to wifi");
}
}
void httpget () { // Proses mengirim nilai sensor ke server
lcd.setCursor(0, 0);
lcd.print("Mohon tunggu...");
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += server;
cmd += "\",80";
Serial.println(cmd);
if (Serial.find("Error")) return;
cmd = "GET /nfc.php";
cmd += " HTTP/1.1\r\n";
cmd += "Host: ";
cmd += server;
cmd += "\r\n";
cmd += "Connection: close\r\n\r\n";
delay(10);
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
delay(2000);
Serial.print(cmd);
// Serial.find("+IPD");
while (Serial.available())
{
lcd.setCursor(0, 0);
lcd.print("Mohon tunggu...");
String line = Serial.readString();
int getIndexStatus = line.indexOf("true");
int getIndexID = line.indexOf(id);
if ( getIndexStatus != -1 && getIndexID != -1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ID terdaftar");
lcd.setCursor(0, 1);
lcd.print("pintu terbuka");
digitalWrite(doorlock, HIGH);
delay(1000);
digitalWrite(doorlock, LOW);
lcd.clear();
reRequest = 0;
goto endGet;
}
else {
reRequest++;
if (reRequest > 2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ID tidak");
lcd.setCursor(0, 1);
lcd.print("terdaftar");
delay(1000);
lcd.clear();
reRequest = 0;
}
else {
lcd.setCursor(0, 0);
lcd.print("Mohon tunggu...");
httpget();
}
}
}
endGet:
delay(1000);
}
void httppost () { // Proses mengirim nilai sensor ke server
sendingAgain:
Serial.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
if ( Serial.find("OK")) {
} delay(1000);
String postRequest =
"POST " + uri + " HTTP/1.0\r\n" +
"Host: " + server + "\r\n" +
"Accept: *" + "/" + "*\r\n" +
"Content-Length: " + data.length() + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"\r\n" + data;
String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
Serial.print(sendCmd);
Serial.println(postRequest.length());
delay(500);
if (Serial.find(">")) {
sending++;
Serial.print(postRequest);
if ( Serial.find("SEND OK")) {
sending = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Data terkirim");
delay(500);
lcd.clear();
while (Serial.available()) {
// httpget();
sameData = 1;
gett = 1;
goto endSend;
}
//
// Serial.println("AT+CIPCLOSE");
}
else {
if (sending >= 5) {
reset();
lcd.setCursor(0, 0);
lcd.print("Reset");
delay(500);
lcd.clear();
sending = 0;
}
goto sendingAgain;
}
}
else {
lcd.setCursor(0, 0);
lcd.print("Mengirim ulang..");
delay(500);
lcd.clear();
goto sendingAgain;
}
endSend:
sameData = 0;
int zer = 0;
// httpget();
}
Screenshot if im able post data to website. [Please open image in new tab]
Screenshot if im able to get data from website. [Please open image in new tab]
Screenshot if im cannot get data from website. [Please open image in new tab]