OlivierLD / raspberry-coffee

JVM related (Java, Groovy, Scala, Kotlin, etc) samples for the Raspberry PI, relying on PI4J. IoT and friends.
http://raspberrypi.lediouris.net/
111 stars 38 forks source link

Exception with battery monitor #9

Closed freshlydoug closed 6 years ago

freshlydoug commented 6 years ago

Thanks for providing this. I also have a boat and solar panel. I'm also using a Witty Pi, USB router and modem to schedule and send back pictures and GPS details. Trying to implement the battery monitor too and seeing the following Exception after initial reading:

pi@raspberrypi:~/raspberry-pi4j-samples $ sudo ./gradlew [--daemon] build

FAILURE: Build failed with an exception.

BUILD FAILED in 10s pi@raspberrypi:~/raspberry-pi4j-samples $

pi@raspberrypi:~/raspberry-pi4j-samples/ADC $ ./battery.monitor -min=289:3.75 -max=872:11.75 -debug=y Read an ADC Parameters are: -calibration or -cal -debug=y|n|yes|no|true|false - example -debug=y (default is n) -ch=[0-7] - example -ch=0 (default is 0) -min=minADC:minVolt - example -min=280:3.75 (default is 0:0.0) -max=maxADC:maxVolt - example -min=879:11.25 (default is 1023:15.0) -tune=ADC:volt - example -tune=973:12.6 (default is 1023:15.0) -scale=y|n - example -scale=y (default is n) -log=[log-file-name] - example -log=[batt.csv] (default is battery.log)

-min & -max are required if -tune is not here, and vice versa. Prms: ADC Channel:0, MinADC:289, MinVolt:3.75, MaxADC:872, maxVolt:11.75 Volt [3.75, 11.75] ADC [289, 872] a=0.013227414, b=0.21569468 Value range: ADC=0 => V=0.21569468, ADC=1023 => V=13.747339 readAdc:858 (0x35A, 0&1101011010) Volume:83% (858) Volt:11.56 Exception in thread "main" java.lang.NullPointerException at adc.sample.BatteryMonitor$1.valueUpdated(BatteryMonitor.java:145) at adc.ADCContext.fireValueChanged(ADCContext.java:39) at adc.ADCObserver.start(ADCObserver.java:121) at adc.ADCObserver.start(ADCObserver.java:85) at adc.sample.BatteryMonitor.(BatteryMonitor.java:154) at adc.sample.BatteryMonitor.(BatteryMonitor.java:89) at adc.sample.BatteryMonitor.main(BatteryMonitor.java:264)

Shutting down Closing log file

Not sure if you can advise?

Thanks, Doug

OlivierLD commented 6 years ago

Hi @freshlydoug , I'll look into this. That should be easy to fix.

OlivierLD commented 6 years ago

Found something, and modified the code. I'll be able to test it later in the week, but @freshlydoug, you might want to pull the new code, compile it and give it a try... Please provide the stack dump as you did if a problem shows up... Thanks!

freshlydoug commented 6 years ago

Thanks @OlivierLD ,

Not sure if this is partly environment related. Deleted directory and did another git. First Error: sudo ./gradlew [--daemon] build Starting a Gradle Daemon, 1 busy Daemon could not be reused, use --status for de tails

FAILURE: Build failed with an exception.

BUILD FAILED in 40s

Now I executed just build:

pi@raspberrypi:~/raspberry-pi4j-samples $ sudo ./gradlew build :common-utils:compileJava :common-utils:processResources NO-SOURCE :common-utils:classes :common-utils:jar :ADC:compileJava :ADC:processResources NO-SOURCE :ADC:classes :ADC:jar :RMI.sample:compileJava :RMI.sample:processResources NO-SOURCE :RMI.sample:classes :RMI.sample:jar :I2C.SPI:compileJava :I2C.SPI:processResources NO-SOURCE :I2C.SPI:classes :I2C.SPI:jar :ADC-benchmark:compileJava :ADC-benchmark:processResources NO-SOURCE :ADC-benchmark:classes :ADC-benchmark:jar :Adafruit.IO.REST:compileJava :Adafruit.IO.REST:processResources NO-SOURCE :Adafruit.IO.REST:classes :Adafruit.IO.REST:jar :AlaMode.101:compileJava :AlaMode.101:processResources NO-SOURCE :AlaMode.101:classes :AlaMode.101:jar :Arduino.RaspberryPI:compileJava :Arduino.RaspberryPI:processResources NO-SOURCE :Arduino.RaspberryPI:classes :Arduino.RaspberryPI:jar :DAC:compileJava :DAC:processResources NO-SOURCE :DAC:classes :DAC:jar :Serial.IO:compileJava :Serial.IO:processResources NO-SOURCE :Serial.IO:classes :Serial.IO:jar :FONA:compileJava :FONA:processResources NO-SOURCE :FONA:classes :FONA:jar :GPIO.01:compileJava :GPIO.01:processResources NO-SOURCE :GPIO.01:classes :GPIO.01:jar :GPS.read:compileJava :GPS.read:processResources NO-SOURCE :GPS.read:classes :GPS.read:jar :HanoiTower:compileJava :HanoiTower:processResources :HanoiTower:classes :HanoiTower:jar :JavaSoundDemo:compileJavaNote: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

