From bb98a06335f1d7f29885bed1fdb13fe2d479ed7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Princh=20Harold=20Can=CC=83al?= Date: Fri, 5 May 2023 17:51:45 +0800 Subject: [PATCH] refactor(hardware): clean up arduino code --- hardware/logistech/logistech.ino | 139 +++++++++++++++ logistechMOTHAFUCKA/logistechMOTHAFUCKA.ino | 187 -------------------- 2 files changed, 139 insertions(+), 187 deletions(-) create mode 100644 hardware/logistech/logistech.ino delete mode 100644 logistechMOTHAFUCKA/logistechMOTHAFUCKA.ino diff --git a/hardware/logistech/logistech.ino b/hardware/logistech/logistech.ino new file mode 100644 index 0000000..a796e50 --- /dev/null +++ b/hardware/logistech/logistech.ino @@ -0,0 +1,139 @@ +#include // for WiFi +#include +#include +#include +#include // for gps + +#define WIEGAND_DATA_0 D3 // Wiegand line pins +#define WIEGAND_DATA_1 D4 +#define MAX_CODES 100 // Maximum number of codes to store + +// Define the pins used for the GPS module +const int gps_rx_pin = 8; // GPS module TX pin to Node MCU D8 pin +const int gps_tx_pin = 7; // GPS module RX pin to Node MCU D7 pin + +// Create a SoftwareSerial object for the GPS module +SoftwareSerial gp_SS(gps_rx_pin, gps_tx_pin); + +static const uint32_t BAUD_RATE = 9600; + +// connecting to wifi +const char* ssid = "PLDTHOMEFIBR0dae0"; +const char* password = "PLDTWIFI44ffx"; + +WIEGAND wg; + +const String SERVER_NAME = "http://192.168.1.9:5001/api/v1"; +const String GPS_API_PATH = "/product-item/gps"; + +uint32_t codes[MAX_CODES]; // Array to store previous codes +int num_codes = 0; // Number of codes currently stored + +void setup() { + Serial.begin(BAUD_RATE); + + // setting up connection with the wifi + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.println("Connecting to WiFi..."); + + switch(WiFi.status()) { + case 6: + Serial.println("WRONG PASSWORD"); break; + case WL_DISCONNECTED: + Serial.println("DISCONNECTED"); break; + case WL_CONNECT_FAILED: + Serial.println("CONNECTION FAILED"); break; + case WL_IDLE_STATUS: + Serial.println("CHANGING STATUS"); break; + case WL_NO_SSID_AVAIL: + Serial.println("WIFI CANNOT BE REACHED"); break; + } + } + Serial.println("Connected to WiFi"); + Serial.println(WiFi.localIP()); + + + // once there is an internet connection.. set-up RFID reading + // default Wiegand Pin 3 and Pin 4 see image on README.md + // for non UNO board, use wg.begin(pinD0, pinD1) whokmijmijmere pinD0 and pinD1 + // are the pins connected to D0 and D1 of wiegand reader respectively. + // wg.begin(WIEGAND_DATA_0, WIEGAND_DATA_1, true, false); + pinMode(WIEGAND_DATA_0, INPUT_PULLUP); + pinMode(WIEGAND_DATA_1, INPUT_PULLUP); + wg.begin(WIEGAND_DATA_0, WIEGAND_DATA_1); + + // after setting-up RFID reader, set-up GPS module + gp_SS.begin(BAUD_RATE); // for gps +} + +void loop() { + if (wg.available()) { + uint32_t new_code = wg.getCode(); // Get the new Wiegand code + + // Check if the new code is unique + bool is_unique = is_code_unique(codes, new_code, num_codes); + + // Check WiFi connection status + if (WiFi.status() == WL_CONNECTED) { + // If the new code is unique, print it and store it in the codes array + if (is_unique) { + codes[num_codes++] = new_code; + send_post_request("{\"longitude\": 48.2082, \"latitude\": 16.3738, \"productItemId\": \"" + String (codes[num_codes]) + "\"}", GPS_API_PATH); + } + + Serial.print("New unique code: "); + Serial.println(new_code, HEX); + Serial.print("Total unique codes: "); + Serial.println(num_codes); + } else { + Serial.println("WiFi Disconnected"); + } + } + + // For GPS + if (gp_SS.available() > 0) { + char c = gp_SS.read(); // while acquiring signal, read the incoming byte + + static char buffer[80]; + static int i = 0; + buffer[i++] = c; // generating string + bool end_of_string = c == '\n'; + if (end_of_string) { + send_post_request("{\"longitude\": 48.2082, \"latitude\": 16.3738, \"productItemId\": \"" + String(buffer) + "\"}", GPS_API_PATH); + } + } +} + +bool is_code_unique(u_int32_t unique_codes[], u_int32_t new_code, int num_codes) { + for (int i = 0; i < num_codes; i++) { + if (new_code == unique_codes[i]) { + return false; + } + } + + return true; +} + +void send_post_request(String request_payload, String api_path) { + WiFiClient client; + HTTPClient http; + + http.begin(client, SERVER_NAME + api_path); + http.addHeader("Content-Type", "application/json"); + + int http_response_status_code = http.POST(request_payload); + + Serial.print("HTTP response status code: "); + Serial.println(http_response_status_code); + + if (http_response_status_code > 0) { + String response = http.getString(); + Serial.print("Response: "); + Serial.println(response); + } + + // Free resources + http.end(); +} diff --git a/logistechMOTHAFUCKA/logistechMOTHAFUCKA.ino b/logistechMOTHAFUCKA/logistechMOTHAFUCKA.ino deleted file mode 100644 index c4633b1..0000000 --- a/logistechMOTHAFUCKA/logistechMOTHAFUCKA.ino +++ /dev/null @@ -1,187 +0,0 @@ -//#include -#include //for WiFi -#include -#include -#include - - -#include //for gps - -#define WIEGAND_DATA_0 D3 // Wiegand line pins -#define WIEGAND_DATA_1 D4\ -#define MAX_CODES 100 // Maximum number of codes to store -// Define the pins used for the GPS module -const int gpsRxPin = 8; // GPS module TX pin to Node MCU D8 pin -const int gpsTxPin = 7; // GPS module RX pin to Node MCU D7 pin - -// Create a SoftwareSerial object for the GPS module -SoftwareSerial gpSS(gpsRxPin, gpsTxPin); - -static const uint32_t BaudRate = 9600; - -//connecting to wifi -const char* ssid = "PLDTHOMEFIBR0dae0"; -const char* password = "PLDTWIFI44ffx"; - -WIEGAND wg; - -String serverName = "http://192.168.1.9:5001/api/v1/gps/product-item"; - - -uint32_t codes[MAX_CODES]; // Array to store previous codes -int num_codes = 0; // Number of codes currently stored - -void setup() { - Serial.begin(BaudRate); - - //setting up connection with the wifi - WiFi.begin(ssid, password); - while (WiFi.status() != WL_CONNECTED) { - delay(1000); - Serial.println("Connecting to WiFi..."); - - if (WiFi.status() == 6){ - Serial.println("WRONG PASSWORD"); - } - else if (WiFi.status() == WL_DISCONNECTED){ - Serial.println("DISCONNECTED"); - } - else if (WiFi.status() == WL_CONNECT_FAILED){ - Serial.println("CONNECTION FAILED"); - } - else if (WiFi.status() == WL_IDLE_STATUS){ - Serial.println("CHANGING STATUS"); - } - else if (WiFi.status() == WL_NO_SSID_AVAIL){ - Serial.println("WIFI CANNOT BE REACHED"); - } - } - Serial.println("Connected to WiFi"); - Serial.println(WiFi.localIP()); - - - //once there is an internet connection.. set-up RFID reading - // default Wiegand Pin 3 and Pin 4 see image on README.md - // for non UNO board, use wg.begin(pinD0, pinD1) whokmijmijmere pinD0 and pinD1 - // are the pins connected to D0 and D1 of wiegand reader respectively. - //wg.begin(WIEGAND_DATA_0, WIEGAND_DATA_1, true, false); - pinMode(WIEGAND_DATA_0, INPUT_PULLUP); - pinMode(WIEGAND_DATA_1, INPUT_PULLUP); - wg.begin(WIEGAND_DATA_0, WIEGAND_DATA_1); - - //after setting-up RFID reader, set-up GPS module - gpSS.begin(BaudRate); //for gps -} - -void loop() { - if(wg.available()){ - uint32_t new_code = wg.getCode(); // Get the new Wiegand code - - // Check if the new code is unique - bool is_unique = true; - for(int i = 0; i < num_codes; i++){ - if(new_code == codes[i]){ - is_unique = false; - break; - } - } - // Check WiFi connection status - if(WiFi.status()== WL_CONNECTED){ - WiFiClient client; - HTTPClient http; - - // Your Domain name with URL path or IP address with path - http.begin(client, serverName); - - // If you need Node-RED/server authentication, insert user and password below - //http.setAuthorization("REPLACE_WITH_SERVER_USERNAME", "REPLACE_WITH_SERVER_PASSWORD"); - - http.addHeader("Content-Type", "application/json"); - // Send HTTP GET request - // If the new code is unique, print it and store it in the codes array - if(is_unique){ - codes[num_codes] = new_code; - int httpResponseCode = http.POST("{\"longitude\": 48.2082, \"latitude\": 16.3738, \"productItemId\": \"" + String (codes[num_codes]) + "\"}"); - - - if (httpResponseCode>0) { - Serial.print("HTTP Response code: "); - Serial.println(httpResponseCode); - String payload = http.getString(); - Serial.println(payload); - } - else { - Serial.print("Error code: "); - Serial.println(httpResponseCode); - } - // Free resources - http.end(); - num_codes++; - Serial.print("New unique code: "); - Serial.println(new_code, HEX); - Serial.print("Total unique codes: "); - Serial.println(num_codes); - } - else if(!is_unique){ - int httpResponseCode = http.POST("{\"longitude\": 48.2082, \"latitude\": 16.3738, \"productItemId\": \"" + String (new_code) + "\"}"); - - if (httpResponseCode>0) { - Serial.print("HTTP Response code: "); - Serial.println(httpResponseCode); - String payload = http.getString(); - Serial.println(payload); - } - else { - Serial.print("Error code: "); - Serial.println(httpResponseCode); - } - // Free resources - http.end(); - Serial.print("New generic code: "); - Serial.println(new_code, HEX); - Serial.print("Total unique codes: "); - Serial.println(num_codes); - } - }else { - Serial.println("WiFi Disconnected"); - } - } - - // For GPS - if (gpSS.available() > 0) { - char c = gpSS.read(); // while acquiring signal, read the incoming byte - - static char buffer[80]; - static int i = 0; - buffer[i++] = c; // generating string - bool end_of_string = c == '\n'; - if (end_of_string) { - WiFiClient client; - HTTPClient http; - - // Your Domain name with URL path or IP address with path - http.begin(client, serverName); - - // If you need Node-RED/server authentication, insert user and password below - //http.setAuthorization("REPLACE_WITH_SERVER_USERNAME", "REPLACE_WITH_SERVER_PASSWORD"); - - http.addHeader("Content-Type", "application/json"); - int httpResponseCode = http.POST("{\"longitude\": 48.2082, \"latitude\": 16.3738, \"productItemId\": \"" + String (buffer) + "\"}"); - - - if (httpResponseCode>0) { - Serial.print("HTTP Response code: "); - Serial.println(httpResponseCode); - String payload = http.getString(); - Serial.println(payload); - } - else { - Serial.print("Error code: "); - Serial.println(httpResponseCode); - } - // Free resources - http.end(); - // send_post_request("{\"longitude\": 48.2082, \"latitude\": 16.3738, \"productItemId\": \"" + String(buffer) + "\"}", "/api/v1/gps/product-item"); - } - } -}