Tags
EC, Ladybug Blue, pH, Playlist, Python
The EC and pH level of a nutrient bath will be different depending on the plant type. The Ladybug client software – at least initially an iOS app – gets the plant type from the user and then uses this info to determine the best pH level and nutrient level for the nutrient bath. This way, the user doesn’t have to constantly look up what the pH and EC values should be. For example, according to this table, a basil plant does best when the pH is between 5.5 and 6.5 and the EC is between 1.0-1.6mS. This table says a tomato plant does best when the pH is between 5.5-6.5 and the EC is between 2.0-5.0. I want to have a table/data base of plant types with the pH and EC values I need to set the nutrient bath.
The Goal
The goal of this post is to build a workflow that scrapes pH and EC values for herbs and vegetables from web sites and create a plist that can be used within an iOS app as an easy to use dictionary.
Open Source
The files I used include:
- Scrape_pH_and_EC_into_plist.py (located at this GitHub location).
- MyPlayground.playground (located at this GitHub location).
Design Choices
- plists or Core Data: iOS has many ways an app can read/write a table of data. I narrowed the choice to using either Core Data or plists. I chose plists because the table of pH and EC values will most likely be less than 100 (although I assume I’ll be adding other plants over time) and the lookup is very simple – given a plant type, what should the pH and EC values of the nutrient bath be at?
- Tool to scrape web pages and create plist: I chose to use the Python that is installed on my Mac’s OS X – Python 2.7 – and PyDev – the Python Eclipse plug-in. The BeautifulSoup package makes it extremely easy to scrape web pages.
Code
Python
Scrape_pH_and_EC_into_plist.py (located at this GitHub location) scrapes:
- http://www.homehydrosystems.com/ph_tds_ppm/ph_herbs_page.html
- http://www.homehydrosystems.com/ph_tds_ppm/ph_vegetables_page.html
Swift
I’m writing the Ladybug client in Swift. I used a Playground to figure out the structure I wanted for the plist file as well as the Swift code I will use to access the pH and EC values. I ended up deciding on a structure for entries within the plist to be:
<key>Basil</key>
<dict>
<key>pH</key>
<string>6.0</string>
<key>EC</key>
<string>1.3</string>
</dict>
I put a copy of the plist that is created from Scrape_pH_and_EC_into_plist.py at this GitHub location.
Here is the code I wrote in the playground to test accessing the entries in the plist:
//: Playground – noun: a place where people can play
import Foundation
if let path = NSBundle.mainBundle().pathForResource(“pH_and_EC_values”, ofType: “plist”) {
print (“Path to plist: \(path)”)
let fileManager = (NSFileManager .defaultManager())
if fileManager.fileExistsAtPath(path) {
if let plants = NSDictionary(contentsOfFile: path) {
for key in plants.allKeys as! [String] {
let plant = plants[key]
print(“plant: \(key) pH: \(plant![“pH”]) | EC: \(plant![“EC”])”)
}
}
}
}
In order to use a plist within a Playground, the plist file must be added to the Playground’s Resources folder. For example, I added a Resources folder within the Playground file I used (located at this GitHub location):
Debugging in a Playground with XCode 7
There seems to be some tweaks to displaying results in XCode 7. The tweaks confused me so I thought I’d document them here. This will probably change in an upcoming release.
I ran across some settings that needed tweaking before the debugging experience was adequate. Prior to changing settings, I would get very little info on what was going on. For example:
In the above example which I pulled from a Stackoverflow posting, notice how debugging just shows there are 4 items. Viewing details just shows the last item. To fix this, I had to hover over the popup that shows results and right click to change the view from “Latest Value” to “Value History”.
The other thing to do is to show the debug area by clicking the button on the far left:
That’s it for now. Thank you for reading this far. Please find many things to smile about.