Circuit diagram

Connect HC-SR04 ultrasonic sensor to the hSens1 port of your CORE2. Connect ESP8266 adapter of course, to make your lid internet connected. Connect also buzzer to any hServo port - we only use it as program controlled power source. Think of how you can put everything in to the lid or print in 3D your customized lid as we did. Then attach CORE2 and microswitch to the lid, so the switch is released when jar is opened. At the and connect power supply. We used 3 rechargeable batteries and attached it with zip ties.

SchematicsSchematicsWeb User InterfaceWeb User InterfaceTighten CORE2 to the lid with 4 screwsTighten CORE2 to the lid with 4 screwsTry to put everything togheter under the lid, as we did Try to put everything togheter under the lid, as we did

Code

Use our open-sourced code provided here on github. If you are familiar with Husarion Cloud just import it as zip, other way follow this Husarion site to connect your CORE2 to the Cloud and program it via web browser.

#include <cstddef>
#include <cstdint> 
#include "hFramework.h" 
#include "hCloudClient.h" 
#include <DistanceSensor.h> 
#define length_of_jar 13 
using namespace hModules; 
/* in cm from the ultrasonic sensor to bottom of a jar 
sensing longer distance activates the alarm */ 
int dist; 
bool armed; 
bool opened; 
hSemaphore sem; 
void cfgHandler() 
{ 
   platform.ui.loadHtml({Resource::WEBIDE, "/ui.html"}); 
} 
void onButtonEvent(hId id, ButtonEventType type) 
{ 
   if (id == "arm") { 
       if (type == ButtonEventType::Pressed) { 
           armed = true; 
       } 
   } 
   if (id == "disarm") { 
       if (type == ButtonEventType::Pressed) { 
           armed = false; 
       } 
   } 
} 
void alarmTask() 
{ 
   while (1) { 
       sem.take(); 
       for (int i = 0; i < 10; i++) { 
           if (!armed) { 
               break; 
           } 
           hServoModule.enablePower(); 
           sys.delay(300); 
           hServoModule.disablePower(); 
           sys.delay(300); 
       } 
       sem.take(0); 
   } 
} 
void hMain() 
{ 
   platform.begin(&RPi); 
   platform.ui.setProjectId("8e597746e026b534"); 
   platform.ui.configHandler = cfgHandler; 
   platform.ui.onButtonEvent = onButtonEvent; 
   DistanceSensor sens(hSens1); 
   sem.take(0); 
   sys.taskCreate(alarmTask); 
   sens.init(); 
   dist = 0; 
   armed = false; 
   opened = false; 
   sys.delay(100); 
   hServoModule.setVoltage6V(); 
   hServoModule.disablePower(); 
   hSens4.pin1.setIn_pd(); 
   for (;;) { 
       sys.delay(300); 
       platform.ui.label("l1").setText("%u", dist); 
       opened = hSens4.pin1.read(); 
       platform.ui.label("l2").setText("%u", opened); 
       if (!opened) { 
           dist = sens.getDistance(); 
       } 
       if (armed && opened) { 
           sem.give(); 
       } 
   } 
} 
At the end connect to the web browser and see if somebody is stealing your candies from your armed jar:

Or simply watch how many candies are left:

We are a team of makers, who actually made some projects.

Here we are, Pawel - web user interface and software, Alicja - UI and 3D design, Pawel - mechanic design and software

Hope you enjoy our projects ;)