mirror of
				https://github.com/Eledio/python-eledio.github.io.git
				synced 2025-10-31 00:12:32 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			425 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			425 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| <!DOCTYPE html>
 | |
| <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
 | |
| <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
 | |
| <head>
 | |
|   <meta charset="utf-8">
 | |
|   <meta http-equiv="X-UA-Compatible" content="IE=edge">
 | |
|   <meta name="viewport" content="width=device-width, initial-scale=1.0">
 | |
|   
 | |
|   
 | |
|   
 | |
|   <link rel="shortcut icon" href="../img/favicon.ico">
 | |
|   <title>Application example - Python Eledio library</title>
 | |
|   <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700" />
 | |
| 
 | |
|   <link rel="stylesheet" href="../css/theme.css" />
 | |
|   <link rel="stylesheet" href="../css/theme_extra.css" />
 | |
|   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/github.min.css" />
 | |
|   
 | |
|   <script>
 | |
|     // Current page data
 | |
|     var mkdocs_page_name = "Application example";
 | |
|     var mkdocs_page_input_path = "application_example.md";
 | |
|     var mkdocs_page_url = null;
 | |
|   </script>
 | |
|   
 | |
|   <script src="../js/jquery-2.1.1.min.js" defer></script>
 | |
|   <script src="../js/modernizr-2.8.3.min.js" defer></script>
 | |
|   <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/highlight.min.js"></script>
 | |
|   <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/languages/python.min.js"></script>
 | |
|   <script>hljs.initHighlightingOnLoad();</script> 
 | |
| </head>
 | |
| 
 | |
| <body class="wy-body-for-nav" role="document">
 | |
| 
 | |
|   <div class="wy-grid-for-nav">
 | |
| 
 | |
|     
 | |
|     <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
 | |
|     <div class="wy-side-scroll">
 | |
|       <div class="wy-side-nav-search">
 | |
|         <a href=".." class="icon icon-home"> Python Eledio library</a>
 | |
|         <div role="search">
 | |
|   <form id ="rtd-search-form" class="wy-form" action="../search.html" method="get">
 | |
|       <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
 | |
|   </form>
 | |
| </div>
 | |
|       </div>
 | |
| 
 | |
|       <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
 | |
|                 <ul>
 | |
|                     <li class="toctree-l1"><a class="reference internal" href="..">Home</a>
 | |
|                     </li>
 | |
|                 </ul>
 | |
|                 <ul>
 | |
|                     <li class="toctree-l1"><a class="reference internal" href="../api_description/">API description</a>
 | |
|                     </li>
 | |
|                 </ul>
 | |
|                 <ul class="current">
 | |
|                     <li class="toctree-l1 current"><a class="reference internal current" href="./">Application example</a>
 | |
|     <ul class="current">
 | |
|     <li class="toctree-l2"><a class="reference internal" href="#state-led-buzzer-and-watchdog-control">State LED, buzzer and watchdog control</a>
 | |
|         <ul>
 | |
|     <li class="toctree-l3"><a class="reference internal" href="#configuration-json-file">Configuration json file</a>
 | |
|     </li>
 | |
|     <li class="toctree-l3"><a class="reference internal" href="#usage">Usage</a>
 | |
|     </li>
 | |
|         </ul>
 | |
|     </li>
 | |
|     <li class="toctree-l2"><a class="reference internal" href="#wifi-rssi-linux-cpu-utilization">WiFi RSSI, linux CPU utilization</a>
 | |
|         <ul>
 | |
|     <li class="toctree-l3"><a class="reference internal" href="#usage_1">Usage</a>
 | |
|     </li>
 | |
|         </ul>
 | |
|     </li>
 | |
|     <li class="toctree-l2"><a class="reference internal" href="#srqirq-handler">SRQ/IRQ handler</a>
 | |
|         <ul>
 | |
|     <li class="toctree-l3"><a class="reference internal" href="#configuration-json-file_1">Configuration json file</a>
 | |
|     </li>
 | |
|     <li class="toctree-l3"><a class="reference internal" href="#usage_2">Usage</a>
 | |
|     </li>
 | |
|         </ul>
 | |
|     </li>
 | |
|     <li class="toctree-l2"><a class="reference internal" href="#error-handler">Error handler</a>
 | |
|         <ul>
 | |
|     <li class="toctree-l3"><a class="reference internal" href="#usage_3">Usage</a>
 | |
|     </li>
 | |
|         </ul>
 | |
|     </li>
 | |
|     </ul>
 | |
|                     </li>
 | |
|                 </ul>
 | |
|                 <ul>
 | |
