-1

We created a website hosted by 000webhost and we're trying to post data from our nodeMCU to the database using the sketch below, but the database doesn't seem to show any received data. How do you fix this? Sketch for the nodemcu

and below is the php code to rtecieve the data: PHP codes

#include <ESP8266WiFi.h>

const char* ssid = "Jino";
const char* pass = "myinternet";
char server[] = "cdgresearch.000webhostapp.com";

WiFiClient client;

int led = 2;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid,pass);
  pinMode(led,OUTPUT);
  digitalWrite(led, HIGH);

  Serial.println("Connecting to: ");
  Serial.println(ssid);
  Serial.print("Connecting");

  while(WiFi.status() != WL_CONNECTED){
    delay(1000);
    Serial.print(".");
    }

  delay(300);
  digitalWrite(led, LOW);
  Serial.println();
  Serial.println("Successfully connected to the network");
  Serial.print("NodeMCU IP Address: ");
  Serial.println(WiFi.localIP());

  if (client.connect(server, 80)){
    String data = "incoming=fromDuino&incoming2=FROMDUINO2";
    client.println("POST /tag.php? HTTP/1.1");
    client.println("Host: cdgresearch.000webhostapp.com");
    client.println("User-Agent: 8266/1.0");
    client.println("Connection: close");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.print("\n\n");
    client.println(data);
    client.stop();

    Serial.println("Connected to server");
    }
    else{
    Serial.println("Failed to Connect to server...");
    }

  client.stop();
  Serial.println("Disconnected from Server");
}

void loop() {
 // put your main code here, to run repeatedly:
}
3
  • post the code as text
    – Juraj
    Oct 13, 2018 at 8:04
  • did you see my answer?
    – Juraj
    Oct 14, 2018 at 19:05
  • im sorry, going to post the codes soon hopefully
    – Jinocide
    Oct 15, 2018 at 11:42

3 Answers 3

0

HTTP requires \r\n as line ending. println adds \r\n. So to print the host name to client use println and remove \n from the string. And replace client.print("\n\n") with client.println(); client.println(); (or println the length and then use one println)

0
-1

You are using POST method in the php script ($_POST['var']) and trying to pass parameters to the script by adding them in the URL. This addition of the parameters would work only if it were a GET method in place of a POST method.

Change wherever you have written POST to GET (Both in the php script and the node mcu code) and your data will be inserted into the database.

1) Change both the $_POST['var'] to $_GET['var'] in the php file.

2) Change client.println("POST /tag.php HTTP/1.1") to client.println("GET /tag.php?incoming=fromDuino&incoming2=fromDuino2 HTTP/1.1")

Also, you can go through this link to learn more about how to make a POST request. And can you show me the whole code or the classes that you have instantiated?

3
  • 1
    what do I do if I want to use POST though? I know GET can also work that way but I need the sending of data to be more discrete, and POST method seems to be more discrete than GET
    – Jinocide
    Oct 14, 2018 at 9:32
  • Can you please post your whole code (Including the libraries and all the declarations)? I might help you with POST method then :D
    – Manav
    Oct 14, 2018 at 10:46
  • @Manav, the POST request is of type form-urlencoded. and the parameters are in body as it should be
    – Juraj
    Oct 14, 2018 at 19:03
-1

1st I would check each part i.e. First check your PHP script can insert by setting some values in the script i.e. incomming = "somejunk";

I assume the PHP include file connects to your DB and changes into the corresponding DB? I assume you have setup a user for the DB in say cPanel and set privileges

Your method of sending data is different to my working format... You are sending the data at the end rather than the beginning...

Also if you look at your server log you can see if the values get sent

if (client.connect(server, httpPort)) {
  // connected to resource
  // send data
  client.print("POST /dir/file.php?field1=");
  client.print(data1&field2=data2); // must be url encoded
  client.println(" HTTP/1.1");
  client.println("Host: www.domainname.com");
  // client.println("Content-Length: " + String(data.length())); // you dont need 
  client.println("User-Agent: ESP8266");
  client.println("Connection: close");
  client.println();
1
  • what is this? POST with url parameters. do you know what is "Content-Type: application/x-www-form-urlencoded"?
    – Juraj
    Oct 18, 2018 at 5:18

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.