Night RShivam Kumar Tiwari
Published © MIT

An Electrician's Wingman

Augmenting electrical safety through Ada

BeginnerFull instructions providedOver 2 days272

Things used in this project

Hardware components

BBC micro:bit board
BBC micro:bit board
×1
Solar Cockroach Vibrating Disc Motor
Brown Dog Gadgets Solar Cockroach Vibrating Disc Motor
×1
LED (generic)
LED (generic)
×1
Non Invasive Line Test Circuit
×1

Software apps and online services

GNAT Community
AdaCore GNAT Community

Hand tools and fabrication machines

Soldering Station, Hobbyist
Soldering Station, Hobbyist

Story

Read more

Schematics

Connection Details for eWingman

Code

BLEADV.ads

ADA
------------------------------------------------------------------------------
--                                                                          --
--                        Copyright (C) 2016, AdaCore                       --
--                                                                          --
--  Redistribution and use in source and binary forms, with or without      --
--  modification, are permitted provided that the following conditions are  --
--  met:                                                                    --
--     1. Redistributions of source code must retain the above copyright    --
--        notice, this list of conditions and the following disclaimer.     --
--     2. Redistributions in binary form must reproduce the above copyright --
--        notice, this list of conditions and the following disclaimer in   --
--        the documentation and/or other materials provided with the        --
--        distribution.                                                     --
--     3. Neither the name of the copyright holder nor the names of its     --
--        contributors may be used to endorse or promote products derived   --
--        from this software without specific prior written permission.     --
--                                                                          --
--   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    --
--   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      --
--   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  --
--   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT   --
--   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
--   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT       --
--   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  --
--   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  --
--   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT    --
--   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  --
--   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   --
--                                                                          --
------------------------------------------------------------------------------

package BLEADV is
   procedure Initialize_Radio;
   procedure Send_Beacon_Packet;
end BLEADV;

main.adb

ADA
------------------------------------------------------------------------------
--                                                                          --
--                       Copyright (C) 2016, AdaCore                        --
--                                                                          --
--  Redistribution and use in source and binary forms, with or without      --
--  modification, are permitted provided that the following conditions are  --
--  met:                                                                    --
--     1. Redistributions of source code must retain the above copyright    --
--        notice, this list of conditions and the following disclaimer.     --
--     2. Redistributions in binary form must reproduce the above copyright --
--        notice, this list of conditions and the following disclaimer in   --
--        the documentation and/or other materials provided with the        --
--        distribution.                                                     --
--     3. Neither the name of the copyright holder nor the names of its     --
--        contributors may be used to endorse or promote products derived   --
--        from this software without specific prior written permission.     --
--                                                                          --
--   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    --
--   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      --
--   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  --
--   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT   --
--   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
--   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT       --
--   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  --
--   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  --
--   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT    --
--   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  --
--   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   --
--                                                                          --
------------------------------------------------------------------------------

with MicroBit.Display;
with MicroBit.IOs;
with BLEADV;

procedure Main is
begin

   BLEADV.Initialize_Radio;

   --  Loop forever
   loop

      MicroBit.Display.Clear;

      --  Check for Analog pin 2 on Microbit
      if MicroBit.IOs.Set (2) then      -- Connection to ET circuit(p.d. after 2.7MOhms)

      --  Turns off the peripherals connected to following pins
         MicroBit.IOs.Set (8, False);   -- Connection to Green LED
         MicroBit.IOs.Set (16, False);  -- Connection to Vibration Motor
         MicroBit.Display.Display (".....");

      else
         --  Turns on the peripherals connected to following pins
         MicroBit.IOs.Set (8, True);   -- Connection to Green LED
         MicroBit.IOs.Set (16, True);  -- Connection to Vibration Motor
         MicroBit.Display.Display ("ALERT");
         BLEADV.Send_Beacon_Packet;

      end if;

   end loop;
end Main;

BLEADV.adb

ADA
------------------------------------------------------------------------------
--                                                                          --
--                        Copyright (C) 2016, AdaCore                       --
--                                                                          --
--  Redistribution and use in source and binary forms, with or without      --
--  modification, are permitted provided that the following conditions are  --
--  met:                                                                    --
--     1. Redistributions of source code must retain the above copyright    --
--        notice, this list of conditions and the following disclaimer.     --
--     2. Redistributions in binary form must reproduce the above copyright --
--        notice, this list of conditions and the following disclaimer in   --
--        the documentation and/or other materials provided with the        --
--        distribution.                                                     --
--     3. Neither the name of the copyright holder nor the names of its     --
--        contributors may be used to endorse or promote products derived   --
--        from this software without specific prior written permission.     --
--                                                                          --
--   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    --
--   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      --
--   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  --
--   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT   --
--   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
--   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT       --
--   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  --
--   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  --
--   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT    --
--   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  --
--   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   --
--                                                                          --
------------------------------------------------------------------------------

