Repairing array of parsing Data to send the data into database via link

Hello everyone. I'm trying to build an IOT project about turbidity and temperature monitoring systems. when the data from the sensor are collected, the data will move to NodeMCU first. and then NodeMCU will parse the data into an array of data and each data will be put in the link so I can update the data in the database.

the problem now is the last data from the array of data has an unrecognized character which causes an error while sending the data via link.

here is the code that I wrote:

#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
//#include <WiFiClient.h>
//#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>

// buat variable untuk software serial (Rx, Tx)
//D6=12, D7=13
SoftwareSerial DataSerial(12, 13);

//millis sebagai pengganti delay
unsigned long previousMillis = 0;
const long interval = 3000;

String dataIn;
String dt[1];
int i;
boolean parsing = false;

//variable array untuk data parsing
String arrData[3];

//variable PIN LED
#define LED_PIN 4  // pin D2 nodeMCU

//konfigurasi WiFi
const char* ssid = "Impfl";
const char* password = "22222222222";

WiFiClient NodeMCU;

void setup() {
  Serial.begin(9600);
  DataSerial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
  dataIn = "";

  //koneksi WiFi
  WiFi.mode(WIFI_OFF);
  delay(1000);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.print("Connecting");

  //cek koneksi
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    digitalWrite(LED_PIN, LOW);
    Serial.print(".");
  }
  //apabila terkoneksi
  digitalWrite(LED_PIN, HIGH);
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // konfigurasi millis
  unsigned long currentMillis = millis();         // baca waktu millis saat ini
  if (currentMillis - previousMillis > interval)  //intinya kalau sudah lewat interval baru direfresh
  {
    //update previousMillis
    previousMillis = currentMillis;

    //prioritaskan pembacaan data dari aruino uno (hasil kirimdata)
    //baca data serial
    String data = "";

    while (DataSerial.available() > 0) {
      data += char(DataSerial.read());
    }
    //buang spasi datanya
    data.trim();
    //uji data
    if (data != "") ;
    {
      //format data "xx#xx" = array (setelah parsing)
      //parsing data (data pecah)
      int index = 0;
      for (int i = 0; i <= data.length(); i++) {
        char delimiter = '#';
        if (data[i] != delimiter)

          arrData[index] += data[i];
        else
          index++;
      }
      //pastikan data lengkap
      //urutannya 0=suhu, 1=kekeruhan
      //arrData[2].remove(' '); // Menghapus semua karakter spasi
      arrData[2].trim();
      if (index == 2) {
        //tampilkan nilai sensor ke serial monitor
        Serial.println(arrData[0]);  //Suhu
        Serial.println(arrData[1]);  //kekeruhan
        Serial.println(arrData[2]);  //kondisi
      }

      HTTPClient http;

      //WiFiClient client;

      String suhu, kekeruhan, getData, Link, httpCode, kondisi;

      suhu = arrData[0];
      kekeruhan = arrData[1];
      kondisi = arrData[2];

      getData = "Suhu=" + suhu + "&Kekeruhan=" + kekeruhan + "&Kondisi=" + kondisi;
      //getData = "?suhu=34&kelembaban=300";
      Link = "https://monitoringaquariumsilvi.000webhostapp.com/input.php?" + getData;
      //Link = "https://monitoringaquariumsilvi.000webhostapp.com/input.php?Suhu=34&Kekeruhan=300&Kondisi=aman";

      http.begin(NodeMCU, Link);

      http.GET();
      String payload = http.getString();

      //Serial.println(httpCode);
      Serial.println(payload);

      http.end();

      arrData[0] = "";
      arrData[1] = "";
      arrData[2] = "";
    }
    //minta data ke arduino uno
    DataSerial.println("Ya");
  }
}

here is the serial monitor display data:

image

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

thank you so much bro, I will repair my post again

I'm sorry bro, but I think in the code It has a semicolon there.

that terminate the if, the next {} runs regardless..

also if you really need https..
BasicHttpsClient
Should look more like the above demo..

good luck.. ~q