.... shortened output, apparent success down till:

:PhoneKeyboard3x4:compileTestJava Download https://repo1.maven.org/maven2/junit/junit/4.11/junit-4.11.pom Download https://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.pom Download https://repo1.maven.org/maven2/org/hamcrest/hamcrest-parent/1.3/hamcrest-parent-1.3.pom Download https://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar Download https://repo1.maven.org/maven2/junit/junit/4.11/junit-4.11.jar :PhoneKeyboard3x4:processTestResources NO-SOURCE :PhoneKeyboard3x4:testClasses :PhoneKeyboard3x4:test

membrane.PinsCustomization > goodFive FAILED junit.framework.AssertionFailedError at PinsCustomization.java:84

10 tests completed, 1 failed :PhoneKeyboard3x4:test FAILED

FAILURE: Build failed with an exception.

BUILD FAILED in 5m 7s 93 actionable tasks: 93 executed w: Detected multiple Kotlin daemon sessions at build/kotlin/sessions

CD ADC pi@raspberrypi:~/raspberry-pi4j-samples/ADC $ sudo ../gradlew shadowJar :common-utils:compileJava UP-TO-DATE :common-utils:processResources NO-SOURCE :common-utils:classes UP-TO-DATE :common-utils:jar UP-TO-DATE :ADC:compileJava UP-TO-DATE :ADC:processResources NO-SOURCE :ADC:classes UP-TO-DATE :ADC:shadowJar The SimpleWorkResult type has been deprecated and is scheduled to be removed in Gradle 5.0. Please use WorkResults.didWork() instead.

BUILD SUCCESSFUL in 20s 4 actionable tasks: 1 executed, 3 up-to-date

As per below all looks good so I think perhaps it's solved! I'm using a 9v battery to test.

pi@raspberrypi:~/raspberry-pi4j-samples/ADC $ ./battery.monitor -min=289:3.75 -max=872:11.75 -debug=y Read an ADC Parameters are: -calibration or -cal -debug=y|n|yes|no|true|false - example -debug=y (default is n) -ch=[0-7] - example -ch=0 (default is 0) -min=minADC:minVolt - example -min=280:3.75 (default is 0:0.0) -max=maxADC:maxVolt - example -min=879:11.25 (default is 1023:15.0) -tune=ADC:volt - example -tune=973:12.6 (default is 1023:15.0) -scale=y|n - example -scale=y (default is n) -log=[log-file-name] - example -log=[batt.csv] (default is battery.log)

-min & -max are required if -tune is not here, and vice versa. Prms: ADC Channel:0, MinADC:289, MinVolt:3.75, MaxADC:872, maxVolt:11.75 Volt [3.75, 11.75] ADC [289, 872] a=0.013227414, b=0.21569468 Value range: ADC=0 => V=0.21569468, ADC=1023 => V=13.747339 readAdc:73 (0x49, 0&01001001) Volume:7% (73) Volt:01.18 readAdc:60 (0x3C, 0&00111100) Volume:5% (60) Volt:01.01 readAdc:844 (0x34C, 0&1101001100) Volume:82% (844) Volt:11.38 readAdc:785 (0x311, 0&1100010001) Volume:76% (785) Volt:10.60 readAdc:799 (0x31F, 0&1100011111) Volume:78% (799) Volt:10.78 readAdc:786 (0x312, 0&1100010010) Volume:76% (786) Volt:10.61 readAdc:799 (0x31F, 0&1100011111) Volume:78% (799) Volt:10.78 readAdc:782 (0x30E, 0&1100001110) Volume:76% (782) Volt:10.56

Thank you!

freshlydoug commented 6 years ago

Hi @OlivierLD , Sorry to come back. I don't appear to be able to populate the log file. I changed the battery.log permissions and explicitly specified the -log=battery.log but nothing appears to be happening. No errors shown, just an empty log file. No issues as I'll capture the output via nohup. Thanks again, Doug

OlivierLD commented 6 years ago

Hey @freshlydoug , I found and fixed - I think - the problem you're mentioning. Please pull the new code, do a ../gradlew build shadowJar from the ADC directory, then the log file should be populated. Please let me know how this goes... Thanks!

