post data with esp8266

Solutions on MaxInterview for post data with esp8266 by the best coders in the world

showing results for - "post data with esp8266"
Adriana
20 Oct 2018
1/*
2 * HTTP Client POST Request
3 * Copyright (c) 2018, circuits4you.com
4 * All rights reserved.
5 * https://circuits4you.com 
6 * Connects to WiFi HotSpot. */
7
8#include <ESP8266WiFi.h>
9#include <WiFiClient.h> 
10#include <ESP8266WebServer.h>
11#include <ESP8266HTTPClient.h>
12
13/* Set these to your desired credentials. */
14const char *ssid = "circuits4you.com";  //ENTER YOUR WIFI SETTINGS
15const char *password = "yourPassword";
16
17//Web/Server address to read/write from 
18const char *host = "192.168.43.128";   //https://circuits4you.com website or IP address of server
19
20//=======================================================================
21//                    Power on setup
22//=======================================================================
23
24void setup() {
25  delay(1000);
26  Serial.begin(115200);
27  WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
28  delay(1000);
29  WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot
30  
31  WiFi.begin(ssid, password);     //Connect to your WiFi router
32  Serial.println("");
33
34  Serial.print("Connecting");
35  // Wait for connection
36  while (WiFi.status() != WL_CONNECTED) {
37    delay(500);
38    Serial.print(".");
39  }
40
41  //If connection successful show IP address in serial monitor
42  Serial.println("");
43  Serial.print("Connected to ");
44  Serial.println(ssid);
45  Serial.print("IP address: ");
46  Serial.println(WiFi.localIP());  //IP address assigned to your ESP
47}
48
49//=======================================================================
50//                    Main Program Loop
51//=======================================================================
52void loop() {
53  HTTPClient http;    //Declare object of class HTTPClient
54
55  String ADCData, station, postData;
56  int adcvalue=analogRead(A0);  //Read Analog value of LDR
57  ADCData = String(adcvalue);   //String to interger conversion
58  station = "A";
59
60  //Post Data
61  postData = "status=" + ADCData + "&station=" + station ;
62  
63  http.begin("http://192.168.43.128/c4yforum/postdemo.php");              //Specify request destination
64  http.addHeader("Content-Type", "application/x-www-form-urlencoded");    //Specify content-type header
65
66  int httpCode = http.POST(postData);   //Send the request
67  String payload = http.getString();    //Get the response payload
68
69  Serial.println(httpCode);   //Print HTTP return code
70  Serial.println(payload);    //Print request response payload
71
72  http.end();  //Close connection
73  
74  delay(5000);  //Post Data at every 5 seconds
75}
76//=======================================================================