That is the point. It should not be there because it is the only code that depends on the outcome of the tes

okay, I'm sorry
anyway, I have removed the semicolon, but the unwanted character in the last data still there and causes the error

image

here I provide the code from arduino uno, maybe it'll help

#include <Fuzzy.h>
#include <OneWire.h>  //Memanggil library OneWire yang diperlukan sebagai dependensi library Dallas Temperature
#include <Wire.h>
#include <DallasTemperature.h>  // Memanggil library Dallas Temperature
#define ONE_WIRE_BUS 7          // Menempatkan PIN hasil pembacaan sensor DS18B20 pada PIN 7.

OneWire oneWire(ONE_WIRE_BUS);       //Membuat variabel oneWire berdasarkan PIN yang telah didefinisikan
DallasTemperature sensor(&oneWire);  //Membuat variabel untuk menyimpan hasil pengukuran
float suhuDS18B20;                   //deklarasi variable suhu DS18B20 dengan jenis data float
int sensorPin = A0;
float volt;
int ntu;

// Instantiating a Fuzzy object
Fuzzy *fuzzy = new Fuzzy();

//FuzzyInput Turbidity
FuzzySet *jernih = new FuzzySet(0, 0, 50, 150);
FuzzySet *agakkeruh = new FuzzySet(50, 150, 1300, 1500);
FuzzySet *keruh = new FuzzySet(1300, 1500, 1600, 1600);

//FuzzyInput Suhu
FuzzySet *dingin = new FuzzySet(0, 0, 15, 23);
FuzzySet *hangat = new FuzzySet(15, 23, 30, 35);
FuzzySet *panas = new FuzzySet(30, 35, 45, 45);

//FuzzyOutput Kondisi
FuzzySet *baik = new FuzzySet(0, 0, 50, 100);
FuzzySet *cukupbaik = new FuzzySet(50, 100, 150, 200);
FuzzySet *bahaya = new FuzzySet(150, 200, 255, 255);

void setup(void) {
  Serial.begin(9600);  //Menginisiasikan setup kecepatan komunikasi
  sensor.begin();      //Menginisiasikan sensor One-Wire DS18B20

  FuzzyInput *turbidity = new FuzzyInput(1);
  turbidity->addFuzzySet(jernih);
  turbidity->addFuzzySet(agakkeruh);
  turbidity->addFuzzySet(keruh);
  fuzzy->addFuzzyInput(turbidity);

  FuzzyInput *suhu = new FuzzyInput(2);
  suhu->addFuzzySet(dingin);
  suhu->addFuzzySet(hangat);
  suhu->addFuzzySet(panas);
  fuzzy->addFuzzyInput(suhu);

  FuzzyOutput *kondisi = new FuzzyOutput(1);
  kondisi->addFuzzySet(baik);
  kondisi->addFuzzySet(cukupbaik);
  kondisi->addFuzzySet(bahaya);
  fuzzy->addFuzzyOutput(kondisi);

  // Building FuzzyRule 1
  FuzzyRuleAntecedent *jernih_dingin = new FuzzyRuleAntecedent();
  jernih_dingin->joinWithAND(jernih, dingin);

  FuzzyRuleConsequent *kondisi_cukupbaik1 = new FuzzyRuleConsequent();
  kondisi_cukupbaik1->addOutput(cukupbaik);

  FuzzyRule *fuzzyRule01 = new FuzzyRule(1, jernih_dingin, kondisi_cukupbaik1);
  fuzzy->addFuzzyRule(fuzzyRule01);

  // Building FuzzyRule 2
  FuzzyRuleAntecedent *jernih_hangat = new FuzzyRuleAntecedent();
  jernih_hangat->joinWithAND(jernih, hangat);

  FuzzyRuleConsequent *kondisi_baik = new FuzzyRuleConsequent();
  kondisi_baik->addOutput(baik);

  FuzzyRule *fuzzyRule02 = new FuzzyRule(2, jernih_hangat, kondisi_baik);
  fuzzy->addFuzzyRule(fuzzyRule02);

  // Building FuzzyRule 3
  FuzzyRuleAntecedent *jernih_panas = new FuzzyRuleAntecedent();
  jernih_panas->joinWithAND(jernih, panas);

  FuzzyRuleConsequent *kondisi_bahaya1 = new FuzzyRuleConsequent();
  kondisi_bahaya1->addOutput(bahaya);

  FuzzyRule *fuzzyRule03 = new FuzzyRule(3, jernih_panas, kondisi_bahaya1);
  fuzzy->addFuzzyRule(fuzzyRule03);

  // Building FuzzyRule 4
  FuzzyRuleAntecedent *agakkeruh_dingin = new FuzzyRuleAntecedent();
  agakkeruh_dingin->joinWithAND(agakkeruh, dingin);

  FuzzyRuleConsequent *kondisi_cukupbaik2 = new FuzzyRuleConsequent();
  kondisi_cukupbaik2->addOutput(cukupbaik);

  FuzzyRule *fuzzyRule04 = new FuzzyRule(4, agakkeruh_dingin, kondisi_cukupbaik2);
  fuzzy->addFuzzyRule(fuzzyRule04);

  // Building FuzzyRule 5
  FuzzyRuleAntecedent *agakkeruh_hangat = new FuzzyRuleAntecedent();
  agakkeruh_hangat->joinWithAND(agakkeruh, hangat);

  FuzzyRuleConsequent *kondisi_cukupbaik3 = new FuzzyRuleConsequent();
  kondisi_cukupbaik3->addOutput(cukupbaik);

  FuzzyRule *fuzzyRule05 = new FuzzyRule(5, agakkeruh_hangat, kondisi_cukupbaik3);
  fuzzy->addFuzzyRule(fuzzyRule05);

  // Building FuzzyRule 6
  FuzzyRuleAntecedent *agakkeruh_panas = new FuzzyRuleAntecedent();
  agakkeruh_panas->joinWithAND(agakkeruh, panas);

  FuzzyRuleConsequent *kondisi_bahaya2 = new FuzzyRuleConsequent();
  kondisi_bahaya2->addOutput(bahaya);

  FuzzyRule *fuzzyRule06 = new FuzzyRule(6, agakkeruh_panas, kondisi_bahaya2);
  fuzzy->addFuzzyRule(fuzzyRule06);

  // Building FuzzyRule 7
  FuzzyRuleAntecedent *keruh_dingin = new FuzzyRuleAntecedent();
  keruh_dingin->joinWithAND(keruh, dingin);

  FuzzyRuleConsequent *kondisi_bahaya3 = new FuzzyRuleConsequent();
  kondisi_bahaya3->addOutput(bahaya);

  FuzzyRule *fuzzyRule07 = new FuzzyRule(7, keruh_dingin, kondisi_bahaya3);
  fuzzy->addFuzzyRule(fuzzyRule07);

  // Building FuzzyRule 8
  FuzzyRuleAntecedent *keruh_hangat = new FuzzyRuleAntecedent();
  keruh_hangat->joinWithAND(keruh, hangat);

  FuzzyRuleConsequent *kondisi_bahaya4 = new FuzzyRuleConsequent();
  kondisi_bahaya4->addOutput(bahaya);

  FuzzyRule *fuzzyRule08 = new FuzzyRule(8, keruh_hangat, kondisi_bahaya4);
  fuzzy->addFuzzyRule(fuzzyRule08);

  // Building FuzzyRule 9
  FuzzyRuleAntecedent *keruh_panas = new FuzzyRuleAntecedent();
  keruh_panas->joinWithAND(keruh, panas);

  FuzzyRuleConsequent *kondisi_bahaya5 = new FuzzyRuleConsequent();
  kondisi_bahaya5->addOutput(bahaya);

  FuzzyRule *fuzzyRule09 = new FuzzyRule(9, keruh_panas, kondisi_bahaya5);
  fuzzy->addFuzzyRule(fuzzyRule09);

  delay(1000);
}

