Sending data from Arduino Mega through ESP8266 to App Inventor

Hi all,

I'm working on a project that use ESP8266-01 on Arduino Mega & connect to MIT App Inventor, the app should be able to control Arduino and Arduino can send sensor data to App Inventor to display it.

The Arduino libary that i am using for ESP8266 is WiFiEsp.h.

Currently, i can use App Inventor to control Arduino using http://192.168.xx.xx/1 method. However, I had no idea on how to send data to the App. I had tried using Get and GotText from Web block on App Inventor but fail to get any response. As the WiFiEsp.h in Arduino does not contain send() function, I had tried using client.wrtie() & client.print().

In the code below is merely use to test if the App Inventor can receive data from Arduino. When the Button1 on app is pressed, the led on Arduino will be turn on/off and at the same time Arduino will send a response to the App Inventor to change background color, which is not functioning.

#include "WiFiEsp.h"

char ssid[] = "SuiBianLa";           
char pass[] = "*#*#*#*#*#";       
int status = WL_IDLE_STATUS;
WiFiEspServer server(80);
RingBuffer buf(8);
int led1 = LOW;

void setup()
{
  pinMode(2, OUTPUT);
  Serial.begin(115200);   
  Serial1.begin(115200);   
  WiFi.init(&Serial1);  
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);
  }

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");
  
  server.begin();
}

void loop()
{
  WiFiEspClient client = server.available();  
  if (client) {                               
    Serial.println("New client");           
    buf.init();                              
    while (client.connected()) {              
      if (client.available()) {            
        char c = client.read();             
        buf.push(c);                        
        if (buf.endsWith("\r\n\r\n")) {
          client.println("HTTP/1.1 200 OK");
         
          client.println("Content-type:text/plain");
          client.println();
          client.println("iii");
          client.write("iii");

          break;
        }
        
        }

        if (buf.endsWith("GET /1")) 
        {
          if(led1 == LOW)
          {
          led1= HIGH;
          digitalWrite(2, HIGH);
          }
          else
          {
            led1=LOW;
            digitalWrite(2, LOW);   
          }          
        }
        
      }
    }
   
    client.stop();
    Serial.println("Client disconnected");
  }

App Inventor code.png

the App Inventor application on the phone or tablet doesn't run a server so you can't request from it

Juraj:
the App Inventor application on the phone or tablet doesn't run a server so you can't request from it

I see, are there any alternative to do the same? I am thinking of using database block in App Inventor like TinyWebDB or CloudDB(Redis), is it posibble and which one is easier?

I had tried FireBaseDB but that's not an option as the Firebase library for Arduino is not compatible with Arduino Mega Board.

If you are going to be in the same room as the Arduino, I would use Bluetooth.

Yes you can read and write serial data over Bluetooth from arduino to App inventor app.