OK solved the first part on my own. Problem was that after reading the socat tunnel with cat/dev/… the socat stops with soclet 1 is at EOF, socket 2 is at EOF, exiting with status 0
, which might be as intended. After not looking at the tty-device I could connect pimatic to it and read the data of mysensors devices. But one problem still stands: when closing the connection, pimatic can not reconnect. So after updating and restarting pimatic, it will not connect to the gateway anymore. Is there a way to reopen socat directly after exiting with status 0?
-
Mysensors WiFi Gateway
-
@Anduril Hi, you have experienced the same thing as I did and for now I have connected the my sensor directly to the pi via USB. I think it is better to change the plugin to make a connection and posted a request at github. I don’t think it will be that hard to do but my java / coffee knowledge is not sufficient.
-
Thats bad to hear because going back to serial gateway is no option for me. I need to have my gateway connectable y multiple clients to use MYSController for ota firmware updates. Maybe someone can tell me how to change the startup of pimatic to first initiate socat connection, wait few seconds and then start pimatic.
Otherwise I will have to wait for the update of the plugin to allow tcp connection, but I don’t think @Dheeraj is very active on this. -
is it possible to add this line for starting a socat tunnel to the pimatic file in /etc/init.d/ ? I would try, but I’m a beginner in linux and don’t want to crash my system by editing without knowledge… Is there a better way to create an autostart?
-
Hey there,
it’s me again asking for help with autostarting socat.
I talked with a friend who was using linux intensivly few years ago about the problem. He said the best thing to solve the problem in his eyes is to programm a little deamon script which checks if pimatic and socat are running and if not restart socat first and few seconds later pimatic. Sadly he is not familiar with programming a deamon any more and needs some time to read into that topic again. So I thought to ask here for help of someone who might also use the script.
To find out if pimatic is running we used the commandps -aux |grep pimatic |grep /usr/local/bin/pimatic
and this gives only one answer if it’s running and none if not.
For socat we tried to look for the serial device withls /dev/ttyMS-Gateway |grep cannot
which will give no answer if device exists and answerls: cannot access /dev/ttyMS-Gateway: No such file or directory
. if it does not exist (and socat tunnel is not running.
Now it’s time to turn this into a deamon… and where help is needed. We thought about running the test, start socat and few seconds later pimatic if needed and sleep for few minutes. -
Hello @dynamite,
I found at least a way to make it work for the reboot. The daemon would still be prefered, but my lack of knowledge prevents me from programming this. If someone wants to contribute, you are still very welcome.For now I solved it by creating a socat.sh in
/etc/init.d
#!/bin/sh ### BEGIN INIT INFO # Provides: socat # 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 /usr/bin/socat -d -d pty,link=/dev/ttyMS-Gateway,raw,echo=0,waitslave tcp:192.168.38.19:5003 &>/dev/NULL
and active it with
sudo update-rc.d socat.sh defaults
.
I don’t know if thats the best way to do it, but it works
But the problem of restarting pimatic (for updates) still exists. Socat will close the tunnel when pimatic stops and no new connection will be established. But in this case I can still reboot my RPi… not the best, but a working way. -
In this case I would create a shell script issueing your pimatic “check” command and upon “true” start socat, more or less like you do now.
Then I would schedule this script in cron to run every 5 or 10 minutes or so.
to run every 5 minutes do:*/5 * * * * /home/<user>/pimatic/check_pim_socat.sh > /dev/null 2>&1
-
By the way: Why are you using
ps -aux |grep pimatic |grep /usr/local/bin/pimatic
?
This will always deliver 2 lines whereasps ax | grep -v grep | grep pimatic.js
will only deliver 1 line -
Hello, i also wan’t to try the ESP8266 Gateway.
Does anybody got it to work now?
(And can give me some support what i have to do)
I have the following hardware:
ESP --> Wemos D1 Mini
Radio --> NRF24L01I think the wiring example would be like the sample on my sensors.org
https://www.mysensors.org/build/esp8266_gatewayBut what are the next steps on/in pimatic?
How do i have to implement the Wemos in Pimatic?
Mit freundlichen Grüßen
Chris -
@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 thevoid 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 -
@Harry-van-der-Wolf I’m not familiar with daemonizing things, but doesn’t your script just start after pimatic start in your rule? Don’t you have the problem of connecting to a not yet existing serial device with MySensors plugin?
It would be great if it works this way, but I don’t think so. -
Yes, you are right. When starting from pimatic it is too late. The device needs to exists before pimatic tries to use it.
I will change it to make some kind of combined pimatic/socat script. -
maybe one should extend the mysensors plugin to either cope with not existing devices and keep trying or directly support the network serial connection all this is just a workarround
maybe use something like this in the plugin:
https://github.com/sizuhiko/tcpsocket-serialport -
I tried to implement it, but no luck so far. This is the way to open a tcp connection in node/javascript:
https://github.com/rakeshpai/mysensors-controller/blob/master/index.js#L32
-
Should be no problem in theory but I did not yet implement a raw tcp connection.
http://www.hacksparrow.com/tcp-socket-programming-in-node-js.html
I however implemented a raw UDP interface for Orvibo S20 sockets which works fine with nodejs.
-
I mentioned that I would create a System V combined script for socat and pimatic, but how does the mechanism work when pimatic starts after updating plugins and so on?
Does pimatic restarts itself or is this done via the system V settings in the init.d script with aservice pimatic restart
?
If it is the first mechanism a combined script has no use.
If it is done via the latter system V mechanism it can be done via a combined init.d script. -
I did make a work around. I left the pimatic service in place and I also left my own pimsocat “service” script in place.
When doing a reboot it works anyway as the pimatic startup is a lengthy one and socat is long running before pimatic is at the stage of the mysensors plugin.I created a second small script which I called restart_pimatic_socat.sh
#!/bin/bash service pimatic stop service pimsocat stop sleep 5 service pimsocat start service pimatic start
Create a ButtonsDevice like described here by @leader21.
Create the rule for the pimatic restart like"if button-pimatic-restart is pressed then execute \"sudo /home/<some_user_name>/pimatic/scripts/restart_pimatic_socat.sh\"",
Now when you have updated your plugins, do not simply press the button “restart pimatic” inside the “Updates” screen, but backout of the “Updates” screen and press your “Restart pimatic” button.
This works fine.
Now let’s hope the mysensors plugin will be modified to support the wifi gateway so that this work-around is no longer necessary -
I’m hoping for a tcp connection for mysensors plugin too.
@Harry-van-der-Wolf do you have a stable connection? I loose connection from time to time with the result of no messages getting to pimatic. But the serial device is still there. I didn’t had a chance upto now to hunt down the issue, maybe bad tcp connection or bug… -
I started last Sunday. I did some experiments with the script and so on so it has never been up more then 10 hours. In these 10 hours it was stable.
At this moment I have no intentions to change it again until the mysensors plugin has been extended
So, I will see how stable it is.
By the way: I copied your command without checking it. But thewaitslave
is wrong. It should bewait-slave
.Another way to make sure that socat restarts after a broken socket is to loop it like
while true; do socat -d -d pty,link=/dev/ttyMS-Gateway,raw,echo=0,reuseaddr,forever,wait-slave tcp:192.168.38.19:5003 &>/dev/NULL done
or better to make sure the OS has enough time to capture the SIGINT signal:
while sleep 1; do socat -d -d pty,link=/dev/ttyMS-Gateway,raw,echo=0,reuseaddr,forever,wait-slave tcp:192.168.38.19:5003 &>/dev/NULL done
If you put this in a script at startup of your RPi like in the /etc/init.d it should work constantly.
I did not try this one yet. Maybe in a few days. See what the stability is. -
@Anduril
Today I lost connection. So my socat-pimatic connection is not stable either.