Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Ethernet.h>
- // MAC address from Ethernet shield sticker under the shield
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- IPAddress server(127, 0, 0, 1); // IP address of the server //webapp link
- int port = 8000; // Port of the server
- EthernetClient client;
- void setup() {
- Serial.begin(9600);
- // start the Ethernet connection:
- if (Ethernet.begin(mac) == 0) {
- Serial.println("Failed to configure Ethernet using DHCP");
- // no point in carrying on, so do nothing forevermore:
- while (true);
- }
- // give the Ethernet shield a second to initialize:
- delay(1000);
- }
- void loop() {
- }
- void sendData(){
- if (client.connect(server, port)) {
- Serial.println("Connected to server");
- // Create JSON payload
- String payload = "{\"peewee_count\":10,\"pullet_count\":20,\"small_count\":30,\"medium_count\":40,\"large_count\":50,\"extra_large_count\":60,\"jumbo_count\":70,\"crack_count\":5}";
- // Make a POST request
- client.println("POST /api/add HTTP/1.1");
- client.println("Host: 127.0.0.1:8000");
- client.println("Content-Type: application/json");
- client.print("Content-Length: ");
- client.println(payload.length());
- client.println();
- client.println(payload);
- delay(1000);
- // Read response from server
- while (client.available()) {
- char c = client.read();
- Serial.print(c);
- }
- // Disconnect from the server
- client.stop();
- } else {
- // If connection fails, print an error message
- Serial.println("Connection failed");
- }
- // Wait for a while before sending the next request
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement