user45012: I'm reading a digital pin and count how many times that pin is enable, after that the data is sent in JSON format. All is working well till here. But I want to introduce a value from webpage to arduino, so the arduino will know from what number it should count. I modified the webpage, I'm converting the value in JSON format, everything. But I can't get it on arduino. I see the value on Serial Monitor right before client disconnect. But I just cannot figure what I'm doing wrong, maybe you guys have a suggestion ?
Here is my Arduino code ( is a bit messy, and I haven't did the part were I tell to arduino the number where it should start to count, I will do that after i can get my numbers from webpage ). If needed i can also add the webpage code.
#include <SD.h>
#include <SPI.h>
#include <Ethernet2.h>
#include <ArduinoJson.h>

int pin = 3;
int state = 0;
//==========================
int push = 0;
//==========================
int laststate = 0;
const int chipS = 4;

byte mac[] = {0x00, 0x08, 0xDC, 0x1C, 0xB8, 0x4C}; // Enter the MAC address that is on your Ethernet shield (sticker) - eg. 00-08-DC-1C-B8-4C  (but use hexidecimal format eg. 0x00)
IPAddress ip(192, 168, 0, 88);                     // The IP address that you will give the Server - will depend on your local network range
EthernetServer server(8880);                       // The port to listen for Web Browsers - the default is 80, but some routers will block this port.. so change to 8081.

//*************************************************************************************************
// setup function
//=================================================================================================
void setup()
{
    Serial.begin(9600); // Begin Serial communication (baud rate = 9600).
    while (!Serial)
    {
        ; // wait for serial port to connect. Needed for Leonardo only
    }

    Ethernet.begin(mac, ip); // Initialise the ethernet library and network settings: https://www.arduino.cc/en/Reference/EthernetBegin
    server.begin();          // Tell the server to begin listening for incoming connections (on port 8081 in this example)
    Serial.print("Server IP address : ");
    Serial.println(Ethernet.localIP()); // If you see the IP address within the Serial monitor - you know that the server started successfully
    Serial.println(" ");

    // cardlaif  -  SD CARD INITALIZATION
    Serial.println("Initializare card...");
    if (!SD.begin(chipS))
    {
        Serial.println(" Initializarea cardului a esuat sau cardul lipseste ");
        while (1)
            ;
    }
    Serial.println(" Card initializat ");
    Serial.println(" ");
    pinMode(3, INPUT);
}

//*************************************************************************************************
// loop function
//=================================================================================================
void loop()
{
    //=====================================================================================================
    // contor  PIN 3 COUNTER
    //======================================================================================================
    state = digitalRead(pin);

    if (state != laststate)
    {
        if (state == HIGH)
        {
            push++;
        }
        delay(50);
    }
    laststate = state;

    Serial.print(" nr. citiri: ");
    Serial.println(push);
    //====================================================================================================
    // card SD CARD WRITE CODE
    //====================================================================================================
    String dataString = " ";
    dataString += String("nr. citiri: ");
    dataString += String(push);

    File dataFile = SD.open("datalog.txt", FILE_WRITE);
    if (dataFile)
    {
        dataFile.println(dataString);
        dataFile.close();
    }
    else
    {
        Serial.println("error opening datalog.txt");
    }
    //========================================================================================================
    //    net
    //========================================================================================================
    EthernetClient client = server.available(); // assign any newly connected Web Browsers to the "client" variable.

    if (client.connected())
    {
        Serial.println("Client Connected");
        // char c = client.read();
        // Serial.print(c);
        while (client.available())
        {
            Serial.write(client.read()); // Uncomment if you want to write the request from the Browser (CLIENT) to the SERIAL MONITOR (and comment out the next line)
            // client.read();                               // This line will clear the communication buffer between the client and the server.
        }

        // Send the Server response header back to the browser.
        client.println(F("HTTP/1.1 200 OK"));                                                                                           // This tells the browser that the request to provide data was accepted
                                                                                                                                        //    client.println("GET");
        client.println(F("Access-Control-Allow-Origin: *"));                                                                            // Tells the browser it has accepted its request for data from a different domain (origin).
        client.println(F("Access-Control-Allow-Methods:  GET,POST,PUT,DELETE,OPTIONS"));                                                // The arduino will accept JSON format data using this
        client.println(F("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With")); // The arduino will accept JSON format data using this
        client.println(F("Content-Type: application/json;charset=utf-8"));                                                              // Lets the browser know that the data will be in a JSON format
        client.println(F("Server: Arduino"));                                                                                           // The data is coming from an Arduino Web Server (this line can be omitted)
        client.println(F("Connection: close"));                                                                                         // Will close the connection at the end of data transmission.
        client.println();                                                                                                               // You need to include this blank line - it tells the browser that it has reached the end of the Server reponse header.

        // Example Transmission: [{"key":0, "value":300},{"key":1, "value":320},{"key":2, "value":143},{"key":3, "value":24},{"key":4, "value":760},{"key":5, "value":470}]
        client.print("[");            // This is tha starting bracket of the JSON data
        client.print("{\"value\": "); // The value I read on pin 3 and send in JSON format to webpage
        client.print(push);           //  The value I read on pin 3 and send in JSON format to webpage
        client.print("}");            //  JSON things
        client.println("]");          // This is the final bracket of the JSON data
    }

    //=================================================================================================
    // trying to print on serial json data
    //=================================================================================================
    char json[] = "{\"index\" }";
    StaticJsonBuffer<300> jsonBuffer;
    JsonObject &root = jsonBuffer.createObject();
    if (!root.success())
    {
        Serial.println("Parsing failed");
    }
    int index = root["index"];
    Serial.print(" index: ");
    Serial.print(index);
    Serial.println(" ");

    client.stop();                       // This method terminates the connection to the client
    Serial.println("Client has closed"); // Print the message to the Serial monitor to indicate that the client connection has closed.
} // VOID LOOP END

And this is what I get on serial monitor:
Client Connected
POST / HTTP/1.1
Host: 192.168.0.88:8880
Connection: keep-alive
Content-Length: 1
Accept: application/json, text/javascript, */*; q=0.01
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)     AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
Content-Type: application/json; charset=UTF-8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,ro;q=0.8,es;q=0.7

5 index: 0 

At the end, that 5 before the " index " word should be where the 0 is.
The 5 it's my JSON data. The reason why I see it it's because I enabled to see the reqest from the browser.
Someone have any idea ?
P.S Sorry for my bad english ^_^

user27653: Below, you create a static string, which is never used again.
char json[] = "{\"index\" }";

Here you create a buffer and instantiate an empty JsonObject.
StaticJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();

Here you check if root (your newly created JsonObject) is valid. Because it's empty and fresh, it is valid.
if (!root.success())
{
  Serial.println("Parsing failed");
}

Here, you try to ask for the value of index from your empty object.
int index = root["index"];
Serial.print(" index: ");
Serial.print(index);

If you never provide the JsonObject with any structure or content to parse, how can it give you anything back besides nothing?
The examples from the ArduinoJSON library prove to be helpful:
Documentation on parseObject

char json[] = "{\"hello\":\"world\"}";
StaticJsonBuffer<200> jsonBuffer;
JsonObject& object = jsonBuffer.parseObject(json);
const char* world = object["hello"];


Note the differences from what you are doing:

The initial JSON string to parse has both a key and a value.
The StaticJsonBuffer declaration includes a buffer size
The JsonObject declaration uses parseObject and passes in the json string, instead of creating a blank/empty object.


45012: <p>I'm reading a digital pin and count how many times that pin is enable, after that the data is sent in JSON format. All is working well till here. But I want to introduce a value from webpage to arduino, so the arduino will know from what number it should count. I modified the webpage, I'm converting the value in JSON format, everything. But I can't get it on arduino. I see the value on Serial Monitor right before client disconnect. But I just cannot figure what I'm doing wrong, maybe you guys have a suggestion ?</p>
<p>Here is my Arduino code ( is a bit messy, and I haven't did the part were I tell to arduino the number where it should start to count, I will do that after i can get my numbers from webpage ). If needed i can also add the webpage code.</p>
<pre><code>#include &lt;SD.h&gt;
#include &lt;SPI.h&gt;
#include &lt;Ethernet2.h&gt;
#include &lt;ArduinoJson.h&gt;

int pin = 3;
int state = 0;
//==========================
int push = 0;
//==========================
int laststate = 0;
const int chipS = 4;

byte mac[] = {0x00, 0x08, 0xDC, 0x1C, 0xB8, 0x4C}; // Enter the MAC address that is on your Ethernet shield (sticker) - eg. 00-08-DC-1C-B8-4C  (but use hexidecimal format eg. 0x00)
IPAddress ip(192, 168, 0, 88);                     // The IP address that you will give the Server - will depend on your local network range
EthernetServer server(8880);                       // The port to listen for Web Browsers - the default is 80, but some routers will block this port.. so change to 8081.

//*************************************************************************************************
// setup function
//=================================================================================================
void setup()
{
    Serial.begin(9600); // Begin Serial communication (baud rate = 9600).
    while (!Serial)
    {
        ; // wait for serial port to connect. Needed for Leonardo only
    }

    Ethernet.begin(mac, ip); // Initialise the ethernet library and network settings: https://www.arduino.cc/en/Reference/EthernetBegin
    server.begin();          // Tell the server to begin listening for incoming connections (on port 8081 in this example)
    Serial.print(&quot;Server IP address : &quot;);
    Serial.println(Ethernet.localIP()); // If you see the IP address within the Serial monitor - you know that the server started successfully
    Serial.println(&quot; &quot;);

    // cardlaif  -  SD CARD INITALIZATION
    Serial.println(&quot;Initializare card...&quot;);
    if (!SD.begin(chipS))
    {
        Serial.println(&quot; Initializarea cardului a esuat sau cardul lipseste &quot;);
        while (1)
            ;
    }
    Serial.println(&quot; Card initializat &quot;);
    Serial.println(&quot; &quot;);
    pinMode(3, INPUT);
}

//*************************************************************************************************
// loop function
//=================================================================================================
void loop()
{
    //=====================================================================================================
    // contor  PIN 3 COUNTER
    //======================================================================================================
    state = digitalRead(pin);

    if (state != laststate)
    {
        if (state == HIGH)
        {
            push++;
        }
        delay(50);
    }
    laststate = state;

    Serial.print(&quot; nr. citiri: &quot;);
    Serial.println(push);
    //====================================================================================================
    // card SD CARD WRITE CODE
    //====================================================================================================
    String dataString = &quot; &quot;;
    dataString += String(&quot;nr. citiri: &quot;);
    dataString += String(push);

    File dataFile = SD.open(&quot;datalog.txt&quot;, FILE_WRITE);
    if (dataFile)
    {
        dataFile.println(dataString);
        dataFile.close();
    }
    else
    {
        Serial.println(&quot;error opening datalog.txt&quot;);
    }
    //========================================================================================================
    //    net
    //========================================================================================================
    EthernetClient client = server.available(); // assign any newly connected Web Browsers to the &quot;client&quot; variable.

    if (client.connected())
    {
        Serial.println(&quot;Client Connected&quot;);
        // char c = client.read();
        // Serial.print(c);
        while (client.available())
        {
            Serial.write(client.read()); // Uncomment if you want to write the request from the Browser (CLIENT) to the SERIAL MONITOR (and comment out the next line)
            // client.read();                               // This line will clear the communication buffer between the client and the server.
        }

        // Send the Server response header back to the browser.
        client.println(F(&quot;HTTP/1.1 200 OK&quot;));                                                                                           // This tells the browser that the request to provide data was accepted
                                                                                                                                        //    client.println(&quot;GET&quot;);
        client.println(F(&quot;Access-Control-Allow-Origin: *&quot;));                                                                            // Tells the browser it has accepted its request for data from a different domain (origin).
        client.println(F(&quot;Access-Control-Allow-Methods:  GET,POST,PUT,DELETE,OPTIONS&quot;));                                                // The arduino will accept JSON format data using this
        client.println(F(&quot;Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With&quot;)); // The arduino will accept JSON format data using this
        client.println(F(&quot;Content-Type: application/json;charset=utf-8&quot;));                                                              // Lets the browser know that the data will be in a JSON format
        client.println(F(&quot;Server: Arduino&quot;));                                                                                           // The data is coming from an Arduino Web Server (this line can be omitted)
        client.println(F(&quot;Connection: close&quot;));                                                                                         // Will close the connection at the end of data transmission.
        client.println();                                                                                                               // You need to include this blank line - it tells the browser that it has reached the end of the Server reponse header.

        // Example Transmission: [{&quot;key&quot;:0, &quot;value&quot;:300},{&quot;key&quot;:1, &quot;value&quot;:320},{&quot;key&quot;:2, &quot;value&quot;:143},{&quot;key&quot;:3, &quot;value&quot;:24},{&quot;key&quot;:4, &quot;value&quot;:760},{&quot;key&quot;:5, &quot;value&quot;:470}]
        client.print(&quot;[&quot;);            // This is tha starting bracket of the JSON data
        client.print(&quot;{\&quot;value\&quot;: &quot;); // The value I read on pin 3 and send in JSON format to webpage
        client.print(push);           //  The value I read on pin 3 and send in JSON format to webpage
        client.print(&quot;}&quot;);            //  JSON things
        client.println(&quot;]&quot;);          // This is the final bracket of the JSON data
    }

    //=================================================================================================
    // trying to print on serial json data
    //=================================================================================================
    char json[] = &quot;{\&quot;index\&quot; }&quot;;
    StaticJsonBuffer&lt;300&gt; jsonBuffer;
    JsonObject &amp;root = jsonBuffer.createObject();
    if (!root.success())
    {
        Serial.println(&quot;Parsing failed&quot;);
    }
    int index = root[&quot;index&quot;];
    Serial.print(&quot; index: &quot;);
    Serial.print(index);
    Serial.println(&quot; &quot;);

    client.stop();                       // This method terminates the connection to the client
    Serial.println(&quot;Client has closed&quot;); // Print the message to the Serial monitor to indicate that the client connection has closed.
} // VOID LOOP END
</code></pre>
<p>And this is what I get on serial monitor:</p>
<pre><code>Client Connected
POST / HTTP/1.1
Host: 192.168.0.88:8880
Connection: keep-alive
Content-Length: 1
Accept: application/json, text/javascript, */*; q=0.01
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)     AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
Content-Type: application/json; charset=UTF-8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,ro;q=0.8,es;q=0.7

