loading

Step 15: Dog Tracker


Video



This last example will be different from all the other sensor projects so far. I’ve been kicking around this dog tracker idea for a while. Recently, there was an “Internet of Things Hack Day” competition held by the local Arduino group here in the Twin Cities (Minnesota, USA). I took this opportunity to form a team to work on this dog tracker idea. This is what my teammates (Wolf Loescher, Russ Terrell, and Patrick Delaney) and I managed to make on competition day.


I’m presenting this idea mainly to highlight how flexible Arduino and DIY automation can be. You can make your own niche sensor. The prototype we made is fully functional, but the electronics needs to be shrunk down in order for it to be worn on the dog. I hope to keep working on it when I have more time. For now, I’ll just post the code and video demo without the fancy wiring diagram found in previous steps. You can pretty much figure out the wiring diagram from the Arduino sketch, so I hope that’s ok.


Motivation
Lots of people let their dog out to poop in the morning. It’s tempting to just let them out the yard by themselves and give them time to sniff around while you stay warm and cozy inside and finish your coffee. But there’s two problems with that.

  1. The dog might run away (if you don’t have a fenced yard).
  2. You don’t know if they poop, or where they poop, and end up spending more time looking for the poop. Neglecting to pick up the poop is NOT an option!


This dog wearables project aims to solve these problems. As seen in the diagram, the idea is to have a dog wear a collar box. The collar box contains a GPS module, tilt switch, and a RFM69 transceiver.

Dog Escape Alarm and Tracking
The GPS location is constantly being sent from the dog unit to the OpenHAB Raspberry Pi at home, via the same gateway previously detailed. The moment the GPS signal strays beyond a virtual boundary defined in OpenHAB, the Raspberry Pi speakers play an audible alarm to alert the dog owner they need to go out and fetch the dog. The owner can look at the OpenHAB screen to see a google map of where their dog currently is (or at least the last GPS signal received). Armed with the initial direction the dog went, he can then use a handheld unit to locate the dog outside. With the great range of the RFM69, the system should be able to locate the dog more than 900 feet away with the hand-held unit, or 700 feet with building obstruction. The range is good enough to be very helpful when locating a lost dog, even if you have no clue where it might have gone. You can drive around waiting for a ping from the handheld unit.




Actually, even without the Arduino hand-held unit, as long as your dog isn’t hell bent on running away, you’ll probably be able to find him just from the google map shown on the OpenHAB screen. You can use your smart phone OpenHAB app, connected to the home OpenHAB server, and see in real time where your dog is wondering off to as you go after him. With such good range, you’re likely to catch him before he goes too far.

Dog Poop Tracking
The system also tells you when your dog poops, and shows a google map of where the poop is. We use the tilt switch signal to tell us when the dog is holding the “poop” position for several seconds. When this happens, the dog Arduino flags a GPS location. The OpenHAB Raspberry Pi plays an audio alert to let you know that the dog has done its business. You then check the map to see where to pick up the poop. The timestamp is there to tell other family members later in the day whether the poop location is current or from the morning bathroom break, in case they miss the audio alert. Family members can use the alarm “OFF” button to clear the alarm and indicate to other family members that this morning’s poop has been picked up.




We’re also using the built-in temperature sensor in the RFM69 to get the ambient temperature of the dog unit. We can send audio alerts if the environment gets too hot or cold. For all of these audio alerts, it’s trivial to also add in email alerts, as seen in the other project examples.

The following demo video is taken in front of my house. Here’s what to look for in the video and screen shots. I wanted to demonstrate the accuracy of the GPS unit. It seems to be good to within 5 feet. I’m able to set the unit at the end of my driveway, right on the grass line. The GPS map shows that location almost exactly. I purposely chose the light post as the poop location to give a sense of how accurate the GPS module is. Pretty impressive for a $15 device.





This is the dog unit placed at the end of the driveway, right on the grass line.


GPS track of me walking around the yard.


Video


Unresolved Issues
We originally planned on putting a magnetometer in both the dog unit and the person unit. Unfortunately, the magnetometer library we wanted to use didn’t play nice with the RFM library. So we had to just skip that functionality. It would’ve been cool to get that working. The OpenHAB was setup to record and chart the direction the dog was facing each time it poops, so that we can determine whether or not dogs really face North when they go.

...