|                     <li class="toctree-l1"><a class="reference internal" href="../contact/">Contact</a>
 | |
|                     </li>
 | |
|                 </ul>
 | |
|       </div>
 | |
|     </div>
 | |
|     </nav>
 | |
| 
 | |
|     <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
 | |
| 
 | |
|       
 | |
|       <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
 | |
|         <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
 | |
|         <a href="..">Python Eledio library</a>
 | |
|       </nav>
 | |
| 
 | |
|       
 | |
|       <div class="wy-nav-content">
 | |
|         <div class="rst-content">
 | |
|           <div role="navigation" aria-label="breadcrumbs navigation">
 | |
|   <ul class="wy-breadcrumbs">
 | |
|     <li><a href="..">Docs</a> »</li>
 | |
|     
 | |
|       
 | |
|     
 | |
|     <li>Application example</li>
 | |
|     <li class="wy-breadcrumbs-aside">
 | |
|       
 | |
|     </li>
 | |
|   </ul>
 | |
|   
 | |
|   <hr/>
 | |
| </div>
 | |
| 
 | |
|           <div role="main">
 | |
|             <div class="section">
 | |
|               
 | |
|                 <h1 id="example-of-usages">Example of usages</h1>
 | |
| <p>There you can find some examples, how to use python-eledio library in your Eledio devices.</p>
 | |
| <h2 id="state-led-buzzer-and-watchdog-control">State LED, buzzer and watchdog control</h2>
 | |
| <h3 id="configuration-json-file">Configuration <em>json</em> file</h3>
 | |
| <p>File name: <em>mpu-config.json</em></p>
 | |
| <pre><code>{
 | |
|   "devices": {
 | |
|     "mpu-dev": {
 | |
|       "bus": "mpu"
 | |
|     }
 | |
|   },
 | |
|   "identifiers": {
 | |
|     "mpuWifiRssi": {
 | |
|       "unit": "mpu-dev",
 | |
|       "parameter": "wifi-rssi"
 | |
|     },
 | |
|     "mpuInternalTemperature": {
 | |
|       "unit": "mpu-dev",
 | |
|       "parameter": "temperature"
 | |
|     },
 | |
|     "mpuCpuPercent": {
 | |
|       "unit": "mpu-dev",
 | |
|       "parameter": "cpu-percent"
 | |
|     },
 | |
|     "mpuLed": {
 | |
|       "unit": "mpu-dev",
 | |
|       "parameter": "led"
 | |
|     },
 | |
|     "mpuBeeper": {
 | |
|       "unit": "mpu-dev",
 | |
|       "parameter": "beeper"
 | |
|     },
 | |
|     "mpuWatchdog": {
 | |
|       "unit": "mpu-dev",
 | |
|       "parameter": "watchdog"
 | |
|     }
 | |
|   }
 | |
| }
 | |
| </code></pre>
 | |
| <h3 id="usage">Usage</h3>
 | |
| <pre><code>import json
 | |
| 
 | |
| from eledio import Eledio
 | |
| from eledio.component.mpu.i2c import SMBus
 | |
| from eledio.device.mpu import MpuFactory
 | |
| 
 | |
| if __name__ == "__main__":
 | |
|     eledio = Eledio()
 | |
|     eledio.register_device_factory("mpu", MpuFactory())
 | |
| 
 | |
|     with open('mpu-config.json') as f:
 | |
|         eledio.append_config(json.load(f))
 | |
| 
 | |
|     eledio["mpuLed"] = (0x40, 0x00, 0x00) # set color of state LED in (R, G, B) format
 | |
|     eledio["mpuBeeper"] = (1500, 10)    # after calling store_outputs() switch on beeper on frequency 1500 Hz for 1s (first parameter is frequency in Hz, second parameter is duration in hundred ms)
 | |
| 
 | |
|     '''
 | |
|     Warning, next statement is dangerous, it couse restart of linux machine (watchdog).
 | |
|     This function is good to use when you have your development complete, and you know, that you communicate with components and devices lower then every x second.
 | |
|     '''
 | |
|     eledio["mpuWatchdog"] = 120 # after calling store_outputs() this statement couse reset of linux every 120 seconds. If there is any communication with devicese, timeout is restarted.
 | |
| 
 | |
|     eledio.store_outputs()
 | |
| </code></pre>
 | |
| <h2 id="wifi-rssi-linux-cpu-utilization">WiFi RSSI, linux CPU utilization</h2>
 | |
| <p>Use <a href="#configuration-json-file">same <em>json</em></a> file as in previous example.</p>
 | |
| <h3 id="usage_1">Usage</h3>
 | |