5 index: 0 
</code></pre>
<p>At the end, that 5 before the &quot; index &quot; word should be where the 0 is.
The 5 it's my JSON data. The reason why I see it it's because I enabled to see the reqest from the browser.</p>
<p>Someone have any idea ?</p>
<p>P.S Sorry for my bad english ^_^</p>
 27653: <p>Below, you create a static string, which is never used again.</p>

<pre><code>char json[] = "{\"index\" }";
</code></pre>

<p>Here you create a buffer and instantiate an empty JsonObject.</p>

<pre><code>StaticJsonBuffer jsonBuffer;
JsonObject&amp; root = jsonBuffer.createObject();
</code></pre>

<p>Here you check if <code>root</code> (your newly created JsonObject) is valid. Because it's empty and fresh, it is valid.</p>

<pre><code>if (!root.success())
{
  Serial.println("Parsing failed");
}
</code></pre>

<p>Here, you try to ask for the value of <code>index</code> from your empty object.</p>

<pre><code>int index = root["index"];
Serial.print(" index: ");
Serial.print(index);
</code></pre>

<p>If you never provide the <code>JsonObject</code> with any structure or content to parse, how can it give you anything back besides nothing?</p>

<p>The examples from the ArduinoJSON library prove to be helpful:</p>

<p><a href="https://arduinojson.org/api/jsonbuffer/parseobject/" rel="nofollow noreferrer">Documentation on <code>parseObject</code></a></p>

<blockquote>
<pre><code>char json[] = "{\"hello\":\"world\"}";
StaticJsonBuffer&lt;200&gt; jsonBuffer;
JsonObject&amp; object = jsonBuffer.parseObject(json);
const char* world = object["hello"];
</code></pre>
</blockquote>

<p>Note the differences from what you are doing:</p>

<ol>
<li>The initial JSON string to parse has both a key and a value.</li>
<li>The <code>StaticJsonBuffer</code> declaration includes a buffer size</li>
<li>The <code>JsonObject</code> declaration uses <code>parseObject</code> and passes in the <code>json</code> string, instead of creating a blank/empty object.</li>
</ol>
