{"id":20696864,"url":"https://github.com/araobp/sensor-network","last_synced_at":"2025-07-24T12:20:39.055Z","repository":{"id":145719288,"uuid":"78276156","full_name":"araobp/sensor-network","owner":"araobp","description":"Wired sensor network for IoT with PIC16F1 MCU","archived":false,"fork":false,"pushed_at":"2018-01-08T07:56:41.000Z","size":28875,"stargazers_count":8,"open_issues_count":2,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-22T20:50:29.162Z","etag":null,"topics":["daisy-chain","i2c","iot","mplabx","pic16f","sensor-network"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/araobp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-07T11:52:57.000Z","updated_at":"2023-06-16T08:04:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"c4af1b51-b397-4b68-a2b0-e35df5b4ad14","html_url":"https://github.com/araobp/sensor-network","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/araobp/sensor-network","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/araobp%2Fsensor-network","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/araobp%2Fsensor-network/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/araobp%2Fsensor-network/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/araobp%2Fsensor-network/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/araobp","download_url":"https://codeload.github.com/araobp/sensor-network/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/araobp%2Fsensor-network/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260301959,"owners_count":22988715,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["daisy-chain","i2c","iot","mplabx","pic16f","sensor-network"],"created_at":"2024-11-17T00:15:41.148Z","updated_at":"2025-06-17T06:03:28.853Z","avatar_url":"https://github.com/araobp.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Local wired sensor network\n\n![sensor_networ](./doc/sensor_network.jpg)\n\n## Background and motivation\n\n### Problems\n\nI have developed a lot of IoT prototypes so far, then I have observed that most of my IoT projects require a communication protocol for wired sensor networking, satisfiying the requirements below:\n\n- low power consumption and lower voltage (5V or 3.3V)\n- bus topology (daisy-chain) rather than hub and spoke (star)\n- two-wire or one-wire\n- cheap (\u003c$10 per node on average) and open\n- small software footprint\n\nThere are a lot of such technologies for in-vehicle network (CAN/LIN), buidling management (BACnet) or factory automation (PROFINET), but none of them satisfies all the requirements above.\n\nThis is a project to develop a networking protocol and building blocks for local wired sensor network.\n\n## Network architecture\n\nThe network is composed of multiple nodes(blocks) and one scheduler.\n\n```\n     -----+---------------------+---------------------------+-------- I2C bus\n          |                     |                           |\n   +------|-----+        +------|-----+              +------|-----+\n   |   [node]   |        |   [node]   |              |   [node]   |\n   |      |     |        |      |     |              |      |     |                      (         )\n   |  [sensor]  |        |  [sensor]  |              |      +------------[IoT GW(*1)]---(   Cloud   )\n   +------------+        +------------+    . . .     +------------+                      (         )\n       block                 block                     scheduler\n      (slave)               (slave)                    (master)\n\n(*1) I use Node-RED (on RasPi or PC) and Android as IoT gateways.\n\n```\n\nNote: I am going to support CAN bus as well.\n\n### Interfaces among blocks\n\nAll the blocks developed in this project support [Plug\u0026Play protocol](./doc/PROTOCOL.md) that runs on UART.\n\n```\n                    USB hub\n                     +---+\n[block A]--UART/USB--|   |\n[block B]--UART/USB--|   |--USB--[IoT GW]\n[block C]--UART/USB--|   |\n                     +---+\n\n               hub\u0026spoke topology\n```\n\nIt also runs on I2C.\n\n```\n      \u003c- - - - - I2C backplane - - - - -\u003e\n[block A]---[block B]---[block C]---[Scheduler]--UART/USB--[IoT GW]\n\n            bus topology (daisy-chain)\n```\n\n### 8bit MCU as core of node\n\nI use [PIC16F1829/PIC16F1825](http://ww1.microchip.com/downloads/en/DeviceDoc/41440A.pdf) that satisfies the requirements of this project.\n\nClock speed:\n- 4MHz typical\n- 32MHz (8MHz w/ PLL) for high sampling rate\n\n### Node prototype\n\nThe base board below is a common hardware part of node:\n\n![prototype3](./doc/prototype3.png)\n\n![pico2](https://docs.google.com/drawings/d/1_WCC4vuPbIT2im9c337ibk5xEq9WKzrT9907IOWTCCA/pub?w=680\u0026h=400)\n\n![pico1](https://docs.google.com/drawings/d/e/2PACX-1vTHoT0TZIyVhAgkDVHyuWkc1-_6oFHT2mF53g2q36bgH_qxplkvvRIkJ3PqJBNuTZauhhMmSiemMoZO/pub?w=680\u0026h=400)\n\n## Networking examples\n\nOne I2C master and three I2C slaves are connected with each other via backplane bus on the back of base board\n\n![compact2](./doc/compact2.png)\n\nA similar construct to the above, but all the boards are connected with each other in a daisy-chain manner:\n\n![daisy_chain](./doc/daisy_chain.png)\n\n## Implementation\n\nI use Microchip's MPLAB-X IDE. I also use [MPLAB Code Configurator (MCC)](http://www.microchip.com/mplab/mplab-code-configurator) to automatically generate code for EUSART, I2C(master/slave), ADC, Timer etc. I modify the generated I2C slave code to support Plug\u0026Play protocol.\n\n### Plug\u0026Play protocol\n\n[Plug\u0026play protocol specification](./doc/PROTOCOL.md)\n\nSome blocks operates in pubsub mode -- [how it works](./doc/PUBSUB.md).\n\n#### Implementation: common part among all nodes\n\nAll nodes need to import this [protocol library](./blocks/pic16f1829/lib/protocol.X):\n- [Step1: include the protocol library directory](./doc/mcc_eusart4.png)\n- [Step2: include the protocol library in your project](./doc/mcc_eusart3.png)\n\n#### Implementation: I2C-slave-specific part\n\nI2C slaves also require I2C-slave-specific code -- I modified MCC-generated I2C slave code (i2c1.c) to support the protocol on I2C slave side. See this modifed code: [i2c1.c](./blocks/pic16f1829/i2c_slave_lcd.X/mcc_generated_files/i2c1.c).\n\n#### Coding\n\nThe following is an example of main routine:\n\n```\nvoid main(void)\n{    \n    // Protocol initialization\n    PROTOCOL_Initialize(DEVICE_ID, NULL, NULL, NULL, inv_handler, 250);\n\n    // avoid using SYSTEM_Initialize() automatically generated by MCC,\n    // because I2C1_Initialize() must be last in the initialization order\n    PIN_MANAGER_Initialize();\n    OSCILLATOR_Initialize();\n    WDT_Initialize();\n    ADC_Initialize();\n    TMR0_Initialize();\n    EUSART_Initialize();\n    I2C1_Initialize();\n\n    // Enable interrupt\n    INTERRUPT_GlobalInterruptEnable();\n    INTERRUPT_PeripheralInterruptEnable();\n\n    // Infinite loop\n    PROTOCOL_Loop();\n}\n```\n\n### Blocks\n\n#### PIC16F1829\n\nIn this project, PIC16F1829 MCU is used for general-purpose blocks such as a scheduler or LCD controller.\n\n- [5V: Scheduler (BACKPLANE-MASTER)](./blocks/pic16f1829/i2c_master.X)\n- [5V: Character LCD actuator block (AQM1602XA-RN-GBW)](./blocks/pic16f1829/i2c_slave_lcd.X)\n- [5V: Acceleration sensor block （KXR94-2050)](./blocks/pic16f1829/i2c_slave_accel.X)\n- [5V: Speed sensor block (A1324LUA-T)](./blocks/pic16f1829/i2c_slave_speed.X)\n- [5V: Temperature and humidity sensor block (HDC1000)](./blocks/pic16f1829/i2c_slave_temp.X)\n- [5V: Position detector block](./blocks/pic16f1829/i2c_slave_position.X)\n\n#### PIC16F1825\n\nIn this project, PIC16F1825 MCU is used for purpose-specific blocks such as a position detector having multiple analog ports.\n\n- [5V: Position detector block](./blocks/pic16f1825/i2c_slave_position.X)\n\nA typical usage of the position detector is to detect a position of a moving object such as a doll on a catwalk miniature (not a belt conveyer).\n\n![catwalk](./doc/catwalk.png)\n\n![position_detector](./doc/position_detector.jpg)\n\nExample of its usage\n```\n#WHO\n$:WHO:MULTI_A1324LUA_T\n#MAP\n$:MAP:21,22\n#RSC\n$:RSC:0,0,0,0|0,0,0,0|0,0,0,0|0,0,0,0|0,0,0,0|0,0,0,0|0,0,0,0\n#POS:0\n#WSC:21\n#POS:1\n#WSC:22\n#RSC\n$:RSC:21,22,0,0|0,0,0,0|0,0,0,0|0,0,0,0|0,0,0,0|0,0,0,0|0,0,0,0\n#I2C:21\n#WHO\n$:WHO:21\n#SET:15\n#I2C:22\n#SET:9\n#I2C:1\n#STA\n%21:UINT8_T:0,1,0,0\n%21:UINT8_T:0,0,0,0\n%21:UINT8_T:0,0,0,1\n%21:UINT8_T:0,0,0,0\n%21:UINT8_T:0,0,0,1\n%21:UINT8_T:0,0,0,0\n%21:UINT8_T:0,1,0,0\n%21:UINT8_T:0,0,0,0\n%21:UINT8_T:1,0,0,0\n%21:UINT8_T:0,0,0,0\n%21:UINT8_T:0,1,0,0\n%21:UINT8_T:0,0,0,0\n%21:UINT8_T:0,0,0,1\n%21:UINT8_T:0,0,0,0\n%21:UINT8_T:0,0,1,0\n%21:UINT8_T:0,0,0,0\n%22:UINT8_T:0,0,0,1\n%22:UINT8_T:0,0,0,0\n%22:UINT8_T:1,0,0,0\n%22:UINT8_T:0,0,0,0\n```\n\n#### Initial config\n\nWrite I2C slave address on the blocks. For exmaple, if the address is 16 in decimal, then:\n```\n#WDA:16\n#RDA\n$:RDA:16\n```\n\n### CLI example\n\n```\n#WHO\n$:WHO:BACKPLANE-MASTER\n#SCN\n#MAP\n$:MAP:16,17,19\n$:RSC:0,0,0,0|0,0,0,0|0,0,0,0|0,0,0,0|0,0,0,0|17,0,0,0|0,0,0,0\n#POS:12\n#WSC:19\n#RSC\n$:RSC:0,0,0,0|0,0,0,0|0,0,0,0|19,0,0,0|0,0,0,0|17,0,0,0|0,0,0,0\n#STA\n%17:UINT16_T:0\n%19:FLOAT:-0.07,-0.08,0.94\n%19:FLOAT:-0.05,-0.06,0.94\n%19:FLOAT:-0.07,-0.06,0.94\n%19:FLOAT:-0.06,-0.07,0.93\n%19:FLOAT:-0.07,-0.08,0.94\n%19:FLOAT:-0.05,-0.09,0.94\n%19:FLOAT:-0.06,-0.09,0.94\n%19:FLOAT:-0.05,-0.08,0.93\n%19:FLOAT:-0.07,-0.08,0.93\n%19:FLOAT:-0.06,-0.07,0.94\n%19:FLOAT:-0.07,-0.07,0.94\n%19:FLOAT:-0.06,-0.07,0.93\n%19:FLOAT:-0.07,-0.08,0.93\n             :\n#I2C:16\n$:WHO:16\n#CLR\n#LED:ON\n#LED:OFF\n#STR:Hello World!\n#NWL\n#STR:Guten Tag!\n#I2C:1\n            :\n#STP\n*:STP:ACK\n```\n\n### Interface to the scheduler\n\nUse the CLI to control the scheduler or stream sensor data to the cloud.\n\n#### via VCP over ttyUSBX(Linux)/COM port(Windows)\n- Python: [pySerial](https://pythonhosted.org/pyserial/)\n- Node.js: [serialport](https://www.npmjs.com/package/serialport)\n\n#### via D2XX driver\n- Java/Android: [D2XX driver](http://www.ftdichip.com/Android.htm)\n\n### Physical wiring among blocks\n\nIn some projects, I used telephone line (6P4C) with RJ11 moduler plug/jack, as I2C bus. I used this tool to make wires among nodes: [Crimper for RJ11](http://www.mco.co.jp/products/tel/telephonetool/ta-642t.html). Telephone line makes physical wirling very easy.\n\n6P4C telephone line is suitable for I2C with power line: SDA, SCL, 5V, GND.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faraobp%2Fsensor-network","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faraobp%2Fsensor-network","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faraobp%2Fsensor-network/lists"}