#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
3 Answers 3
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);
}
-
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.Chris Stratton– Chris Stratton2018年06月13日 17:43:05 +00:00Commented Jun 13, 2018 at 17:43
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
-
1there is client.print(val);2018年06月07日 12:04:30 +00:00Commented Jun 7, 2018 at 12:04
-
opps sorry didn't see that then does the second part of my answer help?Brian Moreau– Brian Moreau2018年06月07日 12:11:18 +00:00Commented Jun 7, 2018 at 12:11
-
he doesn't ask about the php part2018年06月07日 12:32:57 +00:00Commented 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 partBrian Moreau– Brian Moreau2018年06月07日 12:54:29 +00:00Commented 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 itvathsalhari– vathsalhari2018年06月08日 18:44:14 +00:00Commented Jun 8, 2018 at 18:44
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.
-
-
Good, because it looks ugly with all those \r\ns in :) Still need blank lines though, which can just be two println("");Code Gorilla– Code Gorilla2018年06月07日 12:32:40 +00:00Commented 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 hostvathsalhari– vathsalhari2018年06月08日 18:59:02 +00:00Commented Jun 8, 2018 at 18:59
-
please refer my corrected code for reference since it is workingvathsalhari– vathsalhari2018年06月08日 19:00:28 +00:00Commented Jun 8, 2018 at 19:00
val
(its string representation, not sizeof int!).