freshlydoug commented 6 years ago

Hi @OlivierLD ,

I removed the directory and did another git. Maybe I didn't need to do this but I did another build: sudo ./gradlew build ............................. :JavaSoundDemo:compileJavaNote: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

:JavaSoundDemo:processResources NO-SOURCE :JavaSoundDemo:classes :JavaSoundDemo:jar :RangeSensor:compileJava :RangeSensor:processResources NO-SOURCE :RangeSensor:classes :RangeSensor:jar :LelandOilDetector:compileJava/home/pi/raspberry-pi4j-samples/LelandOilDetector/src/adc/levelreader/manager/SevenADCChannelsManager.java:173: error: unreported exception NotOnARaspberryException; must be caught or declared to be thrown obs.start(-1, BETWEEN_LOOPS); // Tolerance -1: all values, pause ^ Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error FAILED

FAILURE: Build failed with an exception.

BUILD FAILED in 36s 34 actionable tasks: 34 executed

But anyway as you mentioned ../gradlew build shadowJar from the ADC directory worked fine.

pi@raspberrypi:~/raspberry-pi4j-samples/ADC $ sudo ../gradlew shadowJar :common-utils:compileJava UP-TO-DATE :common-utils:processResources NO-SOURCE :common-utils:classes UP-TO-DATE :common-utils:jar UP-TO-DATE :ADC:compileJava UP-TO-DATE :ADC:processResources NO-SOURCE :ADC:classes UP-TO-DATE :ADC:shadowJar The SimpleWorkResult type has been deprecated and is scheduled to be removed in Gradle 5.0. Please use WorkResults.didWork() instead.

BUILD SUCCESSFUL in 11s 4 actionable tasks: 1 executed, 3 up-to-date


And battery.log is populating just fine now! Thanks very much again.

OlivierLD commented 6 years ago

Cool! And I think I've now fixed the rest of the errors you've seen as well ;)

freshlydoug commented 6 years ago

Hi @OlivierLD , I'd like to move off my breadboard version to this which arrived today: https://www.tindie.com/products/cburgess129/10-bit-adcvms-board-hat-for-raspberry-pi-v21/ The issue is the pins aren't the same! Can I easily change the pins: clk GPIO05; miso GPIO06; mosi GPIO13; cs GPIO19 and recompile? Thanks!

OlivierLD commented 6 years ago

Hi @freshlydoug , yes, could do modify the pins in ADCObserver, but for convenience, I did a quick modif, you can now provide the pins as parameters to the BatteryMonitor class, and to the battery.monitor script. You should refresh the code, recompile as you did before (../gradlew shadowJar), and run ./battery.monitor, you should see the following ouput:

./battery.monitor 
Read an ADC
Parameters are:
  -calibration or -cal
  -debug=y|n|yes|no|true|false - example -debug=y        (default is n)
  -ch=[0-7]                    - example -ch=0           (default is 0)
  -min=minADC:minVolt          - example -min=280:3.75   (default is    0:0.0)
  -max=maxADC:maxVolt          - example -min=879:11.25  (default is 1023:15.0)
  -tune=ADC:volt               - example -tune=973:12.6  (default is 1023:15.0)
  -scale=y|n                   - example -scale=y        (default is n)
  -simulate=y|n                - example -simulate=y     (default is n)
  -log=[log-file-name]         - example -log=[batt.csv] (default is battery.log)

  -miso=XX                     - example -miso=4         (default is GPIO_04)
  -mosi=XX                     - example -mosi=5         (default is GPIO_05)
  -clk=XX                      - example -clk=1          (default is GPIO_01)
  -cs=XX                       - example -cs=6           (default is GPIO_06)

 -min & -max are required if -tune is not here, and vice versa.
Prms: ADC Channel:0, MinADC:0, MinVolt:0.0, MaxADC:1023, maxVolt:15.0
Reading MCP3008 on channel 0
 Wiring of the MCP3008-SPI (without power supply):
 +---------++-------------------------------------------------+
 | MCP3008 || Raspberry PI                                    |
 +---------++------+--------------+------+---------+----------+
 |         || Pin# | Name         | Role | GPIO    | wiringPI |
 |         ||      |              |      | /BCM    | /PI4J    |
 +---------++------+--------------+------+---------+----------+
 | CLK (13)|| #12  | PCM_CLK/PWM0 | CLK  | GPIO_18 | 01       |
 | Din (11)|| #18  | GPIO_5       | MOSI | GPIO_24 | 05       |
 | Dout(12)|| #16  | GPIO_4       | MISO | GPIO_23 | 04       |
 | CS  (10)|| #22  | GPIO_6       | CS   | GPIO_25 | 06       |
 +---------++------+--------------+-----+----------+----------+
