Raspberry Pi Codesys Project


(Paul Barden) #1

Hi All.

Been using SmartThings for quite some time and finally decided to get a raspberry pi for its codesys integration only.

I’m an automation engineer and work with codesys day to day so finally gonna design a HMI for my house to monitor alarms states and events from all my devices.

The raspberry pi and ethercat master scenario is perfect for my project. Web visualization for the hmi can be accessed from any device with a browser!.

See the link below for an excellent starter guide with codesys and raspberry pi.

Ihttps://www.google.ie/amp/www.instructables.com/id/Programming-Raspberry-Pi-With-CODESYS/%3Famp_page%3Dtrue

I can easily distribute IO around my house using Ethercat (this fieldbus uses Ethernet cable which I have routed around most of my house anyway).

My question related to SmartThings is this:

I would love to be able to integrate my existing SmartThings sensors into my codesys project.

My preffered option is some kind of TCP interface from my SmartThings hub to a Function Block in my codesys project on the raspberry pi. From research it seems the hub doesn’t easily allow TCP/UDP exchange to give feedback on my devices.
Has anyone any experience getting device status over TCP/UDP??.

My other options would be using zwave relays and logic in core on SmartThings to switch the relays and report back to physical IO on my Ethercat IO. I’m looking into this but it seems messy for lots of SmartThings devices.

Finally I’m looking at writing to the onboard GPIO from SmartThings on the raspberry pi and then reading states in this from my codesys project thus giving me feedback. Not sure if this is even possible but I’m looking into it.

Anyway, I’m excited about building this project and would love to hear if anyone would have any advice in integrating my SmartThings device status to my pi/codesys project.

I’ll post progress as I make it here if anyone is interested.

Thanks guys.


(Dan) #2

Paul,

I am definitely interested in your efforts regarding using Codesys at home. I have worked for the past 25 years in the real-time manufacturing control systems field. I have used multiple platforms, including Beckhoff’s Twincat PLC software and their I/O. This is the first I had heard of Codesys be able to run on a Raspberry Pi. Very cool!

As far as network connectivity to SmartThings, there are two basic methods.

  1. LAN connected device that communicates through the ST Hub.
  2. LAN device that connects directly to the ST Cloud via an OAUTH enabled SmartApp.

I have a project that implements the first method above when using an Arduino or ESP8266 based board. You may find some of the network source code useful for your project. My ST_Anything project allows bi-directional monitoring and control of the I/O pins on the Arduino/ESP8266 boards. In case you’re not familiar with the ESP8266 platform, you can buy a NodeMCU v1.0 ESP8266-12e board for under $10 on Amazon (under $5 on eBay from China). This little board provides 9 digital pins (input or output) and 1 analog input pin, along with built-in WiFi.

Good luck in your project and please keep us posted in this forum as you make progress.

Take a look at


(codersaur) #3

You would most likely want to implement the service manager design pattern. See: http://docs.smartthings.com/en/latest/cloud-and-lan-connected-device-types-developers-guide/index.html

Cloud and LAN connected devices are devices that use either a 3rd party service, like the Ecobee thermostat, or communicate over the LAN (local area network) like the Sonos system. These devices require a unique implementation of their device handlers. Cloud and LAN connected devices use a service manager SmartApp along with a device handler for authentication, maintaining connections, and device communications. This guide will walk you through service manager and device handler creation for both of these scenarios.

There’s plenty of example implementations on the community forum, e.g.: SmartThings + Raspberry Pi & Homebridge = Siri HomeKit Controlled Smart Home

Most solutions I’ve seen typically use node.js on the Raspberry Pi side and send JSON back and fourth to an endpoint hosted by a SmartApp on the SmartThings side.


(Paul Barden) #4

Thanks for your replies guys.

Some great info there and something I can defiantly use as you say for all my LAN devices.

What about status of my Zwave and zigbee connected devices connected to my hub??. Is it possible to query the hub for status of all connected devices including zwave and zigbee?.


(Patrick Stuart [@pstuart]) #5

