|
| 1 | +# Getting started |
| 2 | + |
| 3 | +## Prerequisites |
| 4 | + |
| 5 | +1. Python 3.11 or newer installed on your system. |
| 6 | +2. Access to a microgrid system supported by the `frequenz.sdk` or you can use |
| 7 | + the sandbox. |
| 8 | +3. Basic knowledge of microgrid concepts. |
| 9 | +4. [Familiarity with Channels](https://frequenz-floss.github.io/frequenz-channels-python/latest/). |
| 10 | +5. [Install the Frequenz SDK](../index.md#installation) |
| 11 | + |
| 12 | +## Create a project |
| 13 | + |
| 14 | +### Create a Python file |
| 15 | + |
| 16 | +You can start by simply creating a Python script (e.g., `pv_optimization.py`) |
| 17 | +using your favorite text editor. |
| 18 | + |
| 19 | +### Use Frequenz Repository Configuration |
| 20 | + |
| 21 | +As an alternative and specially for larger projects, it's recommended to set up the |
| 22 | +project using the [Frequenz Repository |
| 23 | +Configuration](https://frequenz-floss.github.io/frequenz-repo-config-python/latest). |
| 24 | + |
| 25 | +## Import necessary modules |
| 26 | + |
| 27 | +You can now open the app's main file and start adding content. Begin by |
| 28 | +importing the necessary libraries. |
| 29 | + |
| 30 | +```python |
| 31 | +import asyncio |
| 32 | + |
| 33 | +from datetime import timedelta |
| 34 | +from frequenz.sdk import microgrid |
| 35 | +from frequenz.sdk.actor import ResamplerConfig |
| 36 | +``` |
| 37 | + |
| 38 | +## Create the application skeleton |
| 39 | + |
| 40 | +The main logic of your application will run within an async function. Let's |
| 41 | +create a skeleton that contains all the necessary code to initialize a |
| 42 | +microgrid. |
| 43 | + |
| 44 | +```python |
| 45 | +async def run() -> None: |
| 46 | + # This points to the default Frequenz microgrid sandbox |
| 47 | + microgrid_host = "microgrid.sandbox.api.frequenz.io" |
| 48 | + microgrid_port = 62060 |
| 49 | + |
| 50 | + # Initialize the microgrid |
| 51 | + await microgrid.initialize( |
| 52 | + microgrid_host, |
| 53 | + microgrid_port, |
| 54 | + ResamplerConfig(resampling_period=timedelta(seconds=1)), |
| 55 | + ) |
| 56 | + |
| 57 | + # Define your application logic here |
| 58 | + # ... |
| 59 | +``` |
| 60 | + |
| 61 | +## Define the `main()` function |
| 62 | + |
| 63 | +Create a `main()` function that will set up and run the `run()` function using |
| 64 | +asyncio. |
| 65 | + |
| 66 | +```python |
| 67 | +def main() -> None: |
| 68 | + asyncio.run(run()) |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + main() |
| 72 | +``` |
| 73 | + |
| 74 | +## Implement the application logic |
| 75 | + |
| 76 | +Inside the `run()` function, implement the core logic of your application. This |
| 77 | +will include creating receivers for data streams, processing the data, making |
| 78 | +decisions, and eventually sending control messages to the microgrid system. We |
| 79 | +will cover more details in the following tutorials. For now, let's simply read |
| 80 | +the power measurements from the microgrid's grid meter and print them on the |
| 81 | +screen. The grid meter is a meter that is directly connected to the grid |
| 82 | +connection point. |
| 83 | + |
| 84 | +```python |
| 85 | +async def run() -> None: |
| 86 | + # This points to the default Frequenz microgrid sandbox |
| 87 | + ... |
| 88 | + |
| 89 | + # Define your application logic here |
| 90 | + grid_meter = microgrid.logical_meter().grid_power.new_receiver() |
| 91 | + |
| 92 | + async for power in grid_meter: |
| 93 | + print(power.value) |
| 94 | +``` |
| 95 | + |
| 96 | +## Putting it all together |
| 97 | + |
| 98 | +Here is the full version of your first Frequenz SDK application. |
| 99 | + |
| 100 | +```python |
| 101 | +import asyncio |
| 102 | + |
| 103 | +from datetime import timedelta |
| 104 | +from frequenz.sdk import microgrid |
| 105 | +from frequenz.sdk.actor import ResamplerConfig |
| 106 | + |
| 107 | +async def run() -> None: |
| 108 | + # This points to the default Frequenz microgrid sandbox |
| 109 | + microgrid_host = "microgrid.sandbox.api.frequenz.io" |
| 110 | + microgrid_port = 62060 |
| 111 | + |
| 112 | + # Initialize the microgrid |
| 113 | + await microgrid.initialize( |
| 114 | + microgrid_host, |
| 115 | + microgrid_port, |
| 116 | + ResamplerConfig(resampling_period=timedelta(seconds=1)), |
| 117 | + ) |
| 118 | + |
| 119 | + # Define your application logic here |
| 120 | + grid_meter = microgrid.logical_meter().grid_power.new_receiver() |
| 121 | + |
| 122 | + async for power in grid_meter: |
| 123 | + print(power.value) |
| 124 | + |
| 125 | +def main() -> None: |
| 126 | + asyncio.run(run()) |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main() |
| 130 | +``` |
| 131 | + |
| 132 | +## Run your application |
| 133 | + |
| 134 | +You're now ready to run your application. When working on an existing |
| 135 | +microgrid, make sure to update the `microgrid_host` and `microgrid_port` |
| 136 | +variables before running the script. |
| 137 | + |
| 138 | +```bash |
| 139 | +# Example usage |
| 140 | +python pv_optimization.py |
| 141 | +``` |
0 commit comments