Components and supplies
LED (generic)
Rotary potentiometer (generic)
Arduino UNO
ESP-13 ESP8266 Web Sever Serial WiFi Shield Board Module For Arduino UNO R3
Resistor 330 ohm
Project description
Code
Python Socket Program
python
Here is the Python 3 program that allows for WiFi communication using a socket
1import socket 2import tkinter as tk 3from tkinter import * 4HOST = '' # Symbolic name, meaning all available interfaces 5PORT = 9000 # Arbitrary non-privileged port 6 7# Socket connection is a three step process - create, bind and connect 8# Create the socket 9s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 10 11#Following routines are defined to handle button events from the GUI 12#First routine handles Initialize button event; creates and binds the socket 13def initialize(event): 14 entry1.insert (0,'Socket created') 15 16 try: 17 s.bind((HOST, PORT)) 18 except socket.error as msg: 19 entry1.insert ('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + str(msg[1])) 20 sys.exit() 21 22 entry1.insert(END,' Socket bind complete') 23 print('Socket bind complete') 24 print(s.getsockname()) 25 26#Second routine handles Connect button event; connects the ESP13 shield to the socket 27#and prints out the IP address of the ESP13 shield 28def connect(event): 29 global conn 30 entry2.insert(0,'Socket now listening') 31 s.listen() 32 conn, addr = s.accept() 33 connection = 'Connected with ' + addr[0] + ':' + str(addr[1]) 34 print('connection ',addr) 35 entry2.delete(0,tk.END) 36 entry2.insert(0,connection) 37 print('connect complete') 38 39#Third routine handles Send button event; send the contents of the entry dialog to Arduino 40#via ESP13 shield and gets the return message and populates the entry dialog box with the return 41#message 42def send(event): 43 data = entry3.get() 44 conn.send(data.encode('utf-8')) 45 print('send',data) 46 data1 = conn.recv(70) 47 entry4.delete(0,tk.END) 48 entry4.insert(0,data1) 49 50#Fourth routine handles LED Update button event; sends the value of the LED brightness slider 51#(+ the arbitary header)to the ESP13 52#Reads the return message and updates the potentiometer slider. Dont forget the string to integer conversion 53def update(event): 54 data = '12#$' 55 data = data + str(slider2.get()) 56 print(data) 57 conn.send(data.encode('utf-8')) 58 data1 = conn.recv(70) 59 slider1.set(int(data1)) 60 print(data1) 61 print('Update LED') 62 63#Final routine handles the Exit button; closes the socket, closes the GUI and exits program 64def exit(event): 65 s.close() 66 print('exit') 67 window.destroy() 68 69#All below is tkinter programming to create the GUI. Main window contains two frames, one for entry dialog and sliders 70#second for buttons 71window = tk.Tk() 72window.title("ESP13 Communication") 73 74frame1 = tk.Frame(master=window,relief=tk.FLAT, borderwidth=5) 75frame1.pack() 76frame2 = tk.Frame(master=window,relief=tk.FLAT, borderwidth=5) 77frame2.pack() 78 79label1 = tk.Label(master=frame1,text="Socket Ready",fg="black",width = 25,height = 2) 80label1.grid(row=0,column=0,padx=20,pady=20,sticky="w") 81 82entry1 = tk.Entry(master=frame1,fg="black",bg="white",width=75) 83entry1.grid(row=0,column=1,padx=10,pady=10) 84 85label2 = tk.Label(master=frame1,text="Socket Connected",fg="black",width = 25,height = 2) 86label2.grid(row=1,column=0,padx=20,pady=20,sticky="w") 87 88entry2 = tk.Entry(master=frame1,fg="black",bg="white",width=75) 89entry2.grid(row=1,column=1,padx=10,pady=10) 90 91label3 = tk.Label(master=frame1,text="Message to send",fg="black",width = 25,height = 2) 92label3.grid(row=2,column=0,padx=20,pady=20,sticky="w") 93 94entry3 = tk.Entry(master=frame1,fg="black",bg="white",width=75) 95entry3.grid(row=2,column=1,padx=10,pady=10) 96entry3.bind("<Return>",send) 97 98label4 = tk.Label(master=frame1,text="Message recieved",fg="black",width = 25,height = 2) 99label4.grid(row=3,column=0,padx=20,pady=20,sticky="w") 100 101entry4 = tk.Entry(master=frame1,fg="black",bg="white",width=75) 102entry4.grid(row=3,column=1,padx=10,pady=10) 103 104label5 = tk.Label(master=frame1,text="Analog Input",fg="black",width = 25,height = 2) 105label5.grid(row=4,column=0,padx=20,pady=20,sticky="w") 106 107slider1 = Scale(frame1, from_=0, to=1023, length=600, orient=HORIZONTAL) 108slider1.grid(row=4,column=1,padx=10,pady=10) 109 110label6 = tk.Label(master=frame1,text="LED Brightness",fg="black",width = 25,height = 2) 111label6.grid(row=5,column=0,padx=20,pady=20,sticky="w") 112 113slider2 = Scale(frame1, from_=0, to=255, length=600, orient=HORIZONTAL) 114slider2.grid(row=5,column=1,padx=10,pady=10) 115 116button1 = tk.Button(master=frame2,text="Initialize",width=10,height=2,relief=tk.RAISED,fg="black") 117button1.grid(row=0,column=0,padx=20,pady=20) 118button1.bind("<Button-1>",initialize) 119 120button2 = tk.Button(master=frame2,text="Connect",width=10,height=2,relief=tk.RAISED,fg="black") 121button2.grid(row=0,column=1,padx=20,pady=20) 122button2.bind("<Button-1>",connect) 123 124button3 = tk.Button(master=frame2,text="Send",width=10,height=2,relief=tk.RAISED,fg="black") 125button3.grid(row=0,column=2,padx=20,pady=20) 126button3.bind("<Button-1>",send) 127 128button4 = tk.Button(master=frame2,text="LED Update",width=10,height=2,relief=tk.RAISED,fg="black") 129button4.grid(row=0,column=3,padx=20,pady=20) 130button4.bind("<Button-1>",update) 131 132button5 = tk.Button(master=frame2,text="Exit",width=10,height=2,relief=tk.RAISED,fg="black") 133button5.grid(row=0,column=4,padx=20,pady=20) 134button5.bind("<Button-1>",exit) 135 136window.mainloop() 137print('Program ended') 138
Arduino Sketch
arduino
Arduino sketch that communicates with ESP13 shield and across WiFi
1/* 2 Arduino ESP13 Shield Program 3 4 Once the ESP13 shield has 5 connected to a WiFi Network, it sends data over the network from the Arduino that 6 is 7 written to the Serial lines. All data that the Arduino sends and recieves 8 over the WiFi network uses the Serial.read, Serial.write 9 and Serial.print commands. 10 11 12 This program receives data from program running on a laptop using the socket connection. 13 14 15 In order to distinguish beteeen a simple receive / echo case and the LED brightness 16 adjust, a arbitary string "12#$" is transmitted 17 as the four beginning bytes. 18 If this is detected, then LED brightness is updated and pot value read from reply. 19 (This string is totally 20 arbitary) 21 22 If the header string is not present, 23 then the arduino echos the the message sent. 24*/ 25String incomingString; 26String 27 analogValue; 28String headerString; 29 30int ledPin = 3; 31 32void setup() 33 { 34 // put your setup code here, to run once 35 // Baud rate of ESP13 shield 36 set up in configuration 37 Serial.begin(115200); 38} 39 40void loop() { 41 42 // reply only when you receive data: 43 if (Serial.available() > 0) 44 { 45 46 // read the incoming string and strip out four first bytes: 47 incomingString 48 = Serial.readString(); 49 headerString = incomingString.substring(0,4); 50 51 // If the header string equals "12#$" then update LED brightenss and pot value 52 53 if (headerString.equals("12#$")) 54 { 55 //remove the first four 56 bytes, analogWrite function needs and integer, conversion required 57 incomingString.remove(0,4); 58 59 analogWrite(ledPin,incomingString.toInt()); 60 //read the A0 analog 61 input, convert to string and send back 62 analogValue = String(analogRead(A0),DEC); 63 64 Serial.print(analogValue); 65 } 66 else 67 { 68 // say what 69 you got: 70 Serial.print("ESPRecieved: "); 71 Serial.flush(); 72 73 Serial.print(incomingString); 74 } 75 } 76 else 77 { 78 delay(500); 79 80 } 81 82} 83
Arduino Sketch
arduino
Arduino sketch that communicates with ESP13 shield and across WiFi
1/* 2 Arduino ESP13 Shield Program 3 4 Once the ESP13 shield has connected to a WiFi Network, it sends data over the network from the Arduino that is 5 written to the Serial lines. All data that the Arduino sends and recieves over the WiFi network uses the Serial.read, Serial.write 6 and Serial.print commands. 7 8 This program receives data from program running on a laptop using the socket connection. 9 10 In order to distinguish beteeen a simple receive / echo case and the LED brightness adjust, a arbitary string "12#$" is transmitted 11 as the four beginning bytes. If this is detected, then LED brightness is updated and pot value read from reply. (This string is totally 12 arbitary) 13 14 If the header string is not present, then the arduino echos the the message sent. 15*/ 16String incomingString; 17String analogValue; 18String headerString; 19 20int ledPin = 3; 21 22void setup() { 23 // put your setup code here, to run once 24 // Baud rate of ESP13 shield set up in configuration 25 Serial.begin(115200); 26} 27 28void loop() { 29 // reply only when you receive data: 30 if (Serial.available() > 0) 31 { 32 // read the incoming string and strip out four first bytes: 33 incomingString = Serial.readString(); 34 headerString = incomingString.substring(0,4); 35 // If the header string equals "12#$" then update LED brightenss and pot value 36 if (headerString.equals("12#$")) 37 { 38 //remove the first four bytes, analogWrite function needs and integer, conversion required 39 incomingString.remove(0,4); 40 analogWrite(ledPin,incomingString.toInt()); 41 //read the A0 analog input, convert to string and send back 42 analogValue = String(analogRead(A0),DEC); 43 Serial.print(analogValue); 44 } 45 else 46 { 47 // say what you got: 48 Serial.print("ESPRecieved: "); 49 Serial.flush(); 50 Serial.print(incomingString); 51 } 52 } 53 else 54 { 55 delay(500); 56 } 57 58} 59
Downloadable files
Schematic
Shield Connections
Schematic
Schematic
Shield Connections
Schematic
Comments
Only logged in users can leave comments