-1

I'm trying to control NodeMCU via Android application and I can control NodeMCU easily but when I added the URL to hit the PHP code to enter data in MYSQL, neither NodeMCU is controllable nor NodeMCU is hitting the PHP code. I'm confused with content type: text/html - I'm not sure where to set this piece. Is there any mistake I'm doing or anything I'm missing?

Following is the ESP8266 code on Arduino IDE:

#include <ESP8266WiFi.h>

const char* ssid = "ssid";
const char* password = "password";
const char* host = "aoatech.ml";

WiFiServer server(3000);
int light1 = 13;
int light2 = 15;
int fan = 12;
int socket = 14;
String what;
int which;
String mac;

void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(socket, OUTPUT);
  digitalWrite(light1, 0);
  digitalWrite(light2, 0);
  digitalWrite(fan, 0);
  digitalWrite(socket, 0);  
  Serial.print("Connecting to: ");
  Serial.print(ssid);
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());
}

void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.print("Connecting to ");
  Serial.println(host);
  //  const int httpPort = 3000;
  //  if (!client.connect(host, httpPort)) {
  //    Serial.println("Connection failed!");
  //    return;
  //  }
  mac = WiFi.macAddress();
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  int val;
  if (req.indexOf("/light1/1") != -1){
    val = 1;
    what = "Light_1";
    which = light1;
  }
  else if (req.indexOf("/light1/0") != -1){
    val = 0;
    what = "Light_1";
    which = light1;
  }
  else if (req.indexOf("/light2/1") != -1){
    val = 1;
    what = "Light_2"; 
    which = light2;
  }
  else if (req.indexOf("/light2/0") != -1){
    val = 0;
    what = "Light_2";
    which = light2;
  }
  else if (req.indexOf("/fan/1") != -1){
    val = 1;
    what = "Fan";
    which = fan;
  }
  else if (req.indexOf("/fan/0") != -1){
    val = 0;
    what = "Fan"; 
    which = fan;
  }
  else if (req.indexOf("/socket/1") != -1){
    val = 1;
    what = "Socket"; 
    which = socket;
  }
  else if (req.indexOf("/socket/0") != -1){
    val = 0;
    what = "Socket";
    which = socket;
  }
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  digitalWrite(which, val);  

  String url = "http://aoatech.ml/hitthis.php?room=Bed%20Room%201";
  url += "&state=";
  url += val;
  url += "&button=";
  url += what;
  url += "&mac=";
  url += mac;

  Serial.print("Requesting URL: ");
  Serial.println(url);


  client.flush();
  //  client.println("HTTP/1.1 200 OK");
  //  client.println("Content-Type: text/html");
      client.print(String("GET ") + url + " HTTP/1.1\r\n" +
           "Host: " + host + "\r\n" +
           "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }  
  client.println("");
  client.print(what + " is now: ");

  if(val == 0) {
    client.print("Off");
  } else {
    client.print("On");
  }
  Serial.println("Client disonnected");
}
0

You mixed WiFiServer and client request examples by replacing servers HTTP response text with a HTTP request text. A client object returned by WiFiServer's available function is a counterpart of requesting client (for example web browser).

See WebClient example for a client connecting to some server.

-1

Your PHP server has a certain content type it can receive to process your requests from the ESP8266. The two most generic content-types used for php are either application/x-www-form-urlencoded or multipart/form-data. Try to play with those two and see if that solves it for you.

  • you don't see that the code in question is a mess? – Juraj Sep 28 '18 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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