Sunday 18 November 2018

[Tutorial]Communicating between MCUs (Uno+WiFi, ESP8266 and ESP32) with a mobile phone using WiFi

1. Introduction

HTTP (HyperText Transfer Protocol) is the underlying protocol used by the World Wide Web and this protocol defines how messages are formatted and transmitted, and what actions Web servers and browsers should take in response to various commands. It is used for the application layer.
TCP/IP, or the Transmission Control Protocol/Internet Protocol, is a suite of communication protocols used to interconnect network devices on the internet. TCP/IP uses the client/server model of communication in which a user or machine (a client) is provided a service (like sending a webpage) by another computer (a server) in the network.
The difference between them is that TCP/IP decides the connection between the server and the client. HTTP protocol determines the composition of a message.

Here we will use three different MCUs (Uno+WiFi shield, ESP8266 and ESP32) to test the WiFi wireless communication between a mobile phone and MCUs. As ESP8266 and ESP32 have built-in WiFi module, but Uno doesn't, so we need to add a WiFi shield for Uno. Thus, the main objectives of this tutorial are to be familiar with principle of WiFi communication, to know TCP/IP and HTTP protocols, to learn how to program to transmit the message.

2. WiFi Modes

  • Station mode:WiFi.mode(WIFI_STA);
Under station mode, the ESP32 WiFi acts as a wireless end and it rejects access in but it can link to the AP. For example, our phone, wireless card. 

  • AP mode: WiFi.mode(WIFI_AP);
It provides access in and allows other devices access. It is linkable between APs.
  • AP + Station mode: WiFi.mode(WIFI_AP_STA);
It combines both functions. Just like the household WiFi route, it can access the internet and allows other devices to link it.

3. WIFi roles

  • Client

  • Server 
  • Scan
4. WIFi Encryption

  • 0: OPEN
  • 1: WEP
  • 2: WPA_PSK
  • 3: WPA2_PSK
  • 4:WPA_WPA2_PSK
  • 5: WPA2_Enterprise

5. WiFi module acts as the web server

To test the connectivity between WiFi module and computer, we simply use WiFi module on our device boards (including ESP32, ESP8266 or Arduino Uno with WiFi shield) as the server. Modify the SSID and password of the WiFi you are going to connect. The port number keep unchanged.
#include <WiFi.h>

const char* ssid     = "TALKTALK7122A1";   // the wifi you want to connect
const char* password = "AQEX68WG";

WiFiServer server(80);

int reqCount = 0;  

void setup()
{
    Serial.begin(115200);
    delay(10);
    // We start by connecting to a WiFi network
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("WiFi connected.");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    
    server.begin();

}

void loop() {
    
  WiFiClient client = server.available();
  if (client) {
    Serial.println("New client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          Serial.println("Sending response");
     
          // send a standard http response header
          // use \r\n instead of many println statements to speedup data send
          client.print(
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Connection: close\r\n"  // the connection will be closed after completion of the response
            "Refresh: 20\r\n"        // refresh the page automatically every 20 sec
            "\r\n");
          client.print("<!DOCTYPE HTML>\r\n");
          client.print("<html>\r\n");
          client.print("<h1>Hello World!</h1>\r\n");
          client.print("Requests received: ");
          client.print(++reqCount);
          client.print("<br>\r\n");
          client.print("Analog input A0K:");
          //client.print(ReplyBuffer);
          //client.print(analogRead(0));
          client.print("Analog input A1Z:");
          //client.print(ReplyBuffer2);
          //client.print(analogRead(0));
          client.print("<br>\r\n");
          client.print("</html>\r\n");
          
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(10);

    // close the connection:
    client.stop();
    Serial.println("Client disconnected");
  }
}

6. TCP/IP test tool on the computer

go to google play and search TCP/UDP TEST TOOL, then install it on your phone.

7. How to connect?

before connecting, we need to know that the WiFi module and the computer need to connect the same WiFi router or the connection will not be successful.  
  • obtain the IP address of WiFi module: upload the project code to the board-> open the serial monitor-> record the IP address. From the figure below, the IP address is 192.168.1.4
  • TCP/UDP test tool setup: select TCP CLient, then input IP address 192.168.1.4 and port number 80 and click connect.

  • receive and send messages 


No comments:

Post a Comment

[Research] Recurrent Neural Network (RNN)

1. Introduction As we all know, forward neural networks (FNN) have no connection between neurons in the same layer or in cross layers. Th...