If the communication must be done by a MIT app inventor App.
MIT App-inventor offers a WiFi module which can be used to send / receive data from / to the MIT App and your ESP8266
But there is an easier way to make this work.
Your ESP8266 can act as a little webserver that provides a simple website with control elements like buttons etc.
here is one example-code that uses the ESPUI-library
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
#define dbgi(myFixedText, variableName,timeInterval) \
do { \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
} while (false);
#include <DNSServer.h>
#include <ESPUI.h>
const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 4, 1);
DNSServer dnsServer;
#if defined(ESP32)
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
const byte OnBoard_LED = 2;
const char *home_ssid = "your ssid";
const char *home_password = "your password minimum 8 characters";
//const char* AP_ssid = "ESPUI";
//const char* AP_password = "espui";
const char* AP_hostname = "espui";
uint16_t statusLabel_ID;
uint16_t button1_ID;
uint16_t ClickCounter = 0;
void PrintFileNameDateTime() {
Serial.println( F("Code running comes from file ") );
Serial.println( F(__FILE__) );
Serial.print( F(" compiled ") );
Serial.print( F(__DATE__) );
Serial.print( F(" ") );
Serial.println( F(__TIME__) );
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
}
}
void buttonCallback(Control* sender, int type) {
Serial.print("ID: ");
Serial.print(sender->id);
Serial.print(" ");
Serial.print(sender->label);
switch (type) {
case B_DOWN:
Serial.println(" Button DOWN");
break;
case B_UP:
Serial.println(" Button UP");
ClickCounter++;
ESPUI.updateControlValue(statusLabel_ID, String(ClickCounter) );
break;
}
}
void connectToWiFi() {
#if defined(ESP32)
WiFi.setHostname(AP_hostname); // xxy
#else
WiFi.hostname(AP_hostname);
#endif
// try to connect to existing network
WiFi.begin(home_ssid, home_password);
Serial.print("\n\nTry to connect to existing network");
Serial.print(" named #");
Serial.print(home_ssid);
Serial.println("#");
{
uint8_t timeout = 10;
// Wait for connection, 5s timeout
do {
BlinkHeartBeatLED(OnBoard_LED, 100);
delay(500);
Serial.print(".");
timeout--;
} while (timeout && WiFi.status() != WL_CONNECTED);
// not connected -> create hotspot
if (WiFi.status() != WL_CONNECTED) {
Serial.print("\n\n no connection to SSID #");
Serial.print(home_ssid);
Serial.println("#\n Creating hotspot");
WiFi.mode(WIFI_AP);
delay(100);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
#if defined(ESP32)
uint32_t chipid = 0;
for (int i = 0; i < 17; i = i + 8) {
chipid |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
#else
uint32_t chipid = ESP.getChipId();
#endif
char ap_ssid[25];
snprintf(ap_ssid, 26, "ESPUI-%08X", chipid);
WiFi.softAP(ap_ssid);
Serial.print("SSID #");
Serial.print(ap_ssid);
Serial.println("#");
timeout = 5;
do {
delay(500);
Serial.print(".");
timeout--;
} while (timeout);
}
}
dnsServer.start(DNS_PORT, "*", apIP);
Serial.println("\n\nWiFi parameters:");
Serial.print("Mode: ");
Serial.println(WiFi.getMode() == WIFI_AP ? "Station" : "Client");
Serial.print("IP address: ");
Serial.println(WiFi.getMode() == WIFI_AP ? WiFi.softAPIP() : WiFi.localIP());
Serial.println("type this IP-adress into your browser to connect to the GUI of your ESP");
}
void defineGUI() {
statusLabel_ID = ESPUI.label("Number of Clicks", ControlColor::Turquoise, "0");
// statusLabel_ID : number that is used to identfy the GUI-element
// .label : the type of the GUI-element
// "Number of Clicks" : the text that describes the GUI-element
// ControlColor::Turquoise : explains ITSELF
// "0" initial VALUE shown on the element (will be updated on runtime)
button1_ID = ESPUI.button("Push Button 1", &buttonCallback, ControlColor::Peterriver, "I'm Button 1 click me");
// "Push Button 1": show text "Push Button 1" as the description of the GUI-Element
// &buttonCallback: ampersand-symbol '&' followed by the NAME of the callback-function
// that shall be executed if a user clicks on the button in his browser
// the ampersand-symbol tells the compiler use ADRESS of the named function to execute the function
// ControlColor::Peterriver: explains ITSELF
// "I'm button 1 click me" text shown on the button ITSELF
}
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
ESPUI.setVerbosity(Verbosity::VerboseJSON);
PrintFileNameDateTime();
connectToWiFi();
defineGUI();
//.begin("Title-String"); loads and serves all files from PROGMEM directly.
ESPUI.begin("I am the website created by the ESPUI-Demo");
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED, 500);
dnsServer.processNextRequest();
}