Advertisement
calebsan

Untitled

Apr 25th, 2024
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3.  
  4. // MAC address from Ethernet shield sticker under the shield
  5. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  6. IPAddress server(127, 0, 0, 1); // IP address of the server //webapp link
  7. int port = 8000; // Port of the server
  8.  
  9. EthernetClient client;
  10.  
  11. void setup() {
  12.   Serial.begin(9600);
  13.  
  14.   // start the Ethernet connection:
  15.   if (Ethernet.begin(mac) == 0) {
  16.     Serial.println("Failed to configure Ethernet using DHCP");
  17.     // no point in carrying on, so do nothing forevermore:
  18.     while (true);
  19.   }
  20.  
  21.   // give the Ethernet shield a second to initialize:
  22.   delay(1000);
  23. }
  24.  
  25. void loop() {
  26.  
  27. }
  28.  
  29. void sendData(){
  30.   if (client.connect(server, port)) {
  31.     Serial.println("Connected to server");
  32.  
  33.     // Create JSON payload
  34.     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}";
  35.  
  36.     // Make a POST request
  37.     client.println("POST /api/add HTTP/1.1");
  38.     client.println("Host: 127.0.0.1:8000");
  39.     client.println("Content-Type: application/json");
  40.     client.print("Content-Length: ");
  41.     client.println(payload.length());
  42.     client.println();
  43.     client.println(payload);
  44.  
  45.     delay(1000);
  46.  
  47.     // Read response from server
  48.     while (client.available()) {
  49.       char c = client.read();
  50.       Serial.print(c);
  51.     }
  52.  
  53.     // Disconnect from the server
  54.     client.stop();
  55.   } else {
  56.     // If connection fails, print an error message
  57.     Serial.println("Connection failed");
  58.   }
  59.  
  60.   // Wait for a while before sending the next request
  61.   delay(5000);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement