element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Save The Bees Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Save The Bees Design Challenge
  • More
  • Cancel
Save The Bees Design Challenge
Blog Bees Monitor With Predators Repellent # 7 - Testing The Water Sprayer System
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Save The Bees Design Challenge to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: guillengap
  • Date Created: 18 Apr 2023 5:18 AM Date Created
  • Views 948 views
  • Likes 6 likes
  • Comments 5 comments
  • save the bees
  • savethebeesch
  • nicla vision
  • lora
  • mkr1310
  • arduino
Related
Recommended

Bees Monitor With Predators Repellent # 7 - Testing The Water Sprayer System

guillengap
guillengap
18 Apr 2023
Bees Monitor With Predators Repellent # 7 - Testing The Water Sprayer System

Table of Contents

  • Introduction
  • Getting Started
  • Edge Impulse
  • Improving Edge Impulse Model
  • Testing The Machine Learning Model With OpenMV
  • Adding The Water Sprayer System
  • Testing The Water Sprayer System
  • IoT Ambient Monitoring System | Part1
  • IoT Ambient Monitoring System | Part2
  • Summary

**********************************************************************************************************************

In this section I will show you the programming of the Nicla Vision, and the MKR WAN 1310 in the Water Sprayer system. We will also test the prototype.

image

Programming The Nicla Vision

Below I show you the code for the Nicla Vision board:

# AUTHOR: GUILLERMO PEREZ GUILLEN

import sensor, image, time, os, tf, pyb
from machine import Pin

pin0 = Pin("PG1", Pin.OUT_PP, Pin.PULL_UP)
pin1 = Pin("PG12", Pin.OUT_PP, Pin.PULL_UP)

redLED = pyb.LED(1) # built-in red LED
greenLED = pyb.LED(2) # built-in green LED

sensor.reset()                         # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565)    # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)      # Set frame size to QVGA (320x240)
sensor.set_vflip(True)
sensor.set_hmirror(True)
sensor.set_windowing((240, 240))       # Set 240x240 window.
sensor.skip_frames(time=2000)          # Let the camera adjust.

labels, net = tf.load_builtin_model('bee_or_spider_v2')
found = False

def flashLED1(led): # Indicate with LED when target is detected
    found = True
    led.on()
    pin0.on()
    img.draw_string(5, 12, label)
    pyb.delay(1500)
    led.off()
    pin0.off()
    found = False


def flashLED2(led): # Indicate with LED when target is detected
    found = True
    led.on()
    pin1.on()
    img.draw_string(5, 12, label)
    pyb.delay(1500)
    led.off()
    pin1.off()
    found = False

clock = time.clock()

while not found:
    clock.tick()
    img = sensor.snapshot()
    for obj in tf.classify(net, img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):
        print("**********nPredictions at [x=%d,y=%d,w=%d,h=%d]" % obj.rect())
        img.draw_rectangle(obj.rect())
        predictions_list = list(zip(labels, obj.output()))
        for i in range(len(predictions_list)):
            confidence = predictions_list[i][1]
            label = predictions_list[i][0]
            print("%s = %f" % (label, confidence))
            if confidence > 0.8:
                if label == "bee":
                    print("It's a BEE")
                    #img.draw_string(5, 12, label)
                    flashLED1(greenLED)
                    #pin0.on()
                if label == "spider":
                    print("It's a SPIDER")
                    #img.draw_string(5, 12, label)
                    flashLED2(redLED)
                    #pin1.on()

    print(clock.fps(), "fps")

How does it work? 

  1. This code is similar to the code from chapter 5. The bee, spider and unknow prediction scores are printed through the serial port;
  2. The conditional to activate the detection of a bee or a spider must be greater than 0.8;
  3. If the camera detects a bee, then the green LED lights for 2 seconds and prints the message "It's a BEE" on the serial port; 
  4. Additionally, pin0 (PG1 port) turns ON for 1.5 seconds and then turns OFF;
  5. If the camera detects a spider, then the red LED lights for 2 seconds and prints the message "It's a SPIDER" on the serial port; and
  6. Additionally, pin1 (PG12 port) turns ON for 1.5 seconds and then turns OFF;

Troubleshooting and solution: 

To make this script with MicroPython, I faced the problem of programming the output ports of the Nicla Vision since there is not enough information. Fortunately in the OpenMV discussion forum I found the solution: https://forums.openmv.io/t/controlling-gpio-pins-of-arduino-nicla-vision-board-using-openmv-ide/7533

Programming The MKR WAN 1310

Below I show you the code to upload to the MKR WAN 1310 board, but you can use any arduino board.

/*
AUTHOR: GUILLERMO PEREZ GUILLEN
*/

// We include the library to be able to control the servo
#include <Servo.h>

int Pin1 = 4;
int Pin2 = 5;

// We declare the variable to control the servo
Servo servoMotor;

int bee = 0;
int spider = 0;

void setup() {
  // We start the serial monitor to display the result
  Serial.begin(9600);
  // We start the servo so that it starts working with pin 6
  servoMotor.attach(6);
  pinMode(Pin1,INPUT);
  pinMode(Pin2,INPUT); 
  // initialize the servo
  servoMotor.write(100);
  Serial.println("ITS BEE OR SPIDER?");
}

void loop() { 
  if (digitalRead(Pin1)==HIGH){
    Serial.println("It's a BEE");
    bee++;
    Serial.print("BEE = ");
    Serial.println(bee);  
    Serial.println("--------------------");    
    delay(1500);
  } 
  else if (digitalRead(Pin2)==HIGH){
    Serial.println("It's a SPIDER");
    spider++;
    Serial.print("SPIDER = ");
    Serial.println(spider);    
    Serial.println("--------------------");
    servoMotor.write(175);
    delay(1500);
    servoMotor.write(100);
  }
  else {
    delay(200);  
  } 
}

How does it work?

  1. Pin1 (D4) is used to detect the bees, so a counter keeps track of the bees and prints it on the screen each time it is updated;
  2. Pin2 (D5) is used to detect the spiders, and in the same way a counter keeps track of the spiders and prints it on the screen each time it is updated;
  3. Additionally, the servo (D6) is activated to press the water sprayer for 1500 milliseconds and then release it.

Test

Below I show you a screenshot during the test carried out with this interesting prototype. You can appreciate the bee and spider counts displayed on the serial port.

image

And a zoom to the water spray bottle when it emits a waterjet.

image

Below I show you the video during the tests carried out.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

I also show you a video capture on PC so you can see what is happening in detail.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Conclusion

  1. For me the most important thing is to verify that this works before taking it to the next level, and it's satisfying for me to see that this prototype works;
  2. This project has potential applications, because it not only predicts something, but also acts, and that is difficult, at least for me; and
  3. The architecture of the MKR WAN 1310 board is made for other things, so in the final part of this project I will try to use it in a different way.
  • Sign in to reply
  • lexx28
    lexx28 over 2 years ago

    Great work! Couldn't you get rid of the MKR WAN by just directly attaching a Relay to the Nicla Vision? This Relay then triggers water spray system.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • guillengap
    guillengap over 2 years ago in reply to DAB

    In my summary I will try to touch the topics that you mentioned.

    For now I can tell you that theoretically in chapters 3 and 4 I obtained a maximum precision of 74.1%, and this means that if I do a test with 100 strange samples the model will hit 74 and fail approx 26.

    Also I obtained an accuracy of 81.89% doing a test with the sample images, which means that the model would hit 81 and miss 19 if I had the chance to do a real test with all the bees and spiders of my images.

    Why can't I raise the precision of the Nicla Vision to values above 90%? Because its memory is small, maybe with a Raspberry Pi I can achieve this goal. I think this is what Nicla Vision has to improve in its next version, maybe 1 or 2 MB of Flash memory aaditional.

    Part of what you say is true regarding the size of the images, including lighting, camera resolution, focus, etc. I will try to do a test with spiders that are more common than bees in my locality.

    This model would be ideal to test in a real apiary, and surely I would do and try many models until I get the best one. This is what artificial intelligence experts do!

    Kind regards

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 2 years ago

    It will be interesting to see how well it responds to real bees and spiders.

    I noticed the pictures were large, is the size of the insect going to cause issues?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • guillengap
    guillengap over 2 years ago in reply to dougw

    Thanks a lot! This is one of the few projects that I feel satisfied with... I suppose you have felt something similar with your work.

    Have a nice day.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • dougw
    dougw over 2 years ago

    great work...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube