For a beginner like me, I find I need examples. Some of the documentation I find assumes you know how to fill in the bits and pieces, and well, I don't! So maybe this will help others trying to get started.
In this stripped down example, the ESP just sends the contents of a data file to the server every 60 seconds. So include a small text file in a '/DATA' directory on your ESP8266.
So first, the 'Server' code, which I run in a terminal on my Linux (Xubuntu) laptop, which is my everyday machine. It was set up for Python 2.x, and I haven't changed it to 3 yet, I should to keep the syntax more similar between MicroPython and this, but I just haven't.
THE CODE: So this sets up the socket, and it listens for something from the ESP8266. When I get a connection, I send the text "ACK" to the ESP (that's just my text message, not a system 'ack' or anything - it could be anything you want) , that was initially for debug, but I left it in for some visual feedback that I have two-way comm.
Then I open a file (in my 'real code', I create a file name based on what was sent from the ESP in response to "ACK"), loop through the recv data from the ESP, and keep writing it to a file. When the ESP8266 reaches the end of the file it is reading, I send some added info/stats, and then send a text 'EOF\n' to tell the server that's all folks. Again, that 'EOF\n' was just my choice of a 'signal word', it's not a system level thing. You might want to do something else here.
So with all the data from the ESP, the server writes some of its own info/stats, closes the file, changes ownership, and renames it with a cycle count appended. It closes the socket, and loops back, opens a new socket to be ready for the next transmission (I hope I got that right, I think so, but I couldn't fully test it in this code segment). I'll put up the ESP code next.
SERVER SIDE:
Code: Select all
#!/usr/bin/python
prg_n = 'ServerSocketDemo-from-v29'
#
# This needs to be running on 'Server' BEFORE starting ESP8266 routine
#
import time; import socket; import os; import glob
port = 1247
i_cnt=0
#
# **** While True Loop ****
while True:
s = socket.socket()
host = socket.gethostname()
print host, port
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # added allow to reuse when re-run
s.bind(('',port)) # Needed (('',port)) or client never saw it
s.listen(5)
c, addr = s.accept()
# end of setting socket
#
print 'c, addr: ', c, addr
print 'Waiting for data from ESP8266...'
# it printed... c, addr: <socket._socketobject object at 0x7ff4d063c670> ('192.168.1.200', 3073)
print("Connection accepted from " + repr(addr[1]) + " Server ready for file transfer...\n")
# it printed... Connection accepted from xxxx Server ready for file transfer...
#
# ACK the client, get filename and open file for writes
starttime = time.time() # measure start of ET to get and write file
c.send("ACK...\n") # SEND MESSAGE TO CLIENT TO SEND DATA
#
tfile = "Server-WIP"
fobj = open(tfile, "w")
#
# Incoming Data, loop until "FILE-EOF", then repeat for additional file(s)
# get next line...
client_sent = c.recv(1026)
#
while client_sent != 'EOF\n' :
print client_sent, # comma supresses a linefeed from printer (string already has a linefeed)
fobj.write(client_sent)
client_sent = c.recv(1026)
#
######### HIT EOF\n
localtime = time.asctime( time.localtime(time.time()) )
print (localtime)
E_Time = (time.time() - starttime)
print "Elapsed Time: %.2f Seconds" % E_Time
fobj.write("Server:,,,,,Elapsed Time: %.2f Seconds" % E_Time + '\n' )
fobj.write("Server:,,,,,Program Name: %s " %prg_n + '\n' )
fobj.close()
os.chmod(tfile,0o666) # assign RW- to Owner, Group, Other
os.rename(tfile, tfile[0:18] + '-' + str(i_cnt))
i_cnt += 1
print ('addr[1]: '+repr(addr[1])+' FILE XFER DONE for: '+tfile[0:18]+'-'+ str(i_cnt))
#
### close the socket
### a new socket will be created for next upload;
### as the client closes its socket after uploads
###
print ('ALL FILES RCVD this session')
print ('close socket, open new socket and wait for next upload')
print ('Back to top level # "**** While True Loop ****"\nServer Prog: ' + prg_n)
s.close()