0

So Ive setup the emoncms on my server and posted the example power1:100...power3:300 JSON POST and it logged in my INPUTS. Now Im trying to send data from my arduino and it seems to post fine, but when I go to the INPUTS its not there for some reason.

Here is my code. Im just posting a temperature value to a new node:

#include "SoftwareSerial.h"
//For DHT22 Grove Pro
#include "DHT.h"
#define DHTPIN A1 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
String ssid ="myssid";
String password="mypassword";
String server = "www.santiapps.com"; // www.example.com
String uri = "/emoncms/input/post.json";// our example is /esppost.php
#define EMON_APIKEY F("myapikey")
byte dat [5];
String temp ,hum;
String data;
char dataString[200];
SoftwareSerial esp(6, 7);// RX, TX
void setup() {
 esp.begin(9600);
 Serial.begin(9600);
 Serial.println("setup");
 reset();
 connectWifi();
}
void reset() {
 Serial.println("reset");
 esp.println("AT+RST");
 delay(1000);
 if(esp.find("OK") ) Serial.println("Module Reset");
}
void connectWifi() {
 Serial.println("connect to wifi"); 
 String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
 esp.println(cmd);
 delay(400);
 while (esp.available()){
 String inData = esp.readStringUntil('\n');
 Serial.println("Got reponse from ESP8266: " + inData);
 }
 if(esp.find("OK")) {
 Serial.println("Connected!");
 } else {
 connectWifi();
 Serial.println("Cannot connect to wifi"); }
}
void start_test () {
 //For DHT22 Grove Pro
 static char outstr1[15];
 static char outstr2[15];
 float h = dht.readHumidity();
 float t = dht.readTemperature();
 dtostrf(t, 8, 2, dataString);
 Serial.println(dataString);
}
void loop(){
 Serial.println("loop");
 start_test();
 httppost();
 delay(100000);
}
void httppost () {
 esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
 if( esp.find("OK")) {
 Serial.println("TCP connection ready");
 } 
 delay(1000);
 String postRequest="POST " + uri + " HTTP/1.0\r\n"+"Host: " + server + "\r\n" + "Accept: *" + "/" + "*\r\n"+"Content-Length: " + strlen(dataString) + "\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n"+"\r\n" + "&node=fortnite&data={a:" + dataString + "}" + "&apikey=" + EMON_APIKEY;
 Serial.println(postRequest);
 String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
 esp.print(sendCmd);
 esp.println(postRequest.length());
 delay(500);
 if(esp.find(">")) { 
 Serial.println("Sending.."); 
 esp.print(postRequest);
 if(esp.find("SEND OK")) { 
 Serial.println("Packet sent");
 while (esp.available()) {
 String tmpResp = esp.readString();
 Serial.println(tmpResp);
 }
 // close the connection
 esp.println("AT+CIPCLOSE");
 }
 }
}

Here is my Serial Monitor:

