Home automation with ESP8266 part IV - Script Update

Hello,

after experimenting with the esp for different use cases for quite awhile now, here is an update of the scripts I'm using with a few new features.

The update also seems to be necessary for newer firmware versions. I've noticed that for some reason the connection gets immediately closed after sending the first part of a webpage answer, so nothing was showing up in the browser anymore.

Secondly it supports a delayed automatic switch on (of a fan or something) after power on, if e.g. ESP is connected in parallel to a bathroom light.
Furthermore it will send a power-up message ("timer_on") to a configurable IP address/port to ask if the fan should really be switched on after the delay. This server can answer then with "delayoff" to avoid automated delayed switching on (or not answer at all).  Usecase is, if you wanna keep a bathroom fan switched off eg at night to avoid noise etc.

It also sends every minute an alive message to the router it got its IP address from. I've noticed that otherwise somehow delays are happening if the WLAN connection doesn't get used for awhile. This regular ping-like actions are solving that, beside a few routines to check if there is still coverage by checking if IP is still working to make this thing as reliable as possible.

Then I've implemented a few additional commands allowing for updating the scripts via the WLAN connection. This is not available via webbrowser but by commands sent directly to the ESP.

The complete command list looks like this now:

1. Bring ESP into flashmode with:
echo -n "flashmode" | netcat ip-addr 80 -w 3

2. List files on ESP
echo -n "#listfile" | netcat ip-addr 80 -w 3

3. Delete a file on ESP
echo -n "#deletefile:filename" | netcat ip-addr 80 -w 3

4. Rename a file on ESP (overwrites exisiting files named like newfile!)
echo -n "#renamefile:oldfile,newfile" | netcat ip-addr 80 -w 3

5. Open new file for writing (existing file will be overwritten!)
echo -n "#writefile:filename" | netcat ip-addr 80 -w 3

6. Write into file opened for writing (works only after #writefile-command)
echo -n "text text text" | netcat ip-addr 80 -w 3
Files need to be writen line by line with a 0.5 break inbetween for ESP processing time.

7. Close file 
echo -n "#closefile" | netcat ip-addr 80 -w 3

8. Leave flashmode:
echo -n "#reboot" | netcat ip-addr 80 -w 3

9. Switching relay on:echo -n "ch1on" | netcat ip-addr 80 -w 3

10. Switching relay off:echo -n "ch1off" | netcat ip-addr 80 -w 3

11. Get status of relay:echo -n "ch1?" | netcat ip-addr 80 -w 3

12. Switch on with delay (sss) in seconds:
echo -n "ch1onsss" | netcat ip-addr 80 -w 3

13. Switch off with delay (sss) in seconds:
echo -n "ch1offsss" | netcat ip-addr 80 -w 3

Notice that on some linux systems netcat is named nc.
The -n parameter for echo is necessary to not send a CR after each command. 

The scripts are expecting a configuration file now which is named "server" with 5 figures with a line feed after each line (also the last) :
server-ip
server-port
delay in seconds for fan delay
initial on/off, if relay should be on(1) or off(0) after powering up
IO-port for button
IO-port for relay

So a server file should look like this for example:
192.168.10.1
2701
180
0
4
3
last line

The scripts can be downloaded here. It is for sure not at all an example for proper programming or formating or anything else. More an evolutionary development of a few scripting lines to get to the next step of growing requirements without any redesign. :-) 
So there are also a lot of flaws like that no line of an uploaded file is allowed to have lines starting with #closefile as this would close the file, etc. But pragmatically this usually doesn't happen with a lua script, so it just works fine for me

Have fun. 
:-)


Comments