Raspberry PI is the Master, MCP3008 is the Slave:
- Dout on the MCP3008 goes to MISO on the RPi
- Din on the MCP3008 goes to MOSI on the RPi
Pins on the MCP3008 are numbered from 1 to 16, beginning top left, counter-clockwise.
       +--------+ 
* CH0 -+  1  16 +- Vdd 
  CH1 -+  2  15 +- Vref 
  CH2 -+  3  14 +- aGnd 
  CH3 -+  4  13 +- CLK 
  CH4 -+  5  12 +- Dout 
  CH5 -+  6  11 +- Din 
  CH6 -+  7  10 +- CS 
  CH7 -+  8   9 +- dGnd 
       +--------+ 
       +-----+-----+--------------+-----++-----+--------------+-----+-----+
       | BCM | wPi | Name         |  Physical  |         Name | wPi | BCM |
       +-----+-----+--------------+-----++-----+--------------+-----+-----+
       |     |     | 3v3          | #01 || #02 |          5v0 |     |     |       
       |  02 |  08 | SDA1         | #03 || #04 |          5v0 |     |     |       
       |  03 |  09 | SCL1         | #05 || #06 |          GND |     |     |       
       |  04 |  07 | GPCLK0       | #07 || #08 |    UART0_TXD | 15  | 14  |       
       |     |     | GND          | #09 || #10 |    UART0_RXD | 16  | 15  |       
       |  17 |  00 | GPIO_0       | #11 || #12 | PCM_CLK/PWM0 | 01  | 18  | CLK   
       |  27 |  02 | GPIO_2       | #13 || #14 |          GND |     |     |       
       |  22 |  03 | GPIO_3       | #15 || #16 |       GPIO_4 | 04  | 23  | Dout  
       |     |     | 3v3          | #01 || #18 |       GPIO_5 | 05  | 24  | Din   
       |  10 |  12 | SPI0_MOSI    | #19 || #20 |          GND |     |     |       
       |  09 |  13 | SPI0_MISO    | #21 || #22 |       GPIO_6 | 06  | 25  | CS    
       |  11 |  14 | SPI0_CLK     | #23 || #24 |   SPI0_CS0_N | 10  | 08  |       
       |     |     | GND          | #25 || #26 |   SPI0_CS1_N | 11  | 07  |       
       |     |  30 | SDA0         | #27 || #28 |         SCL0 | 31  |     |       
       |  05 |  21 | GPCLK1       | #29 || #30 |          GND |     |     |       
       |  06 |  22 | GPCLK2       | #31 || #32 |         PWM0 | 26  | 12  |       
       |  13 |  23 | PWM1         | #33 || #34 |          GND |     |     |       
       |  19 |  24 | PCM_FS/PWM1  | #35 || #36 |      GPIO_27 | 27  | 16  |       
       |  26 |  25 | GPIO_25      | #37 || #38 |      PCM_DIN | 28  | 20  |       
       |     |     | GND          | #39 || #40 |     PCM_DOUT | 29  | 21  |       
       +-----+-----+--------------+-----++-----+--------------+-----+-----+
       | BCM | wPi | Name         |  Physical  |         Name | wPi | BCM |
       +-----+-----+--------------+-----++-----+--------------+-----+-----+
Value range: ADC=0 => V=0.0, ADC=1023 => V=15.0

As you can see, you can use the parameters -miso=, -mosi=, -clk=, -cs=. Let me know if that works for you... Thanks!

freshlydoug commented 6 years ago

Hi @OlivierLD , thanks very much for going above and beyond on this. I have tested this but I'm afraid it's not working for me, see exception below:

pi@raspberrypi:~/raspberry-pi4j-samples/ADC $ sudo ./battery.monitor -min=486:6.82 -max=674:9.64 -miso=22 -mosi=23 -clk=21 -cs=24 -debug=y
Read an ADC
Parameters are:
  -calibration or -cal
  -debug=y|n|yes|no|true|false - example -debug=y        (default is n)
  -ch=[0-7]                    - example -ch=0           (default is 0)
  -min=minADC:minVolt          - example -min=280:3.75   (default is    0:0.0)
  -max=maxADC:maxVolt          - example -min=879:11.25  (default is 1023:15.0)
  -tune=ADC:volt               - example -tune=973:12.6  (default is 1023:15.0)
  -scale=y|n                   - example -scale=y        (default is n)
  -simulate=y|n                - example -simulate=y     (default is n)
  -log=[log-file-name]         - example -log=[batt.csv] (default is battery.log)

  -miso=XX                     - example -miso=4         (default is GPIO_04)
  -mosi=XX                     - example -mosi=5         (default is GPIO_05)
  -clk=XX                      - example -clk=1          (default is GPIO_01)
  -cs=XX                       - example -cs=6           (default is GPIO_06)

 -min & -max are required if -tune is not here, and vice versa.
