0
#include <ESP8266WiFi.h>

const char* ssid = "xxxx";
const char* password = "xxxxxxxxxx";

const char* host = "192.168.43.18";
const int ldr = A0;
int val = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println();

  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");
}


void loop()
{
  val = analogRead(ldr);
  WiFiClient client;

  Serial.printf("\n[Connecting to %s ... ", host);
  if (client.connect(host, 80))
  {
    Serial.println("connected]");
    client.println("POST /past.php HTTP/1.1");
    client.println("Host: 192.168.43.10");
    client.print(val);
    Serial.println(val);
    Serial.println("\n[Disconnected]");
  }
  else
  {
    Serial.println("connection failed!]");
    client.stop();
  }
  delay(5000);
}

I have uploaded this code to node mcu but it get's working but the uploaded data in my database is not shown please any one help me out

5
  • 2
    I'd expect one empty line after the headers, and also Content-Length header with actual length of val (its string representation, not sizeof int!).
    – KIIV
    Commented Jun 7, 2018 at 8:14
  • and stop the client after the request
    – Juraj
    Commented Jun 7, 2018 at 9:00
  • @Juraj In this case I'd expect the destructor takes care about it. If not, it's definitely a bug in the library.
    – KIIV
    Commented Jun 7, 2018 at 9:14
  • yes the destructor closes the connection. and why do you close it in else? :-)
    – Juraj
    Commented Jun 7, 2018 at 9:17
  • 1
    the ip addresses are not same
    – Juraj
    Commented Jun 7, 2018 at 12:06

3 Answers 3

1

My corrected code working code:

#include <ESP8266WiFi.h>

const char* ssid = "vathsal";
const char* password = "1234567890";

const char* host = "wwwkprstaffcom.000webhostapp.com";
const int ldr = A0;
int val = 0;
String data;
void setup()
{
  Serial.begin(9600);
  Serial.println();

  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");
}


void loop()
{
  val = analogRead(ldr);
  data = "ldr=";

  data.concat(val);
  WiFiClient client;

  Serial.printf("\n[Connecting to %s ... ", host);
  if (client.connect(host, 80))
  {
    Serial.println("connected]");
    client.println("POST /add.php HTTP/1.1");
    client.println("Host: wwwkprstaffcom.000webhostapp.com");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);
    Serial.println(val);
    Serial.println("\n[Disconnected]");
  }
  else
  {
    Serial.println("connection failed!]");
    client.stop();
  }
  delay(5000);
}
1
  • Generally speaking, just dropping code does not make an answer. To constitute an answer, you really should start with an explanation of what you changed and why. Commented Jun 13, 2018 at 17:43
0

You are not actually posting any data. After client.println("POST /past.php HTTP/1.1"); Add another line(s) like ... client.println("?myvar=65dcsagh&anothervar=ghsadjkj78");

Also the page on the server (past.php) needs to be able to get and process the sent data. If using PHP will be soemthing like.. $data = $_GET[myvar]; And then the PHP code need to connect to your SQL database and insert it using mySQL

6
  • 1
    there is client.print(val);
    – Juraj
    Commented Jun 7, 2018 at 12:04
  • opps sorry didn't see that then does the second part of my answer help? Commented Jun 7, 2018 at 12:11
  • he doesn't ask about the php part
    – Juraj
    Commented Jun 7, 2018 at 12:32
  • No but he does say the data is not entered into the database so without knowing what is in past.php that could be the problem part Commented Jun 7, 2018 at 12:54
  • guys actually i didn't concatenate the data so tat's wat the problem without concatenate we cannot send the data else if we do the php code which is written on other side will not recognise it Commented Jun 8, 2018 at 18:44
0

You are using the WIFIClient to send data to a HTTP server. I don't think the WiFiClient knows how to format HTTP requests properly so you need to do that. I think there is a HTTPClient which may do more for you.

There is a blank line between the header and body of the message and a blank line at the end of the body. you need to have a line that say `client.println();

Does the "SQL server" accept a message that just contains a number? How does it know what the number relates to? If you have some PHP site on the front if the DBS that handles all that then please ignore that question.

You might want to see if you can run Wireshark (or similar) on your PC and see what the MCU is sending, you might also want to look at the Chrome add ins, I believe there are some where you can create raw packets and send those to you server to ensure it responds as expected.

As a final resort you could probably knock up a very quick and dirty C# program using Visual Studio Community Edition (Free) and get that to send a packet to the server. Using this in combination with wireshark might be easier than using the MCU until you know what you should be doing.

4
  • println writes /r/n
    – Juraj
    Commented Jun 7, 2018 at 12:01
  • Good, because it looks ugly with all those \r\ns in :) Still need blank lines though, which can just be two println(""); Commented Jun 7, 2018 at 12:32
  • yes there is HTTP client but it doesn't work so tat i am using wifi client.the wifi client can be used to send data from from node mcu to http server because we are making the node mcu to act as client at this place we are naming the website as host because as we know the host can act as both server r as client based on service so in my corrected code u could see that i am naming as host Commented Jun 8, 2018 at 18:59
  • please refer my corrected code for reference since it is working Commented Jun 8, 2018 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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