App Inventor sends information to ESP8266 by WiFi

Hello friends,

1.- Send message and view it on an LCD screen.

1.- You write a message in TexBox1 and it is sent by WiFi to ESP8266, that message is shown on the LCD screen.
2.- If you write on5, off5, on6, off6, LEDs 5 or 6 turn on/off.

```
// Juan A. Villalpando.
// KIO4.COM
// Send a message
// from  App Inventor

#include <ESP8266WiFi.h>
 
const char* ssid = "Name_Net_WiFi";
const char* password = "Password_WiFi";

// Static IP.
    IPAddress local_IP(192, 168, 1, 12);
    IPAddress gateway(192, 168, 1, 1);
    IPAddress subnet(255, 255, 255, 0); 

#define LED5  D5    // LED in pin 5
#define LED6  D6     // LED in pin 6

#include <LiquidCrystal_I2C.h>
int columns = 16;
int rows = 2;
LiquidCrystal_I2C lcd(0x27, columns, rows);  
// LiquidCrystal_I2C lcd(0x3F, columns, rows); 

WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  lcd.init();                     
  lcd.backlight();
  
// Set Static IP.
   WiFi.config(local_IP, gateway, subnet); 
  
// Connection to WiFi
  Serial.println();
  Serial.print("Connecting with ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected with WiFi.");
 
  // Start WebServer.
  server.begin();
  Serial.println("WebServer started.");
 
  // This is IP of WebServer
  Serial.print("IP of WebServer: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
}
 
void loop() {
  // Check if a client has connected.
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  Serial.print("New client: ");
  Serial.println(client.remoteIP());
   
  // Wait for the client to send data.
  while(!client.available()){ delay(1); }

  /////////////////////////////////////////////////////
  // Read information of client.
  String req = client.readStringUntil('\r');
  Serial.println(req);
  req.replace("+", " ");          // Spaces without +
  req.replace(" HTTP/1.1", "");   // this delete HTTP/1.1
  req.replace("GET /", "");       // this delete GET /

  lcd.clear(); // Borra pantalla.
  lcd.setCursor(0, 0); // Start cursor
  lcd.print("Mensaje");
  lcd.setCursor(0,1); // Next row.
  lcd.print(req);

  // Make the customer's request.
       if (req.indexOf("on5") != -1) {digitalWrite(LED5, HIGH);}
       if (req.indexOf("off5") != -1){digitalWrite(LED5, LOW);}
       if (req.indexOf("on6") != -1) {digitalWrite(LED6, HIGH);}
       if (req.indexOf("off6") != -1){digitalWrite(LED6, LOW);}

  //////////////////////////////////////////////
  // Página WEB. ////////////////////////////
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  Important.

  Serial.print("Client disconnected: ");
  Serial.println(client.remoteIP());
  client.flush();
  client.stop();
}
```

Regards,
http://kio4.com/arduino/345_esp8266_AI2_LCD.htm

With NodeMcu:
https://groups.google.com/forum/#!searchin/mitappinventortest/esp8266$20sends$20message|sort:date/mitappinventortest/t9DJ8U0KE6Y/18orp_xgBwAJ

2 Likes

2.- OLED Screen.

On this website there are codes for OLED screens:
http://kio4.com/arduino/31B_pantallaOLED.htm

2 Likes

A post was merged into an existing topic: Need help with wifi connection please

3.- App sends current time in seconds, on time and off time of an LED.

esp8266_timer.aia (2.9 KB)

Example:
current_seconds: 50740
delay_for_ON: 50760
delay_for_OFF: 50770

50740,50760,50770

The ESP8266 receives these values ​​over WiFi (Router client, static IP: 192.168.1.12)

At the time of delay_LED_ON, turn on LED5.
At the time of delay_LED_OFF turns off LED5.

  • It is a didactic code, the algorithm can be improved.
#include <ESP8266WiFi.h>
 
const char* ssid = "Name_WiFi";
const char* password = "Password_WiFi";

// Configuración de la IP estática.
    IPAddress local_IP(192, 168, 1, 12);
    IPAddress gateway(192, 168, 1, 1);
    IPAddress subnet(255, 255, 255, 0); 

#define LED5  D5    // LED en terminal 5
unsigned long current_seconds = 0;
unsigned long delay_for_ON = 99999999;
unsigned long delay_for_OFF = 0;
unsigned long tiempo = 0;

WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  pinMode(LED5, OUTPUT);
  digitalWrite(LED5, LOW);
// Establecimiento de la IP estática.
   WiFi.config(local_IP, gateway, subnet); 
  
