element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Sense Hat Color Chooser
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Former Member
  • Date Created: 10 Jul 2017 3:58 PM Date Created
  • Views 1458 views
  • Likes 9 likes
  • Comments 2 comments
Related
Recommended
  • sense hat
  • raspberry pi sense hat

Sense Hat Color Chooser

Former Member
Former Member
10 Jul 2017

Ive moved into a new house and came across a sense hat for the raspberry pi which made me remember a little project that I was working on, its basically a html based color chooser which updates the selected colour on the sense hat so I thought I'd share the scripts etc.. incase anybody finds them helpful / useful.

 

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

To start with I was running lighttpd on the Raspberry Pi which is a lightweight webserver, very simple to use and just requires a small modification to its config file to allow it to run Python scripts.

 

Below is the html, javascript, css and python

 

sensehatdisplay.html

<html>
<body>

    <link rel="stylesheet" type="text/css" media="all" href="shstyles.css"/>
    <script src="shcommon.js" type="text/javascript"></script>

    <div id="colordisplay"></div>
    <div id="colorcontrols">
    <p class="colorcontrollabel">R</p>
    <input id="redslider" class="slider" type="range"  min="0" max="255" value="255" onchange="slideRed(this.value)" />
    <p id="redvaluelabel" class="colorvaluelabel">255</p>
    <br>
    <p class="colorcontrollabel">G</p>
    <input id="greenslider" class="slider" type="range"  min="0" max="255" value="90" onchange="slideGreen(this.value)" />
    <p id="greenvaluelabel" class="colorvaluelabel">90</p>
    <br>
    <p class="colorcontrollabel">B</p>
    <input id="blueslider" class="slider" type="range"  min="0" max="255" value="90" onchange="slideBlue(this.value)" />
    <p id="bluevaluelabel" class="colorvaluelabel">90</p>
    </div>
    <input type="button" value="update" onClick="setSenseHatColorDisplay()">
    <p id="outputarea">output area</p>
</body>
</html>

 

shcommon.js

var colorred = 255;
var colorblue = 90;
var colorgreen = 90;

function slideRed(newvalue){
    document.getElementById("colordisplay").style.background="rgb("+newvalue+","+colorgreen+","+colorblue+")";
    document.getElementById("redvaluelabel").innerHTML=newvalue;
    colorred = newvalue;
}

function slideGreen(newvalue){
    document.getElementById("colordisplay").style.background="rgb("+colorred+","+newvalue+","+colorblue+")";
    document.getElementById("greenvaluelabel").innerHTML=newvalue;
    colorgreen = newvalue;
}

function slideBlue(newvalue){
    document.getElementById("colordisplay").style.background="rgb("+colorred+","+colorgreen+","+newvalue+")";
    document.getElementById("bluevaluelabel").innerHTML=newvalue;
    colorblue = newvalue;
}

function setSenseHatColorDisplay(){
var colorstring = colorred+"|"+colorgreen+"|"+colorblue;
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("outputarea").innerHTML = this.responseText;
        }
    };
req.open("POST","/cgi-bin/shcolorchooser.py",true);
req.send(colorstring);

}

 

shstyles.css

html, body{
min-height: 100%;
height: 100%;
max-width: 100%;
}

#colordisplay{
    float: left;
    width: 120px;
    height: 120px;
    border: 1px solid black;
    background-color: rgb(255,90,90);
}

.colorslider{
    display: inline;
    width: 100px;
}

.colorcontrollabel{
    display: inline;
}

.colorvaluelabel{
    display: inline;
}

#colorcontrols{
    float: left;
    border: 1px solid black;
    width: 200px;
}

 

shcolorchooser.py

#! /usr/bin/python
#

import sys
import os
from sense_hat import SenseHat

os.getenv("QUERY_STRING")
colorstring = sys.stdin.read()
#colorstring = "255|90|90"
colortup = colorstring.split("|")
redvalue = colortup[0]
greenvalue = colortup[1]
bluevalue = colortup[2]
print "Content-Type: text/html\n\n"



p = os.popen("sudo python /home/pi/www/cgi-bin/shcolor3.py "+redvalue+" "+greenvalue+" "+bluevalue)
p.close()




print '<html><head><meta content="text/html; charset=UTF-8" />'
print "</body></html>"

 

shcolor3.py

import sys
import os
from sense_hat import SenseHat
sense = SenseHat()

#colorstring = sys.argv[1]


redvalue = int(sys.argv[1])
greenvalue = int(sys.argv[2])
bluevalue = int(sys.argv[3])

colortup = (redvalue,greenvalue,bluevalue)

canvas = [
colortup,colortup,colortup,colortup,colortup,colortup,colortup,colortup,
colortup,colortup,colortup,colortup,colortup,colortup,colortup,colortup,
colortup,colortup,colortup,colortup,colortup,colortup,colortup,colortup,
colortup,colortup,colortup,colortup,colortup,colortup,colortup,colortup,
colortup,colortup,colortup,colortup,colortup,colortup,colortup,colortup,
colortup,colortup,colortup,colortup,colortup,colortup,colortup,colortup,
colortup,colortup,colortup,colortup,colortup,colortup,colortup,colortup,
colortup,colortup,colortup,colortup,colortup,colortup,colortup,colortup
]

sense.set_pixels(canvas)
#sense.show_message(sys.argv[2])

 

It should be possible to merge the 2 python scripts but there was some stumbling over returning the html headers to the raspberry pi and updating the sense hat display from a single script so I used 1 script to get the data, process it, run a second python script and return the headers allowing the 2nd script to update the sense hat.

  • Sign in to reply

Top Comments

  • DAB
    DAB over 8 years ago +1
    Hi Lucie, Nice project. Are you planning to use any of the other sensors? DAB
  • Former Member
    Former Member over 8 years ago in reply to DAB +1
    heya, thanks I remember getting the joystick to work earlier this year but havent done a great deal with the sensors yet, maybe I could get the webpage to auto-update with the sensor data?
  • Former Member
    Former Member over 8 years ago in reply to DAB

    heya, thanks image I remember getting the joystick to work earlier this year but havent done a great deal with the sensors yet, maybe I could get the webpage to auto-update with the sensor data?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 8 years ago

    Hi Lucie,

     

    Nice project.

     

    Are you planning to use any of the other sensors?

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube