Jeff Addleman
Published © GPL3+

Pipe Break Detector

A RSL10 based system that detects water flow in a pipe by monitoring the sound level intensity of a water inlet pipe.

IntermediateFull instructions provided4 hours1,168
Pipe Break Detector

Things used in this project

Hardware components

RSL10-SENSE-GEVK
onsemi RSL10-SENSE-GEVK
×1
Resistor 500 Ohm
×1
5 mm LED: Red
5 mm LED: Red
×1

Software apps and online services

onsemi On Semiconductor IDE

Hand tools and fabrication machines

Breadboard, Clear
Breadboard, Clear

Story

Read more

Schematics

Pipe_Break2 Schematic

Connect LED to the Spare Output.

Code

RSL-10 Pipe_Break2 main.c

C/C++
Replace the main.c file with this one.
//-----------------------------------------------------------------------------
// Copyright (c) 2018 Semiconductor Components Industries LLC
// (d/b/a "ON Semiconductor").  All rights reserved.
// This software and/or documentation is licensed by ON Semiconductor under
// limited terms and conditions.  The terms and conditions pertaining to the
// software and/or documentation are available at
// http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf ("ON Semiconductor Standard
// Terms and Conditions of Sale, Section 8 Software") and if applicable the
// software license agreement.  Do not use this software and/or documentation
// unless you have carefully read and you agree to the limited terms and
// conditions.  By using this software and/or documentation, you agree to the
// limited terms and conditions.
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <BDK.h>
#include "BSP_Components.h"
#include "main.h"

int main(void)
{
    int32_t status = 0;

    BDK_Initialize();

    // ************** Added for Spare Output ***************************
    LED_Initialize(PIN_GIO_SPARE);
    // *****************************************************************

    // ************** Changed LED names ********************************
    LED_Initialize(LED_RED);
    LED_Initialize(LED_GREEN);
    LED_Initialize(LED_BLUE);
    // *****************************************************************

    // ************** Test LEDS with 1 1/2 second delays *******************
    LED_On(LED_RED);
    HAL_Delay(1500);
    LED_Off(LED_RED);

    LED_On(LED_GREEN);
    HAL_Delay(1500);
    LED_Off(LED_GREEN);

    LED_On(LED_BLUE);
    HAL_Delay(1500);
    LED_Off(LED_BLUE);

    // *****************************************************************
    // ************** Initialize on board Button 0**********************
    BTN_Initialize(BTN0);
    // *****************************************************************

    printf("\r\nPipe Break 2 Project.\r\n");

    status = NOA1305_ALS_Initialize();
    ASSERT_DEBUG(status == 0);

    status = NOA1305_ALS_StartContinuous(APP_ALS_PERIODIC_INTERVAL_MS,
            &ALS_ReadCallback);
    ASSERT_DEBUG(status == 0);

    status = NOA1305_ALS_EnableInterrupt(APP_ALS_THRESHOLD_LEVEL_LUX);
    ASSERT_DEBUG(status == 0);

    uint32_t tt = HAL_Time();

    printf("Threshold = %lu\r\n\n", APP_ALS_THRESHOLD_LEVEL_LUX);

    printf("APP: Entering main loop.\r\n");
    while (1)
    {
        /* Execute any events that occurred and refresh Watchdog. */
        BDK_Schedule();

        SYS_WAIT_FOR_INTERRUPT;
    }

    return 0;
}


void ALS_ReadCallback(uint32_t lux, bool is_interrupt)
{
    if (is_interrupt == true)
    {
    	if (lux >= APP_ALS_THRESHOLD_LEVEL_LUX)
        {
        printf("ALS: LUX value passed threshold.\r\n");
    	// Print this line if callback was called in response to lux value
        // passing above / below threshold value.

        // ********** Read Push Button 0 state. **********************************
        bool button0_pressed = BTN_Read(BTN0);
        // ********* Toggle Spare Output if the button 0 is activated *************
    		if (button0_pressed == 1)
			{
    			printf("Button 0 Is Pressed.\r\n");
    			LED_On(PIN_GIO_SPARE);
    			HAL_Delay(5000);
    			LED_Off(PIN_GIO_SPARE);
    	        printf("GPIO SPARE toggled.\r\n");
			}
       	    printf("Interrupt LUX = %lu\r\n\n", lux);
         }
    }
    printf("LUX = %lu\r\n\n", lux);
}

Readme file for Pipe_Break2

HTML
Replace existing readme file in the project with this one.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang xml:lang>
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="pandoc" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  <meta name="author" content />
  <title>Pipe Break2 - NOA1305 Example</title>
  <style>
      code{white-space: pre-wrap;}
      span.smallcaps{font-variant: small-caps;}
      span.underline{text-decoration: underline;}
      div.column{display: inline-block; vertical-align: top; width: 50%;}
  </style>
  <style type="text/css">
table {
border: 2px solid black;
border-collapse: collapse;
}
th {
border-bottom: 2px solid black;
border-right: 1px solid black;
padding-left: 3px;
padding-right: 3px;
}
td {
border-right: 1px solid black;
padding-left: 3px;
padding-right: 3px;
}
tr.odd {
background-color: aliceblue;
}

.level2 {
margin-left: 3em;
}
.level3 {
margin-left: 3em;
}
.level4 {
margin-left: 3em;
}
.auto-style1 {
	font-size: large;
}
</style>
  <!--[if lt IE 9]>
    <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
  <![endif]-->
</head>
<body>
<header id="title-block-header">
<h1 class="title"><span lang="en-us"><em>Pipe_Break2</em></span><em> Example</em></h1>
<p class="author"></p>
</header>
<nav id="TOC" role="doc-toc">
<ul>
<li><a href="#overview">Overview</a></li>
<li><a href="#hardware-requirements">Hardware Requirements</a></li>
<li><a href="#setup">Setup</a></li>
<li><a href="#verification">Verification</a></li>
<li><a href="#notes">Notes</a></li>
</ul>
</nav>
<p>
&nbsp;</p>
	<h1>
	<img src="https://i.imgur.com/O0K6dAI.jpg" alt="Lost Image" width="225" height="225" hspace="150"></h1>
	<p>
	&nbsp;</p>
<section id="overview" class="level1">
<h1><em>Overview</em></h1>
<p>This example us<span lang="en-us">as</span> <span lang="en-us">the</span>
<span lang="en-us">onboard </span>NOA1305 Ambient Light Sensor (ALS)<span lang="en-us"> 
to control a motorized ball valve</span>.</p>
	<p><span lang="en-us">The ball valve will shut off the water supply inlet if 
	a water leak is detected by exceeding the LUX threshold.</span></p>
	<p><span lang="en-us">The interrupt function is used to detect if the sensor 
	LUX value exceeds the threshold set in the &quot;main.h&quot; file.</span></p>
<p><span lang="en-us">The </span>NOA1305_ALS library <span lang="en-us">is used 
to generate an</span>&nbsp; interrupt when <span lang="en-us">the </span>light 
intensity raises above / drops below specified threshold value.</p>
</section>
<section id="hardware-requirements" class="level1">
<h1><em>Hardware Requirements</em></h1>
<p><span lang="en-us">E</span>valuation board:</p>
<ul>
<li>RSL10-SENSE-<span lang="en-us">DB-</span>GEVK<br>
<ul>
<li>3V Lithium CR2032 battery <span lang="en-us">or an external 3V supply </span>is required for proper operation.</li>
<li>Debug probe included with the board<span lang="en-us"> is required for 
programming.</span></li>
</ul></li>
	<li><span lang="en-us">Relay to control the ball valve</span></li>
	<li><span lang="en-us">Motorized ball valve</span></li>
