jbagaskara900 / myfirstproject

my first repository about my first project
0 stars 0 forks source link

Problem: Parsing data with ArduinoJson #1

Open jbagaskara900 opened 4 years ago

jbagaskara900 commented 4 years ago

Hello! This is my first project repository and my first time using GitHub.

I am currently working on a project that uses Arduino Uno R3 and Ethernet Shield R3 to collect data from a server and de-nest the JSON data to make it send-able into a mySerial function to another device.

My problem is: I really wanted to fill the blank spaces nearby each parameter. I tried to use the parseObject and parseArray but still can't find anything. I kindly need

your advice and help. Thanks!

I am attaching my current source code and result photo.

jbagaskara900 commented 4 years ago

firstproject.zip This is the attachment

jbagaskara900 commented 4 years ago

This is my code...

include

include

include

include

const byte rxPin = 7; const byte txPin = 8; SoftwareSerial mySerial(rxPin, txPin); //These ports used as Rx, Tx for mySerial

//define param (open if needed) //char id_trafo[8]; //char tipe_produk[8]; //char degree[8]; //char speed[8]; //char fill[8]; //char diameter_prim[8]; //char length_prim[8]; //char n_wire_prim[8]; //char wire_conn_prim[8]; //char turn_prim[8]; //char diameter_sec[8]; //char length_sec[8]; //char turn_sec[8]; // Below is the MAC address for the designated ethernet shield

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server: IPAddress server(192,168,1,5); // numeric IP for server site (no DNS) //char server[] = "192.168.1.10"; // name address for server site (using DNS)

// Set the static IP address (if the DHCP fails to assign) IPAddress ip(192, 168, 1, 117); IPAddress myDns(192, 168, 1, 1);

// Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): EthernetClient client;

// Variables to measure the speed unsigned long beginMicros, endMicros; unsigned long byteCount = 0; bool printWebData = true; // set to false for better speed measurement

void setup() {

Serial.begin(9600); Serial.flush(); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } mySerial.begin(9600); mySerial.flush(); // initiating Ethernet connection: Serial.println("Initialize Ethernet with DHCP:"); if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // Check for Ethernet hardware if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); while (true) { delay(1); // do nothing here (cond:without eth) } } if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Ethernet cable is not connected."); } // try to congifure using IP address instead of DHCP: Ethernet.begin(mac, ip, myDns); } else { Serial.print(" DHCP assigned IP "); Serial.println(Ethernet.localIP()); } // give the Ethernet shield a second to initialize: delay(1000); Serial.print("connecting to "); Serial.print(server); Serial.println("...");

// if you get a connection, report back via serial: if (client.connect(server, 80)) { Serial.print("connected to "); Serial.println(client.remoteIP()); // Make a HTTP request: client.println("GET /server-trafo/api/proses/1"); client.println("HTTP/1.1"); client.println("Host: 192.168.1.5"); client.println("Connection: close"); client.println(); Serial.println(); while(client.connected()){ while(client.available()){ Serial.write(client.read()); //Read the next byte received from the server the client is connected to } } } else { // if you didn't get a connection to the server: Serial.println("connection failed"); } beginMicros = micros(); }

void loop() { // if there are incoming bytes available // from the server, read them and print them: int len = client.available(); if (len > 0) { byte buffer[80]; if (len > 80) len = 80; client.read(buffer, len); String payload = client.readString(); if (printWebData) { Serial.write(buffer, len); // show in the serial monitor (slows some boards) mySerial.write(buffer, len); // serially send out the data to the other board } byteCount = byteCount + len; } //parsing data

const size_t bufferSize = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) + 3*JSON_OBJECT_SIZE(13) + 610; DynamicJsonBuffer jsonBuffer(bufferSize);

JsonObject& root = jsonBuffer.parseObject(client.readString()); JsonArray& data = root["data"]; //Param JsonObject& data_0 = data[0]; const char data_0_id_trafo = data_0["id_trafo"]; // "1" const char data_0_tipe_produk = data_0["tipe_produk"]; // "Trafo C" const char data_0_degree = data_0["degree"]; // "25" const char data_0_speed = data_0["speed"]; // "17" const char data_0_fill = data_0["fill"]; // "8" const char data_0_diameter_prim = data_0["diameter_prim"]; // "30" const char data_0_length_prim = data_0["length_prim"]; // "27" const char data_0_n_wire_prim = data_0["n_wire_prim"]; // "55" const char data_0_wire_conn_prim = data_0["wire_conn_prim"]; // "10" const char data_0_turn_prim = data_0["turn_prim"]; // "127" const char data_0_diameter_sec = data_0["diameter_sec"]; // "40" const char data_0_length_sec = data_0["length_sec"]; // "65" const char* data_0_turn_sec = data_0["turn_sec"]; // "155"

//SerialMonitor Output Serial.print("ID Trafo = "); Serial.println(data_0_id_trafo); Serial.print("Tipe Trafo = "); Serial.println(data_0_tipe_produk); Serial.print("Derajad = "); Serial.println(data_0_degree); Serial.print("Kecepatan = "); Serial.println(data_0_speed); Serial.print("Fill = "); Serial.println(data_0_fill); Serial.print("Diameter Primer = "); Serial.println(data_0_diameter_prim); Serial.print("Panjang Kawat Primer = "); Serial.println(data_0_length_prim); Serial.print("Jumlah Kawat Primer = "); Serial.println(data_0_n_wire_prim); Serial.print("Kawat Sambung Primer = "); Serial.println(data_0_wire_conn_prim); Serial.print("Jumlah Kumparan Primer = "); Serial.println(data_0_turn_prim); Serial.print("Diameter Sekunder = "); Serial.println(data_0_diameter_sec); Serial.print("Panjang Kawat Sekunder = "); Serial.println(data_0_length_sec); Serial.print("Jumlah Kumparan Sekunder = "); Serial.println(data_0_turn_sec);

// if the server's disconnected, stop the client: if (!client.connected()) { endMicros = micros(); Serial.println(); Serial.println("disconnecting."); client.stop();

Serial.print("Received ");
Serial.print(byteCount);
Serial.print(" bytes in ");
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
Serial.print(seconds, 4);
float rate = (float)byteCount / seconds / 1000.0;
Serial.print(", rate = ");
Serial.print(rate);
Serial.print(" kbytes/second");
Serial.println();
client.flush();

// do nothing forevermore:
while (true) {
  delay(1);
}

} }