void loop(void) {
  // baca permintaan dari NodeMCU
  String minta = "";
  // baca permintaan NodeMCU
  while (Serial.available() > 0) {
    minta += char(Serial.read());
  }
  // buang spasi data yang diterima
  minta.trim();
  // Uji variable minta
  if (minta == "Ya") {
    // jalankan kirimdata
    {
      kirimdata();
    }
  }

  // kosongkan variable minta
  minta = "";
  delay(1000);
}


float round_to_dp(float in_value, int decimal_place) {
  float multiplier = powf(10.0f, decimal_place);
  in_value = roundf(in_value * multiplier) / multiplier;
  return in_value;
}

void kirimdata() {
  //Sensor Suhu Dallas temperature ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //sensor.setResolution(9);  // Sebelum melakukan pengukuran, atur resolusinya
  sensor.setResolution(10);
  //sensor.setResolution(11);
  //sensor.setResolution(12);
  sensor.requestTemperatures();             // Perintah konversi suhu
  suhuDS18B20 = sensor.getTempCByIndex(0);  //Membaca data suhu dari sensor #0 dan mengkonversikannya ke nilai Celsius
  // suhuDS18B20 = (suhuDS18B20*9/5) + 32;
  // suhuDS18B20 = suhuDS18B20 = 273.15;
  // Sensor Kekeruhan Sen0189 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  volt = 0;
  for (int i = 0; i < 800; i++) {
    volt += ((float)analogRead(sensorPin) / 1024) * 5;
    //float voltage = analogRead(sensorPin) * (5.0 / 1024.0);
  }
  volt = volt / 800;
  volt = round_to_dp(volt, 2);
  if (volt < 2.5) {
    ntu = 3000;
  } else {
    ntu = -1103.2 * square(volt) + 5399.2 * (volt)-3592.2;
  }

  // Serial communication ////////////////////////////////////////////////////////////////////////////////////////////////////////

  // Set the random value as an input
  fuzzy->setInput(1, ntu);
  fuzzy->setInput(2, suhuDS18B20);
  fuzzy->fuzzify();

  // Running the Defuzzification
  int output = fuzzy->defuzzify(1);
  //analogWrite(pin_led, output);

  /*
  if (output >= 0 && output <= 49) {
    // Sediakan variable penampung data
    datakirim = String(suhuDS18B20, 2) + "#" + String(ntu) + "#" + String("Baik");
    }
  else if(output >= 50 && output <= 200){
    // Sediakan variable penampung data
    datakirim = String(suhuDS18B20, 2) + "#" + String(ntu) + "#" + String("sedang");
    }
  else (output >= 201 && output <= 255);{
    // Sediakan variable penampung data
    datakirim = String(suhuDS18B20, 2) + "#" + String(ntu) + "#" + String("aman");
    }
    */
  String datakirim;
  datakirim = String(suhuDS18B20, 2) + "#" + String(ntu) + "#" + String(output);

  // kirim data
  Serial.println(datakirim);

  // Tampilan serial Monitor ////////////////////////////////////////////////////////////////////////////////////////////////////
  /*
    //Serial.println(suhuDS18B20, 1);  //Presisi 1 digit
    Serial.print(suhuDS18B20, 2);  //Presisi 2 digit
    //Serial.println(suhuDS18B20, 3);  //Presisi 3 digit
    //Serial.println(suhuDS18B20, 4);  //Presisi 4 digit
    Serial.print(" ");
    Serial.print(output);
    Serial.print(" ");
    Serial.print(volt);
    Serial.print(" Volt ");
    Serial.print(ntu);
    Serial.println(" NTU");
    */
}

arrays are indexed starting at 0..
the above looks 1 char beyond..

change too..

for (int i = 0; i < data.length(); i++) 

good luck.. ~q

true that, just noticed @robby0211 has arrData defined to 3 chars..

~q

thank you so much, it really helps, now the unwanted character is gone, but the error for uploading is still there
image

now we on to the https.. :slight_smile:

ready??

~q

you don't need the cert.h..
go insecure..

~q

