How to Send Serial Data from NodeMCU to MySQL database Only Once

Hello everyone
Here I am trying to send serial data from nodemcu to mysql database
And this is program that I use :

//include libraries
#include <HTTPClient.h>
#include <WiFi.h>

String dataIn;
String dt[10];
int i, berat, w_mati, w_nyala, kirimweb = 0;
boolean parsing = false;

int weight, dead_t, life_t; // Setting up variables
int delay_time = 3000; // Its mean 3 seconds wait to enter first record

//Access point credentials
const char* ssid = “myWifi”;
const char* pwd = “myPass”;
String add_data_url = “http://192.168.43.122/database2/add_data.php”;
WiFiServer server(80); // open port 80 for server connection

void setup()
{
Serial.begin(115200); //initialise the serial communication
delay(20);
WiFi.begin(ssid, pwd);

//starting the server
server.begin();
}

void loop()
{
if (Serial.available() > 0) {
kirimweb = 1;
char inChar = (char)Serial.read();
dataIn += inChar;
if (inChar == ‘\n’) {
parsing = true;
}
}

if (parsing) {
parsingData();
parsing = false;
dataIn = “”;
}

WiFiClient client = server.available();
weight = berat;
dead_t = w_mati;
life_t = w_nyala;

if (kirimweb == 1 && weight > 0 && dead_t > 0 && life_t > 0)
{
add_device_data(weight, dead_t, life_t);
kirimweb = 0;
weight = 0;
dead_t = 0;
life_t = 0;
}
// else
// {
// Serial.println(“Tidak Ada Data Masuk”);
// }
// delay(delay_time);
}

///This is the function that will get status of device that is On or Off
void add_device_data(int weight, int dead_t, int life_t)
{
WiFiClient client = server.available();
HTTPClient http;
String url = add_data_url + “?berat=” + weight + “&waktumati=” + dead_t + “&waktunyala=” + life_t;
http.begin(url);
//GET method
int httpCode = http.GET();
String payload = http.getString();
Serial.println(url);
Serial.println(payload);
http.end();
}

void parsingData() {
int j = 0;

//inisialisasi variabel, (reset isi variabel)
dt[j] = “”;
//proses parsing data
for (i = 1; i < dataIn.length(); i++) {
//pengecekan tiap karakter dengan karakter (#) dan (,)
if ((dataIn == ‘#’) || (dataIn == ‘,’))
* {*
* //increment variabel j, digunakan untuk merubah index array penampung*
* j++;*
* dt[j] = “”; //inisialisasi variabel array dt[j]*
* }*
* else*
* {*
* //proses tampung data saat pengecekan karakter selesai.*
_ dt[j] = dt[j] + dataIn*;
}
}
//kirim data hasil parsing*
* berat = dt[0].toInt();_
w_mati = dt[1].toInt();
w_nyala = dt[2].toInt();
_}*
Here NodeMCU receives data from Arduino Mega, then the data will be stored in the database
But with the program that I use, the data sent by the node to the database is continuous, whereas here I only need one data storage
So, please can someone help me with my problem?
*thank you and have a good day *_

When you send the data, set a flag. The next time through loop(), if the flag is set, skip sending data.

Read the how to use this forum-please read sticky to see how to post code properly.

  WiFiClient client = server.available();

Why? You never use the client object, nor do you even care if you get one.

But with the program that I use, the data sent by the node to the database is continuous, whereas here I only need one data storage

Why does the sender (the Mega) send the same data over and over? It seems more likely that you would want to address that issue.

Under what circumstances should you send data to the database again? Sending once is trivial. Just create a boolean, with global or static scope, initialized to true. Send the data only when the boolean is true. When you send the data, set the boolean to false.

Resetting the boolean when necessary is a different story.