</ul>
</section>
<section id="setup" class="level1">
<h1><em>Setup</em></h1>
	<ol>
		<li><span lang="en-us">Connect a 330 to 500 ohm resistor in series with 
		a LED between the "GIO" output in the 5 pin hole pattern and ground.</span></li>
		<li><span lang="en-us">Copy the NOA1305 example</span></li>
		<li><span lang="en-us">Rename the project Pipe Break 2</span></li>
		<li><span lang="en-us">Rename rteconfig to Pipe_Break2.retconfig</span></li>
		<li><span lang="en-us">Open main.h and change settings as needed</span></li>
		<li><span lang="en-us">Copy and paste main.c code</span></li>
		<li><span lang="en-us">Clean, Build and Debug program</span></li>
		<li><span lang="en-us">Verify that the LED lights when the threashold is 
		exceeded and the Push Button on the outer edge is activated.</span></li>
	</ol>
	<p>&nbsp;</p>
</section>
<section id="verification" class="level1">
<h1><em>Verification</em></h1>
	
	<p><span lang="en-us">If the outside button is actuated (system armed) and 
	the light level is exceeded, the ball valve will operate to shut off the 
	water supply.</span></p>
	<p><span lang="en-us">This can be observed in the debug mode by using the 
	J-Link RTT program.</span></p>
	<p class="auto-style1"><strong>Sample terminal output:</strong></p>
<pre><code>APP: NOA1305_ALS example.
APP: Entering main loop.
ALS: LUX = 150

ALS: Loop LUX = 147
ALS: LUX = 149

ALS: LUX = 152

ALS: LUX value passed threshold.
ALS: LUX = 596

ALS: LUX value passed threshold.
ALS: LUX = 589

ALS: LUX = 587

ALS: Loop LUX = 587
ALS: LUX = 561

ALS: LUX value passed threshold.
ALS: LUX = 591
</code></pre>
</section>
<section id="notes" class="level1">
<h1><em>Notes</em><span lang="en-us">:</span></h1>
	<p><span lang="en-us">The PIN_GIO_SPARE output is used to drive a relay 
	which in turn controls the motorized ball valve to shut off the water inlet.</span></p>
	<p><span lang="en-us">&nbsp;The LED function configures the output as 
	Sys_DIO_Config(PIN_GIO_SPARE, DIO_6X_DRIVE | DIO_NO_PULL | 
	DIO_MODE_GPIO_OUT_0);</span></p>
	<p><span lang="en-us">Be careful not to overload the PIN_GIO_SPARE because 
	it's maximum rating is 12 ma.</span></p>
	<p><span lang="en-us">The output is controlled using the LED function. Turn 
	the output on/off&nbsp; the same way that LEDs are controlled in other 
	example projects.</span></p>
	<p>&nbsp;</p>
<hr />
<p>Copyright (c) 2018 Semiconductor Components Industries, LLC (d/b/a ON Semiconductor).</p>
	<p><span lang="en-us">Modified by J. Addleman 2020</span></p>
</section>
</body>
</html>

Pipe_Break_ALS main.c file

C/C++
Replace the main.c text with this file. Use for Pipe_Break_ALS program. The file is located in the scr folder.
//-----------------------------------------------------------------------------
// Copyright (c) 2018 Semiconductor Components Industries LLC
// (d/b/a "ON Semiconductor").  All rights reserved.
// This software and/or documentation is licensed by ON Semiconductor under
// limited terms and conditions.  The terms and conditions pertaining to the
// software and/or documentation are available at
// http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf ("ON Semiconductor Standard
// Terms and Conditions of Sale, Section 8 Software") and if applicable the
// software license agreement.  Do not use this software and/or documentation
// unless you have carefully read and you agree to the limited terms and
// conditions.  By using this software and/or documentation, you agree to the
// limited terms and conditions.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/*  This program will read the LUX value and compare it to a threshold value in "main.h" file.
    If the "button0" is pressed the system is considered armed.
    If armed and the LUV value exceeds "THRESHOLD_LEVEL_LUX: value in the "main.h" file,
    the spare output is set high for a period defined in "main.h" "DIO_OUTPUT_INTERVAL_MS".
    Note that the output "PIN_GIO_SPARE" is treated as a LED to turn it on/off
    The output "PIN_GIO_SPARE" is labeled "GIO" on the board located in the 5 hole pattern.

    Steps to use this program:
    1)  Copy "sense_ics_firmware" to the Project Explorer View from CMSIS Pack Manager View.
        It is called "Custom Service Firmware (RSL10-SENSE-GEVK)" in the CMSIS View.
        Using this project ensures that all of the required BDK components are included.
    2)  In the Project Explorer View, rename the project.
    3)  Rename "xxxxxxx.rteconfig" file to the new project name.
    4)  De-select the BLE - peripheral server - BASS component to remove battery status.
    5)  Replace the code in the "main.c" file with this code (copy / paste).
    6)  Replace the code in the "main.h" file with the new "main.h code"
    7)  Delete the file CNS_PB.h in the include folder.
    8)  Delete the file CNS_PB.c in the src folder.
    9)  Build the project, Debug and Flash the RSL10.

    See readme.html for more information.  */
