A problem with receiving data by MIT app inventor from ESP01

I designed an android application as follows:

  • Receive a number from ESP01
  • Compare it with the number that is already defined in a variable
  • If the received number is equal to the number defined in app, play a sound previously saved in app.

It should be mentioned that the communication is established through the Web block and IP assignment, and the application's variables are defined as a text string.

I use Web.gotText block to receive and compare as follows:

My app connects to ESP01 and sends data to it. However, it does not receive any data and all variables of “responseCode”, “responseType”, and “resppnseContent” are printed empty.

I was wondering if data is not received by app or the written code does not work properly.
I am a beginner to MIT app inventor. Any comment or instruction would be appreciated.

Pull in a Clock from the Designer's Sensors Drawer.

In the Block Editor, pull in a Clock1.Timer block, and inside it call that getDataFromServer procedure every second.

Thanks for your answer. Yes, I did exactly the same thing, but it still doesn't work.

Think of a way to distinguish between

  • no data ever arrives, or
  • exactly the same data arrives each time.

Does your Android device have a web browser that you can use to visit that URL?

Yes, it has a web browser
Just one point: when we are waiting to receive data from the esp side, this error appears:
Eror 1101: unable to get a response with the specified url: http://192.168.4.1:8887
Do you have any suggestions to solve it?

There are at least 2 possibilities for not getting to a local (192.168...) address:

  • you are using your cellular provider's Internet access instead of local WiFi, and your cellular provider's Internet access does not extend to your local net's device addresses, or
  • your local network does not include a 192.168.4.x range. Most home routers are set up for 192.168.0.n or 192.168.1.x ranges. This can be tested by trying to talk to your home router via a web browser, asking for http://192.168.0.1 or http://192.168.1.1, etc.

I connect directly to the esp01 module through the phone's Wi-Fi and do not use the home router.

Did you change port numbers?

What happened to port 8888, which you have in your global url variable?

the port number is set on 8888 and the error is: unable to get a response with the specified url: http://192.168.4.1:8888

I'm sorry the 8887 was a typo.

Point your phone's web browser to http://192.168.4.1:8888 and take a screen shot.
Post the screen shot here.

OK, I will do it and share the result here

One more question, Do I have to consider special settings for the phone's Wi-Fi to connect the application with the Wi-Fi module?

That depends on the result of the browser test, which you have not yet posted.

I did it. Nothing is displayed on the browser screen, but the serial monitor shows the following message:

That proves that your esp is not responding with a web response.

It's time to show us your esp code.

Please, no screen shots. Copy and paste the text here.

OK. The written code is as follows:


#include <SoftwareSerial.h>
//*********************************************Config Uart_PIN
const int PIN_RXD = 2;
const int PIN_TXT = 3;
String reponse ="" ;
const int hall1 = 4;
const int hall2 = 5;
const int hall3 = 6;
const int hall4 = 7;
int i ,z ;
String sendcommand=" " ;
int hallstate1,  hallstate2,hallstate3,hallstate4 ;
SoftwareSerial esp8266(PIN_RXD, PIN_TXT); // RX, TX
void setup() 
{
  Serial.begin(115200) ;
  esp8266.begin(115200) ;
  pinMode(hall1, INPUT);
  pinMode(hall2, INPUT);
  pinMode(hall3, INPUT);
  pinMode(hall4, INPUT);
  initesp8266() ;

}

void loop() 
{
  
  while(esp8266.available())
     {
       if(esp8266.find("0,CONNECT"))
          {
    
             Serial.println("wifi CONNECT") ;
             i=1 ;
          }
       else if(esp8266.find("0,CLOSED"))
          {
            i=0 ;
          }
  
      }
  while(i)
     {
       hallstate1=digitalRead(hall1) ;
       hallstate2=digitalRead(hall2) ;
       hallstate3=digitalRead(hall2) ;
       hallstate4=digitalRead(hall2) ;
      /* hallstate2=digitalRead(hall2) ;
       hallstate3=digitalRead(hall3) ;
       hallstate4=digitalRead(hall4) ;
       */
       //sendcommand=hallpinsensor(hallstate ,x )  ;
       if(hallstate1==LOW)
       {
        sendhall("CHANNEL1") ;
       }
        if(hallstate2==LOW)
       {
        sendhall("CHANNEL2") ;
       }
        if(hallstate3==LOW)
       {
        sendhall("CHANNEL3") ;
       }
        if(hallstate4==LOW)
       {
        sendhall("CHANNEL4") ;
       }
     } 
}
void initesp8266()
{
  Serial.println("*************");
  Serial.println("****** Wi-Fi settings ******");
  Serial.println("AT");
  sendtoesp8266("AT") ;
  waitforesp8266(1000) ;
  Serial.println("AT+RST");
  sendtoesp8266("AT+RST") ;
  waitforesp8266(1000) ;
  Serial.println("AT+CWMODE=3");
  sendtoesp8266("AT+CWMODE=3") ;
  waitforesp8266(1000) ;
  Serial.println("AT+CIPMUX=1");
  sendtoesp8266("AT+CIPMUX=1") ;
  waitforesp8266(1000) ;
  Serial.println("AT+CIPSERVER=1,8888");
  sendtoesp8266("AT+CIPSERVER=1,8888") ;
  waitforesp8266(1000) ;
  
}
void sendtoesp8266(String command ) 
{
  esp8266.println(command ) ;
}
void waitforesp8266(const int timeout)
{
  String reponse ="" ;
  long int time=millis() ;
  while((time+timeout)>millis())
  {
    while (esp8266.available())
    {
      char c=esp8266.read() ;
      reponse +=c ;
    }
  }
  Serial.print(reponse) ; 
}
/*
String hallpinsensor(int hallstate ,int x )
{
  int z
  String sendcommand="" ;
  if(hallstate==0||x==1)
  {
  
    z=11 ;
   
          
  }
  else 
  {
    z=10;
  }
  if(hallstate==0||x==2)
  {
    z=21;
    sendcommand="channel1" ;
  }
  else 
  {
    z=20;
  }
  if(hallstate==0||x==3)
     {
        z=31;
        sendcommand="channel3" ;
     {
    else 
    {
    z=30;
    }
   if(hallstate==0||x==4)
     {
        z=41;
        sendcommand="channel4" ;
        
     {
    else 
    {
    z=40;
    }
   return sendcommand ;
}
*/
void sendhall(String command) 
{
    
    Serial.println("AT+CIPSEND=0,8");
    sendtoesp8266("AT+CIPSEND=0,8") ;
    waitforesp8266(1000) ;
    while(esp8266.available())
     {
       if(esp8266.find(">"))
          {
    
             Serial.println("command") ;
             sendtoesp8266("command") ;
             waitforesp8266(1000) ;
          }
      }
}

I don't see any WiFI libraries or http server libraries in your code.

Where did you get that code?

I suggest searching this board's Tutorials and Guides section for ESP8266 samples.

I see your code is using a C++ String, which may or may not be compatible with the esp8266 or App Inventor. Usually we use a C string (array of chars)

char[] response;
char[] sendcommand;

How is your ESP8266-01?
This device only has 2 GPIO

Google Images ESP8266-01

I use esp01 .I didn't use the esp8266 which requires a library. We send AT command with Arduino and serial communication.