setup
reset
connect to wifi
Got reponse from ESP8266: AT+CWsetup
reset
Module Reset
connect to wifi
Got reponse from ESP8266:
Got reponse from ESP8266: CLOSED
Got reponse from ESP8266: WIFI DISCONNECT
Got reponse from ESP8266: `BԄQR���ȤRN�ɤRN�H��OAT+CWJAP="tha
connect to wifi
Got reponse from ESP8266: %J:E
Got reponse from ESP8266: HըRP="myssid","mypassword"
Got reponse from ESP8266: busy p...
connect to wifi
Got reponse from ESP8266: AT+CWJAP="myssid","mypassword"
Got reponse from ESP8266: busy p...
Got reponse from ESP8266:
Got reponse from ESP8266: O
connect to wifi
Got reponse from ESP8266: AT+CWJAP="myssid","mypassword"
Got reponse from ESP8266: WIFI DISCONNEC
Connected!
Cannot connect to wifi
Cannot connect to wifi
Cannot connect to wifi
loop
31.00
TCP connection ready
POST /emoncms/input/post.json HTTP/1.0
Host: www.myserver.com
Accept: /
Content-Length: 8
Content-Type: application/x-www-form-urlencoded
 &node=fortnite&data={a: 31.00}&apikey=mykey
 Sending..
 Packet sent
 +IPD,422:HTTP/1.1 200 OK
 Server: nginx
 Date: 2018年10月02日 21:38:18 GMT
 Content-Type: application/json
 Connection: close
 X-Powered-By: PHP/5.4.16
 Expires: 1981年11月19日 08:52:00 GMT
 Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
 Pragm

Here is a snapshot of the posted data when I used this URL directly in my browser: My emoncms http://santiapps.com/emoncms/input/post?node=emontx&fulljson={"power1":100,"power2":200,"power3":300}&apikey=mykey

=========

Based on jose's suggestion:

I changed this code to:

void httppost () {
 esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
 if( esp.find("OK")) {
 Serial.println("TCP connection ready");
 }
// //CODE USED TO BUILD COMPLETE STRING
 String apiKeyString="apikey="; 
 apiKeyString.concat(EMON_APIKEY); //JOIN PARAMETER LITERAL STRING => String Object
 //Serial.println(apiKeyString); //THIS IS NOW A STRING OBJECT
 String nodeData="&node=fortnite&data={\"a\":";
 String combinado=apiKeyString+nodeData; //COMBINE BOTH PREVIOUS STRING OBJECTS
 String finalData=combinado + dataString + "}"; //COMBINE STRING OBJECT WITH CHAR[]
 Serial.println("finalData");
 Serial.println(finalData); 
 //unsigned int tamano = strlen(dataString);
// char postThis[100] = finalData;
// //ADDING THIS TO POSTREQUEST +finalData; && changing length for strlen(finalData)
 delay(1000);
 String postRequest="POST " + uri + " HTTP/1.1\r\n"+"Host: " + server + "\r\n" + "Accept: *" + "/" + "*\r\n"+"Content-Length: " + finalData.length() + "\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n"+"\r\n" + finalData;
 Serial.println("postRequest");
 Serial.println(postRequest);
 String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.

but now I dont even get the HTTP200OK response. The result is:

loop
31.30
finalData
apikey=myKey&node=fortnite&data={"a":31.30}
postRequest
asked Oct 3, 2018 at 0:04

1 Answer 1

2

Your Serial monitor output shows the content of the POST as:

&node=fortnite&data={a: 31.00}&apikey=mykey

I can't tell if the & are actually in your serial monitor, or if your copy/paste did some kind of HTML format attempt, but & is wrong and should be &

Your JSON data is not proper JSON

{a: 31.00}

should be

{"a": 31.00}


Strangely, your serial monitor output also writes Host: www.myserver.com, which doesn't match your code, where server was assigned www.santiapps.com, so that makes it difficult to trust that the serial output you show actually came from the code.

answered Oct 3, 2018 at 15:18
5
  • Ok I fixed the & format issue, it was indeed copy/paste problem. It is the proper output, I just went through it and replaced ssid, password, apikey and server name. Everything else is the same. How do I get the "" around the letter a? Im trying this: "&node=fortnite&data={" + "a:" + dataString + "}"; but I get this => &node=fortnite&data={a:29.00} Commented Oct 3, 2018 at 16:55
  • You can escape the quote character with a backslash: "&node=fortnite&data={" + "\"a\":" + dataString + "}" Commented Oct 3, 2018 at 17:40
  • Thanks, one last thing, it was brought to my attention that my Content Length for the POST request needs to be the length of everything from &node....up until the json data...{"a":29.40}. Could that be the cause of the post not working? Commented Oct 3, 2018 at 18:56
  • You send a content-length generated by strlen(dataString). This is not quite right. The HTTP Post payload portion is found in this part of your code: &node=fortnite&data={a:" + dataString + "}" + "&apikey=" + EMON_APIKEY. Calculate the length of that to find the content-length. I also noticed that you start your POST content with &, but you don't need one at the beginning. It is just to separate fields, like node=fortnite&data={a:" + dataString + "}" + "&apikey=" + EMON_APIKEY Commented Oct 3, 2018 at 19:24
  • I added my most recent try but it also fails to post. Commented Oct 3, 2018 at 20:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.