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 ^_^