Loading...

You have no items in your shopping cart.

 
 
 
×

Registration

Profile Informations

Login Datas

or login

First name is required!
Last name is required!
First name is not valid!
Last name is not valid!
This is not an email address!
Email address is required!
This email is already registered!
Password is required!
Enter a valid password!
Please enter 6 or more characters!
Please enter 16 or less characters!
Passwords are not same!
Terms and Conditions are required!
Email or Password is wrong!
Captcha is required!
Captcha is wrong!

Create a “Chatroom” in Arduino IDE by Using Two Wees(ESP8266)

In previous blog, we’ve showed how to use AT commands to test Wee (ESP8266) module. However, according to the ESP8266 user manual, we find some AT commands can be used to make an interesting project – a “chatroom” in Arduino IDE. In this blog, we will explore the AP model, using AT commands to realize the function of network visit.

Hardware we need

Before you upload the code to Iteaduino Mega, you need to download the latest ITEAD ESP8266 library from the following link. Then extract it to the libraries folder in Arduino IDE.

https://github.com/itead/ITEADLIB_Arduino_WeeESP8266

In these two suite of hardware, one of them shall be upload the server code, the other uploaded the client code.

Server Code

/* Server */

#include "ESP8266.h"

uint32_t len;
uint8_t buffer[128] = {0};
uint8_t mux_id = 0xFF;
ESP8266 wifi(Serial1);

void setup(void)
{
    Serial.begin(9600);
    Serial.print("setup begin\r\n");
    
    if (wifi.setOprToStationSoftAP()) {
        Serial.print("to station + softap ok\r\n");
    } else {
        Serial.print("to station + softap err\r\n");
    }

    if (wifi.setSoftAPParam("WeeMaster","12345678")) {
        Serial.print("Set AP SSID:WeeMaster ok\r\n");
        wifi.restart();
    } else {
        Serial.print("Set AP SSID:WeeMaster err\r\n");
    }

    if (wifi.enableMUX()) {
        Serial.print("multiple ok\r\n");
    } else {
        Serial.print("multiple err\r\n");
    }
    
    if (wifi.startTCPServer(80)) {
        Serial.print("start tcp server ok\r\n");
    } else {
        Serial.print("start tcp server err\r\n");
    }
    
    if (wifi.setTCPServerTimeout(180)) { 
        Serial.print("set tcp server timout 180 seconds\r\n");
    } else {
        Serial.print("set tcp server timout err\r\n");
    }
    
    Serial.print("setup end\r\n");
    Serial.println("Now, I am online and waitting for someone ...");
}

void loop(void)
{
    String data;
    len = wifi.recv(&mux_id, buffer, sizeof(buffer), 200);
    if (len > 0) {
        Serial.print("\r\n\r\n---- Received ----\r\n");
        for(uint32_t i = 0; i < len; i++) {
            Serial.print((char)buffer[i]);
        }
    }
    
    while(Serial.available() > 0) {
        char c = Serial.read();    
        data += c;
        delay(1);
    }
    
    if (mux_id != 0xFF && (data.length() > 0)) {
        Serial.print("\r\n\r\n------ Sent ------\r\n");
        Serial.print(data);
        wifi.send(mux_id, (const uint8_t *)data.c_str(), data.length());
    }
}

Client Code

/* Client */

#include "ESP8266.h"

uint32_t len;
uint8_t buffer[128] = {0};
ESP8266 wifi(Serial1);

void setup(void)
{
    Serial.begin(9600);
    Serial.print("setup begin\r\n");
    
    if (wifi.setOprToStation()) {
        Serial.print("to station ok\r\n");
    } else {
        Serial.print("to station err\r\n");
    }

    if (wifi.joinAP("WeeMaster", "12345678")) {
        Serial.print("Join AP success\r\n");
        Serial.print("IP: ");       
        Serial.println(wifi.getLocalIP().c_str());
    } else {
        Serial.print("Join AP failure\r\n");
    }
    
    if (wifi.disableMUX()) {
        Serial.print("single ok\r\n");
    } else {
        Serial.print("single err\r\n");
    }

    if (wifi.createTCP("192.168.4.1", 80)) {
        Serial.println("createTCP ok");
    } else {
        Serial.println("createTCP err");
    }
    
    Serial.print("setup end\r\n");
    Serial.println("Now, I am online and launch a conversation to someone ...");

}

void loop(void)
{
    String data;
    len = wifi.recv(buffer, sizeof(buffer), 200);
    if (len > 0) {
        Serial.print("\r\n\r\n---- Received ----\r\n");
        for(uint32_t i = 0; i < len; i++) {
            Serial.print((char)buffer[i]);
        }
    }
    
    while(Serial.available() > 0) {
        char c = Serial.read();    
        data += c;
        delay(1);
    }
    
    if (data.length() > 0) {
        Serial.print("\r\n\r\n------ Sent ------\r\n");
        Serial.print(data);
        wifi.send((const uint8_t *)data.c_str(), data.length());
    }
}

After codes upload to two Iteduino Mega respectively, open the serial port monitor. The serial port monitor of server is as below:

The serial port monitor of client is as below:

Note that when they connect to each other, the communication request shall be launched by client end first. The code here doesn’t support server sending the communication request first.