Blog

March 22, 2014 | Python , Raspberry Pi |

Talking to a Raspberry Pi from your Phone using Bottle (Python)

Here's a short post on communicating with the raspberry from your phone's browser.

Our goal is to turn an LED connected to the Pi on and off, by accessing a web page on the phone's browser. Both the phone and the Pi are on the local WiFi network.

Here's how we do it:

Start a web server on the Pi. For this, we will use the simple and elegant Bottle web framework, which consists of a single source file. Accessing the LED control web page displays a button, and clicking on it uses jQuery AJAX to send a request to the web server, which in turn changes the GPIO pin state to turn the LED on.

Here's the code:

To start it, first run the server on your Pi. (You can ssh into your pi for this.)

pi@raspberrypi ~/code/python/bottle $ sudo python ledctrl.py

Then, access the web page from your phone's browser. In my case, the address is:

http://192.168.4.31:8080/led

You can control the LED from anywhere as long as you are in the local network. This can also work, from outside provided you do port forwarding on your router. I plan to explore this myself.

This is the starting point for me for a Raspberry Pi based home monitor robot. Watch this site for updates on this topic!


Consulting

Need help with a hardware project or product? Drop us an email at info@electronut.in. We offer consulting services on AVR and Nordic nRF BLE - hardware design, firmware development, prototyping, PCB design/assembly, sourcing and manufacturing. We can help you bring your product to market!


Bluey nRF52 BLE IoT dev board

bluey
Check out our Bluey nRF52 BLE development board with built-in Accelerometer/Gyroscope, Temperature/Humidity and Ambient Light sensors. It's a fabulous tool to learn Nordic nRF52 BLE programming and explore IoT technologies.

Questions/Comments

We love hearing from our readers. Email us at info@electronut.in for questions or comments on this article. If you found this article useful, please support us by buying some of our hardware products.


Please sign up for updates

Once in a while, we will send you an email update on the latest Electronut Labs projects and products. Your email address will never be shared or abused, ever.

2016 Electronut Labs. All rights reserved.