Aunaaa

Nodemcukirim data

Apr 18th, 2019
198
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266HTTPClient.h>
  3. WiFiClient client;
  4. #include <SPI.h>
  5. #include <MFRC522.h>
  6. #define SS_PIN 4
  7. #define RST_PIN 5
  8. MFRC522 rfid(SS_PIN, RST_PIN);
  9. MFRC522::MIFARE_Key key;
  10. String request_string;
  11. const char* host = “192.168.1.9”;
  12. HTTPClient http;
  13. void setup() {
  14. // put your setup code here, to run once:
  15. WiFi.disconnect();
  16. WiFi.begin(“KOST_RAMA”,”bayardulu500″);
  17. while ((!(WiFi.status() == WL_CONNECTED))){
  18. delay(300);
  19. }
  20. Serial.begin(9600);
  21. SPI.begin();
  22. rfid.PCD_Init();
  23. Serial.println(“I am waiting for card…”);
  24. }
  25. String strID,val;
  26. void loop() {
  27. // put your main code here, to run repeatedly:
  28. if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial())
  29. return;
  30. // Serial.print(F(“PICC type: “));
  31. MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  32. // Serial.println(rfid.PICC_GetTypeName(piccType));
  33. // Check is the PICC of Classic MIFARE type
  34. if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
  35. piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  36. piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  37. Serial.println(F(“Your tag is not of type MIFARE Classic.”));
  38. return;
  39. }
  40. //id kartu dan yang akan dikirim ke database
  41. strID = “”;
  42. for (byte i = 0; i < 4; i++) {
  43. strID +=
  44. (rfid.uid.uidByte[i] < 0x10 ?0: “”) +
  45. String(rfid.uid.uidByte[i], HEX) +
  46. (i != 3 ?:: “”);
  47. }
  48.  
  49. strID.toUpperCase();
  50. Serial.print(“Tap card key:);
  51. Serial.println(strID);
  52. val =500; // nilai kartu yang akan dikirim
  53.  
  54. koneksi_database();
  55. delay(1000);
  56. }
  57. void koneksi_database()
  58. {
  59. if (!client.connect(host,80)) {
  60. Serial.println(“Gagal Konek”);
  61. return;
  62. }
  63. request_string =/esp8266/index.php?data1=;
  64. request_string += strID;
  65. request_string +=&data2=;
  66. request_string += val;
  67. Serial.print(“requesting URL:);
  68. Serial.println(request_string);
  69. client.print(String(“GET “) + request_string + “HTTP/1.1rn” + “Host:+ host + “rn” + “Connection: closernrn”);
  70.  
  71. unsigned long timeout = millis();
  72. while (client.available() == 0) {
  73. if (millis() – timeout > 5000) {
  74. Serial.println(>>> Client Timeout !);
  75. client.stop();
  76. return;
  77. }
  78. }
  79. }
RAW Paste Data