// Conecta a la red wifi.
  Serial.println();
  Serial.print("Conectando con ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Conectado con WiFi.");
 
  // Inicio del Servidor web.
  server.begin();
  Serial.println("Servidor web iniciado.");
 
  // Esta es la IP
  Serial.print("Esta es la IP para conectar: ");
  Serial.println("http://");
  Serial.print(WiFi.localIP());
}
 
void loop() {
  if(millis() - tiempo > 1000){
    Serial.println ("1 segundo.");
    tiempo = millis(); 
    current_seconds = current_seconds + 1;
    if(current_seconds > delay_for_ON && current_seconds < delay_for_OFF){
        Serial.println("LED5 ON");
        digitalWrite(LED5, HIGH);  
    }
    if(current_seconds > delay_for_OFF){
        Serial.println("LED5 OFF");
        digitalWrite(LED5, LOW);  
    }
        }
  // Consulta si se ha conectado algĂşn cliente.
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  Serial.print("Nuevo cliente: ");
  Serial.println(client.remoteIP());
   
  // Espera hasta que el cliente envĂ­e datos.
  while(!client.available()){ delay(1); }

  /////////////////////////////////////////////////////
  // Lee la informaciĂłn enviada por el cliente.
  String received = client.readStringUntil('\r');
  Serial.println(received);
  received.replace("+", " ");          // Para que los espacios no salgan con +
  received.replace(" HTTP/1.1", "");   // Para quitar HTTP/1.1
  received.replace("GET /", "");       // Para quitar GET /
  int i1 = received.indexOf(',');
  int i2 = received.indexOf(',', i1+1);
  int i3 = received.indexOf(',', i2+1);
  current_seconds = atol(received.substring(0, i1).c_str());
  delay_for_ON = atol(received.substring(i1 + 1, i2).c_str());
  delay_for_OFF = atol(received.substring(i2 + 1).c_str());
  Serial.println(current_seconds);
  Serial.println(delay_for_ON);
  Serial.println(delay_for_OFF);
  //////////////////////////////////////////////
  // Página WEB. ////////////////////////////
  String responseContent = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nTimes: ";
  responseContent += received;
  client.println(responseContent);
}
1 Like

4.- App requests two random numbers to the ESP8266 over WiFi .

esp8266_random.aia (2.5 KB)

  • App requests two random numbers to the ESP8266 over WiFi (Router client, static IP)
  • ESP8266 sends two random numbers separated by commas.

esp8266_61

#include <ESP8266WiFi.h>
 
const char* ssid = "Name_WiFi";
const char* password = "Password_WiFi";

// Configuración de la IP estática.
    IPAddress local_IP(192, 168, 1, 12);
    IPAddress gateway(192, 168, 1, 1);
    IPAddress subnet(255, 255, 255, 0); 

int random_1 = 0;
int random_2 = 0;
String random_out = "0,0";

WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
// Establecimiento de la IP estática.
   WiFi.config(local_IP, gateway, subnet); 
  
// Conecta a la red wifi.
  Serial.println();
  Serial.print("Conectando con ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Conectado con WiFi.");
 
  // Inicio del Servidor web.
  server.begin();
  Serial.println("Servidor web iniciado.");
 
  // Esta es la IP
  Serial.print("Esta es la IP para conectar: ");
  Serial.println("http://");
  Serial.print(WiFi.localIP());
}
 
void loop() {
  // Consulta si se ha conectado algĂşn cliente.
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  Serial.print("Nuevo cliente: ");
  Serial.println(client.remoteIP());
   
  // Espera hasta que el cliente envĂ­e datos.
  while(!client.available()){ delay(1); }

  // Lee la informaciĂłn enviada por el cliente.
  String received = client.readStringUntil('\r');
  Serial.println(received);
  received.replace("+", " ");          // Para que los espacios no salgan con +
  received.replace(" HTTP/1.1", "");   // Para quitar HTTP/1.1
  received.replace("GET /", "");       // Para quitar GET /
  if (received.indexOf("give_me_random") != -1){
   random_1 = random(10,50);
   random_2 = random(50,90);
   random_out = (String) random_1 + "," + (String) random_2;
   }
  
  //////////////////////////////////////////////
  // Página WEB. ////////////////////////////
  String responseContent = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
  responseContent += random_out;
  client.println(responseContent);
}
1 Like

5.- Create the ESP8266 Sketch code using blocks.

There are several websites to create the ESP8266 code using blocks...

http://easycoding.tn/tuniot/demos/code/

http://easycoding.tn/index.php/nodemcu/video-tutorials/local-network/

I prefer to continue using the Arduino IDE and write it in text.

2 Likes