| <pre><code>import json
 | |
| 
 | |
| from eledio import Eledio
 | |
| from eledio.component.mpu.i2c import SMBus
 | |
| from eledio.device.mpu import MpuFactory
 | |
| 
 | |
| if __name__ == "__main__":
 | |
|     eledio = Eledio()
 | |
|     eledio.register_device_factory("mpu", MpuFactory())
 | |
| 
 | |
|     with open('mpu-config.json') as f:
 | |
|         eledio.append_config(json.load(f))
 | |
| 
 | |
|     eledio.load_inputs()
 | |
| 
 | |
|     rssi = eledio["mpuWifiRssi"] # place to variable rssi, WiFi signal RSSI
 | |
|     utilization = eledio["mpuCpuPercent"] # place to variable utilitation, linux CPU utilization
 | |
| </code></pre>
 | |
| <h2 id="srqirq-handler">SRQ/IRQ handler</h2>
 | |
| <p>If you want to used quick access to variables like received code from RF433 control, you can use SRQ handler.</p>
 | |
| <p>Inside SRQ handler, in this example function <code>handle_srq</code>, you received dictionary of eledio identifiers and their new value after SRQ.</p>
 | |
| <h3 id="configuration-json-file_1">Configuration <em>json</em> file</h3>
 | |
| <p>File name: <em>map.json</em></p>
 | |
| <pre><code>{
 | |
|   "devices": {
 | |
|     "unit-40": {
 | |
|       "bus": "i2c",
 | |
|       "address": 40,
 | |
|       "datacrc": 43200,
 | |
|       "compilermagic": 4006394777
 | |
|     }
 | |
|   },
 | |
|   "identifiers": {
 | |
|     "gatewayHWEvent": {
 | |
|       "unit": "unit-40",
 | |
|       "partition": "sdp",
 | |
|       "offset": 0,
 | |
|       "entrysize": 2,
 | |
|       "depth": 0,
 | |
|       "type": 1,
 | |
|       "prescaler": 0,
 | |
|       "a": 1,
 | |
|       "b": 0,
 | |
|       "datatype": "u16",
 | |
|       "srq": 0
 | |
|     },
 | |
|     "gatewayHWState": {
 | |
|       "unit": "unit-40",
 | |
|       "partition": "sdp",
 | |
|       "offset": 2,
 | |
|       "entrysize": 2,
 | |
|       "depth": 0,
 | |
|       "type": 1,
 | |
|       "prescaler": 0,
 | |
|       "a": 1,
 | |
|       "b": 0,
 | |
|       "datatype": "u16"
 | |
|     },
 | |
|     "rf433": {
 | |
|       "unit": "unit-40",
 | |
|       "partition": "sdp",
 | |
|       "offset": 4,
 | |
|       "entrysize": 4,
 | |
|       "depth": 0,
 | |
|       "type": 1,
 | |
|       "prescaler": 0,
 | |
|       "a": 1,
 | |
|       "b": 0,
 | |
|       "datatype": "u32",
 | |
|       "srq": 1
 | |
|     },
 | |
|     "gatewayHWAction": {
 | |
|       "unit": "unit-40",
 | |
|       "partition": "adp",
 | |
|       "offset": 0,
 | |
|       "entrysize": 2
 | |
|     }
 | |
|   }
 | |
| }
 | |
| </code></pre>
 | |
| <h3 id="usage_2">Usage</h3>
 | |
| <pre><code>import json
 | |
| 
 | |
| from eledio import Eledio
 | |
| from eledio.component.mpu.i2c import SMBus
 | |
| from eledio.device.mpu import MpuFactory
 | |
| 
 | |
| def handle_srq(identifiers):
 | |
|     """
 | |
|     User handling of SRQ (called in context of eledio.wait_events)
 | |
|     :param identifiers: dictionary of eledio identifiers and their new value after SRQ
 | |
|     :return:
 | |
|     """
 | |
|     print("Service requests!", identifiers)
 | |
| 
 | |
| if __name__ == "__main__":
 | |
|     eledio = Eledio()
 | |
|     eledio.register_device_factory("i2c", PcuFactory(SMBus(0)))
 | |
|     eledio.register_srq(Srq(), handle_srq())
 | |
| 
 | |
|     # repeat for every configuration file
 | |
|     with open('map.json') as f:
 | |
|         eledio.append_config(json.load(f))
 | |
| 
 | |
|  while True:
 | |
|         # load state of inputs and outputs
 | |
|         eledio.load_inputs()
 | |
| 
 | |
|         # do something else or wait some time
 | |
|         eledio.wait_events(1.0)
 | |