Not the hub. But cloud yes with a custom smartapp acting as a rest API. GitHub.com/opendash has a robust working API smartapp for reference.


(Paul Barden) #6

Great.

Thanks for the tip. So with open dash I can query the device status using its smart app API?.

I’ll have a look at this today.

Thanks again.


Open source community created dashboard solution Open-Dash.com - Alpha Version in Testing
(Patrick Stuart [@pstuart]) #7

Yes. We have an endpoint for all devices or a specific device based on its uuid. And a lot more.


(Paul Barden) #8

I’ve just read most of your post on open dash. Great work!!. From my side on the PLC layer I have installed a HTTP client. I’m learning fast about accessing REST API but still have lots to go.!


(Paul Barden) #9

So I have installed the client on my target and have visualization for the HTTP Client.

Does this look like what you expect to talk to the cloud and your API?. Or course the code behind it requires the correct URL.

I’m still learning so forgive me if these are stupid questions. 1. How can I link my account/cloud to your REST API?.
2. After this, do I just need the URL for the REST API and then send requests over HTTP thereafter?.
3. Is there a Smart App that I need to install in the Smarthings IDE?.

Thanks again for your help with this.

Edit:
I’ve just installed the smart app from code in my IDE. I’ve set the devices and it’s now automating in Smart things.

Now I’ve just gotta read the API documentation to start getting some device info from my PLC HTTP client.


(Patrick Stuart [@pstuart]) #10

And you will need to get the access token or do an oauth2 connection to st. Hint, enable debugging and check live logging.


(Paul Barden) #11

Just in the process as we speak but I’m stuck on redirect URI. I have my client ID and secret. I need to get the first authorization code in the oauth2.0 flow but I’m stumped on the redirect uri?. Do I need to create this?


(Patrick Stuart [@pstuart]) #12

http://docs.smartthings.com/en/latest/smartapp-web-services-developers-guide/tutorial-part2.html?highlight=oauth2


(Paul Barden) #13

Thanks for the link. I used the redirect uri within but still no response from the get request.

I’m using hurl.it to make the HTTP request. Definitely have the client ID correct and matching redirect uri in the smart app. So close but can’t get beyond this?


(Patrick Stuart [@pstuart]) #14

oauth2 flows require a responding url, aka a server to receive the callback and immediately respond to get the token with the authcode.

Easier way is just grab the debug token in the ide live logging.


(Paul Barden) #15

Thanks so much for your continued help with this. I’ve copied the token from the opendash live log and added it in the header under the name authorization : Bearer.

I’m now getting a return of invalid token. Im assuming my uuid is the name I’ve given it in the smart app settings.

Here’s my get request.

Does this like right to you?. Assuming my token is correctly copied from SmartThings of course. :slight_smile:


(Patrick Stuart [@pstuart]) #16

destination looks wrong, need to replace opendash with the uuid of the smartapp install same info in live logging.


(Paul Barden) #17

Hi Patrick,

So I have tested using the live log info from my smart things IDE:

I’m sure all info I have is correct. See below: Live Logging Data from open dash which includes the uuid and token.

Corresponding info is entered into the HTTP Client. Here using Hurl it.

Finally the response giving me a bad token error. Could the test token generated in the smart app be invalid?.

I’m so close and eager to start this project but without device data I’m nowhere.

Thanks again.


(Patrick Stuart [@pstuart]) #18

your headers look wrong…

should be two lines, autherization (sp?) and then value of Bearer
and second line should be “access_token” (or however it is in the logging url with a value of the token.

I think…


(Paul Barden) #19

Finally got it working with a response from lots of end points. I didn’t use the header at all and just used the full test URL adding the end points before the token.

Brought this is to my PLC HTTP client and no response however. Looks like it doesn’t support JSON. I’ve found an additional json library for this platform but it costs €90 to buy!.

http://store.codesys.com/json-http-client-library.html

Shame the standard HTTP client doesn’t work when I get responses from the multiple free clients online.


(Patrick Stuart [@pstuart]) #20

You could modify the API to return whatever you want. Beauty of open source.