You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/how-tos/create-module.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -163,9 +163,9 @@ In this step, you will code the logic that is unique to your model.
163
163
164
164
{{% alert title="Tip (optional)" color="tip" %}}
165
165
166
-
If you are using Golang, use the [Golang Module templates](https://github.com/viam-labs/module-templates-golang) which contain detailed instructions for creating your module.
166
+
If you are using Python or Golang, use the [`viam module generate` CLI command](/cli/#module) to generate the scaffolding for a module with one resource model.
167
167
168
-
If you are using Python, you can use the [Viam module generator](https://github.com/viam-labs/generator-viam-module/) to generate the scaffolding for a module with one resource model.
168
+
For a step-by-step guide that uses the CLI module generator, see [Create a Hello World module](/how-tos/hello-world-module/).
When prompted for a model triplet, use `<your organization public namespace>:<repo name>:<what you want to call your sensor model>`.
187
-
For example, `jessamy:weather:meteo_PM`.
188
-
189
-
- You can find your organization namespace by going to your organization settings in the [Viam app](https://app.viam.com).
190
-
- The repo name (also called family name) is generally the name of the GitHub repo where you will put your module code.
191
-
Name it something related to what your module does.
192
-
- Name your sensor based on what it supports, for example, if it supports a model of ultrasonic sensor called "XYZ Sensor 1234" you could call your model `XYZ_1234` or similar.
193
-
194
-
For more information, see [Name your new resource model](/how-tos/create-module/#name-your-new-resource-model).
195
-
196
-
{{% /tablestep %}}
197
-
{{% tablestep %}}
198
-
**4. Specify the API**
199
-
200
-
For the API triplet, enter `rdk:component:sensor`.
201
-
This means that you are implementing the standard Viam sensor API.
202
-
203
-
When asked whether this is a Viam SDK built-in API, enter `yes`.
169
+
2. Follow the prompts, naming your module and selecting from the options.
204
170
205
-
{{% /tablestep %}}
206
-
{{< /table >}}
207
-
<br>
171
+
<!--prettier-ignore-->
172
+
| Prompt | Description |
173
+
| -------| ----------- |
174
+
| Module name | The module name describes the module or the family of devices it supports. It is generally the same as the name of the GitHub repo where you will put your module code. For example, `weather`. |
175
+
| Language | The language for the module. To follow this guide, choose Python. |
176
+
| Visibility | Choose `Private` to share only with your organization, or `Public` to share publicly with all organizations. If you are testing, choose `Private`. |
177
+
| Namespace/Organization ID | In the [Viam app](https://app.viam.com), navigate to your organization settings through the menu in upper right corner of the page. Find the **Public namespace** and copy that string. In the example snippets below, the namespace is `jessamy`. |
178
+
| Resource to add to the module (API) | The [component API](/appendix/apis/#component-apis) or [service API](/appendix/apis/#service-apis) the resource you're creating implements. Choose `Sensor Component` for this guide. |
179
+
| Model name | Name your sensor based on what it supports, for example, if it supports a model of ultrasonic sensor called “XYZ Sensor 1234” you could call your model `XYZ_1234` or similar. |
180
+
| Enable cloud build | You can select `No` for this guide because you'll build the module yourself before uploading it. If you select `Yes` and push the generated files (including the <file>.github</file> folder) and create a release of the format `vX.X.X`, the module will build and upload to the Viam registry. |
181
+
| Register module | Select `Yes` unless you are creating a local-only module for testing purposes and do not intend to upload it. |
208
182
209
-
The generator creates a `run.sh` file, a `requirements.txt` file, a readme, and source code files.
210
-
In the next section, you'll customize some of these files to support your sensor.
183
+
The generator will create a folder containing stub files for your modular sensor component.
184
+
In the next section, you'll customize some of the generated files to support your sensor.
211
185
212
186
## Implement the sensor API
213
187
@@ -218,18 +192,19 @@ You need to implement this method so your sensor supports the sensor API:
218
192
{{% tablestep %}}
219
193
**1. Edit configuration code**
220
194
221
-
In the generated <file>/YOUR_MODULE_NAME/src/</file> directory, open the </file>MODEL_NAME.py</file> file.
195
+
In the generated <file>/YOUR_MODULE_NAME/src/</file> directory, open the </file>main.py</file> file.
222
196
223
197
Edit the config attributes to fit your sensor.
224
-
For example, if your sensor requires two pins, copy the `some_pin` lines and add another pin with a different name.
225
-
If you want to be able to configure something else, for example the location to get online data from, you can add attributes for that.
226
-
If your sensor doesn't require any configuration, delete the `some_pin` lines but don't delete the `validate` and `reconfigure` functions entirely; they're needed for the module to function even if they don't actually validate the input or reconfigure the resource.
198
+
For example, if your sensor requires two pins, edit the validate function to check that they are configured.
199
+
Edit the reconfigure function to get the configured values of each parameter from the configuration.
200
+
If you want to be able to configure something else, for example the location to get online data from, you can add attributes for that (see example code in the expander below).
201
+
If your sensor doesn't require any configuration, leave the `validate` and `reconfigure` functions as they are; they're needed for the module to function even if they don't actually validate the input or reconfigure the resource.
227
202
228
203
{{% /tablestep %}}
229
204
{{% tablestep %}}
230
205
**2. Define `get_readings`**
231
206
232
-
In the `get_readings` function definition, paste your test script.
207
+
In the `get_readings` function definition, replace `raise NotImplementedError()` by pasting your test script.
233
208
Edit the script to return a dictionary of readings instead of printing them.
234
209
Be sure to add any required imports to the top of the file.
235
210
@@ -248,52 +223,45 @@ Be sure to add any required imports to the top of the file.
248
223
The following code puts the functionality of the [example test script](#start-with-a-test-script) into the `get_readings` function definition.
# Set up the Open-Meteo API client with cache and retry on error
329
300
cache_session = requests_cache.CachedSession(
330
301
'.cache', expire_after=3600)
@@ -351,13 +322,17 @@ class meteo_PM(Sensor, Reconfigurable):
351
322
current_pm10 = current.Variables(0).Value()
352
323
current_pm2_5 = current.Variables(1).Value()
353
324
354
-
LOGGER.info(current_pm2_5)
325
+
self.logger.info(current_pm2_5)
355
326
356
327
# Return a dictionary of the readings
357
328
return {
358
329
"pm2_5": current_pm2_5,
359
330
"pm10": current_pm10
360
331
}
332
+
333
+
334
+
if__name__=="__main__":
335
+
asyncio.run(Module.run_from_registry())
361
336
```
362
337
363
338
{{< /expand >}}
@@ -370,15 +345,6 @@ Most modules have their implementation code linked on their module page, so you
370
345
Update the generated <file>requirements.txt</file> file to include any packages that must be installed for the module to run.
371
346
Depending on your use case, you may not need to add anything here beyond `viam-sdk` which is auto-populated.
372
347
373
-
## Make the module executable
374
-
375
-
You need an executable file so that `viam-server` can run your module.
376
-
The module generator already created the <file>run.sh</file> "entrypoint" file for you, so all you need to do is make this file executable by running the following command with your correct file path:
0 commit comments