Difference between revisions of "Micropython Bluemix"

From wiki.emacinc.com
Jump to: navigation, search
(Created page with "{{#seo: |title=Micropython |titlemode=append |keywords=Micropython |description=The following page can be used to get familiarized with Micropython on EMAC products. }} <!-- /...")
 
 
(7 intermediate revisions by the same user not shown)
Line 8: Line 8:
 
<!-- /****************************************  Page Description Text  ****************************************/ -->
 
<!-- /****************************************  Page Description Text  ****************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
Micropython is an implementation of the Python 3 programming langauge optimized to run on microcontrollers in a constrained environment.
+
Bluemix is a cloud computing service developed by IBM.
 +
 
 +
Bluemix has the capabilities to host a variety of services from web services to crypto-currencies.
 +
 
 +
This Wiki contains information on how to use the methods that EMAC has developed for Micropython to interact with IBM Bluemix IoT.
  
Micropython requires only 256k of code space and 16k RAM and aims to be as compatible as possible with normal Python 3 to allow for easy portability from desktop to microcontroller.
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /***************************************** Background Information ****************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*****************************************  General Information  *****************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
{{:Templateimpl:geninfo | initials=BS | title=Micropython | desc=The following page can be used to get familiarized with Micropython on EMAC products. | project=OE 5.0 }}
 
=== Tools Required ===
 
* Desktop PC
 
* USB to mini-USB cable
 
* Serial to USB converter (optional, may be required if board does not have USB)
 
* STLink programmer (optional, may be required if board does not have USB)
 
=== Setup ===
 
* USB (needs link)
 
* STLink (needs link)
 
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*****************************************  Using/Working With  ******************************************/ -->
 
<!-- /*****************************************  Using/Working With  ******************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
{{:Templateimpl:using | initials=BS | title=EMAC Micropython | desc=The following page can be used to get familiarized with Micropython on EMAC products. | project=OE 5.0 }}
+
{{:Templateimpl:using | initials=BS | title=EMAC methods for Micropython with IBM Bluemix | desc=The following page can be used to get familiarized with Micropython on EMAC products. | project=OE 5.0 }}
EMAC has developed multiple drivers and features for our products running Micropython.
+
===class Bluemix===
 +
*This class provides a tailored interface to the MQTTClient class, for the purpose of connecting to the IBM Bluemix IoT service. It is imported from the 'bluemix' module. The following is an example usage.
 +
*<syntaxhighlight lang=python>
 +
client = bluemix.Bluemix(ORG, DEVICE_ID, DEVICE_TYPE, AUTH_TOKEN)
 +
client.connect()
 +
msg = '{d:{value:"42"}}'
 +
client.publish('desired/topic/to/use', msg)
 +
</syntaxhighlight>
 +
 
 +
===Constructor===
 +
*<syntaxhighlight lang=python>
 +
bluemix.Bluemix(ORG, DEVICE_ID, DEVICE_TYPE, AUTH_TOKEN, ssl=False)
 +
</syntaxhighlight>
 +
*This constructor creates the Bluemix object, which inherits the MQTTClient Class. Returns the Bluemix object. Arguments are:
 +
**ORG          The organization ID of the Bluemix service that is to be connected to
 +
**DEVICE_ID    The unique Bluemix identifier for the board that is connecting.
 +
**DEVICE_TYPE  The Bluemix board type.
 +
**AUTH_TOKEN    The secret authentication token for the board that is connecting.
 +
**ssl          Keyword only. False by default. Will use the port: 8883.
 +
 
 +
===Methods===
 +
*<syntaxhighlight lang=python>
 +
Bluemix.connect()
 +
</syntaxhighlight>
 +
**Connect to the Bluemix IoT service using the credentials provided in the constructor.
 +
 
 +
*<syntaxhighlight lang=python>
 +
Bluemix.disconnect()
 +
</syntaxhighlight>
 +
**Disconnect from the Bluemix service.
 +
 
 +
*<syntaxhighlight lang=python>
 +
Bluemix.publish(topic, message)
 +
</syntaxhighlight>
 +
**Send the given message to the given topic. This method requires the board to be connected to the Bluemix service.
  
Documentation on specific features is listed below:
+
*<syntaxhighlight lang=python>
 +
Bluemix.set_cb(callback)
 +
</syntaxhighlight>
 +
**Sets the callback to use when subscribed to a topic. An example callback follows:
 +
***<syntaxhighlight lang=python>
 +
def sub_cb(topic, msg):
 +
    print(topic, msg)
 +
</syntaxhighlight>
 +
**The callback should have two arguments, one for the topic and one for the message. This method should be called before connect() if subscribe() is to be used.
  
* Micropython with IBM Bluemix (needs link)
+
*<syntaxhighlight lang=python>
* MQTT on the RS9113 (needs link)
+
Bluemix.subscribe(topic)
* Wifi on the RS9113 (needs link)
+
</syntaxhighlight>
 +
**Subscribe to the given topic. This method requires the board to be connected, and a callback set with set_cb().
  
{{:Templateimpl:whatnext | initials=BS | title=Getting Started with the EMAC OE SDK | desc=Basic tutorial for using the EMAC OE SDK. | project=OE 5.0 }}
+
*<syntaxhighlight lang=python>
* add link here for Mitipy
+
Bluemix.wait_msg()
* add link here for Cutipy
+
</syntaxhighlight>
 +
**Waits for a message to appear on the topic that is subscribed to.

Latest revision as of 14:08, 5 June 2018

Bluemix is a cloud computing service developed by IBM.

Bluemix has the capabilities to host a variety of services from web services to crypto-currencies.

This Wiki contains information on how to use the methods that EMAC has developed for Micropython to interact with IBM Bluemix IoT.

EMAC methods for Micropython with IBM Bluemix

class Bluemix

  • This class provides a tailored interface to the MQTTClient class, for the purpose of connecting to the IBM Bluemix IoT service. It is imported from the 'bluemix' module. The following is an example usage.
  • client = bluemix.Bluemix(ORG, DEVICE_ID, DEVICE_TYPE, AUTH_TOKEN)
    client.connect()
    msg = '{d:{value:"42"}}'
    client.publish('desired/topic/to/use', msg)
    

Constructor

  • bluemix.Bluemix(ORG, DEVICE_ID, DEVICE_TYPE, AUTH_TOKEN, ssl=False)
    
  • This constructor creates the Bluemix object, which inherits the MQTTClient Class. Returns the Bluemix object. Arguments are:
    • ORG The organization ID of the Bluemix service that is to be connected to
    • DEVICE_ID The unique Bluemix identifier for the board that is connecting.
    • DEVICE_TYPE The Bluemix board type.
    • AUTH_TOKEN The secret authentication token for the board that is connecting.
    • ssl Keyword only. False by default. Will use the port: 8883.

Methods

  • Bluemix.connect()
    
    • Connect to the Bluemix IoT service using the credentials provided in the constructor.
  • Bluemix.disconnect()
    
    • Disconnect from the Bluemix service.
  • Bluemix.publish(topic, message)
    
    • Send the given message to the given topic. This method requires the board to be connected to the Bluemix service.
  • Bluemix.set_cb(callback)
    
    • Sets the callback to use when subscribed to a topic. An example callback follows:
      • def sub_cb(topic, msg):
             print(topic, msg)
        
    • The callback should have two arguments, one for the topic and one for the message. This method should be called before connect() if subscribe() is to be used.
  • Bluemix.subscribe(topic)
    
    • Subscribe to the given topic. This method requires the board to be connected, and a callback set with set_cb().
  • Bluemix.wait_msg()
    
    • Waits for a message to appear on the topic that is subscribed to.