Prms: ADC Channel:0, MinADC:486, MinVolt:6.82, MaxADC:674, maxVolt:9.64
Reading MCP3008 on channel 0
 Wiring of the MCP3008-SPI (without power supply):
 +---------++-------------------------------------------------+
 | MCP3008 || Raspberry PI                                    |
 +---------++------+--------------+------+---------+----------+
 |         || Pin# | Name         | Role | GPIO    | wiringPI |
 |         ||      |              |      | /BCM    | /PI4J    |
 +---------++------+--------------+------+---------+----------+
 | CLK (13)|| #40  | PCM_DOUT     | CLK  | GPIO_21 | 29       |
 | Din (11)|| #16  | GPIO_4       | MOSI | GPIO_23 | 04       |
 | Dout(12)|| #15  | GPIO_3       | MISO | GPIO_22 | 03       |
 | CS  (10)|| #18  | GPIO_5       | CS   | GPIO_24 | 05       |
 +---------++------+--------------+-----+----------+----------+
Raspberry PI is the Master, MCP3008 is the Slave:
- Dout on the MCP3008 goes to MISO on the RPi
- Din on the MCP3008 goes to MOSI on the RPi
Pins on the MCP3008 are numbered from 1 to 16, beginning top left, counter-clockwise.
       +--------+
* CH0 -+  1  16 +- Vdd
  CH1 -+  2  15 +- Vref
  CH2 -+  3  14 +- aGnd
  CH3 -+  4  13 +- CLK
  CH4 -+  5  12 +- Dout
  CH5 -+  6  11 +- Din
  CH6 -+  7  10 +- CS
  CH7 -+  8   9 +- dGnd
       +--------+
       +-----+-----+--------------+-----++-----+--------------+-----+-----+
       | BCM | wPi | Name         |  Physical  |         Name | wPi | BCM |
       +-----+-----+--------------+-----++-----+--------------+-----+-----+
       |     |     | 3v3          | #01 || #02 |          5v0 |     |     |     
       |  02 |  08 | SDA1         | #03 || #04 |          5v0 |     |     |     
       |  03 |  09 | SCL1         | #05 || #06 |          GND |     |     |     
       |  04 |  07 | GPCLK0       | #07 || #08 |    UART0_TXD | 15  | 14  |     
       |     |     | GND          | #09 || #10 |    UART0_RXD | 16  | 15  |     
       |  17 |  00 | GPIO_0       | #11 || #12 | PCM_CLK/PWM0 | 01  | 18  |     
       |  27 |  02 | GPIO_2       | #13 || #14 |          GND |     |     |     
  Dout |  22 |  03 | GPIO_3       | #15 || #16 |       GPIO_4 | 04  | 23  | Din 
       |     |     | 3v3          | #01 || #18 |       GPIO_5 | 05  | 24  | CS  
       |  10 |  12 | SPI0_MOSI    | #19 || #20 |          GND |     |     |     
       |  09 |  13 | SPI0_MISO    | #21 || #22 |       GPIO_6 | 06  | 25  |     
       |  11 |  14 | SPI0_CLK     | #23 || #24 |   SPI0_CS0_N | 10  | 08  |     
       |     |     | GND          | #25 || #26 |   SPI0_CS1_N | 11  | 07  |     
       |     |  30 | SDA0         | #27 || #28 |         SCL0 | 31  |     |     
       |  05 |  21 | GPCLK1       | #29 || #30 |          GND |     |     |     
       |  06 |  22 | GPCLK2       | #31 || #32 |         PWM0 | 26  | 12  |     
       |  13 |  23 | PWM1         | #33 || #34 |          GND |     |     |     
       |  19 |  24 | PCM_FS/PWM1  | #35 || #36 |      GPIO_27 | 27  | 16  |     
       |  26 |  25 | GPIO_25      | #37 || #38 |      PCM_DIN | 28  | 20  |     
       |     |     | GND          | #39 || #40 |     PCM_DOUT | 29  | 21  | CLK 
       +-----+-----+--------------+-----++-----+--------------+-----+-----+
       | BCM | wPi | Name         |  Physical  |         Name | wPi | BCM |
       +-----+-----+--------------+-----++-----+--------------+-----+-----+
Volt [6.82, 9.64]
ADC  [486, 674]
a=0.013605343, b=0.46999928
Value range: ADC=0 => V=0.46999928, ADC=1023 => V=14.388266
Created log-file [battery.log]
^C
Shutting down
java.lang.Throwable
        at adc.sample.BatteryMonitor.lambda$main$0(BatteryMonitor.java:389)
        at java.lang.Thread.run(Thread.java:748)
