r/learnjava • • Jul 07 '19

My very first Java / Raspberry Pi project is almost complete!

Been learning Java for a month now and I almost have my first project completed. It's an automatic plant watering system using a Raspberry Pi with a relay controlled water pump and a soil moisture sensor. The README.md in the git explains more. I'm just having a couple small problems with getting the pi4j library integrated into my project - a serious noob problem I know, but hence, I am a noob - any help here would grant you much karma.

here is a link to my github: https://github.com/jayJavaGGDev/autoWaterPlantSystem0.git

any help would be greatly appreciated.

edit:

&& pix or it didnt happen

https://imgur.com/eoq4T8X

https://imgur.com/GOiGUvB

https://imgur.com/mSOpL9d

https://imgur.com/EOcHRBL

edit 2:

if you're wondering I was able to use Runetime.getRuntime().exec("path to bashfile"); to point my java timer program to a bash file that runs sudo python turnOn.py and one for turnOff, which are just 4 line scripts that turn on and off the pins except I think my relay might have been damaged because it's not turning on at all! of course this method isn't going to work to get input from my moisture sensors...

edit 3:

july, 8 11:52 am.

after a day of debugging my code and wiring i discovered that i hadn't shorted out my relay after all. somewhere along the lines i had set my pinout to bcm and not updated my relay pin code from board... highly embarassing!

So I've gotten my java code to run on my pi and call a bash script that runs the python script to turn on and off the pins. but the problem is i have two separate python scripts for on and off and when my java code executes the powerOn.py file, the gpio gets turned on but the program stays running forever then pumpOff.py is executed and they both run simultaneously and the pin just gets turned on and off again until i reset the Pi.. help plz!

47 Upvotes

42 comments sorted by

5

u/Slickbock Jul 07 '19

I'd suggest using a build tool like maven. You can specify dependencies and compile them into a single jar file (just an example of the many things maven can do). On mobile so cant link the docs (sorry!) but its very easily found with google

1

u/bwz3r Jul 07 '19

I've been wondering about maven, but not sure exactly what it is or how to use it. Could u maybe explain a bit further in detail, or link to a good source that could explain it for me if you know of one? sometimes finding a reliable source can be a bit iffy on these more in depth subjects..

3

u/AnEmortalKid Jul 08 '19

1

u/AnEmortalKid Jul 08 '19

You can then use a fat jar to have all your dependencies and run that from your raspberry pi

1

u/bwz3r Jul 08 '19

I've been researching it today I'm losing my mind trying to figure it out

2

u/AnEmortalKid Jul 08 '19

I can make a PR with the changes needed if you’d like!

Or walk you through it on discord

1

u/bwz3r Jul 08 '19

i would be super happy to get some help with anything. i've been doing everything on my own so far. but hey i got it working by using Runetime.getRuntime().exec("pythonscript");

except now my relay won't turn on. i tested my code by linking it to a seperate python script that just sends an email when i press a button... that worked. so why is my relay not turning on now? i made two py files called powerOn and powerOff that call GPIO.HIGH and GPIO.LOW and used Runtime to call a bash that runs them in sudo mode since it crashed when it didn't get called in sudo. so i know my code works to call the py scripts and now my old scripts won't work on the relay.. i think it might be burnt out?? but i used gpio readall in terminal and the pin state didn't change at all between calling the powerOn and powerOff scripts.. there was a small accident where the pump was pumping air for a couple seconds i think i might have fudged it up.

1

u/AnEmortalKid Jul 08 '19

Could be burnt :(

I can help you out with maven and pi4java but I haven’t played much with a PI. I’d probably have to learn a bit to help wit the rest.

1

u/bwz3r Jul 08 '19

yes senpai. teach me the ways of maven, from what i gather it is a way to organize which libraries i want to use, and keep them as like a template to build and rebuild projects from the cloud?

2

u/AwakenedToNightmare Jul 08 '19

I wrote a couple of guide comments on Maven. You can check out: this and this. On the bottom of one comment are screenshots from Idea Intellij. That's more about adding dependencies, but there's more to Maven, you could setup a lot of stuff inside the <build> tag.

1

u/AnEmortalKid Jul 08 '19

Sort of. It’s a way to specify which libraries you depend on within your project. Then anyone can build the project and run it. So we’d probably use maven to get that pi4java dependency and use that in your project.

→ More replies (0)

1

u/bwz3r Jul 07 '19

hmm thinking a bit about github.. could i push a git from my pi with the pi4j library to my github and then git it on my desktop and continue with the library already installed somehow? since pi4j libary is already installed on my pi, either way i actually have the library on my desktop as well but when i try to add to dependencies its not working

2

u/Moktok Jul 07 '19

Not 100% sure, but git comes preinstalled on most Linux distros. Dunno if raspbian cuts it out. But I think it should be in the repos

1

u/iamsooldithurts Jul 08 '19

Git is just for revision control. It’s a service to store your work products, as they all (svn, rcs, sccs, etc) are.

You should never commit reproducible type things to the repository. The external library, your project’s executable/jar/whatevs, none of it. Just the source required to reproduce it. https://en.wikipedia.org/wiki/Version_control

1

u/[deleted] Jul 08 '19

If you are using intellij you can just add the maven dependency without having to compile anything yourself.

Also im pretty sure these are the compiled libs https://pi4j.com/download/pi4j-1.2.zip

1

u/bwz3r Jul 08 '19

the problem is I can't figure out how to add them correctly into my intellij project.. is it safe to run the code on my desktop? I'm sure they probably won't even have access to my motherboard but I wouldn't want to turn something off!

1

u/[deleted] Jul 08 '19

File -> Project Structure -> Modules -> Dependecies -> Click on + -> maven -> Type com.pi4j:pi4j-core:0.0.5 in the input field and click enter

1

u/bwz3r Jul 09 '19

I'm not seeing Maven anywhere. am I missing a plug-in?

1

u/[deleted] Jul 09 '19

It may be that you have to create a maven project instead of a normal java project

1

u/iamsooldithurts Jul 08 '19

Weirdly enough, there’s a website for that: https://maven.apache.org/

1

u/ab624 Jul 07 '19

Cool ! Can you show us the physical hardware as well..

2

u/bwz3r Jul 07 '19 edited Jul 07 '19

if you look in the wiki page of the git there are a few pictures

edit1: you know what my links are broken check back in a bit I'll fix soon

edit2: heres direct links to pix: https://imgur.com/eoq4T8X https://imgur.com/GOiGUvB https://imgur.com/mSOpL9d https://imgur.com/EOcHRBL

i'm fixing the gitwiki now

1

u/ab624 Jul 08 '19

Thank you :-)

1

u/[deleted] Jul 07 '19 edited Jul 07 '19

I had no idea Raspberry Pis could run java.. The reason why I haven't picked one up is because I CBA to learn Python.

Heads up - if you use eclipse/intelliJ, go to refactor and press format, makes it so much easier to read but looks like you've got the right thing going.

1

u/matemik Jul 07 '19

It can indeed. On raspbian, java 8 comes preinstalled as well.

1

u/do11abill Jul 08 '19

While yes, it can run a JVM, the JDK/JRE needed is a special kind. While in most cases (including this post's particular case) this is fine, if you were to use something that required x86 architecture, a Pi will not work for you. I wanted to setup a thing called Kubernetes using a cluster of 3 pis and then deploy to it but because the architecture's were ARM instead of x86, I could not use the same Docker image. So ALL development had to be done directly on a pi (something I didn't want to do. Just wanted to let you, and maybe others, know that while it is Java, the architecture's are not quite the same so just something to look out for.

1

u/bwz3r Jul 08 '19

so what you're saying is I have to hook my pi up to my TV and code on my tiny little battery powered keyboard and mouse?

2

u/[deleted] Jul 07 '19

oh man oh man oh man, time to get ordering one.

1

u/AutoModerator Jul 07 '19

It seems that you possibly have a screenshot of code in your post My very first Java / Raspberry Pi project is almost complete! in /r/learnjava.

Screenshots of code instead of actual code text is against the Code posting rules of /r/learnjava as is outlined in the sidebar - Code posting.

  • No screenshots of code!

If you posted an image merely to illustrate something, kindly ignore this message and do not repost. Your post is still visible to others. I am a bot and cannot distinguish between code screenshots and other images.

If you indeed did this wrong, please edit the post so that it uses one of the approved means of posting code.

  • For small bits of code (less than 50 lines in total, single classes only),
    the default code formatter is fine
    (one blank line before the code, then 4 spaces before each line of code).

  • Pastebin for programs that consist of a single class only

  • Gist for multi-class programs, or programs that require additional files

  • Github or Bitbucket repositories are also perfectly fine as are other dedicated source code hosting sites.

  • Ideone for executable code snippets that use only the console

Please do not reply to this message, because I am a bot.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/do11abill Jul 08 '19

Only to compile it. You should have no problems in your case.

0

u/[deleted] Jul 08 '19

[deleted]

1

u/bwz3r Jul 08 '19

about 2 months ago I started looking around YouTube for free programming videos and about one month ago I bought my first paid class and after that it was a wrap. I've always been interested in computers an I recently tore my ACL and meniscus so I've been living with my parents the last couple months doing nothing but advancing thought my Java courses.