Community
OpenEnergyMonitor

Community

Data posted to emoncms not logged into INPUTS

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="mypwd";
String server = "santiapps.com"; // www.example.com
String uri = "/emoncms/input/post";
#define EMON_APIKEY F("mykey")

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");
  //esp.print("AT+UART_DEF=9600,8,1,0,0");
  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
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  dtostrf(t, 4, 2, dataString);  //convert flat to char
  Serial.println(dataString);
}
void loop(){
  Serial.println("loop");
  start_test();
  httppost();
  delay(100000);
}

/* A POST contains a header block and then a single line of the data values, something like this:
 * 
 * POST /emoncms/input/post.json HTTP/1.1\r\n
 * Host: URL_OF_THE_SERVER\r\n
 * Content-Type: application/x-www-form-urlencoded\r\n
 * Content-Length: 100\r\n
 * \r\n
 * apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&node=xx&csv=xx,xx,xx,xx\r\n
 * 
 * Most of the content is the same every time and simply hardcoded in here with print() direct to the 
 * ethernet stream client. Two things change every time, the data themselves and the length of the
 * string containing the data. 
 * 
 * The fixed strings are all written into the flash program space with F("") to prevent them filling
 * runtime RAM.
 */

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 nodeData="node=fortnite&data={\"a\":";
  String nodeWithJson=nodeData + dataString + "}"; //COMBINE STRING OBJECT WITH CHAR[]

  String apiKeyString="&apikey="; 
  apiKeyString.concat(EMON_APIKEY); //JOIN PARAMETER LITERAL STRING => String Object
  //Serial.println(apiKeyString); //THIS IS NOW A STRING OBJECT
  
  String finalData=nodeWithJson+apiKeyString; //COMBINE BOTH PREVIOUS STRING OBJECTS
  Serial.println("finalData");
  Serial.println(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.
  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
Module Reset
connect to wifi
Got reponse from ESP8266: 
Got reponse from ESP8266: WIFI DISCONNECT
Got reponse from ESP8266: bBֆQR���ȤRN�ȤRN�H��O��d#D�M�AT+CWJAP="tha
connect to wifi
Got reponse from ESP8266: WIF�=QJ���JAP="myssid","mypwd"
Got reponse from ESP8266: busy p..
Connected!
Cannot connect to wifi
loop
28.00
TCP connection ready
finalData
node=fortnite&data={"a":28.00}&apikey=mykey
postRequest
Sending..
loop
28.00
finalData
node=fortnite&data={"a":28.00}&apikey=mykey
postRequest
loop
28.00
TCP connection ready
finalData
node=fortnite&data={"a":28.00}&apikey=mykey
postRequest
Sending..

When I do it this way nothing new posts in my server. The data that is in my server I posted using this example link directly in the browser:

http://santiapps.com/emoncms/input/post?node=emontx&fulljson={“power1”:100,“power2”:200,“power3”:300}&apikey=mykey