Closing log file
Shutting down the GPIO ports...

However I have also configured the adafruit example:

 GNU nano 2.2.6             File: simpletest.py
# Simple example of reading the MCP3008 analog input channels and printing
# them all out.
# Author: Tony DiCola
# License: Public Domain
import time
# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
# Software SPI configuration:
CLK  = 05
MISO = 06
MOSI = 13
CS   = 19
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

And I see the following reading on Channel 1:

pi@raspberrypi:~/Adafruit_Python_MCP3008/examples $ sudo python simpletest.py   Reading MCP3008 values, press Ctrl-C to quit...
|    0 |    1 |    2 |    3 |    4 |    5 |    6 |    7 |
---------------------------------------------------------
|  608 |    0 |    0 |    0 |    0 |    0 |    6 |   15 |
|  609 |    0 |    0 |    0 |    1 |    1 |    0 |    0 |
|  607 |    0 |    0 |    0 |   10 |   20 |   30 |   31 |
|  608 |    0 |    0 |    0 |    3 |    0 |    1 |    1 |
|  608 |    0 |    0 |    0 |    0 |    0 |    0 |   10 |
|  607 |    0 |    0 |    0 |   21 |   23 |   18 |    9 |
|  608 |    0 |    0 |    0 |    0 |    1 |    1 |    0 |
|  609 |    0 |    0 |    0 |   17 |   26 |   30 |   31 |
|  607 |    0 |    0 |    0 |    0 |    1 |    1 |    1 |
|  608 |    0 |    0 |    0 |    0 |    3 |   12 |   21 |
|  608 |    0 |    0 |    0 |   16 |   12 |    6 |    0 |
|  608 |    0 |    0 |    0 |    0 |    0 |    0 |    0 |
^CTraceback (most recent call last):
  File "simpletest.py", line 39, in <module>
    time.sleep(0.5)
KeyboardInterrupt

The following all appear to have pin references and I was wondering if they would all need modification? ADC\src\adc\ADCObserver.java ADC\src\analogdigitalconverter\ADCReader.java ADC\C\mcp3008reader.c ADC\src\analogdigitalconverter\mcp3008\MCP3008Reader.java

Thanks again, Doug

freshlydoug commented 6 years ago

I should add I was also getting this error when I wasn't able to specify the SPI values on the previous release which makes me think they aren't being applied everywhere needed.

freshlydoug commented 6 years ago

I actually just manually modified the GPIO values to miso=22 -mosi=23 -clk=21 -cs=24 in the following files: ADC\src\adc\ADCObserver.java ADC\src\analogdigitalconverter\ADCReader.java ADC\src\analogdigitalconverter\mcp3008\MCP3008Reader.java

recompiled and Executed: sudo ./battery.monitor -min=486:6.82 -max=674:9.64

And all appears to be working well. Just need to hook up the potentiometer and calibrate!

2018-05-19 06:23:38.733;609;59;08.76 2018-05-19 06:23:42.968;580;56;08.36 2018-05-19 06:23:45.585;606;59;08.71 2018-05-19 06:23:46.591;0;0;00.47 2018-05-19 06:23:52.523;601;58;08.65 2018-05-19 06:23:53.928;578;56;08.33 2018-05-19 06:23:54.931;547;53;07.91

Thanks for the guidance. The pin mapping was the bit I wasn't sure on until your previous post.

OlivierLD commented 6 years ago

Ah yes, pin numbers..., always a mess. There are the physical numbers, BCM numbers, WiringPi numbers, and pin names... Are the pin numbers used by the Adafruit Python the BCM ones? In that case, have you tried to run the soft like this? :

 $ ./battery.monitor -miso=6 -mosi=13 -clk=5 -cs=19

It gives an output like that:

 Read an ADC
+- IMPORTANT -----------------------------------+
| For miso, mosi, clk & cs, use BCM pin numbers |
+-----------------------------------------------+
Parameters are:
  -calibration or -cal
  -debug=y|n|yes|no|true|false - example -debug=y        (default is n)
  -ch=[0-7]                    - example -ch=0           (default is 0)
  -min=minADC:minVolt          - example -min=280:3.75   (default is    0:0.0)
  -max=maxADC:maxVolt          - example -min=879:11.25  (default is 1023:15.0)
  -tune=ADC:volt               - example -tune=973:12.6  (default is 1023:15.0)
  -scale=y|n                   - example -scale=y        (default is n)
  -simulate=y|n                - example -simulate=y     (default is n)
  -log=[log-file-name]         - example -log=[batt.csv] (default is battery.log)

  -miso=XX                     - example -miso=4         (default is GPIO_04)
  -mosi=XX                     - example -mosi=5         (default is GPIO_05)
  -clk=XX                      - example -clk=1          (default is GPIO_01)
  -cs=XX                       - example -cs=6           (default is GPIO_06)

 -min & -max are required if -tune is not here, and vice versa.
