# Color transfer with EnviroPHAT and UnicornPHAT

**URL:** https://forums.pimoroni.com/t/color-transfer-with-envirophat-and-unicornphat/2703
**Category:** Projects
**Created:** [July 20, 2016, 10:36pm UTC](https://forums.pimoroni.com/t/color-transfer-with-envirophat-and-unicornphat/2703 "2016-07-20T22:36:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![allo](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@allo](https://forums.pimoroni.com/u/allo)
#### Post date: [July 20, 2016, 10:36pm UTC](https://forums.pimoroni.com/t/color-transfer-with-envirophat-and-unicornphat/2703/1 "2016-07-20T22:36:35Z")

</div>

To test the new Enviro PHAT color sensor, i though i could couple it with the Unicorn-PHAT with its RGB-LEDs.

You need:

- 2 Raspberry Pi (i tested with two pi zero)
- Enviro PHAT
- Unicorn PHAT

Install the needed libraries, put both Raspberry Pi on a network.

Script for the Unicorn-PHAT Raspberry Pi (server side):

```
import SocketServer

import unicornhat as u
u.set_layout(u.PHAT)

def set_color(r, g, b):
    for x, y in [divmod(i, 4) for i in xrange(32)]:
        u.set_pixel(x, y, int(r), int(g), int(b))
    u.show()

class ColorHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        try:
            data = self.request[0].strip()
        r, g, b = data.split(",")
        set_color(r, g, b)
    except Exception, e:
        print "Bullshit received!", e

server = SocketServer.UDPServer(("0", 2000), ColorHandler)
server.serve_forever()

```

Code on the Enviro-PHAT Raspberry Pi (client):

```
import envirophat as e
import time, socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);sock.connect(("server-hostname-or-ip", 2000))
while True:
    time.sleep(0.1)
    sock.sendall("{0},{1},{2}\n".format(*e.light.rgb()))
sock.close()

```

Now try some light of different colors on the Enviro-PHAT and see the unicorn PHAT illuminate in that color.

Note that the code is very simple, you should not catch all `Exception`s without proper handling on the server side and provide some reaonable socket closing on the client side (like catching `KeyboardInterrupt`). But it works for basic testing of both hats.

---

<div class="post-metadata">

### Author: ![sandyjmacdonald](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/sandyjmacdonald/32/393_2.png) [@sandyjmacdonald](https://forums.pimoroni.com/u/sandyjmacdonald)
#### Post date: [July 21, 2016, 3:24pm UTC](https://forums.pimoroni.com/t/color-transfer-with-envirophat-and-unicornphat/2703/2 "2016-07-21T15:24:45Z")

</div>

Very nice! I particularly like your error catch message!

---

<div class="post-metadata">

### Author: ![allo](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@allo](https://forums.pimoroni.com/u/allo)
#### Post date: [July 21, 2016, 5:12pm UTC](https://forums.pimoroni.com/t/color-transfer-with-envirophat-and-unicornphat/2703/3 "2016-07-21T17:12:30Z")

</div>

For a more sophisticated implementation you may want TCPServer and StreamRequestHandler, but i had some issues with disconnected sockets and to keep it simple (i.e. almost no error handling) i just switched to udp.

I did not manage to transfer a blinking bike rear light, i guess either the sensor or the led switching just isn’t fast enough.
