#include
#include
#include
#include
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 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
#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
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
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