ESP8266 Peer to Peer

In this setup, we have the one ESP8266 setup as an AP (Access Point) and another as a STA (Station). The STA then connects to the AP and sends TCP messages like “LED ON” and “LED OF”. The AP reads them and turns ON or OFF an LED.

WIFI Server Code

#include "SoftwareSerial.h"

#define WIFISSID "KAYARIOT"      // WIFI Username
#define WIFIPASS ""         // WIFI Password

SoftwareSerial Serial1(2,3);
void sendToESP8266AndWaitForResponse (const char *cmd, const char *resp, bool waitForResponse, int duration) {
  String bytes;
  Serial.print ("CMD: "); Serial.println(cmd);
  do {
    Serial.print(".");
    Serial1.println (cmd);
    delay(duration);
    bytes = Serial1.readString();
  } while ( (waitForResponse) && (bytes.indexOf(resp)<0));
  Serial.print ("RESPONSE: ");
  Serial.print (bytes.c_str());
  Serial.println("\n------------------------------");
}

void setupAP() {
  String cwsapCmd = "AT+CWSAP=\"";
  cwsapCmd+=WIFISSID;  cwsapCmd+="\",\""; cwsapCmd+=WIFIPASS; cwsapCmd+="\",8,0";
  sendToESP8266AndWaitForResponse (cwsapCmd.c_str(), "OK", true, 50);
}

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(11,OUTPUT);

  // Setup Wifi as AP
  sendToESP8266AndWaitForResponse ("AT+CWMODE=2", "", false, 5);
  setupAP();
  sendToESP8266AndWaitForResponse ("AT+CIPMUX=1", "OK", false, 50);
  sendToESP8266AndWaitForResponse ("AT+CIPSERVER=1", "OK", false, 50);
}

// the loop function runs over and over again forever
void loop() {
  if(Serial1.available())
  {
   String message = Serial1.readString();
   Serial.println (message);
    if(message.indexOf("LED ON")>0)
    {
      digitalWrite(11,HIGH);
    }
    else if(message.indexOf("LED OFF")>0)
    {
      digitalWrite(11,LOW);
    }
    else
    {
      Serial.println ("Nothing to do...");
    }
  }
}

WIFI Client Code

#include "SoftwareSerial.h"

#define WIFISSID "KAYARIOT"     // WIFI Username
#define WIFIPASS ""             // WIFI Password
#define SERVERIP "192.168.4.1"  // WIFI Server
SoftwareSerial Serial1(2,3);
void sendToESP8266AndWaitForResponse (const char *cmd, const char *resp, bool waitForResponse, int duration) {
  String bytes;
  Serial.print ("CMD: "); Serial.println(cmd);
  do {
    Serial.print(".");
    Serial1.println (cmd);
    delay(duration);
    bytes = Serial1.readString();
  } while ( (waitForResponse) && (bytes.indexOf(resp)<0));
  Serial.print ("RESPONSE: ");
  Serial.print (bytes.c_str());
  Serial.println("\n------------------------------");
}

void connectToWIFI() {
  String cwjapCmd = "AT+CWJAP=\"";
  cwjapCmd+=WIFISSID;  cwjapCmd+="\",\""; cwjapCmd+=WIFIPASS; cwjapCmd+="\"";
  sendToESP8266AndWaitForResponse (cwjapCmd.c_str(), "OK", true, 50);
}

void connectToServer() {
  String cipstartCmd = "AT+CIPSTART=\"TCP\",\"";
  cipstartCmd += SERVERIP; cipstartCmd += "\",333";
  sendToESP8266AndWaitForResponse (cipstartCmd.c_str(), "Linked", false, 10);
}

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);

  // Setup Wifi as STA and connect to AP
  sendToESP8266AndWaitForResponse ("AT+CWMODE=1", "", false, 5);
  connectToWIFI();
  sendToESP8266AndWaitForResponse ("AT+CIPMUX=0", "OK", true, 50);
}

// the loop function runs over and over again forever
void loop() {
  if (Serial.available()) {
    String text = Serial.readString();
    connectToServer();
    char cipsend[100];
    sprintf (cipsend, "AT+CIPSEND=%d",text.length());
    sendToESP8266AndWaitForResponse (cipsend, ">", true, 10);
    sendToESP8266AndWaitForResponse (text.c_str(), "", false, 10);
  }
}

The commands used in the Code above for both Server and Client, can also be given through a Serial connection to the ESP8266 (commands entered manually instead of through a program).

Those commands are given below

WIFI Client Commands Text

AT
OK

AT+CWMODE?
+CWMODE:1
OK

AT+CWLAP
+CWLAP:(2,"no-alpha-no-beta",-86,"c8:3a:35:1f:a2:a8",1,-26)
+CWLAP:(0,"KAYARIOT",-64,"1a:fe:34:f4:74:53",1,21)

AT+CWJAP="KAYARIOT",""
WIFI CONNECTED
WIFI GOT IP
OK

AT+CIFSR
+CIFSR:STAIP,"192.168.4.2"
+CIFSR:STAMAC,"5c:cf:7f:8b:5b:a2"

AT+CIPMUX?
+CIPMUX:0
OK

AT+CWJAP?
+CWJAP:"KAYARIOT","1a:fe:34:f4:74:53",1,-62
OK

AT+CIPSTART="TCP","192.168.4.1",333
CONNECT
OK

AT+CIPSEND=6
OK
> 
Recv 6 bytes
busy s...
SEND OK

WIFI Server Commands Text

AT
OK

AT+CWMODE?
+CWMODE:2
OK

AT+CWSAP?
+CWSAP:"AI-THINKER_F47453","",1,0
OK

AT+CWSAP="KAYARIOT","",8,0
OK

AT+CWSAP?
+CWSAP:"KAYARIOT","",8,0
OK

AT+CIFSR
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"1a:fe:34:f4:74:53"
OK

AT+CWLIF
192.168.4.2,5c:cf:7f:8b:5b:a2
OK

AT+CIPMUX=1
OK

AT+CIPSERVER=1
OK
0,CONNECT

+IPD,0,6:LED ON

 

One thought on “ESP8266 Peer to Peer

Leave a Reply