I'm sorry, what is that? is that a library?

look at the demo i posted a link to on post#6..
i knew it was coming..
~q

and now i see that as well..
it's an array of strings not chars..
lol, so use to not using string here, just assumed it was a char array..

idk, guess that parts working now, we're on to adding some bears into the sketch..
should be fairly easy, i think anyways, idk..

~q

I have checked the post, but I'm not really understand about jigsaw_host and jigsaw_port. and where should I put the link and the data?

forget jigsaw, that's related to the cert.h that was generated for it..
look at the comment just below setFingerprint, you're going to do that instead..
no need for cert.h..

~q

if you want..
post your revised code and i'll take a stab at it for you..
never used it before either, so we both learn..

~q

#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
//#include <WiFiClient.h>
//#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>

// buat variable untuk software serial (Rx, Tx)
//D6=12, D7=13
SoftwareSerial DataSerial(12, 13);

//millis sebagai pengganti delay
unsigned long previousMillis = 0;
const long interval = 3000;

String dataIn;
String dt[1];
int i;
boolean parsing = false;

//variable array untuk data parsing
String arrData[3];

//variable PIN LED
#define LED_PIN 4  // pin D2 nodeMCU

//konfigurasi WiFi
const char* ssid = "Impfl";
const char* password = "22222222222";

WiFiClient NodeMCU;

void setup() {
  Serial.begin(9600);
  DataSerial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
  dataIn = "";

  //koneksi WiFi
  WiFi.mode(WIFI_OFF);
  delay(1000);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.print("Connecting");

  //cek koneksi
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    digitalWrite(LED_PIN, LOW);
    Serial.print(".");
  }
  //apabila terkoneksi
  digitalWrite(LED_PIN, HIGH);
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // konfigurasi millis
  unsigned long currentMillis = millis();         // baca waktu millis saat ini
  if (currentMillis - previousMillis > interval)  //intinya kalau sudah lewat interval baru direfresh
  {
    //update previousMillis
    previousMillis = currentMillis;

    //prioritaskan pembacaan data dari aruino uno (hasil kirimdata)
    //baca data serial
    String data = "";

    while (DataSerial.available() > 0) 
    {
      data += char(DataSerial.read());
    }
    //buang spasi datanya
    data.trim();
    //uji data
    if (data != "")
    {
      //format data "xx#xx" = array (setelah parsing)
      //parsing data (data pecah)
      int index = 0;
      for (int i = 0; i < data.length(); i++) 
      {
        char delimiter = '#';
        if (data[i] != delimiter)

          arrData[index] += data[i];
        else
          index++;
      }
      //pastikan data lengkap
      //urutannya 0=suhu, 1=kekeruhan
      if (index == 2) 
      {
        //tampilkan nilai sensor ke serial monitor
        Serial.println(arrData[0]);  //Suhu
        Serial.println(arrData[1]);  //kekeruhan
        Serial.println(arrData[2]);  //kondisi
      }

      HTTPClient http;

      //WiFiClient client;

      String suhu, kekeruhan, getData, Link, httpCode, kondisi;

      suhu = arrData[0];
      kekeruhan = arrData[1];
      kondisi = arrData[2];

      getData = "Suhu=" + suhu + "&Kekeruhan=" + kekeruhan + "&Kondisi=" + kondisi;
      //getData = "?suhu=34&kelembaban=300";
      Link = "https://monitoringaquariumsilvi.000webhostapp.com/input.php?" + getData;
      //Link = "https://monitoringaquariumsilvi.000webhostapp.com/input.php?Suhu=34&Kekeruhan=300&Kondisi=aman";

      http.begin(NodeMCU, Link);

      http.GET();
      String payload = http.getString();

      //Serial.println(httpCode);
      Serial.println(payload);

      http.end();

      arrData[0] = "";
      arrData[1] = "";
      arrData[2] = "";
    }
    //minta data ke arduino uno
    DataSerial.println("Ya");
  }
}

here is my revised code. so I need to insert new library into the code? this one? WiFiClientSecureBearSSL.h