Monitoring An Electric Fence With LoRaWAN

Making sure that an electric fence which is keeping one’s cattle and sheep from wandering off is still working properly seems like a fairly daunting task, especially when this fence is quite a distance from one’s home so checking up on it is time-consuming. After a friend of [kiu] got called a few times by the police because some of the sheep had pulled a prison break, the obvious technological solution was to IoT-enable the fence with LoFence.

This solution is nothing if not elegant in its simplicity. For phoning home with status data, the system uses the Microchip RN2483 IC, which handles pretty much all aspects of LoRaWAN, so that one merely has to send data to its serial interface to transmit. Because this system uses The Things Network (TTN) there are no service costs due to the low data rates. This was the easy part, aside from having to add a LoRaWAN gateway to boost the signal in the area with the electric fence.

With that side covered, the rest of the build features an AVR ATmega328p MCU, a resistor divider and op-amp (TLV9062) along with some passives. The resulting circuit measures voltage, essentially to detect whether the fence is still forming a full circuit. Hacking into the little box that energizes the fence might be a possible upgrade there, but at least it is a fairly uncomplicated way to measuring things. Electric fences do work best with a voltage on them, after all.

At the other end of the LoRaWAN network, the data is parsed and analyzed by a service so that it can be displayed on a Grafana dashboard, ensuring that a single glance suffices to see the current state of the fence and whether one has to dash out in the rain at 1 AM to fix it or not.