Prms: ADC Channel:0, MinADC:0, MinVolt:0.0, MaxADC:1023, maxVolt:15.0
Reading MCP3008 on channel 0
 Wiring of the MCP3008-SPI (without power supply):
 +---------++-------------------------------------------------+
 | MCP3008 || Raspberry PI                                    |
 +---------++------+--------------+------+---------+----------+
 |         || Pin# | Name         | Role | GPIO    | wiringPI |
 |         ||      |              |      | /BCM    | /PI4J    |
 +---------++------+--------------+------+---------+----------+
 | CLK (13)|| #29  | GPCLK1       | CLK  | GPIO_05 | 21       |
 | Din (11)|| #33  | PWM1         | MOSI | GPIO_13 | 23       |
 | Dout(12)|| #31  | GPCLK2       | MISO | GPIO_06 | 22       |
 | CS  (10)|| #35  | PCM_FS/PWM1  | CS   | GPIO_19 | 24       |
 +---------++------+--------------+-----+----------+----------+
Raspberry PI is the Master, MCP3008 is the Slave:
- Dout on the MCP3008 goes to MISO on the RPi
- Din on the MCP3008 goes to MOSI on the RPi
Pins on the MCP3008 are numbered from 1 to 16, beginning top left, counter-clockwise.
       +--------+ 
* CH0 -+  1  16 +- Vdd 
  CH1 -+  2  15 +- Vref 
  CH2 -+  3  14 +- aGnd 
  CH3 -+  4  13 +- CLK 
  CH4 -+  5  12 +- Dout 
  CH5 -+  6  11 +- Din 
  CH6 -+  7  10 +- CS 
  CH7 -+  8   9 +- dGnd 
       +--------+ 
       +-----+-----+--------------+-----++-----+--------------+-----+-----+
       | BCM | wPi | Name         |  Physical  |         Name | wPi | BCM |
       +-----+-----+--------------+-----++-----+--------------+-----+-----+
       |     |     | 3v3          | #01 || #02 |          5v0 |     |     |       
       |  02 |  08 | SDA1         | #03 || #04 |          5v0 |     |     |       
       |  03 |  09 | SCL1         | #05 || #06 |          GND |     |     |       
       |  04 |  07 | GPCLK0       | #07 || #08 |    UART0_TXD | 15  | 14  |       
       |     |     | GND          | #09 || #10 |    UART0_RXD | 16  | 15  |       
       |  17 |  00 | GPIO_0       | #11 || #12 | PCM_CLK/PWM0 | 01  | 18  |       
       |  27 |  02 | GPIO_2       | #13 || #14 |          GND |     |     |       
       |  22 |  03 | GPIO_3       | #15 || #16 |       GPIO_4 | 04  | 23  |       
       |     |     | 3v3          | #01 || #18 |       GPIO_5 | 05  | 24  |       
       |  10 |  12 | SPI0_MOSI    | #19 || #20 |          GND |     |     |       
       |  09 |  13 | SPI0_MISO    | #21 || #22 |       GPIO_6 | 06  | 25  |       
       |  11 |  14 | SPI0_CLK     | #23 || #24 |   SPI0_CS0_N | 10  | 08  |       
       |     |     | GND          | #25 || #26 |   SPI0_CS1_N | 11  | 07  |       
       |     |  30 | SDA0         | #27 || #28 |         SCL0 | 31  |     |       
   CLK |  05 |  21 | GPCLK1       | #29 || #30 |          GND |     |     |       
  Dout |  06 |  22 | GPCLK2       | #31 || #32 |         PWM0 | 26  | 12  |       
   Din |  13 |  23 | PWM1         | #33 || #34 |          GND |     |     |       
    CS |  19 |  24 | PCM_FS/PWM1  | #35 || #36 |      GPIO_27 | 27  | 16  |       
       |  26 |  25 | GPIO_25      | #37 || #38 |      PCM_DIN | 28  | 20  |       
       |     |     | GND          | #39 || #40 |     PCM_DOUT | 29  | 21  |       
       +-----+-----+--------------+-----++-----+--------------+-----+-----+
       | BCM | wPi | Name         |  Physical  |         Name | wPi | BCM |
       +-----+-----+--------------+-----++-----+--------------+-----+-----+
Value range: ADC=0 => V=0.0, ADC=1023 => V=15.0

If you have time, would you let me know what the expected wiring should be? What RPi pins are MCP3008's CLK, Dout, Din and CS supposed to go on? I'd like to get it right...

freshlydoug commented 6 years ago