//-----------------------------------------------------------------------------


#include <stdio.h>

#include <BDK.h>
#include <BSP_Components.h>
#include <BLE_Components.h>

#include <ics/CS.h>
#include <ics/CS_Nodes.h>



// ************** Added for ALS constants ******************************
// ****** main.h was changed to add Intervals and Threshold ************
#include "main.h"
// *********************************************************************

uint32_t mystart_time;
uint32_t myCheck;


int main(void)
{
	/* Initialize required components */

	BDK_Initialize();



    // ************** Added for Spare Output ***************************
    LED_Initialize(PIN_GIO_SPARE);
    // *****************************************************************

    // ************** Initialize on-board LEDs *************************
    LED_Initialize(LED_RED);
    LED_Initialize(LED_GREEN);
    LED_Initialize(LED_BLUE);
    // *****************************************************************

    // ********* Test LEDS with 1 1/2 second delays on startup**********
    LED_On(LED_RED);
    HAL_Delay(1500);
    LED_Off(LED_RED);

    LED_On(LED_GREEN);
    HAL_Delay(1500);
    LED_Off(LED_GREEN);

    LED_On(LED_BLUE);
    HAL_Delay(1500);
    LED_Off(LED_BLUE);
    // *****************************************************************

    // ************** Initialize on board Button 0**********************
    BTN_Initialize(BTN0);
    // *****************************************************************

    printf("\r\nPipe_Break_ALS Project\r\n");
    NOA1305_ALS_Initialize();
    NOA1305_ALS_StartContinuous(APP_ALS_PERIODIC_INTERVAL_MS,
            &ALS_ReadCallback);

     printf("Threshold = %d\r\n\n", THRESHOLD_LEVEL_LUX);

    // *****************************************************************
    // *****************************************************************

    /* Initialize CS protocol, start Peripheral Server, Add Custom Service Profile to it. */
    CS_Init();

    /* Optionally set advertising interval to be 200 to 250 ms long. */
    BDK_BLE_SetAdvertisementInterval(320, 400);

    /* Optionally set custom device name.
     * The name needs to contain one of 'IDK', 'BDK', 'RSL10' or 'BLE_Terminal'
     * patterns to be recognized by RSL10 Sense & control mobile application.
     * Default: 'HB_BLE_Terminal'
     */

     BDK_BLE_SetLocalName("HB_RSL10");

    /* Also add battery service if its RTE component is enabled. !!! I disabled this in the RTE!!!! */
    #if defined (RTE_BDK_BLE_PERIPHERAL_SERVER_BASS)
        BLE_BASS_Initialize(1000, 16);
        BLE_BASS_SetVoltageRange(CALC_VBAT_MEASURED(2.4f), CALC_VBAT_MEASURED(3.0f));
        // BLE_BASS_SetBattLevelInd(BattLevelChangeCallback);
    #endif /* RTE_BDK_BLE_BASS_PRESENT */

    // Change default bus speed to 400kHz
    HAL_I2C_SetBusSpeed(HAL_I2C_BUS_SPEED_FAST);

    /* Add all enabled Custom Service Nodes. */
    ASSERT_ALWAYS(CSN_ALS_CheckAvailability() == true);
    ASSERT_ALWAYS(CS_RegisterNode(CSN_ALS_Create()) == CS_OK);

    /* ASSERT_ALWAYS(CSN_ENV_CheckAvailability() == true);
    ASSERT_ALWAYS(CS_RegisterNode(CSN_ENV_Create()) == CS_OK);
    */

	ASSERT_ALWAYS(CSN_AO_CheckAvailability() == true);
    ASSERT_ALWAYS(CS_RegisterNode(CSN_AO_Create()) == CS_OK);


    // ASSERT_ALWAYS(CS_RegisterNode(CSN_PB_Create()) == CS_OK);

    /* Indication - Initialization complete. */
    LED_On(LED_GREEN);
    HAL_Delay(500);
    LED_Off(LED_GREEN);

     printf("Entering main loop.\r\n");

    // Set timer to the time counter when the while loop begins.
    // uint32_t start_time = HAL_Time();



    while (1)
    {
        /* Execute any events that have occurred and refresh Watchdog. */
        BDK_Schedule();

        // Reset mystart_time every hour to prevent stack overflow.
  	    myCheck = HAL_TIME_ELAPSED_SINCE(mystart_time);

        if (myCheck > 45000) // 3600000
        {
        	mystart_time = HAL_Time();
        	printf("Timer reset in overflow test. = %lu\r\n", myCheck);
        }

        /* Enter sleep mode until an interrupt occurs. */
        SYS_WAIT_FOR_INTERRUPT;
    }

    return 0;
}


