Another coding post I’m afraid (and I suspect the next post will also be code related to some extent).
This will probably be a quick post, with hopefully a longer, more detailed post to follow…it will make sense. Maybe.
So, on Thursday evening, Claire and I went to a thing. A really interesting thing that has kind of inspired me to do something a bit different. As a result of this I wanted to try and gather a bit of data from the Raspberry Pi and envirophat I have from the Connected Hull people (The event we attended was organised by these guys as you might have guessed).
I decided to fire up the Raspberry Pi 3 and the envirophat that I have been lent to help write some instructions for the LORAWAN setup in the city (see this post for more on that).
Anyway… I accessed the pi through my remote connection (No need to shuffle HDMI cables around. Also, there’s no point in trying to get access hacker dudes – my home network doesn’t talk to the outside world or this blog) and double checked the python commands worked to pull data from the envirophat. Which it did. It’s always nice to know that you have actually remembered something that you learned a few weeks ago.
A few short commands later (and a bit of editing of the code I wrote last month to output the data into a nice .txt file) and I had a couple of little programs.
I wrote a program… I’m a programmer… (kind of).
These programs are almost exactly the same, they take the data from the envirophat. Light data and temperature data to be exact, each program outputs to a nicely named .txt file, one for the temperature data and one for the light data (I could probably have worked out how to output the data to two text files from the simple program, but that’s for another day).
#!/usr/bin/pyhton from envirophat import light //Import the code to read the sensor f = open("light.txt","a") //This opens the named text file in append mode and assigns it to 'f' lux = light.light() //This sets the data from the light sensor as 'lux' f.write("%s \n" % lux) //This writes the data from the sensor to the file f.close() //This closes the updated file
#!/usr/bin/pyhton from envirophat import weather //Import the code to read the sensor f = open("temp.txt","a") //This opens the named text file in append mode and assigns it to 'f' temp = weather.temperature() //This sets the data from the temperature sensor as 'temp' f.write("%s \n" % temp) //This writes the data from the sensor to the file f.close() //This closes the updated file
Great.. run the programs and they happily create the text file and drop the data into it. Even better, each time the program runs, it adds a new line with the data it pulls. Which is exactly what I wanted. A simple list of numbers. You can do stuff with numbers.
Now, I love learning and I love making this do cool stuff, but I’m not going to sit at a terminal and type the run command into the terminal over and over again to gather different data. A solution is needed.
Enter crontab. For those of you who don’t know, crontab is a scheduler for the raspberry pi (and linux in general, that’s the operating system (OS) the Rasperry Pi OS is based on too). Using crontab you can schedule any event to run at pretty much any time on any day of any month. Want something to run at five minutes past seven on every Monday? Simple. If you know how it works.
Knowing that I wasn’t exactly sure about how this works (it can be crazy complex) and knowing what I wanted to do I popped over to google and had a quick look. 30 seconds later my crontab schedule is set up to run these two little programs every two minutes. Great. My data gathering system is up and running.
Now my Raspberry Pi and it’s envirophat output the temperature and light readings every two minutes into a .txt file that I can (maybe) do all sorts of cool things with…
Which brings me to my reasons for doing this… Which you’ll have to wait until next time to find out about 😉
It might even be worth the wait…
#data #IoT #Pi #linux #python #Coding #envirophat #Tech #RaspberryPi