1

I am working on a NodeMCU board to post sensor data on my website service. Basically my project is about POST and GET requests both in single code.

I am succeeding in sending GET requests from my website. However, I just can't post data to my website.

To post I have 2 different links (asp services): one will post data and another will be used to get data same as transmitted by first link.

Here is my code, and also how to post 2 variables in http post request?

#include (ESP8266WiFi.h)
#include (ESP8266HTTPClient.h)

const char* ssid = "oppo";
const char* password = "11111111";    
int giSwitchNum = 1, buttonPin = 2;

void setup () {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  pinMode(D1, OUTPUT);
  pinMode(D2, INPUT);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print("Connecting..");
  }
  giSwitchNum = 1;
  Serial.print("Connect");
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
    HTTPClient Post;
    Post.begin("http://lemontechnologies.net/Services/Service.svc/SetData"); 
    Post.addHeader("operator", "text/plain");  
    Post.POST("Key=hi&val=jagrut1");
    String payload = Post.getString();
    Post.end();
  } 
  delay(500);  
}
  • 1
    remove the 'operator' header and add a "Content-Type" header with value "application/x-www-form-urlencoded" – Juraj Mar 19 '18 at 19:02
0
void wifi_connect(){
  WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
  delay(1000);
  WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot
  //WIFI_STA It's very important !!!!

  WiFi.begin(ssid, password);     //Connect to your WiFi router
  while(WiFi.status() != WL_CONNECTED){
    delay(1000);
    Serial.print(".");
  }

  Serial.println("Wifi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  return;
}
  • I think you may try "WiFi.mode(WIFI.STA)" – Dahd Oct 25 at 10:13
-1

One way is to use the HTTPClient.h. It can be used for both HTTP and HTTPS communication(I prefer WiFiClientSecure.h for https though). Here is the link to the tutorial which shows the usage. The blog has all sorts of examples like GET Request, POST Request etc.

https://techtutorialsx.com/2017/05/20/esp32-http-post-requests/

The code is pretty simple and easy to implement. Hope this helps.

EDIT

client.print(String("POST ") + url + " HTTP/1.1\r\n" +
           "Host: " + host + "\r\n" +
           "Connection: close\r\n"+   
           "Content-Length: " + data.length() + "\r\n" +
           "Content-Type: application/json;charset=UTF-8\r\n\r\n"+
            data +"\r\n");

Try this as the format to send. Make a json data to send the variables.

  • no, i need to solution for only http post request. I've a one web service link for post data on server. which is post two variable values on server. if i'm try same code with one variable based data post link. then it's working well. but it's not work with two variable based link.(same code in both cases) – Jignesh narola Mar 19 '18 at 12:51
  • 1
    @Mrenosert, he uses HTTPClient. the esp8266 version. – Juraj Mar 20 '18 at 8:49
  • how should this help him? – Juraj Mar 20 '18 at 8:51
  • Yes, I know, even I have used it for a similar purpose and it is working. – Mreno sert Mar 20 '18 at 11:19
  • In the blog I mentioned above the entire working is completely written in a precise manner . – Mreno sert Mar 20 '18 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.