<p>In step 5, if I'm reading the schematic correctly, there's a 1M resistor going from (unregulated) power to A3, and there's another 1M resistor going from A3 to Ground. Admittedly, I'm very noob to circuits and such, but wouldn't this cause a short? I'm not really following the logic here, and how this allows us to monitor for anything on A3.</p>
<p>And this is probably going to come up. You can't put the battery voltage directly on A3. Well, you can, but it wont help you measure low battery situation. Your Vin is the 3.3V from the regulator. So the A3 pin will read saturation (vin=255) until the battery voltage drops below 3.3 volts. By that time, you're already 0.7V below when your regulator can stably regulate the 3.3V output (because of regulator drop-down voltage). You need to be able to measure when battery reaches 4.0V.</p><p>Hence the need to divide the battery voltage.</p>
<p>You could easily simplify lots of connections using this board: <a href="https://talk2.wisen.com.au/product-talk2-whisper-node-avr/" rel="nofollow">https://talk2.wisen.com.au/product-talk2-whisper-n...</a></p><p>It's an Arduino compatible board with RFM69 and is designed to run on AA batteries.</p>
<p>I should've pointed that out specifically, it's a common question. Schematically, it looks like this: Battery_Voltage----R1----R2---GND. There's two resistors in series between the battery voltage and GND, so it's not a short. A3 is placed between R1 and R2. They form a voltage divider (google this term). A3 measures the battery voltage at the point between R1 and R2, which is half the battery voltage if R1=R2.</p>
<p>Oh, and the two resistors are always consuming current, and are a total waste of power. You can make them 10Mohms if you want, which would decrease current consumption to 1/10th. But high resistances also lower current flow into A3, which prevents accurate voltage readings on the analog pin. The capacitor is there to keep the reading accurate when higher value resistors are used. In the spec for the ATMEGA328, there's some mention of max impedance that you can use without the capacitor. I'm not sure what it is, I just used what I had.</p>
<p>Thank you for the great explanation! Makes perfect sense. </p><p>I wanted to also throw out there to the crowd that I do now have a working version of the RFM69 chip directly connected to the Raspberry Pi! I was able to use the information located here:</p><p><a href="https://github.com/abouillot/HomeAutomation/tree/master/piGateway" rel="nofollow">https://github.com/abouillot/HomeAutomation/tree/m...</a></p><p>I'm getting really low RSSI right now that I can't quite figure out (Typically around -100db when the two chips are only 3ft apart), but it's working! My antenna wire is about 3 inches on both, so thinking I need to give them larger antennas. Just need a minute to do some resoldering...</p>
<p>Hi, I got it working too (yay!) but the same low signal strength as you report, did you get it higher? by resoldering?</p>
<p>if you have the RFM69 at 433 MHz, you may want to use this antenna. Made a big difference for me: </p><p><a href="https://arduinodiy.wordpress.com/2015/07/25/coil-loaded-433-mhz-antenna/" rel="nofollow">https://arduinodiy.wordpress.com/2015/07/25/coil-l...</a></p>
<p>I did finally get my RSSI higher, but I'm not completely sure what the reason is yet. I have done two things - I updated the Gateway code for the raspberry pi and I purchased a mini wireless module from anarduino. I'm now getting around -25 to -30 RSSI. I purchased the new wireless module to help isolate the issue, since I then don't have to worry about soldering issues. I updated the Gateway code on the raspberry pi before I bought the module.</p><p>My goal is to move back to my pro micro module prior to putting this solution into production, but I'm planning on perfecting my code first so that I can focus later on just fixing the hardware.</p>
<p>Hi, Im trying the RF on Pi as well, first got stuck on installing the MQTT, but now on the Gateway install. Instruxtion says &quot;cd ~/piGateway&quot;, while I have no such folder, should I copy all files from the GIT into a that folder?</p><p>thanks</p>
<p>The piGateway directory will be located whereever you completed your download at. My guess is that your files are actually located in:<br>~/HomeAutomation-master/piGateway. It really doesn't matter where the files are located - You can just go to the directory containing the files and continue on with the instructions. </p><p>Keep in mind that wherever you compile the piGateway program (the &quot;sudo ./Gateway&quot; command) is where the program will be located. Feel free to copy or move the program wherever is handy. And personally, I've called the program from rc.local so that Gateway starts when the raspberry pi is started. That way I don't have to open a terminal window every time the pi is power cycled. </p><p>Good luck!</p>
<p>thanks for your quick response! I actually never downloaded the files from GIT &lt;ahem&gt;, so copied those by hand in the ./piGateway folder. Had to reinstall MQTT for the 3rd time, now from a package (see here: </p><p>http://www.xappsoftware.com/wordpress/2014/10/27/installing-mosquitto-on-raspberry-pi/ )</p><p>AND had to enable SPI pins with help of Rpi-config... Now I'm looking at the msg </p><p>Listening at 915 Mhz...setup complete<br>connection refused</p><p>Not sure if the latter is because I don't have another RF69HW in place or something else, but slowly making progress :)</p>
<p>Not sure... Looking at the Gateway.c code, it definitely looks to be an issue with Mosquitto, but I'm somewhat of a beginner myself in all this. Sounds like you've removed the blacklisting for SPI... Outside of that, I'm not sure what to tell you. </p><p>All I can suggest is to keep looking into your mosquitto setup. Now that you've un-blacklisted your SPI, maybe you could try installing MQTT via apt-get. </p><p>Btw, I'm actually having some problems myself with the RFM69HW chip. I don't think it's related to any of this software, but thought I'd throw it out there. I can't seem to get more than a -90 RSSI. I'm sure that my issue is unrelated to yours, but wanted you to know that my installation isn't running totally perfect either.</p>
<p>Hi, Can I use (Arduino Pro Mini 3.3V) instead of Buono R3 ?</p><p>Thanks</p>
<p>Kind of. The Pro mini probably can't supply the current needed for the RFM69 chip. Might need an off-board voltage regulator.</p>
<p>this is an awesome instructable! I'm working on something similar to this now, thanks for your info and perspective!!!</p>
<p>Can I use a non-switchable Arduino so long as I am careful to attach RFM69 to 3.3V?</p>
<p>No, your I/O would still be at 5V. It's not just the 3.3V power to the RFM69 that you need to worry about.</p>
<p>I was not paying attention when buying the arduino boards (...) and bought non switchable ones, it seems you need something like a level sifter to change the 5v voltage of the board to 3.3v, I bought the one below and hope it will solve that problem so I can hook up the RFM69HW</p><p><a href="http://rover.ebay.com/rover/0/e11400.m1842.l3160/7?euid=df6f828d63b64eb8ac435b0e9f351890&loc=http%3A%2F%2Fcgi.ebay.com%2Fws%2FeBayISAPI.dll%3FViewItem%26item%3D310965629892%26ssPageName%3DADME%3AL%3AOC%3ANL%3A3160" rel="nofollow">New IIC I2C Logic Level Converter Bi-Directional Module 5V to 3.3V For Arduino</a></p>
<p>First of all: thanks for the great project and all the work that you've done! I've bought all the stuff needed and will try to start building as soon as possible. However, when experimenting with the uber sensor and the arduino hooked up to my pc I cannot seem to get any response via the serial monitor. I didn't change anything in the code so far and I am only using the light sensor at the moment but the serial monitor remains empty. The selected baud rate is 9600 which is the same as in the sketch. The serial monitor works fine in other sketches. Do you have any idea what this might be?</p><p>Thanks!</p>
<p>Hi,</p><p>Try taking everything off, and running the RFM69 demo that's part of the RFM69 Arduino library? It sounds you're not even making it through setup.</p>
<p>Hi,</p><p>Indeed there was an issue with the RFM69 transceiver so the problem with the serial monitor is solved now. I've tried the setup with the RFM69 directly connected to the raspberry pi as described by Alexander Bouillot but now I've come to the point where it should all be working. However, when testing nothing is happening. When I've time later this week I will continue searching. Thanks for your response!</p>
<p>For those of you looking for an easier wifi version of this project, you can use this Smart Mouse Trap project:</p><p><a href="http://www.instructables.com/id/Better-Smarter-Mousetrap/" rel="nofollow">http://www.instructables.com/id/Better-Smarter-Mou...</a></p>
<p>How to you provide internet to the arduino from the raspberry pi, which is already connected to the internet? I need to find out a way to provide internet to the arduino without using a ethernet module.</p><p>// ANY IDEAS WOULD BE VERY HELPFUL //</p>
<p>Can I provide internet connection to the arduino using the raspberry pi, without using an Ethernet shield?</p><p>//PLEASE LET ME KNOW//</p>
<p>you should read a tutorial how to connect arduino with raspberry with I2C or RS485 or with usb serial cable</p>
<p>Hello.</p><p>Can I use NRF905 transceiver instead ?</p>
Hello, sorry perhaps I am asking the wrong question. Ca I use nrf905 instead of rfm69hw ? I cannot find in my area rfm69hw.<br>
<p>Hello Uber , a qestion .I would use a system like yours to build Home Automation , control heating, lights ... My first think was KNX but I am considering a solution like yours.Then the question is : how I can connect sensor and module to arduino considering the distance between the arduino board and the devices controlled ? </p><p>In a knx system we have a certidied twist bus cable but in your images I see just some very thick cables. thanks</p>
If you have an OpenHAB server (not raspberryPi) that handles all IoT things do you need the gateway? Can the sensor go directly to the MQTT on server?
<p>hi, i dont have a RFM69HW chip. I want to use this one instead: <a href="https://www.amazon.com/UCEC-XY-MK-5V-Transmitter-Receiver-Raspberry/dp/B017AYH5G0/ref=sr_1_1?ie=UTF8&qid=1476469297&sr=8-1&keywords=rf+433." rel="nofollow">https://www.amazon.com/UCEC-XY-MK-5V-Transmitter-R...</a></p><p>How to cahnge the sketch? 433MhZ communication between 2 Arduinos is working perfect. But i dont know, how to tell my arduino to send the received Data to my ethernet gateway</p>
thank you very much!<br><br>does anyone have a simple sketch to connect all the sensors to the protoshield?
<p>Hi all,</p><p>First, let me say this is an awesome project. The level of detail and clarity is great. Kudos to the author.</p><p>I recently completed the project (only two of the sensors for now) using the combined ethernet gateway method and everything was working fine using an Arduino, a generic Ethernet shield with an RFM69HW 915Mhz module piggybacked on it as the gateway (see pic). The various sensors are using RFM69W 915Mhz modules to communicate with the gateway and they work fine.</p><p>I just upgraded my internet service and decided to upgrade my router from a DLink to a Motorola SBG6580 at the same time. After doing this the gatway will no longer get the DHCP address from the router as long as the RFM69W module is attached. The com port on my Arduino IDE shows the message &quot;Error getting IP address via DHCP, trying again...&quot; repeatedly forever and never gets the IP. If I remove the RF module only then the gateway will connect to the router just fine and returns the IP address. This issue can be reproduced at will simply by removing and replacing the RF module.</p><p>Any thoughts as to why it worked ok with the DLink router but not the Motorola? Because it worked previously I'm doubtful that there is a conflict with the h/w. </p><p>Any input would be appreciated.</p>
<p>Hi, bruster999. Yes this Uber Home Automation is inspiring. The documentation is detailed, but that version has self-admitted limitations, maybe addressed in the comments, but I found all that hard to follow.</p><p> Near the top of Uber..., notice what the author put: </p><p>&quot;[EDIT] I created a forum to collaborate on gateway code. </p><p></p><p><a href="http://homeautomation.proboards.com/board/2/openhab-rfm69-based-arduino" rel="nofollow">http://homeautomation.proboards.com/board/2/openha...</a></p><p> &quot;</p><p>I believe you'll find more reliable info there, including references to CompuTourist's STABLE work on Gateway &amp; nodes which he has provided at</p><p><a href="https://github.com/computourist/RFM69-MQTT-client/archive/master.zip" rel="nofollow">https://github.com/computourist/RFM69-MQTT-client/...</a></p><p>I have provided some detailed documentation on using Buono Uno Arduino compatibles to build a version of CompuTourist's design at</p><p><a href="http://homeautomation.proboards.com/thread/62/success-home-automation-arduinos-rfm69s?page=2" rel="nofollow">http://homeautomation.proboards.com/thread/62/succ...</a></p><p>Besides more recent &amp; reliable information on this project, I believe you'll also find more help (if you supply us with enough info on how you've done things &amp; the results you are getting.)</p><p></p>
<p>Thanks for the feedback. I'll check out your links. </p><p>I think I have narrowed it down to being a library issue as there are a number of threads out there discussing the same issue. </p>
<p>Can't get my transcievers to communicate with each other. Any advice?</p>
<p>I have provided detailed documentation on using Buono Uno Arduino <br>compatibles to build a version of CompuTourist's design AND troubleshooting at</p><p>http://homeautomation.proboards.com/thread/62/success-home-automation-arduinos-rfm69s?page=2</p>
<p>How can I find the mqtt broke ip address?</p>
<p>Hi Bruster!</p><p>My code works with the ethernetshield2. But only a fiew times...after some loops, the arduino hang it up and nothing will be delivered anymore. Have you an idea what happened?</p><p>http://forum.arduino.cc/index.php?topic=375936.0</p>
Hi Ron,<br>I had the same problem. I was using RFM69 radios and it turned out that the both the Ethernet shield and the radio modules needed access to pin 10 on the Arduino controller. Several posts suggested changing the pin requirements in the sketch but I could never get it to work consistently. It would work sometimes and then sometimes not. The original method of having a separate radio and Ethernet gateway seemed to be fairly stable though.<br>Bruce
<p>Hi Bruce</p><p>Thanks for your reply! It's a bit strange. Because the ethernetshield2 should handle multiple SPI. But it seems that it could not. Or i made something wrong. That made me a little bit angry :) My thought was, that you could configure your arduino by buying multiple shields, plug them in and write the code. I feel like a little dreamer :)<br>Ordering a third Arduino is not my prefered solution...but when it is the only solution, i have to buy one again....<br>Thanks, Dino</p>
<p>Hi Bruster</p><p>Could you post your Arduino Project please? This project is the reason why i bought two Arduinos :) Two UNO and one with the Ethernet Shield on top. My setup looks like yours. ;) I'm a &quot;greenhorn&quot; on Arduino programming. I't would be nice, if i could get your code to learn from.</p><p>Thanks for your help!</p>
Hi ronmueller,<br> Here is the code I used. Let me know if you have any questions and I will be happy to help if I can.<br> Bruster<br> <br> /*<br> Based on work from author:&nbsp; Eric Tsai<br> Gateway ncorporating both the RFM69 and the ethernet part<br> Revised by Alexandre Bouillot<br> <br> License:&nbsp; CC-BY-SA, https://creativecommons.org/licenses/by-sa/2.0/<br> Date:&nbsp; 10-23-2014<br> File: Gateway.ino<br> This sketch receives RFM wireless data and forwards it to Mosquitto relay<br> <br> Modifications Needed:<br> 1)&nbsp; Update encryption string &quot;ENCRYPTKEY&quot;<br> 2)&nbsp; Adjust SS - Chip Select - for RFM69<br> 3)&nbsp; Adjust MQTT server address<br> */<br> <br> /*<br> RFM69 Pinout:<br> MOSI = 11<br> MISO = 12 &lt;&lt;&lt;&lt;<br> SCK = 13<br> SS = 8<br> */<br> <br> /*<br> Ethernet Pinout:<br> MOSI = 11<br> MISO = 12<br> SCK = 13<br> SS = 10<br> DI00 to Pin 2 (interrupt)<br> */<br> <br> //general --------------------------------<br> #define SERIAL_BAUD&nbsp;&nbsp; 115200<br> #if 0<br> #define DEBUG1(expression)&nbsp; Serial.print(expression)<br> #define DEBUG2(expression, arg)&nbsp; Serial.print(expression, arg)<br> #define DEBUGLN1(expression)&nbsp; Serial.println(expression)<br> #else<br> #define DEBUG1(expression)<br> #define DEBUG2(expression, arg)<br> #define DEBUGLN1(expression)<br> #endif<br> //RFM69&nbsp; ----------------------------------<br> #include &lt;RFM69.h&gt;<br> #include &lt;SPI.h&gt;<br> #define NODEID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp; //unique for each node on same network<br> #define NETWORKID&nbsp;&nbsp;&nbsp;&nbsp; 100&nbsp; //the same on all nodes that talk to each other<br> //#define FREQUENCY&nbsp;&nbsp; RF69_433MHZ<br> //#define FREQUENCY&nbsp;&nbsp; RF69_868MHZ<br> #define FREQUENCY&nbsp;&nbsp;&nbsp;&nbsp; RF69_915MHZ<br> #define ENCRYPTKEY&nbsp;&nbsp;&nbsp; &quot;MysampleEncryptKey&quot; //exactly the same 16 characters/bytes on all nodes!<br> #define IS_RFM69HW&nbsp;&nbsp;&nbsp; //uncomment only for RFM69HW! Leave out if you have RFM69W!<br> #define ACK_TIME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 30 // max # of ms to wait for an ack<br> #define RFM69_SS&nbsp; 8<br> RFM69 radio(RFM69_SS);<br> bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network<br> <br> #include &lt;Ethernet.h&gt;<br> bool conn_ok;<br> bool mydebug = true;<br> <br> //Ethernet<br> byte mac[]&nbsp;&nbsp;&nbsp; = {<br> &nbsp; 0x90, 0xA2, 0xDA, 0x0D, 0x11, 0x11<br> };<br> byte server[] = {192, 168, 0, 134 }; //Linux server address<br> <br> IPAddress ip(192, 168, 0, 4); //address of gateway device<br> EthernetClient ethClient;<br> #define DHCP_RETRY 500<br> <br> // Mosquitto---------------<br> #include &lt;PubSubClient.h&gt;<br> PubSubClient client(server, 1883, callback, ethClient);<br> #define MQTT_CLIENT_ID &quot;arduinoClient&quot;<br> #define MQTT_RETRY 500<br> int sendMQTT = 0;<br> unsigned long MQTT_reconnect = 0;<br> <br> <br> void MQTTSendInt(PubSubClient* _client, int node, int sensor, int var, int val);<br> void MQTTSendULong(PubSubClient* _client, int node, int sensor, int var, unsigned long val);<br> void MQTTSendFloat(PubSubClient* _client, int node, int sensor, int var, float val);<br> <br> //use LED for indicating MQTT connection status.<br> int led = 13;<br> <br> typedef struct {<br> &nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nodeID;<br> &nbsp; int&nbsp;&nbsp; sensorID;<br> &nbsp; unsigned long&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var1_usl;<br> &nbsp; float&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var2_float;<br> &nbsp; float&nbsp;&nbsp; var3_float;<br> }<br> Payload;<br> Payload theData;<br> <br> volatile struct<br> {<br> &nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nodeID;<br> &nbsp; int&nbsp;&nbsp; sensorID;<br> &nbsp; unsigned long&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var1_usl;<br> &nbsp; float&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var2_float;<br> &nbsp; float&nbsp;&nbsp; var3_float;&nbsp; //<br> &nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var4_int;<br> }<br> SensorNode;<br> <br> void setup()<br> {<br> &nbsp; Serial.begin(SERIAL_BAUD);<br> &nbsp; Serial.println(&quot;Rebooting Ethernet_RFM_Gateways_Combinedv1.1&quot;);<br> <br> &nbsp; //Ethernet -------------------------<br> &nbsp; //Ethernet.begin(mac, ip);<br> <br> &nbsp; //&nbsp; //wait for IP address<br> &nbsp; while (Ethernet.begin(mac) != 1) {<br> &nbsp;&nbsp;&nbsp; Serial.println(&quot;Error getting IP address via DHCP, trying again...&quot;);<br> &nbsp;&nbsp;&nbsp; delay(DHCP_RETRY);<br> &nbsp; }<br> <br> &nbsp; Serial.println(&quot;ethernet OK&quot;);<br> &nbsp; // print your local IP address:<br> &nbsp; Serial.println(&quot;My IP address: &quot;);<br> &nbsp; for (byte thisByte = 0; thisByte &lt; 4; thisByte++) {<br> &nbsp;&nbsp;&nbsp; // print the value of each byte of the IP address:<br> &nbsp;&nbsp;&nbsp; Serial.print(Ethernet.localIP()[thisByte], DEC);<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;.&quot;);<br> &nbsp; }<br> &nbsp; Serial.println();<br> &nbsp; Serial.println(&quot;Connecting to MQTT via ethernet: &quot;);<br> &nbsp; // Mosquitto ------------------------------<br> &nbsp; while (client.connect(MQTT_CLIENT_ID) != 1) {<br> &nbsp;&nbsp;&nbsp; DEBUGLN1(&quot;Error connecting to MQTT&quot;);<br> &nbsp;&nbsp;&nbsp; delay(MQTT_RETRY);<br> &nbsp; }<br> &nbsp; Serial.println(&quot;Connected... &quot;);<br> <br> &nbsp; //RFM69 ---------------------------<br> &nbsp; Serial.println(&quot;Initializing radio&quot;);<br> &nbsp; radio.initialize(FREQUENCY, NODEID, NETWORKID);<br> #ifdef IS_RFM69HW<br> &nbsp; radio.setHighPower(); //uncomment only for RFM69HW!<br> &nbsp; Serial.println(&quot;setHighPower ok &quot;);<br> #endif<br> &nbsp; radio.encrypt(ENCRYPTKEY);<br> &nbsp; radio.promiscuous(promiscuousMode);<br> &nbsp; Serial.println(&quot;Radio initialized&quot;);<br> &nbsp; char buff[50];<br> &nbsp; sprintf(buff, &quot;\nListening at %d Mhz...&quot;, FREQUENCY == RF69_433MHZ ? 433 : FREQUENCY == RF69_868MHZ ? 868 : 915);<br> &nbsp; Serial.println(buff);<br> <br> &nbsp; Serial.println(&quot;setup complete&quot;);<br> }&nbsp; // end of setup<br> <br> byte ackCount = 0;<br> long watchdogInterval = 2000;<br> long watchdog = 0;<br> <br> void loop() {<br> <br> &nbsp; // calling client.loop too often block the system at some point quite early (~up to 5 loop)<br> &nbsp; // Here is a temporized call to it on a regular interval<br> &nbsp; // This need to be as fast as the fastest sensor received<br> &nbsp; //&nbsp; if (millis() &gt; watchdog) {<br> &nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.print(&quot;loop &quot;);<br> &nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(millis());<br> &nbsp; //&nbsp;&nbsp;&nbsp; watchdog += watchdogInterval;<br> &nbsp; //&nbsp;&nbsp;&nbsp; //client.loop needs to run every iteration.&nbsp; Previous version did not.&nbsp; Big opps.<br> &nbsp; //&nbsp;&nbsp;&nbsp; client.loop();<br> &nbsp; //&nbsp; }<br> &nbsp; //Serial.println(&quot;Other loop&quot;);<br> &nbsp; if (radio.receiveDone()) {<br> &nbsp;&nbsp;&nbsp; if (mydebug)&nbsp; Serial.print('[');<br> &nbsp;&nbsp;&nbsp; if (mydebug)&nbsp; Serial.print(radio.SENDERID, DEC);<br> &nbsp;&nbsp;&nbsp; if (mydebug)&nbsp; Serial.print(&quot;] &quot;);<br> &nbsp;&nbsp;&nbsp; if (promiscuousMode) {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (mydebug)&nbsp;&nbsp;&nbsp;&nbsp; Serial.print(&quot;to [&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (mydebug)&nbsp;&nbsp;&nbsp; Serial.print(radio.TARGETID, DEC);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (mydebug)&nbsp;&nbsp;&nbsp; Serial.print(&quot;] &quot;);<br> &nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; if (mydebug)&nbsp; Serial.println();<br> <br> &nbsp;&nbsp;&nbsp; if (mydebug) Serial.println(&quot;before checking payload&quot;);<br> <br> &nbsp;&nbsp;&nbsp; if (radio.DATALEN != sizeof(Payload))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(F(&quot;Invalid payload received, not matching Payload struct!&quot;));<br> &nbsp;&nbsp;&nbsp; else {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; theData = *(Payload*)radio.DATA; //assume radio.DATA actually contains our struct and not something else<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (mydebug)&nbsp; Serial.println(&quot;payload is valid&quot;);<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //save it for i2c:<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SensorNode.nodeID = theData.nodeID;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SensorNode.sensorID = theData.sensorID;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SensorNode.var1_usl = theData.var1_usl;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SensorNode.var2_float = theData.var2_float;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SensorNode.var3_float = theData.var3_float;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SensorNode.var4_int = radio.RSSI;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sendMQTT = 1;<br> &nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp; if (radio.ACK_REQUESTED)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (mydebug)&nbsp; Serial.println(&quot;ACK Requested&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte theNodeID = radio.SENDERID;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; radio.sendACK();<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // When a node requests an ACK, respond to the ACK<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // and also send a packet requesting an ACK (every 3rd one only)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // This way both TX/RX NODE functions are tested on 1 end at the GATEWAY<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (ackCount++ % 3 == 0)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Serial.print(&quot; Pinging node &quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Serial.print(theNodeID);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Serial.print(&quot; - ACK...&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //delay(3); //need this when sending right after reception .. ?<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //if (radio.sendWithRetry(theNodeID, &quot;ACK TEST&quot;, 8, 0))&nbsp; // 0 = only 1 attempt, no retries<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp; Serial.print(&quot;ok!&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //else Serial.print(&quot;nothing&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; }//end if radio.ACK_REQESTED<br> &nbsp; } //end if radio.receive<br> <br> <br> &nbsp; if (sendMQTT == 1)<br> &nbsp; {<br> <br> &nbsp;&nbsp;&nbsp; if (mydebug) Serial.println(&quot;starting MQTT send&quot;);<br> &nbsp;&nbsp;&nbsp; conn_ok = client.connected();<br> &nbsp;&nbsp;&nbsp; if (conn_ok == 1)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led, HIGH);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (mydebug)&nbsp; Serial.println(&quot;MQTT connected OK from MQTT Send&quot;);<br> &nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; else<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led, LOW);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (mydebug)&nbsp;&nbsp; Serial.println(&quot;MQTT NOT connected OK from MQTT Send&quot;);<br> &nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp; //no connection, reconnect<br> &nbsp;&nbsp;&nbsp; if (conn_ok == 0)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.disconnect();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(5000);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (client.connect(&quot;arduinoClient&quot;) != 1)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led, LOW);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(&quot;Error connecting to MQTT&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(4000);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led, HIGH);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led, HIGH);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(&quot;reconnected to MQTT&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MQTT_reconnect = millis();<br> &nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp; int varnum;<br> &nbsp;&nbsp;&nbsp; char buff_topic[6];<br> &nbsp;&nbsp;&nbsp; char buff_message[12];<br> <br> &nbsp;&nbsp;&nbsp; Serial.println(&quot;******************************&quot;);<br> <br> &nbsp;&nbsp;&nbsp; //send var1_usl<br> &nbsp;&nbsp;&nbsp; varnum = 1;<br> &nbsp;&nbsp;&nbsp; buff_topic[6];<br> &nbsp;&nbsp;&nbsp; buff_message[12];<br> &nbsp;&nbsp;&nbsp; //sprintf creates buff_topic by combining SensorNode.nodeID, SensorNode.sensorID, varnum<br> &nbsp;&nbsp;&nbsp; sprintf(buff_topic, &quot;%02d%01d%01d&quot;, SensorNode.nodeID, SensorNode.sensorID, varnum);<br> &nbsp;&nbsp;&nbsp; dtostrf (SensorNode.var1_usl, 10, 1, buff_message);<br> &nbsp;&nbsp;&nbsp; client.publish(buff_topic, buff_message);<br> &nbsp;&nbsp;&nbsp; Serial.println(&quot;Publishing&quot;);<br> <br> &nbsp;&nbsp;&nbsp; decodeBuffTopic(buff_topic);<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;buff_topic: &quot;);<br> &nbsp;&nbsp;&nbsp; Serial.print(buff_topic);<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot; varnum 1: &quot;);<br> &nbsp;&nbsp;&nbsp; Serial.println(buff_message);<br> <br> &nbsp;&nbsp;&nbsp; //%02d means format the integer with 2 digits, left padding with zeros<br> &nbsp;&nbsp;&nbsp; //send var2_float<br> &nbsp;&nbsp;&nbsp; varnum = 2;<br> &nbsp;&nbsp;&nbsp; buff_topic[6];<br> &nbsp;&nbsp;&nbsp; buff_message[7];<br> &nbsp;&nbsp;&nbsp; sprintf(buff_topic, &quot;%02d%01d%01d&quot;, SensorNode.nodeID, SensorNode.sensorID, varnum);<br> &nbsp;&nbsp;&nbsp; dtostrf (SensorNode.var2_float, 2, 1, buff_message);<br> &nbsp;&nbsp;&nbsp; client.publish(buff_topic, buff_message);<br> <br> &nbsp;&nbsp;&nbsp; decodeBuffTopic(buff_topic);<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;buff_topic: &quot;);<br> &nbsp;&nbsp;&nbsp; Serial.print(buff_topic);<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot; varnum 2: &quot;);<br> &nbsp;&nbsp;&nbsp; Serial.println(buff_message);<br> &nbsp;&nbsp;&nbsp; delay(200);<br> <br> &nbsp;&nbsp;&nbsp; //send var3_float<br> &nbsp;&nbsp;&nbsp; varnum = 3;<br> &nbsp;&nbsp;&nbsp; sprintf(buff_topic, &quot;%02d%01d%01d&quot;, SensorNode.nodeID, SensorNode.sensorID, varnum);<br> &nbsp;&nbsp;&nbsp; dtostrf (SensorNode.var3_float, 2, 2, buff_message);<br> &nbsp;&nbsp;&nbsp; client.publish(buff_topic, buff_message);<br> &nbsp;&nbsp;&nbsp; decodeBuffTopic(buff_topic);<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;buff_topic: &quot;);<br> &nbsp;&nbsp;&nbsp; Serial.print(buff_topic);<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot; varnum 3: &quot;);<br> &nbsp;&nbsp;&nbsp; Serial.println(buff_message);<br> &nbsp;&nbsp;&nbsp; delay(200);<br> <br> &nbsp;&nbsp;&nbsp; //send var4_int, RSSI<br> &nbsp;&nbsp;&nbsp; varnum = 4;<br> &nbsp;&nbsp;&nbsp; sprintf(buff_topic, &quot;%02d%01d%01d&quot;, SensorNode.nodeID, SensorNode.sensorID, varnum);<br> &nbsp;&nbsp;&nbsp; sprintf(buff_message, &quot;%04d%&quot;, SensorNode.var4_int);<br> &nbsp;&nbsp;&nbsp; client.publish(buff_topic, buff_message);<br> &nbsp;&nbsp;&nbsp; decodeBuffTopic(buff_topic);<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;buff_topic: &quot;);<br> &nbsp;&nbsp;&nbsp; Serial.print(buff_topic);<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot; varnum 4: &quot;);<br> &nbsp;&nbsp;&nbsp; Serial.println(buff_message);<br> &nbsp;&nbsp;&nbsp; Serial.println(&quot;******************************&quot;);<br> &nbsp;&nbsp;&nbsp; sendMQTT = 0;<br> &nbsp;&nbsp; // Serial.println(&quot;finished MQTT send&quot;);<br> &nbsp; }//end if sendMQTT<br> <br> }//end loop<br> <br> void MQTTSendInt(PubSubClient* _client, int node, int sensor, int var, int val) {<br> &nbsp; char buff_topic[6];<br> &nbsp; char buff_message[7];<br> <br> &nbsp; sprintf(buff_topic, &quot;%02d%01d%01d&quot;, node, sensor, var);<br> &nbsp; sprintf(buff_message, &quot;%04d%&quot;, val);<br> &nbsp; _client-&gt;publish(buff_topic, buff_message);<br> }<br> <br> void MQTTSendULong(PubSubClient* _client, int node, int sensor, int var, unsigned long val) {<br> &nbsp; char buff_topic[6];<br> &nbsp; char buff_message[12];<br> <br> &nbsp; sprintf(buff_topic, &quot;%02d%01d%01d&quot;, node, sensor, var);<br> &nbsp; sprintf(buff_message, &quot;%u&quot;, val);<br> &nbsp; _client-&gt;publish(buff_topic, buff_message);<br> }<br> <br> void MQTTSendFloat(PubSubClient* _client, int node, int sensor, int var, float val) {<br> &nbsp; char buff_topic[6];<br> &nbsp; char buff_message[12];<br> <br> &nbsp; sprintf(buff_topic, &quot;%02d%01d%01d&quot;, node, sensor, var);<br> &nbsp; dtostrf (val, 2, 1, buff_message);<br> &nbsp; _client-&gt;publish(buff_topic, buff_message);<br> }<br> <br> // Handing of Mosquitto messages<br> void callback(char* topic, byte* payload, unsigned int length) {<br> &nbsp; // handle message arrived<br> &nbsp; Serial.println(F(&quot;Mosquitto Callback&quot;));<br> }<br> <br> void decodeBuffTopic (char mybufftopic[6])<br> {<br> &nbsp; //print the OpenHAB Items value<br> &nbsp; int myInt;<br> &nbsp; myInt = atoi(mybufftopic);<br> <br> &nbsp; if (myInt == 4032)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;Garage Distance: &quot;);<br> &nbsp; }<br> &nbsp; else if (myInt == 4122)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;Mailbox count: &quot;);<br> &nbsp; }<br> &nbsp; else if (myInt == 4124)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;Mailbox RSSI: &quot;);<br> &nbsp; }<br> &nbsp; else if (myInt == 4123)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;Mailbox battery: &quot;);<br> &nbsp; }<br> &nbsp; else if (myInt == 4022)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;Garage light: &quot;);<br> &nbsp; }<br> &nbsp; else if (myInt == 4042)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;Garage temp: &quot;);<br> &nbsp; }<br> &nbsp; else if (myInt == 4043)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;Garage humidity: &quot;);<br> &nbsp; }<br> &nbsp; else if (myInt == 3042)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;Dryer: &quot;);<br> &nbsp; }<br> &nbsp; else if (myInt == 3052)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;Washer: &quot;);<br> &nbsp; }<br> &nbsp; else if (myInt == 3032)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;Water leak: &quot;);<br> &nbsp; }<br> &nbsp; else if (myInt == 3062)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;Laundry light: &quot;);<br> &nbsp; }<br> &nbsp; else<br> &nbsp;&nbsp;&nbsp; Serial.print(&quot;unknown: &quot;);<br> }
<p>Wow thanks a lot! I will check it on the weekend!</p>
<p>To be honest, I gave up trying because of this but if you are dedicated to this project you could try separating the RFM69 and the Ethernet shield onto different shields. I am currently trying to do a similar project using Lowpowerlab.com Moteino Framework Gateway (http://lowpowerlab.com/gateway/). It doesn't use OpenHab but has similar functionality. This project uses the LowpowerLab hardware and a RasPi so I'm not sure if it will work with standard Uno hw. There's no need for an ethernet shield in this project.</p><p>If you want to take this conversation off-line you can email me at bruce calder at outlook dot com (no spaces).</p><p>Bruster999</p>
<p>Hi Bruster</p><p>I checked your code an i have the same problem as you with SPI! How do you solve de problem? I connected the RF69 (433mHz) SS port to pin 8. But i can't get an IP.</p><p>Thanks a lot for your reply<br>Ronald</p>
<p>Can't get my transceivers to communicate. Any suggestions? Could it be because I haven't soldered the wires to the board yet?</p>
<p>I can't get any communication. Nothing communicates with openhab and I've followed the instructions over and over</p>
Nice instructable, however I would skip the arduino gateway (i2c to mqtt) and take the i2c directly from the RF gateway to Raspi, then mqtt it.
<p>Can you tell me how I can find the mqtt broker ip address to place in my arduino code</p>

About This Instructable

1,205,428views

4,234favorites

License:

Bio: I love home automation. I hate home automation.
More by electronichamsters:Gingerbread Home Automation Tweeting Pregnancy Test Physical Home Automation Interface 
Add instructable to: