86 lines
4.5 KiB
HTML
86 lines
4.5 KiB
HTML
define(`TITLE', `WiFi Powermeter')
|
|
define(`DATE', `2015-06-05')
|
|
define(`CONTENT', `
|
|
The WiFi Powermeter is a electric power meter which sends its measurement results every minute using the MQTT protocol via WiFi to an MQTT broker in the local network.
|
|
|
|
Here is the open test setup:
|
|
|
|
<a href="http://a385e5.files.wordpress.com/2015/06/img_3151.jpg"><img class="alignnone size-medium wp-image-593" src="http://a385e-5.de/wp-content/uploads/IMG_3151-300x225.jpg" alt="IMG_3151" width="300" height="225" /></a>
|
|
|
|
<!--more-->
|
|
|
|
|
|
|
|
This is how it looks in its box:
|
|
|
|
<a href="http://a385e-5.de/wp-content/uploads/IMG_3217.jpg"><img class="alignnone size-medium wp-image-594" src="http://a385e-5.de/wp-content/uploads/IMG_3217-225x300.jpg" alt="IMG_3217" width="225" height="300" /></a>
|
|
|
|
I used it to measure the power consumption of the fridge in the basement or of the PC of my son and so on.
|
|
|
|
On the MQTT broker there is one subscriber who transfers all the measurements into a MongoDB, where it now waits for further analysis.
|
|
|
|
The hardware is rather simple: an Arduino Mega (since it has more than one serial interface), an Arduino WiFi shield and on top a RS485 adaptor for the Modbus communication.
|
|
|
|
For the software I'`m using the Modbus library from <a href="https://code.google.com/p/simple-modbus/">https://code.google.com/p/simple-modbus/</a>. Unfortunately, it disappeared right there, don'`t know why. Seems, that I have to be really careful with the files. The MQTT library I'`m using is from <a href="http://knolleary.net/arduino-client-for-mqtt/">http://knolleary.net/arduino-client-for-mqtt/</a>.
|
|
|
|
With the couple of the WiFi library and the MQTT library I ran into two problems.
|
|
|
|
First, it was not possible to establish an MQTT connection at all. Strange, since via Ethernet it wasn'`t a problem at all. I found, supported by Google that that the WiFi library and the shield handles a <tt>stop()</tt> on a fresh client socket somewhat strange: The socket can not be open or will be closed immediately after opening. Since at least in my case I always give a fresh <tt>WiFiClient</tt> into the <tt>PubSubClient</tt> I removed the <tt>connected()</tt> call from the top of the <tt>connect</tt> method in <tt>PubSubClient.cpp</tt>.
|
|
|
|
Second, I ran into the problem that messages I published via PubSubClient using an Arduino Mega and an Arduino Wifi Shield which are large than about 80 octets did not appear at the broker. I found by googling that it is not possible to send more than 90 octets using the Wifi Shield and the related library at once: http://mssystems.emscom.net/helpdesk/knowledgebase.php?article=51
|
|
And that exactly what I experienced too: I increased the buffer size one by one and at 90 octets it stopped and the WiFi library lost the connection.
|
|
|
|
Using this patch, which I applied against release 1.9.1 everything works fine now:
|
|
|
|
[code language="cpp"]
|
|
42,43c42
|
|
< // if (!connected()) {
|
|
< if (true) {
|
|
---
|
|
> if (!connected()) {
|
|
217d215
|
|
< // Serial.print("pub len: "); Serial.println(length-5);
|
|
219,220d216
|
|
< } else {
|
|
< // Serial.println("connection lost");
|
|
290,308c286
|
|
<
|
|
< // Serial.print("write len: "); Serial.println(length+1+llen);
|
|
< // size_t olen = length + 1 + llen;
|
|
< // rc = _client->write(buf+(4-llen),length+1+llen);
|
|
<
|
|
< const size_t SEND_AT_ONCE = 64;
|
|
< size_t remains = length + 1 + llen;
|
|
< // Serial.print("write len: "); Serial.println(remains);
|
|
< const uint8_t *writebuf = buf + (4 - llen);
|
|
< bool result = true;
|
|
< while ((remains > 0) && result) {
|
|
< size_t actuallySendChars = (remains > SEND_AT_ONCE) ? SEND_AT_ONCE : remains;
|
|
< // Serial.print("tbs: "); Serial.println(actuallySendChars);
|
|
< size_t sentChars = _client->write(writebuf, actuallySendChars);
|
|
< result = sentChars == actuallySendChars;
|
|
< remains -= sentChars;
|
|
< writebuf += sentChars;
|
|
< }
|
|
<
|
|
---
|
|
> rc = _client->write(buf+(4-llen),length+1+llen);
|
|
311,312c289
|
|
< // return (rc == 1+llen+length);
|
|
< return result;
|
|
---
|
|
> return (rc == 1+llen+length);
|
|
357,362c334
|
|
< //Serial.print("rc: "); Serial.println(rc);
|
|
< if (!rc) {
|
|
< //Serial.println("would stop");
|
|
< _client->stop();
|
|
< // while (true);
|
|
< }
|
|
---
|
|
> if (!rc) _client->stop();
|
|
[/code]
|
|
|
|
|
|
Sources for the firmware are here: <a href="http://a385e-5.de/wp-content/uploads/WiModbusGateway.zip">WiModbusGateway</a>
|
|
') |