|
| 1 | +#include <ESP8266WiFi.h> // for WiFi |
| 2 | +#include <Wiegand.h> |
| 3 | +#include <WiFiClient.h> |
| 4 | +#include <ESP8266HTTPClient.h> |
| 5 | + |
| 6 | + |
| 7 | +#include <SoftwareSerial.h> // for gps |
| 8 | + |
| 9 | +#define WIEGAND_DATA_0 D3 // Wiegand line pins |
| 10 | +#define WIEGAND_DATA_1 D4 |
| 11 | +#define MAX_CODES 100 // Maximum number of codes to store |
| 12 | +// Define the pins used for the GPS module |
| 13 | +const int gpsRxPin = 8; // GPS module TX pin to Node MCU D8 pin |
| 14 | +const int gpsTxPin = 7; // GPS module RX pin to Node MCU D7 pin |
| 15 | + |
| 16 | +// Create a SoftwareSerial object for the GPS module |
| 17 | +SoftwareSerial gpSS(gpsRxPin, gpsTxPin); |
| 18 | + |
| 19 | +static const uint32_t BaudRate = 9600; |
| 20 | + |
| 21 | +// connecting to wifi |
| 22 | +const char* ssid = "PLDTHOMEFIBR0dae0"; |
| 23 | +const char* password = "PLDTWIFI44ffx"; |
| 24 | + |
| 25 | +WIEGAND wg; |
| 26 | + |
| 27 | +String server_name = "http://192.168.1.9:5001"; |
| 28 | + |
| 29 | +uint32_t codes[MAX_CODES]; // Array to store previous codes |
| 30 | +int num_codes = 0; // Number of codes currently stored |
| 31 | + |
| 32 | +void setup() { |
| 33 | + Serial.begin(BaudRate); |
| 34 | + |
| 35 | + // setting up connection with the wifi |
| 36 | + WiFi.begin(ssid, password); |
| 37 | + while (WiFi.status() != WL_CONNECTED) { |
| 38 | + delay(1000); |
| 39 | + Serial.println("Connecting to WiFi..."); |
| 40 | + |
| 41 | + switch(Wifi.status()) { |
| 42 | + case 6: |
| 43 | + Serial.println("WRONG PASSWORD"); break; |
| 44 | + case WL_DISCONNECTED: |
| 45 | + Serial.println("DISCONNECTED"); break; |
| 46 | + case WL_CONNECT_FAILED: |
| 47 | + Serial.println("CONNECTION FAILED"); break; |
| 48 | + case WL_IDLE_STATUS: |
| 49 | + Serial.println("CHANGING STATUS"); break; |
| 50 | + case WL_NO_SSID_AVAIL: |
| 51 | + Serial.println("WIFI CANNOT BE REACHED"); break; |
| 52 | + } |
| 53 | + } |
| 54 | + Serial.println("Connected to WiFi"); |
| 55 | + Serial.println(WiFi.localIP()); |
| 56 | + |
| 57 | + |
| 58 | + // once there is an internet connection.. set-up RFID reading |
| 59 | + // default Wiegand Pin 3 and Pin 4 see image on README.md |
| 60 | + // for non UNO board, use wg.begin(pinD0, pinD1) whokmijmijmere pinD0 and pinD1 |
| 61 | + // are the pins connected to D0 and D1 of wiegand reader respectively. |
| 62 | + // wg.begin(WIEGAND_DATA_0, WIEGAND_DATA_1, true, false); |
| 63 | + pinMode(WIEGAND_DATA_0, INPUT_PULLUP); |
| 64 | + pinMode(WIEGAND_DATA_1, INPUT_PULLUP); |
| 65 | + wg.begin(WIEGAND_DATA_0, WIEGAND_DATA_1); |
| 66 | + |
| 67 | + // after setting-up RFID reader, set-up GPS module |
| 68 | + gpSS.begin(BaudRate); // for gps |
| 69 | +} |
| 70 | + |
| 71 | +void loop() { |
| 72 | + if(wg.available()) { |
| 73 | + uint32_t new_code = wg.getCode(); // Get the new Wiegand code |
| 74 | + |
| 75 | + // Check if the new code is unique |
| 76 | + bool is_unique = is_code_unique(codes, new_code, num_codes); |
| 77 | + |
| 78 | + // Check WiFi connection status |
| 79 | + if (WiFi.status() == WL_CONNECTED) { |
| 80 | + // If the new code is unique, print it and store it in the codes array |
| 81 | + if (is_unique) { |
| 82 | + codes[num_codes++] = new_code; |
| 83 | + send_post_request("{\"longitude\": 48.2082, \"latitude\": 16.3738, \"productItemId\": \"" + String (codes[num_codes]) + "\"}", "/api/v1/gps/product-item"); |
| 84 | + } else { |
| 85 | + send_post_request("{\"longitude\": 48.2082, \"latitude\": 16.3738, \"productItemId\": \"" + String (codes[num_codes]) + "\"}", "/api/v1/gps/product-item"); |
| 86 | + } |
| 87 | + |
| 88 | + Serial.print("New unique code: "); |
| 89 | + Serial.println(new_code, HEX); |
| 90 | + Serial.print("Total unique codes: "); |
| 91 | + Serial.println(num_codes); |
| 92 | + } else { |
| 93 | + Serial.println("WiFi Disconnected"); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // For GPS |
| 98 | + if (gpSS.available() > 0) { |
| 99 | + char c = gpSS.read(); // while acquiring signal, read the incoming byte |
| 100 | + |
| 101 | + static char buffer[80]; |
| 102 | + static int i = 0; |
| 103 | + buffer[i++] = c; // generating string |
| 104 | + bool end_of_string = c == '\n'; |
| 105 | + if (end_of_string) { |
| 106 | + send_post_request("{\"longitude\": 48.2082, \"latitude\": 16.3738, \"productItemId\": \"" + String(buffer) + "\"}", "/api/v1/gps/product-item"); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +bool is_code_unique(u_int32_t unique_codes[], u_int32_t new_code, int num_codes) { |
| 112 | + for (int i = 0; i < num_codes; i++) { |
| 113 | + if (new_code == unique_codes[i]) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return true; |
| 119 | +} |
| 120 | + |
| 121 | +void send_post_request(String request_payload, String api_path) { |
| 122 | + WiFiClient client; |
| 123 | + HTTPClient http; |
| 124 | + |
| 125 | + http.begin(client, server_name + api_path); |
| 126 | + http.addHeader("Content-Type", "application/json"); |
| 127 | + |
| 128 | + int http_response_status_code = http.POST(request_payload); |
| 129 | + |
| 130 | + Serial.print("HTTP response status code: "); |
| 131 | + Serial.println(http_response_status_code); |
| 132 | + |
| 133 | + if (http_response_status_code > 0) { |
| 134 | + String response = http.getString(); |
| 135 | + Serial.print("Response: "); |
| 136 | + Serial.println(response); |
| 137 | + } |
| 138 | + |
| 139 | + // Free resources |
| 140 | + http.end(); |
| 141 | +} |
0 commit comments