OpenWrt Forum Archive

Topic: Using OpenWRT router to send energy consumption to the IoT

The content of this topic has been archived on 4 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hello everyone,

Just like to share something I did recently with my TP-1043ND, on a rainy sunday afternoon.

I have an energy consumption monitor (branded Envir Current Cost), that has a USB interface. I though of connecting it to Internet Of Things (IoT) so I could see some graphs online. And what would be the best device around the house for that? Of course, my OpenWRT router, that is online 24/7!

I'm using the site Xively (formely known as Cosm, formely known as Pachube). I'll not get into details on how to create an account on it and how to configure it. I'll just be focusing on the OpenWRT side.

Just a small detail: The device is actualy far away from the router, but I used a pair of USB-Over-Ethernet-Extensions and its running great over a cable extension of over ~20meters. You can find it on ebay for a couple of euros.

Most of the examples online require advanced functions of phyton (read serial) and I couldn't get it working on OpenWRT, so I did it via a simple shell script.
The main dificulty was to know how to read from the /dev/ttyUSBx and deal with it within the shell script itself.

After you know how to do it, it is quite simple, the problem is to get there. smile

As pre-requisites, you'll need some OpenWRT packages:

kmod-usb-serial-pl2303
coreutils-stty
curl

After you connect the device to the router (it has to be a USB capable router) you can test if the messages are getting there.

First, identify the tty being assigned to your device. In my case it was /dev/ttyUSB1.
Then, just configure that port and "listen" to it:

stty -F /dev/ttyUSB1 57600
cat /dev/ttyUSB1

You should be seeing something like:

<msg><src>CC128-v1.48</src><dsb>00208</dsb><time>21:20:41</time><tmpr>17.3</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00287</watts></ch1></msg>

for every 6 seconds or so.

Then, its just a matter of capturing that information via a script and sent it to Xively with curl.
Here is my script. I'm calling it every 5 minutes via crontab.

# Code to send ENVIR CurrentCost data to PACHUBE/COSM/XIVELY
#
#!/bin/sh

stty -F /dev/ttyUSB1 57600

for i in `cat /dev/ttyUSB1 | head -1`
do
 power=`echo $i | awk -F '<watts>' '{print $2}'`
 power=`echo $power | awk -F '</watts>' '{print $1}'`
 temp=`echo $i | awk -F '<tmpr>' '{print $2}'`
 temp=`echo $temp | awk -F '</tmpr>' '{print $1}'`
done

file="{\"version\":\"1.0.0\",\"datastreams\":[" #Starts the file
file="$file {\"id\":\""Cozinha"\",\"current_value\":\"$temp\"},"
file="$file {\"id\":\""Electricidade"\",\"current_value\":\"$power\"},"
file=`echo $file | sed '$s/.$//'`  # takes out the last comma (not needed anymore)
file="$file ]}"  # closes the file
echo $file > /tmp/xively

# Submit information to COSM
# Beware - information transfer is not secure - so use an API-KEY with permissions limited to update

curl --request PUT --data-binary @/tmp/xively --header "X-ApiKey: USE-YOUR-APIKEY" --verbose http://api.xively.com/v2/feeds/YOUR-FEED

I admit it's not the most elegant way of doing it, but it's working and for me that's what is the most important!

Enjoy and reduce your electricity bill!

Jabss

Nice smile

jabss wrote:

..., but I used a pair of USB-Over-Ethernet-Extensions and its running great over a cable extension of over ~20meters.

Did you mean this USB Female to Male Ethernet RJ45 Connector?

Not exatly, I mean this

BR,
Jabss

jabss wrote:

Not exatly, I mean this

BR,
Jabss

Do both the dongle have to connect to RJ-45 ports on your 10/100/1000 Mbps switches or they are connected piggyback on the RJ45 ports?

mazilo wrote:
jabss wrote:

Not exatly, I mean this

BR,
Jabss

Do both the dongle have to connect to RJ-45 ports on your 10/100/1000 Mbps switches or they are connected piggyback on the RJ45 ports?

i think its a usb to serial or ttl adapter and the Cat5 is just to link those together not a true network usb card

nice script thx for it

Indeed, this can't be switched over ethernet switches. It has to be point-to-point.

I suspect it uses 4 wires of the cable for the USB data and uses the other 4 to feed a kind of amplifier in the far end (or at least something similar).

I've used it for other purposes like webcams outside the house and it works relatively well.

For long distances you can add ferrite cores near each end to reduce noise.

jabss wrote:

Indeed, this can't be switched over ethernet switches. It has to be point-to-point.

That's what I thought so.

I suspect it uses 4 wires of the cable for the USB data and uses the other 4 to feed a kind of amplifier in the far end (or at least something similar).

The way I see this is it makes use of twisted-pair cable to transmit for a longer distance.

mazilo wrote:
jabss wrote:

Indeed, this can't be switched over ethernet switches. It has to be point-to-point.

That's what I thought so.

I suspect it uses 4 wires of the cable for the USB data and uses the other 4 to feed a kind of amplifier in the far end (or at least something similar).

The way I see this is it makes use of twisted-pair cable to transmit for a longer distance.

It should be more than twisted cables. It should have some active components.

Even typical 10m pure USB cables have some active components in the far-end.

The discussion might have continued from here.