0

I'm trying to create some connected device which would stay local. But I would like to create an app which when I'm connected to my home WiFi could control this device.

To control the device I'm using an Node MCU so I can connect to the WiFi with my ESP8266 module, I will call it ESP from now on. For now I'm just trying to blink the integrated LED on the card to test it and it works when I do some HTTP request through Chrome App. I'm using ESP8266WebServer library to receive and respond HTTP requests.

My idea was that it would be easy to just make an app with a button sending an HTTP request to the local IP of my ESP (192.168.1.57), so I downloaded Android Studio and made an app with a button triggering a request. To do so I'm using Volley library which seems perfect for what I'm trying and the app works when requesting https://www.google.com .

My problem is that it can make a request but not to my ESP server ( http://192.168.1.57 ).

I saw that some people advised to set the ESP IP on a port of the box so I can go through internet to reach my ESP but I would like to stay local.

Here are the codes of :

  • Node MCU V3 (ESP 8266)
#include <Arduino.h>
#include <Servo.h>

#include <ESP8266WiFi.h>

#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>


/* VARIABLES */
/* Server */
char name[] = "FlowerPot";
char password[] = "xxxxxxxx";
int port = 80;

ESP8266WebServer server(port);
int request = 0;


/* Output */
int led1 = LED_BUILTIN;   // Led de la carte , Pin D4, est allumé pour 0 et éteinte pour 1
int servo_pin = 2;        // Variable pin PWM servo, ici Pin D4
Servo servo1;             // Variable servo


/* FUNCTIONS DECLARATION */
void handleRoot();
void handleLED();
void handleNotFound();


/* CODE */
void setup() {
  /* Server setup */
  Serial.begin(9600);
  WiFiManager wifiManager;

  //se connecte au WIFI
  if (!wifiManager.autoConnect(name,password)) {
    Serial.println("failed to connect and hit timeout");
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(1000);
  }
  Serial.println("Connected!");

  //demarre le serveur
  server.on("/", handleRoot);               // Call the 'handleRoot' function when a client requests URI "/"
  server.on("/LED", handleLED);             // Call the 'handleLED' function quand on appelle l'URL avec "/LED"
  //server.on("/LED", HTTP_POST, handleLED);  // Call the 'handleLED' function when a POST request is made to URI "/LED"
  server.onNotFound(handleNotFound);        // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"

  server.begin();                           // Actually start the server
  Serial.println("HTTP server started");

  
  /* Blink setup */
  pinMode(led1,OUTPUT);

  
  /* Servo setup *//* 
  servo1.attach(servo_pin); */
}

void loop() {

  /* Server */
  server.handleClient();                    // Listen for HTTP requests from clients

  /* Blink */
  if(request){
  digitalWrite(led1,0);
  delay(1000);
  digitalWrite(led1,1);
  delay(4000);
  }

  
  /* Servo controle *//* 
  servo1.write(10);
  delay(1000);
  servo1.write(90);
  delay(1000);
  servo1.write(170);
  delay(1000); */
}

void handleRoot() {
  //server.send(200, "text/html", "<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>");
  server.send(200, "text/html", "Root");
  digitalWrite(led1,!digitalRead(led1));
}

void handleLED() {                          // If a POST request is made to URI /LED
  digitalWrite(led1,!digitalRead(led1));      // Change the state of the LED
  /* server.sendHeader("Location","/");        // Add a header to respond with a new location for the browser to go to the home page again
  server.send(303);                         // Send it back to the browser with an HTTP status 303 (See Other) to redirect */
  server.send(200, "text/html", "LED");
}

void handleNotFound(){
  server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
}
  • Android App Main Activity
package com.example.flowerpotapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendMessage(View view){

        Button myButton = (Button)findViewById(R.id.button);
        final TextView myTextView = (TextView)findViewById(R.id.errors);

        

        RequestQueue queue = Volley.newRequestQueue(this);
        String url ="http://192.168.1.57";
        //String url ="https://www.google.com";

        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        myTextView.setText("Response is: "+ response.substring(0,500));
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                myTextView.setText("That didn't work!");
            }
        });



        // Add the request to the RequestQueue.
        queue.add(stringRequest);

        if(myButton.getText() == "OFF"){
            myButton.setText("ON");
        } else {
            myButton.setText("OFF");
        }

    }
}

Some precision I test it with my phone a OnePlus 7 on android 10 and connected on the same WiFi as my ESP with my phone.

  • Your phone will not be able to talk to your ESP8266 unless it's on the same wifi network as the ESP8266. That's simply the way the network works. If your phone isn't connected to the same wifi network - if it's on a different wifi network, or it's using cellular data - your ESP is not at all visible to it. That's why people are telling you to map a port on your router to the ESP. That's the only way you'll make this work while still using HTTP. – romkey Aug 15 '20 at 16:52
  • On Android Q? Then http protocol will not work. – blackapps Aug 15 '20 at 17:35
  • @romkey, I am connected on the same WiFi as my ESP – Marc Chanet Aug 15 '20 at 20:19
  • @blackapps Android Q does not allow http anymore ? Why can I send http request through Chrome app ? What should I do then ? Switch my ESP to https ? – Marc Chanet Aug 15 '20 at 20:23
  • Yes Switching your esp device with https server should work as android doesn't support unsecure connection given they are on same network and chrome still handles http connection thats the reason it works with chrome but not on Android – Manoj Mohanty Aug 15 '20 at 21:09
  • You can enable/continue http support under Q by adding usesClearTextTraffic true to manifest file. – blackapps Aug 15 '20 at 21:22

0 Answers0