Next step is to test the I2C_Test_PiTapHat sketch on your TapHat with the Raspberry Pi.
Here are the steps to take to make your Raspberry Pi work as a Tap controller:
1. Preparing Raspberry Pi
2. Testing the TapHat / Pi connection – Arduino side
3. Testing the TapHat / Pi connection – Pi side
4. Connecting your Hardware
5. Configuring your TapHat and valves
6. Tap Control from the Internet
7. Finishing our Python Tap Control program
If you have completed step 2, we now want to test if your TapHat Arduino works!
Log on onto your Raspberry Pi using preferably a terminal. I called my pi “waterpi”. First step is to see if your Pi can see the Arduino as a slave:
pi@waterpi:~ $ i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: 10 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --
You can see that my Pi shows the Arduino at address 0x10 (that is what I put in the setup routine of course: Wire.begin(0x10);).
Now we want to send some bytes (0x3A) to the Arduino. I prefer to use python for that, but any other programming language that is supported by your Pi should work.
Make a Python program using:
pi@waterpi:~ $ sudo nano TWI_Test.py
Note: I normally use vi, but nano is a little bit easier for Linux novices.
# Pi side i2c Test (I2C_Test_PiTapHat) # J.A. Korten July 12, 2017 # V1.0 import smbus import time import struct bus = smbus.SMBus(1) address = 0x10 def toggleLed(): bus.write_byte(address,0x3A) while True: time.sleep(1) toggleLed()
This Python code initializes the second (1) i2c bus. Since we set the Arduino as a slave listening to address 0x10, we will now try to send 0x3A bytes to the TapHat.
The toggleLed code is the Python way of declaring a method that we then call in the “loop” equivalent of the Python program (15 – 17). In line 16 we put the loop to sleep every second and then we call toggleLed() in Line 17.
This call the method of line 8 and 9 and sends 0x3A to the i2c slave at address 0x10.
Now save the sketch by pressing “CTRL+X” and choosing “Yes”.
To run the sketch then issue:
pi@waterpi:~ $ sudo python TWI_Test.py
Now watch your TapHat: the amber LED should blink every other second.
If this works we can now move to the next step: connecting the valve(s) to the TapHat board and start operating them!
Note: press CTRL+Z if you want to terminate the script again.
Next step: 4. Connecting your Hardware