Hi guys! First of all I want to say that I've managed to insert some data readings from my light sensor,arduino mega and an ethernet shield with a GET request and a php script.
This is my arduino code by far :
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);
int photocellPin = 0;
int photocellReading;
char server[] = "192.168.1.116";
EthernetClient client;
void setup() {
Serial.begin(9600);
if(Ethernet.begin(mac)==0){
Serial.println("Failed to configure Ethernet using DHCP");
}
dht.begin();
delay(10000);
// start the Ethernet connection
Ethernet.begin(mac, ip);
}
void loop() {
}
photocellReading = analogRead(photocellPin);
if (client.connect(server, 80)) {
Serial.println("you have connected " );
client.print("GET /write_data.php?");
Serial.println(" get has been made " ) ;
client.print("value="); // This
client.print(photocellReading);
client.println(" HTTP/1.1"); // Part of the GET request
client.println("Host: 192.168.1.116");
client.println("Connection: close");
client.println(); // Empty line
client.println(); // Empty line
client.stop(); // Closing connection to server
}
else {
Serial.println("--> connection failed\n");
}
delay(10000);
}
But my teacher wants me to implement a POST request with a JSON body and use of the JAVA Spring framework.
This is the part of my Controller class .
@RequestMapping(value="/light/save",method = RequestMethod.POST)
public Light save(@RequestBody Light light) {
return lightService.saveLight(light);
}
My URL si : localhost:8081/light/save
So as a conclusion I want to know what should I change in the Arduino code above as I want to make use of my controller ? I forgot to say that I am working with Tomcat Server and Eclipse right now . Before this implementation I worked with XAMPP and phpmyadmin .
Thanks in advance