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
1
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;
}
|improve this answer|||||
  • I think you may try "WiFi.mode(WIFI.STA)" – Dahd Oct 25 '19 at 10:13

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