4

First time here, it says to be specific... So here goes.

I'm doing a small project to connect Salesforce to my Raspberry Pi. The goal is to make a light (Think a beacon, siren-like light) flash when a high priority case comes in from a client in Salesforce. At the moment, clients usually send an email to a certain address, and this creates a case. It goes to the 'Unassigned Queue' and emails the team that this case is there waiting to be assigned.

Salesforce uses REST, so I need to be able to get the Pi to accept JSON so it can easily understand what Salesforce wants it to do.

Currently, I guess I have won half of the battle. I have a web server (Lighttpd) running on the Pi, which hosts an index page and a Python script. I am also using a Python wrapper, which allows me to easily run a command from a Telldus program I have installed. This program controls a USB RF Transmitter that I have connected, it is paired to a RF Socket, which is connected to the mains power supply with a light connected to it.

So the Python script is called power.py, and can be controlled with URL variables, so if I go to power.py?device=1&power=on&time=10&password=hunter2 that turns on device 1 for 10 seconds. I also created a POST form on the index page, which just POSTS to the python script, and runs it in the same way as using the URL variables. That all works great.

So all I need to do, is connect it to Salesforce. I would like to use REST and JSON, so that if I ever move away from Salesforce to another CRM program, it will easily be able to adapt and receive instructions from new places.

I have posted the Python script I am using here: https://github.com/7ewis/RemotePiControl/blob/master/power.py

The Pi isn't currently allowed out of the local network, so I will need to somehow develop a way to send JSON commands, and the recieve and convert them to work using the correct variables etc. I'm not a programmer, I've just exposure to languages from hacking things and exploring. Hence why I need some guidance with this.

I have never used REST or JSON before, so what would I need to do to achieve this?

Lewis Lebentz
  • 817
  • 4
  • 13
  • 24

2 Answers2

3

Seems like adding Flask http://flask.pocoo.org to your Raspberry Pi Webserver would be a good move. It allows server-side python to be run in response to JQuery ajax (and regular) requests. Check out a couple of examples here:

http://flask.pocoo.org/docs/patterns/jquery/

and this stack overflow question: how can I use data posted from ajax in flask?

Flask is pretty straightforward to get up and running, and is happy working with a number of servers, including Lighttpd. Writing RESTful flask is also a perfectly reasonable proposition, see: http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask

Additionally, lots of people have used flask on the raspberry pi already-- so that could help get you up and running smoothly: http://mattrichardson.com/Raspberry-Pi-Flask/

Good luck!

Community
  • 1
  • 1
Joan Smith
  • 931
  • 6
  • 15
  • 1
    Ah beat me to the punch. Yes use Flask, much easier than print out statements. – Cormac Mulhall Feb 28 '14 at 15:52
  • I'll have a look at Flask then! So is that actually AJAJ? I've never actually used REST or JSON, what is the relation? Thanks for your help Joan! – Lewis Lebentz Feb 28 '14 at 17:03
  • I admit, I had to look up AJAJ, but according to Wikipedia AJAX is generally used without regard to format, so, yes, with a caveat:) http://en.m.wikipedia.org/wiki/AJAJ. Rest and Json can be used independently, so no particular relationship between the two. Just two useful web technologies that go together nicely. If this answers your question, please accept the answer with the check mark:) – Joan Smith Feb 28 '14 at 17:08
  • I had to look at AJAX on Wikipedia to remind myself what it was anyway, that's how I came across AJAJ! I think I will give Flask a go. Do you know I will actually have to do to make it work with Salesforce though? – Lewis Lebentz Mar 04 '14 at 13:43
  • Honestly, no idea. that's a separate problem! – Joan Smith Mar 04 '14 at 16:28
  • Ok, thanks Joan! I've accepted your answer now. I have now installed Flask, and am just trying to work out how to parse the requests and make it forward to the Python script. – Lewis Lebentz Mar 26 '14 at 10:52
0

Firstly don't use a Python script that prints out result directly to CGI. You will be forever debugging it.

Use a light weight framework like Flask. You can do something as simple as

from flask import Flask

application = Flask(__name__)

