It is of critical importance for the farmers that have dairy milk production cows to know the exact day and hour their cows have an estrous cycle period, which indicates that the animal is ready for insemination. Usually this is done by monitoring the cows for various physical signs. If the farmer misses the day then the cow will not get pregnant and this will result in no milk production for around 20 days.
The ideaThankfully, when the cows have an estrous cycle, they start running here and there like crazy and generally speaking they are more active. This activity can be detected with a small device that measures the steps a cow takes on a daily basis and when it exceeds them, the farmer will know that it is ready for insemination.
The device can be a very tiny device (in the size of a smartwatch) that contains an acceleration sensor, a small battery and a tiny processor. The device should be able to do very simple mathematical calculations in order to detect a step. Such device can also be worn either on the foot of the cow or in the head or neck.
Also, if we have a farm that has 1000 cows being monitored by such devices, there is somehow a need to upload their data to the cloud. Obviously it is impossible to install a GSM modem in every device as the cost of the device will go up quite a lot. In order to solve that problem, the devices will need to connect in other nearby devices that have internet connection. These devices will act like Access Points although they will play a slightly different role.
In the above picture, we can see 7 cows, each one connecting via WiFi to a nearby Access Point. Thanks to Android Things, this process can be done very easily by implementing Network Discovery Services. In reality, on the first boot, these Access Points will register themselves as servers on the WiFi network. Every 4 hours, the devices that cow wear (CowBits), are going to do a network discovery to find such nearby Access Point. When they find one, they will connect to it and send it the data. The data will be processed by the Access Point and when it is ready it will upload them to the cloud.
In our project, we implement the cow devices that are going to count the steps by using the PICO-PI-IMX6UL board from Wandboard and we implement the Access Point (lets call it server) part by using a Raspberry Pi 3.
Ok! Lets start!
HardwareMaking the connection
In order to successfully make a step counter, we need to connect the Adafruit Precision NXP 9-DOF Breakout Board to the Pico-Pi board. Do the connection as indicated in the Fritzing diagram bellow.
Your hardware connections should end up looking like the bellow 2 pictures.
Flashing the devices
The next thing you need to do is to flash both the Pico-Pi and the Raspberry Pi devices with their latest O/S images.
In order to flash the Raspberry Pi, follow the directions found in this link.
To flash the Pico-Pi, follow the directions found in this link.
FirebaseFor the project, we will use the Google Firebase DB in order to store the sensor data. Lets initialize it!
Go to https://firebase.google.com/ and register if you haven't done it yet. Sign in and go to the console by pressing the link indicated in the bellow image.
Press the Add Project button to create a new project.
Enter a project name, select a Country/region and press the Create Project button.
After the project gets created, click on the Authentication option in the menu, then on the Sign-In Method and enable the Email/Password authentication.
Then, click on the Users tab in the Authentication page to enter a new user. For now, as an email enter test@test.com and as a password the '123456' (numbers from 1 to 6). Later on we can enter different user and change these values from the program if needed but for now enter it like that.
Keep a note of the User UID on the right side of the user. We will need that ID in the next step. Take the following Json.
{
"users" : {
"<Enter the user ID>" : {
"clientID" : "testClient",
"mobileAppUser" : true,
"uploadUser" : true
}
}
}
Replace the key <Enter the user ID> with the ID that you copied from the users tab in the previous step. Save it to a folder with a name you can remember (eg Users.json).
Press on the Database option of the Firebase menu. Then click on the Get Started button under the Realtime Database section. On the Data tab you will see three dots nearby the link of the database. Click them and then click on the Import JSON menu item. There, browse and select the file you just created (eg Users.json) and press the import button. Your DB data should look like bellow (look at the user ID).
The Pico-Pi device is doing the following things. Take the sensor readings, analyzes them and then pushes them to the Access point for further processing and upload of the results to the cloud. Currently, the data (the steps number) are stored on the DB on a per minute interval. What about if the user wants to see the steps taken by a cow for a specific day? In that case we need to read all the steps stored on the 1440 minutes of that day and sum them. This is an intensive task to be done on a mobile device. It requires also a lot of data to be downloaded from the server. Not good right?
Thankfully Google is supplying very good tools in order to give solutions to such problems. What we need to do is actually do the computation task on the server side. So, when a client stores step values to the DB, the server gets triggered and starts doing various sums in order to calculate cumulative step counts for various intervals (eg. per hour, per day, per month, etc). This functionality can be implemented by using the Google Cloud Functions for Firebase.
In order for us to create a Firebase cloud function we need to install and setup NPM that comes with Node.js. Go to https://nodejs.org/en/ and download Node.js for the version of your operating system. Then install it.
If you haven't done yet, it is a time to download the source-code of CowBit from Github. Go to https://github.com/ekalyvio/CowBit and download or clone the repository. After you do that, open a command prompt and go to the directory you have unzipped the repository. You should see a Firebase folder inside that. Go to that folder and also enter the functions folder (repository/Firebase/functions). Run the following command to install the CLI.
npm install firebase-tools
Depending your internet connection and the speed of your computer, this process might take several minutes. Ensure that you have a big cup of coffee nearby you as you will need fuels to continue!
For a reason I don't quite understand we have to run also the following command.
npm install
Please, let me know in the comments bellow if you know the reason or if I have done some mistake. That is the Hackster community for!
Then run
firebase --version
and ensure that version is above 3.5. Run
firebase login
and login with your Google account. Run
firebase use --add
and select the Firebase project from the list and later an alias for the project when asked (eg. default). Run
firebase deploy
The above command will generate the function that sums the steps into the cloud. If everything gone smooth so far you should be able to see your function on the Firebase console. Click on Functions option on the Firebase menu and ensure that the sumCowSteps function exists (see picture bellow).
Cows ruminate. That means that they have a temporary stomach (second one) and store the food there after they eat it. Later, during the day they bring back the food and they ruminate it. When the cows have diseases, during the rumination they usually spit the food. We could say that they are vomiting it. Usually farmers find on the ground the vomits but they don't know from which cow it comes from. The diseases are also being spread rapidly to other cows as they eat from the ground and they take the virus in. At that point in time, there is an urgent need for the farmer to find the sick cow, isolate it and give it treatment and medicines but this is impossible as the farmer doesn't know which of them is sick. Because of that reason, the farmers start giving antibiotics to all the cows either they need them or not. Antibiotics are a huge cost for milk production farms!
CowBit is getting raw data from the sensors and then passes them to the access point. The access point parses them and by using Tensorflow it can detect specific movements during the cow rumination process. That means that it will be detecting if the cow puked the food and as such if it is sick or not.
LOL!!! I am joking you!
Such functionality is not yet implemented but it is on the to-do list to be done in the near future. When it gets implemented I will update the project on the Github repository and as such have a close look at it.
Last step before running the appsGo to the Firebase console. On the right side of the Overview menu item, there is a gear icon. Click it and then click on the Project Settings.
Click on the ADD APP button. Forgive me for the messy screen shot. I had to blank out information I believed they were slightly sensitive to the public ;).
Click on the Add Firebase to your Android app button. As a package name enter the following:
com.kaliviotis.efthymios.cowsensor.serverapp
For app nickname enter Server App.
After you register the app, press the Continue and then the Finish buttons. You do not need to download anything yet.
Now, our server application is setup to communicate with Firebase. We have to redo the whole process one more time in order to enable our client (mobile) app to communicate with Firebase too.
Just this time, as a package name enter:
com.kaliviotis.efthymios.cowsensor.mobileapp
and as nickname enter Mobile App.
After you press the Register button, do not forget to download the google-services.json file as instructed on the next screen.
Open the project in Android Studio. On the project structure, open the ServerApp module. Drag & drop the google-services.json file there. The folder structure should be like bellow.
Do the same for the MobileApp module (recopy the file there).
Congratulations! You have finished the whole process. You are now ready to run the applications and start gathering data!
Running the appsInside the Android Studio project, there are three modules. One called MobileApp, one called SensorApp and one called ServerApp. You have to run the SensorApp on the Pico-Pi device that has the sensor, the ServerApp on the Raspberry Pi that will act as a kind of Access Point and the MobileApp on any Android mobile device.
Keep in mind the following. Before running the SensorApp and the ServerApp, ensure that in their manifest files located in src/main/AndroidManifest.xml under each project, the following line is not commented out.
<category android:name="android.intent.category.IOT_LAUNCHER" />
After running the applications for the very first time, then you can reboot both devices. The applications should start by themselves after the devices boot. You should see the first data coming after approximately 4 hours or a little bit longer.
If you run the mobile app, on a device, depending on how many sensors you have and how many data you retrieved, you should see pictures like the bellow.
In order to sign in on the mobile device, use as email the test@test.com (the one we created at the first step). As a password enter '123456' (numbers from 1 to 6). We have already created that user on the DB. You remember it? After signing-in, tap a cow from the list (if it exists one) to go to its details. There you can change its name to something more meaningful rather than looking at hexadecimal ID. Also, by clicking the bottom-right button you can alter the graph to display the number of steps taken at a specific day, month or year.
Some info about the softwareInside the StepsDataStorage.java file of the SensorApp module, there is a constant called lMinutesToUpload
. If you want to test the apps, instead of waiting for 4 hours for the data to get uploaded you can make this process occur faster. Change that variable to something like 60*1000. This is 60000 milliseconds (one minute).
Inside the SensorFusion.java file, there is a constant called ProcessorType
. If you set that value to 1, then the device will be storing raw data from the sensor to various files. No calculation of step counting will happen. If you set that value to 2, then only step counting calculation will occur (plus it will be pushing the data to the Access point). If that value is 3, both of the above functions will occur at the same time.
In the same file (SensorFusion.java), in case you do not own a Pico-Pi board but you own a second Raspberry Pi 3, you can uncomment the following line in the Raspberry Pi 3 section and comment it in the Pico-Pi section.
public static final String I2CInterface = "I2C1";
Remember that you have to do the correct wiring on the Raspberry Pi as it is different with the Pico-Pi.
Also, in order to read the sensor in Raspberry Pi 3 you have to issue the following command from the shell as root.
echo -n 1 > /sys/module/i2c_bcm2708/parameters/combined
This command is enabling combined mode on Raspberry Pi. Without running the command, the Raspberry can not read the sensor data. Google has been informed by an incident for that matter and hopefully they will make it run automatically at boot time on the next version of Android Things for Raspberry Pi. A person from Google has issued a bug report on my behalf on the following link.
Thoughts and the future of the projectAndroid Things on the sensor side, permits us to create a fully functioning prototype software in a very short time to be used as a proof of concept. Soon, the device & the software is going to be tested on a real cow and we are going to take valuable information from the sensors. We will also verify the step counting algorithm and ensure that such algorithm can operate smoothly without any problems. Obviously the cow will have to walk in various type of surfaces (concrete, sand, grass, etc.) in order to see if the algorithm operates correctly in all these cases.
As stated above, the Pico-Pi device is not intended to be used as a commercial product in such kind of project. It is not energy efficient and it runs at least at 50 milliamps on average. As such, it will only be used as a proof of concept. When we see that the software operates smoothly in the Pico-Pi, it will be rewritten on an other small form factor device made only for such operations.
On the above picture you can see an assembled Adafruit Feather Huzzah board that has the ESP8266 cpu. That board together with the sensor shouldn't consume more than 2 milliamps of power in the worst case scenario. A simple 750 mAh battery as the one on the picture can keep the whole device up and running for weeks (and even months) without the need to recharge it.
Depending on the veterinarians suggestions we might also embed a temperature sensor in the device in order to get the temperature readings of the cows. Also, by utilizing various sensor fusion algorithms like the Mahony or the Madgwick (see that link) we can know the orientation of the device. As such we can know if the cow is laying on the ground and is sleeping or not. Generally speaking, we can gather plenty of data in order to be able to detect the future behaviors of each cow. This can be done with the use of Artificial Intelligence toolkits like Tensorflow.
FinishingI hope that by now I have gathered your attention to such project. If you have any ideas or suggestions on how it can become better, please let me know in the comments bellow. I will constantly observe them and I hope I will be able to answer your questions promptly.
Comments