| </code></pre>
 | |
| <h2 id="error-handler">Error handler</h2>
 | |
| <p>In runtime and while you develop your solution, there could rise some errors. You can catch them by definition of <em>error handler</em>.</p>
 | |
| <h3 id="usage_3">Usage</h3>
 | |
| <p>In this example <em>error handler</em> is represented by function <code>handler_error</code>.</p>
 | |
| <pre><code>import json
 | |
| 
 | |
| from eledio import Eledio
 | |
| from eledio.component.mpu.i2c import SMBus
 | |
| from eledio.device.pcu import PcuFactory
 | |
| from eledio.device.mpu import MpuFactory
 | |
| from eledio.device.srq import Srq
 | |
| 
 | |
| 
 | |
| def handle_srq(identifiers):
 | |
|     """
 | |
|     User handling of SRQ (called in context of eledio.wait_events)
 | |
|     :param identifiers: dictionary of eledio identifiers and their new value after SRQ
 | |
|     :return:
 | |
|     """
 | |
|     print("Service requests!", identifiers)
 | |
| 
 | |
| 
 | |
| def handle_error(src, ex):
 | |
|     """
 | |
|     Handle error inside eledio library
 | |
|     :param src:
 | |
|     :param ex:
 | |
|     :return:
 | |
|     """
 | |
|     print(src, ex)
 | |
| 
 | |
| 
 | |
| if __name__ == "__main__":
 | |
|     eledio = Eledio()
 | |
|     eledio.register_device_factory("i2c", PcuFactory(SMBus(0)))
 | |
|     eledio.register_device_factory("mpu", MpuFactory())
 | |
|     eledio.register_srq(Srq(), handle_srq)
 | |
|     eledio.error_handler = handle_error
 | |
| 
 | |
|     # repeat for every configuration file
 | |
|     with open('config.json') as f:
 | |
|         eledio.append_config(json.load(f))
 | |
| 
 | |
|     with open('mpu-config.json') as f:
 | |
|         eledio.append_config(json.load(f))
 | |
| 
 | |
|     while True:
 | |
|         # load state of inputs and outputs
 | |
|         eledio.load_inputs()
 | |
| 
 | |
|         # manipulate with inputs and outputs by identifier
 | |
|         # e.g.
 | |
|         #   print(eledio["test1"])
 | |
|         #   eledio["test2"] = True
 | |
| 
 | |
|         # apply final value to hardware
 | |
|         eledio.store_outputs()
 | |
| 
 | |
|         # do something else or wait some time
 | |
|         eledio.wait_events(1.0)
 | |
| </code></pre>
 | |
|               
 | |
|             </div>
 | |
|           </div>
 | |
|           <footer>
 | |
|   
 | |
|     <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
 | |
|       
 | |
|         <a href="../contact/" class="btn btn-neutral float-right" title="Contact">Next <span class="icon icon-circle-arrow-right"></span></a>
 | |
|       
 | |
|       
 | |
|         <a href="../api_description/" class="btn btn-neutral" title="API description"><span class="icon icon-circle-arrow-left"></span> Previous</a>
 | |
|       
 | |
|     </div>
 | |
|   
 | |
| 
 | |
|   <hr/>
 | |
| 
 | |
|   <div role="contentinfo">
 | |
|     <!-- Copyright etc -->
 | |
|     
 | |
|       <p>Eledio © 2019</p>
 | |
|     
 | |
|   </div>
 | |
| 
 | |
|   Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
 | |
| </footer>
 | |
|       
 | |
|         </div>
 | |
|       </div>
 | |
| 
 | |
|     </section>
 | |
| 
 | |
|   </div>
 | |
| 
 | |
|   <div class="rst-versions" role="note" aria-label="versions">
 | |
|   <span class="rst-current-version" data-toggle="rst-current-version">
 | |
|     
 | |
|     
 | |
|       <span><a href="../api_description/" style="color: #fcfcfc">« Previous</a></span>
 | |
|     
 | |
|     
 | |
|       <span><a href="../contact/" style="color: #fcfcfc">Next »</a></span>
 | |
|     
 | |
|   </span>
 | |
| </div>
 | |
|     <script>var base_url = '..';</script>
 | |
|     <script src="../js/theme_extra.js" defer></script>
 | |
|     <script src="../js/theme.js" defer></script>
 | |
|       <script src="../search/main.js" defer></script>
 | |
|     <script defer>
 | |
|         window.onload = function () {
 | |
|             SphinxRtdTheme.Navigation.enable(true);
 | |
|         };
 | |
|     </script>
 | |
| 
 | |
| </body>
 | |
| </html>
 |