Hi,
I am trying to post some test data to emoncms, but no luck so far. Do any of you have working code for an esp32 or arduino post request? If yes can you share it, please? I am getting an “HTTP/1.1 302 Found” response.
here are the data from serial monitor and my code
WiFi connected
IP address:
192.168.1.96
connecting to emoncms.org
Requesting URL: /input/post.json?node=2&private_key=exxxxxxxxxxxxxxxxxx1b&json={temperature:1}
HTTP/1.1 302 Found
Date: Wed, 20 Jun 2018 16:44:44 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.25
Set-Cookie: PHPSESSID=av4f2o44ega1orqhgta4ikt8k4; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: http://emoncms.org//user/login
Vary: Accept-Encoding
Content-Length: 0
Connection: close
#include <WiFi.h>
const char* ssid = "xxxxxxxxxxx";
const char* password = "pxxxxxxxxxxxxxo";
const char* host = "emoncms.org";
const char* nodeId = "2";
const char* privateKey = "e3abxxxxxxxxxxxxxxxxx6ff1b";
void setup()
{
Serial.begin(115200);
delay(10);
//connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop()
{
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String json = "{temperature:";
json += value;
json += "}";
String url = "/input/post.json?node=";
url += nodeId;
url += "&private_key=";
url += privateKey;
url += "&json=";
url += json;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}