void ALS_ReadCallback(uint32_t lux, bool is_interrupt)
{
    myCheck = HAL_TIME_ELAPSED_SINCE(mystart_time);
    printf("MyCheck = %lu\r\n\n", myCheck);


	if (lux >= THRESHOLD_LEVEL_LUX)
    {
  	   // Print this line when the lux value is greater than the threshold.
      printf("LUX value passed threshold.\r\n");
      printf("Exceeded LUX = %lu\r\n\n", lux);

       // ********** Read Push Button 0 state. **********************************
       bool button0_pressed = BTN_Read(BTN0);

        // If the button is pressed, set GIO_SPARE high and reset timer.
  		if (button0_pressed == 1)
			{
  			printf("Button 0 Is Pressed.\r\n");
  	        mystart_time = HAL_Time();
  	        printf("Timer reset.\r\n");
  			LED_On(PIN_GIO_SPARE);
  	        printf("GPIO SPARE triggered ON.\r\n");
           }
    }
  	    if (HAL_TIME_ELAPSED_SINCE(mystart_time) > DIO_OUTPUT_INTERVAL_MS)
  	    {
  	    	LED_Off(PIN_GIO_SPARE);
  		    // printf("GPIO SPARE triggered OFF.\r\n");
  	    }
}

Pipe_Break_ALS main.h file

C/C++
Replace the text in the Pipe_BreakASL main.h file. This file sets up the parameters for the main.c file.
This file is located in the Include Folder of the program.
//-----------------------------------------------------------------------------
// Copyright (c) 2018 Semiconductor Components Industries LLC
// (d/b/a "ON Semiconductor").  All rights reserved.
// This software and/or documentation is licensed by ON Semiconductor under
// limited terms and conditions.  The terms and conditions pertaining to the
// software and/or documentation are available at
// http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf ("ON Semiconductor Standard
// Terms and Conditions of Sale, Section 8 Software") and if applicable the
// software license agreement.  Do not use this software and/or documentation
// unless you have carefully read and you agree to the limited terms and
// conditions.  By using this software and/or documentation, you agree to the
// limited terms and conditions.
//-----------------------------------------------------------------------------
#ifndef MAIN_H_
#define MAIN_H_

#define APP_ALS_PERIODIC_INTERVAL_MS    (1000)

#define THRESHOLD_LEVEL_LUX     (1000)

#define APP_ALS_LOOP_INTERVAL_MS        (2000)

#define DIO_OUTPUT_INTERVAL_MS        (15000)

void ALS_ReadCallback(uint32_t lux, bool is_interrupt);

#endif /* MAIN_H_ */

Credits

Jeff Addleman

Jeff Addleman

1 project • 1 follower

Comments