Enable an RPi to serve as an ioT gateway for our P2 Hardware - while dedicating only 2 pins for serial communication
On this Page:
- Add Gateway to P2 Project
- Configure and Restart Daemon if turning on new service
- Make calls to gateway object
Additional pages:
- Main Page
- Technology/Tools Used in this IoT Gateway
- Developing your own P2 Application (this page)
We use a simple Full-duplex serial link between your P2 board and the Raspberry Pi. We've crafted two .spin2 objects which are compiled into your code (The gateway object and a serial line-receiving queue object.) To configure the gateway you specify which two P2 pins are to be used when communicating with the RPi and iidenitfy the application running the gateway.
To use this in your own project you simply compile-in the gateway objects, start it and then make calls to it.
The following objects are compiled into your app.
- isp_rpi_iot_gw.spin2
- isp_queue_serial.spin2
- jm_serial.spin2
- jm_nstrings.spin2
You simply include them with something like:
OBJ { Objects Used by this Object }
IoT_GW : "isp_rpi_iot_gw" ' serial I/O to/from RPi
rxQue : "isp_queue_serial" ' acces our received data
Starting the gateway in Spin2 is also pretty simple:
CON { RPi Gateway io pins }
RX_GW = 25 { I } ' Raspberry Pi (RPi) Gateway
TX_GW = 24 { O }
GW_BAUDRATE = 624_000 ' 624kb/s - allow P2 rx to keep up!
DAT { our hardware ID strings and 1-wire buffers, collection names }
p2HardwareID byte "{your hardware descr here}",0
PUB main() | eOpStatus, nIdx, nCollId, eRxQStatus, eCmdId, tmpVar
'' Start the RPi serial communications and COGs
IoT_GW.startx(RX_GW, TX_GW, GW_BAUDRATE) ' tell singleton our pins and rate
' (one time) tell the RPi about about this hardware
IoT_GW.identify(@p2HardwareID)
... and do your app stuff from here on ...
IoT_GW.stop() ' if you wish to shutdown COGs and release serial lines
If it is your first time using a new service of this gateway you may have to place configuration values in the config.ini Daemon settings file. The following services need one-time configuration:
Service | config needed |
---|---|
if using sendmail(1) the default values should work (use_sendgrid = false ),if using SendGrid then enable sendgrid ( use_sendgrid = true ), set the from address and add your API key |
|
(more services TBA) |
Our gateway object supports a number of capabilites and will support more in the future. The current support consists of:
Method | description |
---|---|
--Gateway Set up-- | |
startx(rxpin, txpin, baud) | Start RPi gateway serial coms on rxpin and txpin at baud |
stop() | Release the serial pins (if needed) |
identify(pHardwareId) | Report to the RPi the name of this device (for email signature, etc.) |
--email-- | |
sendEmail(pEmailTo, pEmailFrom, pEmailSubj, pEmailBody) | Tell the RPi to send an email given these specifics |
--SMS (texting)-- | |
sendSMS(pSmsPhoneNbr, pSmsTextMessage) | Tell the RPi to send an SMS text message given these specifics |
--KV Collections-- | |
fileAccess(nFolderId, nFileMode, pFileName) : opStatus, nCollId | Tell the RPi to establish access to a named KV collection Returns {nCollId} our handle to the collection |
fileWriteLong(nCollId, pVarName, nVarValue) : opStatus | Tell the RPi to write a named long to collection {nCollId} |
fileReadLong(nCollId, pVarName) : opStatus, nVarValue | Tell the RPi to read a named long from collection {nCollId} |
fileWriteString(nCollId, pVarName, pVarString) : opStatus | Tell the RPi to write a named string to collection {nCollId} |
fileReadString(nCollId, pVarName, pTargetBffr, nTargetLen) : opStatus, pVarStr | Tell the RPi to read a named string from collection {nCollId} |
getCollectionNames(nFolderId) : opStatus, pNameSet | Request the list of filenames found in {nFolderId} on RPi Returns {pNameSet} which is the pointer to a contiguous set of longs the first of which contains the count of additional longs in the set. Each additional long is a pointer to a zstr (name of a collection) |
getKeyNamesInCollection(nCollId) : opStatus, pKeySet | Request the list of keys found in {nCollId} on RPi Returns {pNameSet} which is the pointer to a contiguous set of longs the first of which contains the count of additional longs in the set. Each additional long is a pointer to a zstr (key found in collection) |
--Serial Link Testing-- | |
sendTestMessage(bShouldReset) : eOpStatus, nRpiErrCt, pRpiMsg | Tell the RPi to send a test message and compare responses, etc. NOTE: Invoke getErrorCtrs() after testing loop to get ending status |
getErrorCtrs() : nRPiCt, nP2Ct | Return the test Tx and Rx error counters (cleared on sendTestMessage(reset=true)) |
If you like my work and/or this has helped you in some way then feel free to help me out for a couple of ☕'s or 🍕 slices!
Raspberry Pi is registered trademark of Raspberry Pi (Trading) Ltd.
Parallax, Propeller Spin, and the Parallax and Propeller Hat logos are trademarks of Parallax Inc., dba Parallax Semiconductor
This project is a community project not for commercial use.
This project is in no way affiliated with, authorized, maintained, sponsored or endorsed by Raspberry Pi (Trading) Ltd. or any of its affiliates or subsidiaries.
Likewise, This project is in no way affiliated with, authorized, maintained, sponsored or endorsed by Parallax Inc., dba Parallax Semiconductor or any of its affiliates or subsidiaries.
Licensed under the MIT License.
Follow these links for more information: