{"id":13802479,"url":"https://github.com/Josverl/micropython-p1meter","last_synced_at":"2025-05-13T13:31:24.651Z","repository":{"id":48409102,"uuid":"302170065","full_name":"Josverl/micropython-p1meter","owner":"Josverl","description":"A ESP32 sensor to read an p1 electricity meter and publish this to MQTT and HomeAssistant, written in MicroPython","archived":false,"fork":false,"pushed_at":"2024-07-02T20:41:14.000Z","size":2189,"stargazers_count":21,"open_issues_count":1,"forks_count":15,"subscribers_count":6,"default_branch":"main","last_synced_at":"2024-11-15T09:26:12.566Z","etag":null,"topics":["esp32","homeassistant","mqtt","p1meter","simulation"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Josverl.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2020-10-07T21:59:03.000Z","updated_at":"2024-07-02T20:41:18.000Z","dependencies_parsed_at":"2024-01-13T02:59:07.623Z","dependency_job_id":"ab9a4162-5db1-48a4-aa6f-e6cbe12fbdde","html_url":"https://github.com/Josverl/micropython-p1meter","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Josverl%2Fmicropython-p1meter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Josverl%2Fmicropython-p1meter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Josverl%2Fmicropython-p1meter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Josverl%2Fmicropython-p1meter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Josverl","download_url":"https://codeload.github.com/Josverl/micropython-p1meter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225218076,"owners_count":17439713,"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":["esp32","homeassistant","mqtt","p1meter","simulation"],"created_at":"2024-08-04T00:01:45.334Z","updated_at":"2024-11-18T17:31:27.966Z","avatar_url":"https://github.com/Josverl.png","language":"Python","funding_links":[],"categories":["Libraries"],"sub_categories":["Sensors"],"readme":"# MicroPython P1 meter \n\n\n\n![image-20201220223145001](docs/img/image-20201220223145001.png)\n\nOverview \n\n\n\nIf you want to monitor your energy usage\n\n\n\n**The P1_Meter is a a sensor device that:** \n\n- connects to your electricity meter's P1 port \n- Reads the P1 output using one of the ESP32 hardware UARTs\n- reads the OBIS coded information \n- runs a CRC check on the received information to suppress incorrect readings\n- translates the OBIS codes to readable topics\n- publishes the information to a MQTT server on your network\n- so you can read the information in HomeAssistant (or any other tool) \n- uses mDns to simplify configuration and avoid the need for fixed IP addresses or static DNS leases\n- allows the configuration to be changed though a simple config file \n\n**in addition it:** \n\n- logs a few relevant sensors statistics to MQTT (client_id,  free memory, CPU core temperature)\n- Allows you to view logs and other terminal output over wifi via the webrepl\n- Allows you to update update the configuration over the network using FTP \n\n- how does it work\n  \n  [Diagram]\n\n- show me some pictures \n   [a few screenshots / photos]\n\n\n## Hardware \u0026 Firmware \n**Hardware:** \n\n - Any ESP32 board\n   no SPI ram is required,  the firmware auto detects if it is present and runs on either.\n - A single 1K Ohm resistor \n - A cable with a 4 or 6 pin RJ12 connector \n   (optionally a 4 pin RJ11 connector and be used) \n - Some sort of casing \n - Optional: a sort strip of 3 neopixels to use as indicators.\n   \n\n**Firmware:**\n\n - Use micropython 1.13 for or newer for the esp32 http://micropython.org/download/esp32/\n   you can download the firmware directly from \n   \n    - ESP32 GENERIC-SPIRAM : [esp32spiram-idf3-20200902-v1.13.bin](http://micropython.org/resources/firmware/esp32spiram-idf3-20200902-v1.13.bin)\n   \n - Install the micropython firmware to the ESP32 using the procedure documented in:\n   [Getting started with MicroPython on the ESP32 — MicroPython 1.13 documentation](http://docs.micropython.org/en/latest/esp32/tutorial/intro.html#deploying-the-firmware)\n\n - the code in this repo; see the next paragraph.\n\n   \n\n## Install the P1 meter software on the ESP32 MCU\n - git clone this repo\n  - adjust config.py settings :\n    - homenet : WiFi SSID and Password \n    - broker : MQtt broker address, port, user and password\n    - RX pins to connect to the P1 Port\n    - Options (TX pin if you want to test drive without a connection to a P1 port)\n - upload code from /src folder to the board\n    - flash Micropython 1.13 or newer ( 1 time) \n    - upload the source code ( using pymakr or any other tool)\n  - reboot the board \n\n\n### Assembling the P1 meter circuit diagram\nthe circuit is quite simple:\n\n  1.  ESP32, any devkit will do and will allow you to simply flash the Firmware and power the ESP by USB\n  2.  a 1K Ohm resistor , needed to stabilize the serial data by pulling it to +3.3 volt \n  3.  a female RJ-12 (or RJ11) connector, or you could solder a 4 wire phone cable directly to the board \noptionally\n  4.  some sort of case / box or container \n  5.  a small piece of perfboard , although if you want it should be able to solder directly on the devkit as well\n  3.  a string of 3 neopixel leds to provide status on Network , MQTT and the received P1 data, \n      if you rather want a few simple leds to provide signals  there are enough pins left , but switched to neopixels to simplify the wiring.\n      the code should be in one of the earlier commits.\n\n![circuit diagram](docs/circuit.png) \n\n\n\nThe ESP hardware UART 1 is used to connect to pin 2 ( you specify in config.py \nthis allows normal functionality to use the USB port (UART 0) for configuration and monitoring of the ESP32.\n\n### Connection to P1 meter \n\nThe RJ12 connector in the electricity meter uses the following layout \n![Rj12](docs/RJ-12.png)\n\nConnect the ESP32 to an RJ12 cable/connector using the below diagram.\n\n**Connection via straight 4/6 wire cable :**  \n*Note:* This will reverse the pin numbers on the female connector that you are using\n\n| RJ12 P1       | cable|RJ12 Meter| ESP32 Pin | RJ12 6w cable | 4w cable| comments\n| --------------|----- |----------| ----------| ----------|---------|------------\n| 1 - 5v out    | ===\u003e | 6        | 5v or Vin |           |         | [Optional] 1️⃣ \n| 2 - CTS       | \u003c--- | 5        | gpio-5    | blue      | black   | Clear to Send,  High = allow P1 Meter to send data\n| 3 - Data GND  | ---- | 4        | GND       | black     | red     | \n| 4 - nc        |      | 3        | -         |           | green   | \n| 5 - RXD (data)| ---\u003e | 2        | gpio-15   | yellow    | yellow  | 1K external pull-up resistor needed\n| 6 - Power GND | \u003c=== | 1        | GND       |           |         | [Optional] 1️⃣\n\n1️⃣ max 250 mA When using a 6 pin cable you can use the power source provided by the meter.\n\n\n## Adjusting for Straight or Cross cables\n\n\nIf you are unsure which cable will be used to connect,\nor if you want to build some flexibility in your hardware design , it is possible to allow both cable types by :\n - Output connecting both Pin 3 and Pin 4  Ground \n\nthis will ten allow you to swap the functions of pin 18 and 19 from \n- CTS / RXD  for a straight cable\n- RDX / CTS for a cross cable\n\n| RJ12 P1       | cable| Straight Cable | Cross Cable| -\n| --------------|----- |----------|---------- |-\n| 1 - 5v out    | ===\u003e | 1        | 6         | not connected\n| 2 - CTS       | \u003c--- | 2        | 5         | CTS / RXD  software select\n| 3 - GND data  | ---- | 3        | 4         | \\ GND\n| 4 - GND data  | ---- | 4        | 3         | / GND\n| 5 - RXD (data)| ---\u003e | 5        | 2         | RDX / CTS software select\n| 6 - GND power | \u003c=== | 6        | 1         | not connected\n\nI recommend that in this design you do not connect the Power 5v to your board \n\n\n# Internal wiring:\n\n## P1 In Connector \nESP32 Pin | Color      | Female Connector |\n----------|------------|------------------\nGND       | black      | pin 4 \u0026 3 \ngpio-2    | pale blue  | pin 5\ngpio-5    | yellow/blk | pin 2 | \n\n## operation\n\n3 Neopixel Leds (top to bottom):\n\n|led| purpose | Red           | Green                    | other\n|---|---------|---------------|--------------------------|---\n| 2 | P1 meter| CRC Error     | CRC OK                   | **Blue**: data received , **Purple** : Simulator sending Data\n| 1 | mqtt    | Not connected | Connected to MQTT broker | **Yellow**: Data could not be send to Broker\n| 0 | wifi    | Not connected | IP address acquired      |\n\n### configuration file \n\n\u003cdocument What to change in the config file\u003e\n\nPlease adjust the relevant settings in [config.py](src/config.py)\n``` python\n# Serial Pins for meter connection\n# TX pin is only used for testing/simulation but needs to be specified\nRX_PIN_NR = const(2)\nTX_PIN_NR = const(15)\nRTS_PIN_NR = const(5)\n\n# Base SSID to connect to\nhomenet = {'SSID': 'IoT', 'password': 'MicroPython'}\n\n#the mqtt broker to connect to\nbroker = {'server': 'homeassistant.local', 'user': 'sensor', 'password': 'beepbeep'}\n\nHOST_NAME = b'p1_meter_' + hexlify(unique_id())\nROOT_TOPIC = b\"p1_meter\"\n\n#also publish telegram as json\npublish_as_json = False\n```\n\n\n\n## Updating the configuration over WiFi\n\nAssuming that you have cloned the repo to your PC , and have updated the configuration file  `config.py` . \nYou will need to transfer the updated configuration file to the ESP32. \n\nYou can do this via serial connection, or as the  P1_meter and your pc are likely in in different locations you can do this over WifI \n\n\n\n### Uploading config.py using webrepl\n\nThe MicroPython webrepl is started as part of the standard configuration, and advertises itself as `p1_meter.local` using the standard webrepl port (8266).\n\nIf you are on the same network , this allows you to connect to the P1_meter to verify its operation.\n\nThe below link uses the hosted webrepl, and specifies the board to connect to.\nhttp://micropython.org/webrepl/#p1_meter.local:8266/\n\n![image-20201220230034023](docs/img/image-20201220230034023.png) \n\n1. [Connect] to the board \n\n2. Enter the current webrepl password. default: p1meter\n\n3. Select the config.py file to upload\n\n4. click [Send to device]\n\n5. Click in the terminal window \n\n6. Press Ctrl-C once, to interrupt the running instance.\n   You should see something like the below, and the 3 leds will turn red \n\n   ```\n   INFO     mqttclient Published 4 meter readings                                  \n   INFO     main       Clear async loop retained state                             \n   Rebooting in 30 seconds, Ctrl-C to abort  \n   ```\n\n7. the device will automatically reboot in 30 seconds, and the new configuration will be activated.\n\n![Upload_reboot](docs/img/Upload_reboot.gif) \n\n\n\n### Updating configuration or code via passive ftp \n\n\n\n\n\n\u003cimg src=\"docs/img/image-20201220225411048.png\" alt=\"image-20201220225411048\" style=\"zoom:50%;\" /\u003e\n\n\n\n\n\n\n\n\n\n### Prereqs : \n\n - git client\n - python 3.x installed \n\n### Recommended: \n - vscode\n - pymakr extension\n - pip install micropy-cli \n\n\n### Building\n\n\nAs the software is written in Micropython building is not needed. if you really want or need to minimize the footprint on the device you can pre-compile the .py files to .mpy using the micropython cross-compiler\n\n\n### Testing\nYou can run the built-in  simulator for testing (using TX_PIN_NR)\n\n  - connect the rx and tx pins with a wire \n  ``` python\n    # Serial Pins for meter connection\n    # TX pin is only used for testing/simulation but needs to be specified\n    RX_PIN_NR = const(2)\n    TX_PIN_NR = const(15)\n    CTS_PIN_NR = const(5)\n\n    #------------------------------------------------\n    # A few Leds - optional\n    NEOPIXEL_PIN = const(13)\n\n  ```\n    - edit the configuration file `config.py` to enable the Simulator \n  ``` python\n      RUN_SIM = True\n````\n## Simulation / test mode \nThe p1 meter comes with a built-in test and simulation mode that allows you to test and  change the software, without needing to physically connect it to a electricity meter.\n\nthis simulation mode can be enabled  by wiring, or by making a change to the config.py file \n\nTo enable this via wiring: \n 1. Connect Pin 18 --\u003e GND , enable Simulator \n 2. Connect Pin 15 --\u003e Pin 2 , connect simulator TX to RX \n\n**By default:**  \n- the root topic is changed \n- a fake P1 message is generated every 10 seconds on Pin 15 \n  - this message has a few random values added to it \n  - the CRC16 is calculated before sending\n- the message is passed of the serial connect ( see .2 above) to the input \n- the message is is processed by the normal software and sent to mqtt using a different root topic to avoid interfering with actual input..\n\n![simulated output in mqtt](docs/simulator_mqtt.png)\n\nTo change the fake message see [p1meter_sym.py](src/p1meter_sym.py)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJosverl%2Fmicropython-p1meter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJosverl%2Fmicropython-p1meter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJosverl%2Fmicropython-p1meter/lists"}