with nRF51.Tasks;
with nRF51.Events;
with nRF51.Radio;     use nRF51.Radio;
with nRF51.Clock;     use nRF51.Clock;

with Bluetooth_Low_Energy.Packets; use Bluetooth_Low_Energy.Packets;
with Bluetooth_Low_Energy;         use Bluetooth_Low_Energy;
with Bluetooth_Low_Energy.Beacon;  use Bluetooth_Low_Energy.Beacon;

with HAL; use HAL;

package body BLEADV is

   Current_Adv_Channel : BLE_Advertising_Channel_Number := 37;
   Beacon_Packet       : BLE_Packet;

   ----------------------
   -- Initialize_Radio --
   ----------------------

   procedure Initialize_Radio is
      Beacon_UUID : constant BLE_UUID :=
        Make_UUID ((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
   begin

      Beacon_Packet := Make_Beacon_Packet
        (MAC   => (16#AD#, 16#AC#, 16#04#, 16#E0#, 16#00#, 16#00#),
         UUID  => Beacon_UUID,
         Major => 5,
         Minor => 25,
         Power => 60);

      --  Setup high frequency clock for BLE transmission
      Set_High_Freq_Source (HFCLK_RC);
      Start_High_Freq;
      while not High_Freq_Running loop
         null;
      end loop;

      --  Setup radio module for BLE
      Setup_For_Bluetooth_Low_Energy;

      --  Set BLE advertising address
      Set_Logic_Addresses (Base0 => 16#89_BE_D6_00#,
                           Base1 => 16#00_00_00_00#,
                           Base_Length_In_Byte => 3,
                           AP0   => 16#8E#,
                           AP1   => 16#00#,
                           AP2   => 16#00#,
                           AP3   => 16#00#,
                           AP4   => 16#00#,
                           AP5   => 16#00#,
                           AP6   => 16#00#,
                           AP7   => 16#00#);

      --  Select logic address
      Set_TX_Address (0);

      --  Transmission power
      Set_Power (Zero_Dbm);

      --  Enable shortcuts for easier radio operation
      Enable_Shortcut (Ready_To_Start);
      Enable_Shortcut (End_To_Disable);
   end Initialize_Radio;

   ------------------------
   -- Send_Beacon_Packet --
   ------------------------

   procedure Send_Beacon_Packet is
   begin
      Configure_Whitening (True, UInt6 (Current_Adv_Channel));

      Set_Frequency
        (Radio_Frequency_MHz (Channel_Frequency (Current_Adv_Channel)));

      if Current_Adv_Channel /= BLE_Advertising_Channel_Number'Last then
         Current_Adv_Channel := Current_Adv_Channel + 1;
      else
         Current_Adv_Channel := BLE_Advertising_Channel_Number'First;
      end if;

      --  Set TX packet address
      Set_Packet (Memory_Address (Beacon_Packet));

      --  Clear all events
      nRF51.Events.Clear (nRF51.Events.Radio_DISABLED);
      nRF51.Events.Clear (nRF51.Events.Radio_ADDRESS);
      nRF51.Events.Clear (nRF51.Events.Radio_PAYLOAD);
      nRF51.Events.Clear (nRF51.Events.Radio_END);

      --  Start transmission
      nRF51.Tasks.Trigger (nRF51.Tasks.Radio_TXEN);

      --  Wait for end of transmission
      while not nRF51.Events.Triggered (nRF51.Events.Radio_DISABLED) loop
         null;
      end loop;
   end Send_Beacon_Packet;

end BLEADV;

Credits

Night R

Night R

12 projects • 23 followers
R&D Engineer @ IoT Solutions Provider, Robotics Engineer @ SIS Corp., Passionate for Hardware hacking, 12+ years experience in programming..
Shivam Kumar Tiwari

Shivam Kumar Tiwari

0 projects • 1 follower
Design Engineer (PCB and COMPONENT focus) Working as PCB and Circuit Design Engineer at Acme Digitek Solutions Pvt LTD from 5 years
Thanks to Fabien Chouteau.

Comments