#include <SPI.h>
#include <Ethernet.h>
#define TOKEN "token"
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(10, 3, 130, 160);
IPAddress myDns(10, 0, 80, 8);
EthernetClient client;
//char server[] = "www.arduino.cc"; // also change the Host line in httpRequest()
IPAddress server(10, 0, 50, 107);
unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 10*1000;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
Ethernet.begin(mac, ip, myDns);
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
delay(1000);
}
void loop() {
if (client.available()) {
char c = client.read();
Serial.write(c);
}
if (millis() - lastConnectionTime > postingInterval) {
httpPost();
}
}
void httpPost() {
client.stop();
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
client.println("POST /admin/inverters/1 HTTP/1.1");
Serial.println("POST /admin/inverters/1 HTTP/1.1");
client.println("Host: 10.0.50.107");
Serial.println("Host: 10.0.50.107");
client.println("User-Agent: python-request/2.21.0");
Serial.println("User-Agent: python-requests3/1.8");
client.println("Accept-Encoding: gzip, deflate");
Serial.println("Accept-Encoding: gzip, deflate");
client.println("Accept: */*");
Serial.println("Accept: */*");
client.println("Connection: keep-alive");
Serial.println("Connection: keep-alive");
client.println("Authorization: Bearer token");
Serial.println("Authorization: Bearer token");
client.println("Content-Type: application/json");
Serial.println("Content-Type: application/json");
String out="{\"client_date\": \"2021-09-13 12:22:00\", \"ac_voltage\": 100, \"ac_current\": 600, \"voltage_1\": 50, \"current_1\": 67, \"voltage_2\": 50, \"current_2\": 67}";
//Serial.println(out.length());
client.println("Content-Length: 144");
Serial.println("Content-Length: 144");
client.println(out);
Serial.println(out);
client.println("Connection: close");
Serial.println("Connection: close");
client.println();
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
it is solar system data sent to api server /laravel/. but always 400 bad requests. how discuss is it