vectiva
Published © Apache-2.0

Easy Wireless Communications with the Doit ESP-13 Shield

The ESP-13 shield gets a bad rap from many people, but it's easy to use if you know the secrets.

IntermediateFull instructions provided8,106
Easy Wireless Communications with the Doit ESP-13 Shield

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
An UNO should also work
×1
Doit ESP-13 Wireless shield
×1

Story

Read more

Schematics

wiring diagram

This is the wiring diagram between the Arduino 2560 and the ESP-13

Code

Python Socket Server

Python
The socket server that talks to the Arduino client.
'''
    Simple socket server using threads
'''
 
import socket
import sys
from _thread import *

HOST = ''   # Symbolic name, meaning all available interfaces
PORT = 9000 # Arbitrary non-privileged port
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ('Socket created')
 
#Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print ('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
    sys.exit()
     
print ('Socket bind complete')
 
#Start listening on socket
s.listen(10)
print ('Socket now listening')
 
#Function for handling connections. This will be used to create threads
def clientthread(conn):
    #Sending message to connected client
    sendstr = 'Welcome to the server. Ready to receive your data.'
    
    conn.send(sendstr.encode('utf-8')) #send only takes string

    try:

    #infinite loop so that function do not terminate and thread do not end.
      while True:
         
        #Receiving from client
        data = conn.recv(1024)
        print(str(data.decode()))
        #print(str(data))
        #conn.send(data)
        if not data: 
            break
     
        #conn.sendall(reply.encode('utf-8'))

    except KeyboardInterrupt:
      pass

    #came out of loop
    conn.close()
    print ('Socket closed by server')
 
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print ('Connected with ' + addr[0] + ':' + str(addr[1]))
     
    #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
    start_new_thread(clientthread ,(conn,))
 
s.close()

Arduino Wireless Client

Arduino
This is the client code that runs on the Arduino and communicates with your socket server on another computer over the ESP-13 wireless shield.
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  //  Serial.println("ready jeeves");
}

void loop() {
  if(Serial.available())
  {
    String a = Serial.readStringUntil("\n");  
    Serial.flush();
    Serial.println("client1 heard the server say - " + a);
  }
  else
  {
     delay(1000);
     Serial.println("client1 is waiting for a data transmission from the server");
  }
  
}

Credits

vectiva

vectiva

0 projects • 0 followers

Comments