EtherCard icon indicating copy to clipboard operation
EtherCard copied to clipboard

httpPost body parameter not working

Open xujaan opened this issue 5 years ago • 1 comments

Hi! I'm trying to post my data with ether.httpPost but still not working, before that i try using stash and no response at all on my server. When i use httpPost i see no data on body post request. This is my code

#include <EtherCard.h>
#include <Filters.h>
#include <Wire.h>
#include "EmonLib.h"

EnergyMonitor emon1, emon2, emon3;
double power1, power2, power3;
double Irms1, Irms2, Irms3;

#define REQUEST_RATE 5000 // milliseconds

// ethernet interface mac address
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
// ethernet interface ip address
static byte myip[] = { 192, 168, 1, 5 };
// ethernet interface ip netmask
static byte mask[] = { 255, 255, 255, 0 };
// gateway ip address
//static byte gwip[] = { 0,0,0,0 };
static byte gwip[] = { 192, 168, 1, 1 };
// remote website ip address and port
static byte hisip[] = { 192, 168, 1, 1 };
const unsigned int websiteport = 8080;
// remote website name
const char website[] PROGMEM = "192.168.1.1:8080";
static byte session;

byte Ethernet::buffer[300];   // a very small tcp/ip buffer is enough here
static long timer;
//uint32_t timer;
Stash stash;
//end ether var

//start volt var
float testFrequency = 50;                     // test signal frequency (Hz)
float windowLength = 40.0 / testFrequency;   // how long to average the signal, for statistist

int Voltage1 = 0;
int Voltage2 = 0;
int Voltage3 = 0;

float intercept = -0.04; // to be adjusted based on calibration testing -0.04
float slope = 0.0405; // to be adjusted based on calibration testing

float current_Volts1;
float current_Volts2;
float current_Volts3;

unsigned long printPeriod = 1000; //Refresh rate
unsigned long previousMillis = 0;
//end v

void setup () {
  Serial.begin(57600);
  Serial.println("\n[getStaticIP]");

  emon1.current(27, 25);
  emon2.current(34, 25);
  emon3.current(35, 25);

  if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
    Serial.println( "Failed to access Ethernet controller");

  ether.staticSetup(myip, gwip, NULL, mask);
  //  ether.staticSetup(myip, NULL, NULL, mask);

  ether.copyIp(ether.hisip, hisip);
  ether.printIp("Server: ", ether.hisip);

  while (ether.clientWaitingGw())
    ether.packetLoop(ether.packetReceive());
  Serial.println("Gateway found");

  timer = - REQUEST_RATE; // start timing out right away
}

void loop () {
  Irms1 = emon1.calcIrms(1480);
  Irms2 = emon2.calcIrms(1480);
  Irms3 = emon3.calcIrms(1480);

  RunningStatistics inputStats1, inputStats2, inputStats3;
  inputStats1.setWindowSecs( windowLength );
  inputStats2.setWindowSecs( windowLength );
  inputStats3.setWindowSecs( windowLength );

  while ( true ) {
    Voltage1 = analogRead(A3);
    Voltage2 = analogRead(A4);
    Voltage3 = analogRead(A5);
    inputStats1.input(Voltage1);
    inputStats2.input(Voltage2);
    inputStats3.input(Voltage3);

    if ((unsigned long)(millis() - previousMillis) >= printPeriod) {
      previousMillis = millis();

      Serial.println();

      current_Volts1 = intercept + slope * inputStats1.sigma();
      current_Volts2 = intercept + slope * inputStats2.sigma();
      current_Volts3 = intercept + slope * inputStats3.sigma();

      current_Volts1 = current_Volts1 * (120.212765);
      current_Volts2 = current_Volts2 * (120.212765);
      current_Volts3 = current_Volts3 * (120.212765);

      power1 = current_Volts1 * Irms1;
      power2 = current_Volts2 * Irms2;
      power3 = current_Volts3 * Irms3;

      Serial.print("Voltage 1: ");
      Serial.println(current_Volts1);
      Serial.print("Voltage 2: ");
      Serial.println(current_Volts2);
      Serial.print("Voltage 3: ");
      Serial.println(current_Volts3);

      Serial.print("Current 1: ");
      Serial.println(Irms1);
      Serial.print("Current 2: ");
      Serial.println(Irms2);
      Serial.print("Current 3: ");
      Serial.println(Irms3);

      Serial.print("Power 1: ");
      Serial.println(power1);
      Serial.print("Power 2: ");
      Serial.println(power2);
      Serial.print("Power 3: ");
      Serial.println(power3);

      ether.packetLoop(ether.packetReceive());

      if (millis() > timer + REQUEST_RATE) {
        timer = millis();
        Serial.println("\n>>> REQ");
        ether.hisport = 8080;
        //just for test
        ether.httpPost(PSTR("/api/data"), website, PSTR("Content-Type: application/json"), PSTR("{\"tengangan\": \"220\", \"arus\": \"2\",\"daya\": \"440\",\"spot\": \"Sensor+1\"}"), my_result_cb);
      }
    }
  }

}

static void my_result_cb (byte status, word off, word len) {
  Serial.println("<<<< RES");
  //Serial.print(millis() - timer);
  //Serial.println(" ms");
  Serial.println((const char*) Ethernet::buffer + off);
}

Help me 😭

xujaan avatar Aug 08 '19 16:08 xujaan

Use: ether.httpPost(PSTR("/api/data"), website, PSTR("Content-Type: text/html"), "test=7&help=23", my_result_cb);

MrBrain-YT avatar Feb 19 '24 14:02 MrBrain-YT