The Internet of Things (IoT) architecture is quickly becoming popular even outside of its originating scenario of home automation. This paper reports the design, implementation, and performance of an IoT hardware and software architecture conceived for the continuous monitoring of livestock located in barns and during grazing. We have adopted the LoRa low power wide area network (LPWAN) technology to cover the diverse environments, and a suitable configuration of web services to perform data storage, analysis, and visualization. Since the LoRa LPWAN (LoRaWAN) medium access control (MAC) layer does not provide a listen-before-talk (LBT) mechanism, we propose a custom MAC layer with LBT-based carrier-sense multiple access with collision avoidance (CSMA/CA). The devised system has been implemented using off-the-shelf hardware, and its performance has also been estimated with the help of a C++ event-based simulator. The preliminary results of our HW implementation on the field confirm the stability of the conceived system and its reliability. suburban or rural environments [15], making this technology viable for both small and medium-scale applications. It has also been shown that a single LoRa gateway can manage a large number of EDs with little maintenance and easy deployment [5].LoRa is a proprietary wireless communication standard developed by Semtech, based on a chirp spread spectrum (CSS) modulation [16,17]. The physical layer (PHY) uses chirp waveforms with different chirp rate, to maintain orthogonality among different classes of users, and with different initial frequency, to convey the actual information message [15]. Channel coding is also used, with interleaving and Hamming block codes. Although the details of the LoRa PHY are not public, several authors have tried and succeeded to reverse-engineer, almost completely, its PHY communication techniques [18].On the other side, LoRaWAN is an open MAC protocol, built on top of the LoRa PHY, that is specifically designed for low-powered devices operating over long-range wireless links. Even though LoRaWAN is commonly used in deploying LoRa networks [19], in this work we have decided to implement a custom MAC layer in order to have more control over the architectural design and to remove some features of the LoRaWAN layer [20] that are not used in our application, such as the large header size when short payloads have to be delivered, and the packet acknowledgments managed by a LoRa server. Continuous and near-real-time monitoring of livestock, in particular of dairy cattle, can help to provide information vital to study the animal behavior and to prevent pathological states that have a negative impact on livestock health [21,22].In particular, our proposed architecture aims at analyzing, almost in real-time, the status of dairy cattle in a cowshed or during grazing by means of sensors embedded in smart collars. By the way, this approach may be extended also to other kinds of livestock, such as beef cattle, swine, ovine, and even equine.In this paper we first de...