@application.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # use Flask's build in json decoder
        the_data = request.get_json()
        # then do something with the data
        return "This was a POST request, how interesting..."
    else:
        # request was GET rather than POST, so do something with else
        return "Hello World!"

See how to configure Flask to run with Lighttpd here http://flask.pocoo.org/docs/deploying/fastcgi/

If you want to test this you can either write another Python script to send JSON data to your server (I recommend looking at the Python Requests library for this http://www.python-requests.org/en/latest/), or you can do this manually using a HTTP request builder, such as HTTPRequester for Firefox (https://addons.mozilla.org/en-US/firefox/addon/httprequester/)

Cormac Mulhall
  • 1,197
  • 1
  • 8
  • 12
  • Sorry, I don't know what the first sentence means, ELI5 (Mainly the CGI bit)? So with that code (What would the file extension be?) I could send a HTTP POST to it, and it would return the corresponding message? Seems fairly straightforward! Then I just need to include some JSON, but how do I get it to understand it, and run the Python command/script to turn the light on? I'll check out the addon. Thanks! – Lewis Lebentz Feb 28 '14 at 17:11
  • Firstly, to explain what I mean by the first sentence: In your example code you have a lot of print statements that are producing the output of the server. The Lighttpd web server runs the Python script and takes the output of these print statements and returning them to the web browser. This works, but it is very messy and difficult to debug, for example you are constructing your own HTTP header (the "Content-Type" bit at the top). If you want to change stuff it is a lot of work on your end. A framework like Flask will do all that for you so you don't have to worry about it – Cormac Mulhall Feb 28 '14 at 17:53
  • Secondly, the JSON bit. Flask has a function that takes the body of the HTTP request that you POST to the server and turns it from JSON into Python dictionary. So if you POST {"variable_name":"variable_value"} to the server Flask will turn that into a Python dictionary {variable_name: "variable_value"} You can then just check what was sent. That is what would be in the "the_data" variable above. – Cormac Mulhall Feb 28 '14 at 17:56
  • I think I understand, thank you. Can Flask do everything on it's own, or is it best to keep Lighttpd with it? And after installing Flask, it won't need any setup, after installing, it will accept JSON staright away? When I have done this, how can I send a test POST? – Lewis Lebentz Mar 04 '14 at 13:50
  • You don't need to put Flask behind Lighttpd, you can start Flask as its own stand alone server. If you are running a large application in production it is recommended to run behind a HTTP server, but that is for scaling, stability, security and easy of deployment. For a small personal project it shouldn't be necessary and will save you some headache in getting Lightttp to launch your Python code. Flask processes JSON out of the box. Just POST some JSON data to it (using something like HttpRequester in Firefox) and it will process it. – Cormac Mulhall Mar 05 '14 at 14:13
  • Ah yes I see. I have now got it receiving JSON, how do I tell it what to do with that code though? – Lewis Lebentz Mar 26 '14 at 10:46
  • Flask converts the JSON into a Python dictionary. Just query the dictionary and do something based on what is in the dictionary, eg if my_json_stuff['power_on'] == True etc – Cormac Mulhall Mar 26 '14 at 12:12
  • Where is the dictionary? In the .py file or somewhere else? It also uses a Python Wrapper, so would this still work? I linked to the .py script in the OP, could this work? – Lewis Lebentz Mar 26 '14 at 12:15
  • the dictionary is returned by a Flask call request.get_json() if the POST request has a Content-Type of "application/json". So POST JSON data to your web application, making sure the content type if application/json and get_json will return it as a Python dictionary. Good place to start is the Flask tutorial http://flask.pocoo.org/docs/quickstart/ – Cormac Mulhall Mar 26 '14 at 12:42
  • I'll have a read of that. Thank you. How would I send a test POST with that content type etc. from a Chrome extenstion, like Postman or Advanced REST Client? – Lewis Lebentz Mar 26 '14 at 13:05
  • Yeah Postman will work. I personally prefer HttpRequester, a Firefox Add on. It has all you need and I find it easier to use than Postman. https://addons.mozilla.org/en-US/firefox/addon/httprequester/ – Cormac Mulhall Mar 26 '14 at 15:43