Hi @OlivierLD I copied the wrong execute command before. For the adafruit it's the BCM pins, correct, yes, but for your application I did translate them to PI4J numbers:

Adafruit= BCM: clk GPIO05; miso GPIO06; mosi GPIO13; cs GPIO19 The above translates to PI4J

pi@raspberrypi:~/raspberry-pi4j-samples/ADC $ sudo ./battery.monitor -cal -min=486:6.82 -max=674:9.64 -miso=22 -mosi=23 -clk=21 -cs=24 Read an ADC Parameters are: -calibration or -cal -debug=y|n|yes|no|true|false - example -debug=y (default is n) -ch=[0-7] - example -ch=0 (default is 0) -min=minADC:minVolt - example -min=280:3.75 (default is 0:0.0) -max=maxADC:maxVolt - example -min=879:11.25 (default is 1023:15.0) -tune=ADC:volt - example -tune=973:12.6 (default is 1023:15.0) -scale=y|n - example -scale=y (default is n) -simulate=y|n - example -simulate=y (default is n) -log=[log-file-name] - example -log=[batt.csv] (default is battery.log)

-miso=XX - example -miso=4 (default is GPIO_04) -mosi=XX - example -mosi=5 (default is GPIO_05) -clk=XX - example -clk=1 (default is GPIO_01) -cs=XX - example -cs=6 (default is GPIO_06)

-min & -max are required if -tune is not here, and vice versa. Prms: ADC Channel:0, MinADC:486, MinVolt:6.82, MaxADC:674, maxVolt:9.64 Reading MCP3008 on channel 0 Wiring of the MCP3008-SPI (without power supply): +---------++-------------------------------------------------+ | MCP3008 || Raspberry PI | +---------++------+--------------+------+---------+----------+ | || Pin# | Name | Role | GPIO | wiringPI | | || | | | /BCM | /PI4J | +---------++------+--------------+------+---------+----------+ | CLK (13)|| #40 | PCM_DOUT | CLK | GPIO_21 | 29 | | Din (11)|| #16 | GPIO_4 | MOSI | GPIO_23 | 04 | | Dout(12)|| #15 | GPIO_3 | MISO | GPIO_22 | 03 | | CS (10)|| #18 | GPIO_5 | CS | GPIO_24 | 05 | +---------++------+--------------+-----+----------+----------+ Raspberry PI is the Master, MCP3008 is the Slave:

Since the above wasn't working for me I went back to the backup folder of the previous release manually modified the GPIO values the PI4J values miso=22 -mosi=23 -clk=21 -cs=24 in the following files: ADC\src\adc\ADCObserver.java ADC\src\analogdigitalconverter\ADCReader.java ADC\src\analogdigitalconverter\mcp3008\MCP3008Reader.java

The above setup is all because the board I bought pre-build had a different wiring to that on your board, which apparently is ok to do.

I would add from a few posts I've seen and the standard adafruit setup the standard / most common wiring setup is actually your default i.e. BCM CLK=18; MISO=23; MOSI=24; CS=25 > PI4J CLK=01; MISO=04; MOSI=05; CS=06.

Thanks,

Doug

freshlydoug commented 6 years ago

Ah so I see, you made it so I didn't need to translate them to PI4J, the following BCM numbers work!: ./battery.monitor -miso=6 -mosi=13 -clk=5 -cs=19. Therefore I guess your default values should be updated to the BCM ones CLK=18; MISO=23; MOSI=24; CS=25. Thanks, Doug

OlivierLD commented 6 years ago

Ok, I've set the default (in BatteryMonitor) to the value that work (-miso=6 -mosi=13 -clk=5 -cs=19). This way, there is no need to override them. Cheers!

freshlydoug commented 6 years ago

Hi @OlivierLD , thanks for that. I was thinking that you would keep your defaults in line with your wiring tutorial but just change the printed defaults from PI4J values to the more standard BCM ones CLK=18; MISO=23; MOSI=24; CS=25.

-miso=XX - example -miso=4 (default is GPIO_04) -mosi=XX - example -mosi=5 (default is GPIO_05) -clk=XX - example -clk=1 (default is GPIO_01) -cs=XX - example -cs=6 (default is GPIO_06)

to -miso=XX - example -miso=4 (default is GPIO_23) -mosi=XX - example -mosi=5 (default is GPIO_24) -clk=XX - example -clk=1 (default is GPIO_18) -cs=XX - example -cs=6 (default is GPIO_25)

Obviously defaulting to the values I need is slightly easier for me but I'm concerned other who follow will be confused.

Cheers,

Doug

freshlydoug commented 6 years ago

20180527_190128 Thanks again @OlivierLD , the finished product attached.

OlivierLD commented 6 years ago

Very nice! Congratulations!