@Anduril and others
Hi, I also started today with the WiFi Gateway. As the mysensors plugin still doesn’t support the wifi gateway I continued with @Anduril socat script and modified it to “daemonize” it. I also renamed it to pimsocat (not! pimsocat.sh).
See here:
#!/bin/bash
### BEGIN INIT INFO
# Provides: socat
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start socat tunnel for use with MySensors gateway
# Description: Open socat tunnel between /dev/ttyMS-Gateway and my
# ESP8266 wifi gateway for MySensors
### END INIT INFO
# Initial author: user Anduril @ pimatic forum
# Expanded: Harry van der Wolf <hvdwolf@gmail.com>
# Install:
# cp pimsocat /etc/init.d/pimsocat
# chmod +x /etc/init.d/pimsocat
# chown root:root /etc/init.d/pimsocat
# update-rc.d pimsocat defaults
# Specify here the address of your ESP MySensors WiFi Gateway
IPADDRESS="192.168.144.240"
PORT="5003" # default on 5003 but change this if you changed it on your gateway ino
PATH=/bin:/usr/bin:/sbin:/usr/sbin
SOCAT=/usr/bin/socat
PKILL=/usr/bin/pkill
DAEMON=/usr/bin/socat
NAME="socat"
OPTIONS="-d -d pty,link=/dev/ttyMS-Gateway,raw,echo=0,waitslave tcp:${IPADDRESS}:${PORT}"
# Do the binaries exist
test -f $SOCAT || exit 0
test -f $PKILL || exit 0
# Get lsb functions
. /lib/lsb/init-functions
case "$1" in
start)
log_begin_msg "Starting socat as listener for our mysensors gateway"
$SOCAT -d -d pty,link=/dev/ttyMS-Gateway,raw,echo=0,wait-slave tcp:${IPADDRESS}:${PORT} & >/dev/NULL
log_end_msg $?
;;
stop)
log_begin_msg "Stopping socat as listener for our mysensors gateway"
$PKILL $NAME
log_end_msg $?
;;
restart)
$0 stop
sleep 5
$0 start
;;
status)
status_of_proc "$DAEMON" pimsocat
;;
*)
echo "Usage: (sudo) service pimsocat {start|stop|restart|status}" >&2
exit 2
;;
esac
exit 0
As socat is not a real daemon it doesn’t have a stop command. It can only be killed. However, every kill or pkill command immediately terminates a script. Therefore we can’t kill socat from this script and then restart it from the same script immediately. Hence the trick with the internal call.
I also modified the default Esp8266Gateway.ino as I want a fixed ip address.
You will find the following lines in the void setup()
(void)WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
I changed that to the following:
(void)WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
// Configure a static ip address
IPAddress ip(192,168,144,240);
IPAddress gateway(192,168,144,1);
IPAddress subnet(255,255,255,0);
WiFi.config(ip, gateway, subnet);
Change this to your own network setup!
(This works of course for every esp ino)
And finally: As socat works like a daemon (but is not a daemon) you can also restart it with (sudo) service pimsocat restart
That means you can (re)start the socat “daemon” whenever pimatic (re)starts with a rule like:
"rule": "if pimatic is starting then execute \"service pimsocat restart\"",
or with a button like explained in this howto from @leader21