57 thoughts on “Monitoring An Electric Fence With LoRaWAN

  1. I need one of these. Or more likely several of them. Debugging an electric fence to find the short or the open that is making it fail usually involves a lot of walking. Sometimes miles and miles of walking.

    Does not solve all the problems, though. My neighbor’s livestock get out because of his poor understanding of fences, and I cannot really help, because he does not know the difference between a short and an open, and is not of any mind to learn.

        1. I had a thought, couldn’t you use a swr meter? So like, you are just measuring the standing waves and I guess mathematically you could determine the length of the line as it’s able to act like an antenna. It could sweep through a basic set of frequencies getting the standing wave ratio and then it can solve for the optimal line length (versus the other way around). Shouldn’t be too hard to rig this up on a cheap analogue SWR meter, I looked up these reflectometry devices and they seem to be pricey.

          1. The thing that I have no solution for, anything you use to measure this, needs to be prepared to withstand 12KV spikes in case the fence suddenly works again.

    1. If you really want to hack, I built something years ago (60’s). It would probably violate all sorts of FCC regulations. Essentially you use a tunnel diode to generate pulses with very sharp edges. Use the reflected wave to pinpoint the distance to a break; there is a trick (that I have forgotten) to catch both open/short. It gets a little hazy here but I think it was driven by a 500kHz oscillator, snap the diode and then it unsnaps when the wave is reflected. For testing I used TV twin x cable; which would be unreasonable for a fence but I thought I could manage without it. It’s touchy, I had access to a IBM computer with APL and ran tolerancing; it would/did run over temperature and part variations. The computer had 4-8k (I forget what) and then rolled data back to disk. LOL! the thing sounded like a washing machine :):) . I was a little worried about breaking it and getting into trouble.
      I got it to work but let it go. The wife got agrevated having twinX running all over the house and back:) And the FCC problem.
      If your interested I could try to reconstruct the circuit but using tunnel diodes requires care; they have broad tolerances. If they still make them. Snap diodes coud probably be made to work but the tunnell diodes are bistable (with correct bias and bias impedance) and nonlinear. I still think they are probably a good fit; on the cheap.
      YMWV
      I presently do not have the equipment to build it though.
      Ray

  2. I do wonder why a lot of the builds using the RN2348 always have an additional MCU.

    I have only done one example on one of Microchips training courses, but it’s possible to run basic user code on the RN2348 module itself.

    http://ww1.microchip.com/downloads/en/DeviceDoc/40001864B.pdf

    The module is a combo of a PIC18F & a SX1276 internally, so you can pull in the LoRaWAN™ Lib for MPLAB, and as long as you don’t get in the way of the LoRa stack too much, you can do some basic stuff, like monitoring a digital signal, *maybe* some ADC stuff, though I can’t recall, and might be too intensive w/ LoRa.

    Could always do the tricky stuff “off-air”, and handle the LoRa when all the data is good to go!

      1. Where did you find either the AcSIP part or the Ronoth boards? Everything I’ve found so far for the Ronoth board says it’s still in the Crowd Funding stage. And AcSIP doesn’t have any distributors, nor do they publish any prices.

        I’ve used the Murata LoRa modules on a handful of projects. They’re not the cheapest solution, but they’re already FCC certified, so we only need to do a scan, and not a full suite. If we go higher volume we’ll roll our own design and certify.

      1. Several useful links are in comments above. (But a SWR meter is NOT what you are looking for.)

        Essentially, you send a pulse (or a step transition, or a frequency sweep, depending on implementation) onto the line. That signal propagates down the line and will reflect some power at line discontinuities (opens or shorts). By measuring the polarity of that reflection you determine whether it’s an increase in line impedance (an open) or descrease (a short). By measuring the time for its return, you determine how far away it is.

        It depends on the line being relatively low loss and having a fairly uniform impedance, so works best for well-characterized lines like co-ax or ladderline (or optical fiber), but likely will work for a uniform decently-built fence.

        Because it does depend on pretty wide-band power injection and reception you’d need to take some care in protecting yourself against the HV spikes, but not loading the line so much they get sopped up.

        1. If I am not mistaken, the fence differs from a transmission line in at least two ways. First, it is neither ladderline nor co-ax. Second, it is not terminated in a load except at the instant an animal temporarily becomes a load. An additional complication is that many fences are not merely a single unterminated wire, but instead a tree of wires. Many of mine are three-wire (or more) fences, and it is not uncommon for a tap from one area’s fence to feed a second fence that goes off in a different direction. Also, most of my fences use wires of various sizes and materials.

          1. Cool, so that’s a feature, not a bug: You could use a TDR to characterize and map the entire fence network, warts and all, and then report on any significant change from the map.

            You’d probably get signal changes from rain, ice, tree branches, and animal contact, and dog pee, and maybe even the odd bird alighting.

            Oh dear, does this mean we can use IoT and cloud processing and machine learning and all that stuff to monitor an electric fence now?

    1. TDR? To test an electric fence?

      You want to use a TDR on a wire that has barbs every so many feet, is wrapped around a insulator at ever fence pole and probably has patches made by twisting bare wires together outdoors?

      That’s a lot of reflections to sort through. Good Luck!

  3. I dont think monitoring the box that generates the electric fence pulses generally will help.

    There’s way to many failure modes that happen in the field, and the length/capacitance/inductance of the wires would make central monitoring difficult. Field measurement is definitely more reliable.

  4. Hi I’m interested in this product I’m a farmer who used electric fence my whole life I’ve been using the handheld lately that gives voltage direction of the short and amperage draw. If this unit would work like the people are talking about I was thinking I would put it on the end of the fence away from the Energizer unit. That way I would know if the fence is working . If I’m looking for a problem I always go to the weakest link at the end of the fence the readings will be good or better at the Energizer but going to the end of the fence always tells the story If I am looking for a problem I work backwards on the fence. Or from the end of way from the Energizer. you could find the short a lot faster that way. How do I get more information on this ?

  5. I hate batteries.

    Seriously, my life’s goal is to eliminate them wherever possible and encourage others to do the same. They are the first thing to need replace and if left too long, allowed to get hot or just plain defective they leak acid and poisonous heavy metals all over your hard work.

    That’s the one thing I would change. Lose the battery!

    My assumption was that you are trickle charging it using electricity from the fence that you are monitoring. After reading other comments I think maybe it isn’t charging at all, you just have to periodically replace the battery. Why not run it off the fence itself?

    So anyway, I thought.. replace the battery with a supercap. It might only run a short time after the fence goes down but that’s ok. Whatever alerts you at the receiving end should just be programmed to keep alerting you until either it comes back up or you turn it off.

    Then again… why does it have to transmit after the power goes out at all? Maybe you don’t even need the super cap. Just rectify the power from the fence and use it to periodically transmit an “all good” message. If the “all good” message goes silent then all is not good. Done!

    1. I actually thought about that as well, but I didn’t want to make it even more complicated, rather focus on how to facilitate TTN.

      Assuming not receiving a message means fence failure will likely in the real world lead to trouble. The LoRaWAN coverage of rural areas is not that great, so missed packets are a typical situation you have to deal with. The TTN network has no QoS whatsover and with rising popularity has outages on a regular basis.

      1. it can be a star arrangement sometimes, so need to monitor multiple branches.
        but the LC circuits could simply be tuned for different frequencies.
        and it could be listened to from the HV PS location, AC couple it off the line and can choose an LC frequency(frequencies) of your choice.

  6. i would consider TDR by creating a pulse by pulling down the HV briefly. not all the way to ground, just to create a suitable pulse. with a floating gate, maybe opto coupled, it’s only holding off the voltage of the pulse height.

    AC couple out the results.

  7. Hi Kiu

    Great project with huge application. I am very much the noob with electronics so please forgive my ignorance. Can you tell me if there is any permanent voltage drop in the fence as a result of having the high voltage fence wire permanently earthed? And if not, why not?

    1. Electric fences suffer from a gigantic amount of misunderstanding. A friend claimed her fence never worked, no matter what brand-new fence charger she used. The problem was that she felt it needed a “complete circuit” so she tied the far end of the fence directly to the ground (or “earth”) terminal on the charger. That’ll be your “permanently earthed” condition. Total misunderstanding. Total failure.

      Other people feel that lacking “just two or three” of the necessary insulators on steel fence posts won’t cause any problem. Again, hot tied to ground. Failure.

      Some people feel reassured that the fence is working fine if, when making the connection from “hot” to the fence, they see a big, fat spark. Sorry, the fence is probably shorted to ground somewhere, and it’s time for that long, long walk.

      A. P. Ershov famously said, “Finding errors in a computer program is like finding mushrooms in the forest. Having found one, look for others in the same place.” This principal applies to hardware as well, and in our current discussion, potentially to fences. If one insulator is broken, others nearby will be loose or missing.

      On that long walk, you will wish you could turn the fence on and off remotely, so that you could effect repairs without getting zapped and then test to see if your repairs are actually complete, or instead there are other problems.

      1. Thanks Peter. I should clarify my question. Unless I’m mistaken this monitor is permanently connected between the hot wire and ground. If this is the case what permanent voltage drop is occurring in the hot wire?

        1. It causes no significant drop in the voltage at the hot. As [kiu] says, the voltage divider is about 21 megohms. You could hang a hundred of them on the fence and the effect would be less than wet grass.

          Some of the things you can do with a fence have more of an effect than might initially be suspected. For example, you can run the wire underground, perhaps at a gate. A substantial about of underground wire will form a capacitor to ground, and that might flatten the waveform of the voltage spike. (No, I’ve not scoped it to check.)

          I run quite a lot of fence through weeds, and notice that where I might think I should have 8 to 10 kv, there’s really only about 1 or 2 kv. That’s fine for horses, but not sheep or goats. Horses respect fence and can be left with it turned off for weeks, but goats test it frequently.

      2. Shutoffs are very helpful, especially to find the short you missed walking by. Start at an end, then test power changes shutting off sections. Or with miles starting in the middle can shorten the job. But you need shutoffs. And if you can’t fix it hot it is not so far to shut off.

    2. An electric fence is an open circuit. The part which zaps you (hot wire) is not connected to anything. If an animal or human touches the fence, he is creating the shortcut from hot to ground. That shortcut through tissue, muscles, etc is around 1k Ohm.

      The measurement device uses a resistive divider with 21M Ohm between hot and ground. So there is kind of a shortcut because of the device, but it is really weak (12000 V / 21000000 Ohm = 600 uA). The device will not have much of an impact on the overall fence.

  8. What would be valuable to me is a GPS type collar for livestock protection dogs. Something that has a long life, month plus? battery life. Something that will correct the dog when out of its area. I was offered free use of pasture but could not use it as the fences were not dog tight. They held livestock just fine but not dogs or coyotes, which will both dig under even new fences. I knew I would be losing sheep weekly without dogs with them.

    1. It’s awfully expensive to install buried 2×4 mesh fence, and in some places nearly impossible, but sometimes that’s what’s needed. The people at Premier seem to have good fencing solutions, and we have had better luck with their expensive German-built fence chargers than with the ones available locally at the farm stores. Premier have several herds of sheep, in Iowa I believe, and so have first-hand experience in the products they sell.

      Another method for sheep is to use natural barriers. The Forbes family once kept sheep on Nashawena Island, and avoided all fencing problems, but coyotes swam in and destroyed them. They now have Scottish Highland cattle there.

      Well-trained dogs can really help with sheep, and I suspect that the GPS collar is probably a mistaken approach. But how to tell? Build a few, test them out, and report the results here!

      We keep a small jennet donkey with our sheep, and one of our neighbors keeps a big jack donkey with his cattle. In our area it is always open season on coyotes. 00 buckshot is preferred to 22 LR because of a more certain kill, the coyote’s target area being so small.

      1. Buried wire is not feasible for 5 miles that goes through steep areas, creeks, and ditches. I do get a lot of fence supplies from Premier. But for large areas ensuring that guard dogs will gets buzzed, maybe just with a vibration, to encourage return, seems like the best solution. 2 relatives went out of the sheep business, one was on 1,200 acres, due to fear of dogs getting out. I have no idea how to design electronic collars. I just would like to see someone do it.

    1. You don’t have to use TTN. You could use a Multitech running NodeRed and handle all the processing and alerts locally. Use a ESP32/ESP8266 to poll the MQTT feed for a local indicator, and also forward an alert up the internet if it’s up.

    2. Not much. It tries to send the current fence status once an hour. At some point the network will be back.
      You have to keep in mind that depending on which kind of animal you are herding, they can go quite some time till they notice that the fence is actually out of order. This application doesn’t need that kind of reliability where an immediate action is required.
      TTN has no SLA, but measuring over a three months period didn’t show significant (>24h) outages in the EU region.

  9. What about a cheap LoRaWAN monitor every 1/4 mile on the fence? I don’t know much about how electric fences work, but perhaps measure the magnetic field around the fence? When that field goes away, there has been a break “before” the sensor. This would localize the break within a 1/4 mile. Of course, the distance between sensors can be adjusted.

  10. Hi Kiu,
    Nice project. One question I have is in relation to the errata where you note that you perhaps should have placed the measurement circuitry to float somewhere mid-fence voltage. I’m curious why you would want the electronics floating at high potential rather than close to earth?

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.