<commit_before># Door Sensor Kit Instructions

The Losant Door Sensor Kit includes everything required to be notified when a door is opened or left open for a certain amount of time. These instructions provide a step-by-step overview to build and configure your door sensor to alert you via SMS or email when your door or window is opened.

![Door Sensor](/images/getting-started/losant-iot-dev-kits/door-sensor/door-sensor.jpg "Door Sensor")

As you follow these instructions, if you run into any issues, please refer to the [Losant Documentation](https://docs.losant.com) and the [Losant Forums](https://forums.losant.com) for help.

Your kit should include the following items:

* 1 NodeMCU development board
* 1 door sensor
* 3 jump wires
* 1 resistor
* 1 screw terminal block
* 1 solderless breadboard
* 1 USB cable

## Environment Setup

In order to properly program the kit's microcontroller, please follow the [environment setup instructions](/getting-started/losant-iot-dev-kits/environment-setup/). These steps only need to be completed once, so if you've already done this for a previous kit, you can skip this step.

## Losant Setup

In this section, you’ll register for a Losant account, create your application, and add the device for your door sensor kit.

### Create Account

If you don't already have an account, navigate to [https://accounts.losant.com/create-account](https://accounts.losant.com/create-account) to register.

### Create Application

Create an application for the door sensor kit. You can name it whatever you want.

![Create Application](/images/getting-started/losant-iot-dev-kits/builder-kit/create-application.png "Create Application")

![Application Settings](/images/getting-started/losant-iot-dev-kits/door-sensor/application-name.png "Application Settings")

### Add Device

The next step is to register the door sensor kit device with the platform. Click the `Add Device` button on the top right or select `Add Device` from the Devices dropdown.

![Add Device Menu](/images/getting-started/losant-iot-dev-kits/door-sensor/add-device-menu.png "Add Device Menu")

Next, choose the `Create from Scratch` option.

![Create From Scratch](/images/getting-started/losant-iot-dev-kits/door-sensor/add-from-scratch.png "Create From Scratch")

![Device Settings](/images/getting-started/losant-iot-dev-kits/door-sensor/device-settings.png "Device Settings")

1. Name the device anything you want.
1. "Description" can be left blank, or fill it with whatever you want.
1. Leave "Virtual Device" unchecked.
1. No device tags are required.
1. Add a device attribute with the name "open" and the type `Number`.

The device attributes specify what state information the device reports. The firmware that you’ll flash in the following sections will report `{ "open" : 1 }` when the door opens and `{ "open" : 0 }` when the door closes. Device attributes are ***case sensitive***, so make sure the name is ***all lowercase***.

Click the `Create Device` button. The screen will change and show the device’s ID. This ID is required for the following sections. You might want to copy/paste it somewhere convenient.

![Device ID](/images/getting-started/losant-iot-dev-kits/door-sensor/device-id.png "Device ID")

### Generate Security Tokens

Now we need to generate some security tokens so your device can authenticate against the Losant platform. Select `More > Access Keys` from the application menu.

![Access Keys Menu](/images/getting-started/losant-iot-dev-kits/door-sensor/access-keys-menu.png "Access Keys Menu")

Generate an access key that provides access to all devices in your application.

![Token Restrictions](/images/getting-started/losant-iot-dev-kits/builder-kit/token-restrictions.png "Token Restrictions")

This will cause a popup to appear with your access tokens. Losant ***DOES NOT*** store the secret, so you’ll have to copy it somewhere secure. Both of these values will be needed in the following sections, so make sure you copy/paste them somewhere convenient.

![Access Token Popup](/images/getting-started/losant-iot-dev-kits/builder-kit/access-token-popup.png "Access Token Popup")

## Get Door Sensor Firmware

Now let's get the firmware you'll be flashing to the device. Download and extract the following zip file to your computer. We'll be editing and opening these files using the Arduino IDE, so put it somewhere convenient.

[https://github.com/Losant/losant-kit-door-sensor/archive/master.zip](https://github.com/Losant/losant-kit-door-sensor/archive/master.zip)

If you're familiar with git, you can also clone the repository from here:

[https://github.com/Losant/losant-kit-door-sensor](https://github.com/Losant/losant-kit-door-sensor)

## Wiring

***Disconnect the microcontroller from USB before wiring.***

In this step we're going to to connect the door sensor to the microcontroller. Below is the wiring diagram.

![Wiring Diagram](/images/getting-started/losant-iot-dev-kits/door-sensor/wiring-diagram.png "Wiring Diagram")

1. Push the microcontroller into terminals 1-15 on either side of the center line, which are rows `b` and `i`. The USB port should be facing away from the breadboard.
1. Connect the GND pin on the microcontroller `a2` to the negative rail with a jump wire. The color of the jump wire doesn't matter.
1. Using a small screwdriver, attach the two leads of the door sensor to the screw terminal block. Which lead attaches to which terminal doesn't matter.
1. Insert the screw terminal block into the breadboard using terminals `e26` and `e28`.
1. Connect terminal `a26` to the negative rail using the resistor.
1. Connect pin D1 on the microcontroller `a14` to terminal `b26`.
1. Connect the 3V3 pin on the microcontroller `a10` to terminal `b28`.

![Wiring Image](/images/getting-started/losant-iot-dev-kits/door-sensor/wiring-image.jpg "Wiring Image")

## Flash the Firmware

Connect the microcontroller to your computer with the supplied USB cable. Make sure the device shows up under the Arduino IDE's `Tools -> Port` menu and it's selected. If the device does not show up, please review the [Environment Setup](/getting-started/losant-iot-dev-kits/door-sensor-kit/#environment-setup) guide.

With the Arduino IDE, use the `File -> Open` menu to navigate to where you downloaded and extracted the kit source code and open the file `losant-kit-door-sensor/losant-kit-door-sensor.ino`. Edit the following variables at the top of the file.

1. WIFI_SSID = the name of your WiFi network
1. WIFI_PASS = <PASSWORD>
1. LOSANT_DEVICE_ID = set this to the ID you received after adding the device to the Losant application
1. LOSANT_ACCESS_KEY = set this to the access key you generated after creating the Losant application
1. LOSANT_ACCESS_SECRET = set this to the access secret you generated after creating the Losant application

![Update Variables](/images/getting-started/losant-iot-dev-kits/door-sensor/update-variables.png "Update Variables")

Your code will automatically be saved as part of the upload process when it's flashed to the device. After editing the code, open the Arduino IDE's Serial Monitor and change the baud rate to 115200. The serial monitor can be opened using the button on the top right of the Arduino IDE or using the `Tools -> Serial Monitor` menu.

![Open Serial Monitor](/images/getting-started/losant-iot-dev-kits/door-sensor/serial-monitor-button.png "Open Serial Monitor")

![Serial Monitor Baud](/images/getting-started/losant-iot-dev-kits/builder-kit/serial-monitor-baud.png "Serial Monitor Baud")

The serial monitor won't print any information until we flash the device with the source code in the next step.

Upload the firmware to the device by clicking the `Upload` button or the `Sketch -> Upload` menu. This will take a few seconds and the microcontroller will rapidly blink a blue light while it’s uploading.

![Upload Button](/images/getting-started/losant-iot-dev-kits/door-sensor/upload-button.png "Upload Button")

After the upload is complete, the Serial Monitor should start printing information about it connecting to WiFi and Losant. It may also print some garbage, but that's just part of the board booting up. It will then print a "Door opened" and "Door closed" message whenever the door sensor parts get close together or far apart. Go ahead and give it a try. Make sure the two parts of the sensor are oriented the same way (the screw holes are facing the same direction) and then touch them together and move them apart. You should see corresponding messages on the Serial Monitor output.

![Open Close Video](/images/getting-started/losant-iot-dev-kits/door-sensor/open-close-video.gif "Open Close Video")

![Serial Output](/images/getting-started/losant-iot-dev-kits/door-sensor/serial-output.png "Serial Output")

Every time the door sensor parts come together, this firmware publishes `{ "open": 1 }` to Losant. Every time the sensor parts move away, this firmware publishes `{ "open" : 0 }`.

It’s now time to go into Losant and make use of this data.

## Setup Alerts

Now that real door sensor data is flowing into Losant, we can set up our alerts to be notified by SMS and email whenever a door opens or is left open for a certain amount of time. For this, we're going to use [Losant Workflows](/workflows/overview/).

First, create a new workflow and name it whatever you want.
<commit_msg>Fixed a typo in how door sensor publishes state.<commit_after># Door Sensor Kit Instructions

The Losant Door Sensor Kit includes everything required to be notified when a door is opened or left open for a certain amount of time. These instructions provide a step-by-step overview to build and configure your door sensor to alert you via SMS or email when your door or window is opened.

![Door Sensor](/images/getting-started/losant-iot-dev-kits/door-sensor/door-sensor.jpg "Door Sensor")

As you follow these instructions, if you run into any issues, please refer to the [Losant Documentation](https://docs.losant.com) and the [Losant Forums](https://forums.losant.com) for help.

Your kit should include the following items:

* 1 NodeMCU development board
* 1 door sensor
* 3 jump wires
* 1 resistor
* 1 screw terminal block
* 1 solderless breadboard
* 1 USB cable

## Environment Setup

In order to properly program the kit's microcontroller, please follow the [environment setup instructions](/getting-started/losant-iot-dev-kits/environment-setup/). These steps only need to be completed once, so if you've already done this for a previous kit, you can skip this step.

## Losant Setup

In this section, you’ll register for a Losant account, create your application, and add the device for your door sensor kit.

### Create Account

If you don't already have an account, navigate to [https://accounts.losant.com/create-account](https://accounts.losant.com/create-account) to register.

### Create Application

Create an application for the door sensor kit. You can name it whatever you want.

![Create Application](/images/getting-started/losant-iot-dev-kits/builder-kit/create-application.png "Create Application")

![Application Settings](/images/getting-started/losant-iot-dev-kits/door-sensor/application-name.png "Application Settings")

### Add Device

The next step is to register the door sensor kit device with the platform. Click the `Add Device` button on the top right or select `Add Device` from the Devices dropdown.

![Add Device Menu](/images/getting-started/losant-iot-dev-kits/door-sensor/add-device-menu.png "Add Device Menu")

Next, choose the `Create from Scratch` option.

![Create From Scratch](/images/getting-started/losant-iot-dev-kits/door-sensor/add-from-scratch.png "Create From Scratch")

![Device Settings](/images/getting-started/losant-iot-dev-kits/door-sensor/device-settings.png "Device Settings")

1. Name the device anything you want.
1. "Description" can be left blank, or fill it with whatever you want.
1. Leave "Virtual Device" unchecked.
1. No device tags are required.
1. Add a device attribute with the name "open" and the type `Number`.

The device attributes specify what state information the device reports. The firmware that you’ll flash in the following sections will report `{ "open" : 1 }` when the door opens and `{ "open" : 0 }` when the door closes. Device attributes are ***case sensitive***, so make sure the name is ***all lowercase***.

Click the `Create Device` button. The screen will change and show the device’s ID. This ID is required for the following sections. You might want to copy/paste it somewhere convenient.

![Device ID](/images/getting-started/losant-iot-dev-kits/door-sensor/device-id.png "Device ID")

### Generate Security Tokens

Now we need to generate some security tokens so your device can authenticate against the Losant platform. Select `More > Access Keys` from the application menu.

![Access Keys Menu](/images/getting-started/losant-iot-dev-kits/door-sensor/access-keys-menu.png "Access Keys Menu")

Generate an access key that provides access to all devices in your application.

![Token Restrictions](/images/getting-started/losant-iot-dev-kits/builder-kit/token-restrictions.png "Token Restrictions")

This will cause a popup to appear with your access tokens. Losant ***DOES NOT*** store the secret, so you’ll have to copy it somewhere secure. Both of these values will be needed in the following sections, so make sure you copy/paste them somewhere convenient.

![Access Token Popup](/images/getting-started/losant-iot-dev-kits/builder-kit/access-token-popup.png "Access Token Popup")

## Get Door Sensor Firmware

Now let's get the firmware you'll be flashing to the device. Download and extract the following zip file to your computer. We'll be editing and opening these files using the Arduino IDE, so put it somewhere convenient.

[https://github.com/Losant/losant-kit-door-sensor/archive/master.zip](https://github.com/Losant/losant-kit-door-sensor/archive/master.zip)

If you're familiar with git, you can also clone the repository from here:

[https://github.com/Losant/losant-kit-door-sensor](https://github.com/Losant/losant-kit-door-sensor)

## Wiring

***Disconnect the microcontroller from USB before wiring.***

In this step we're going to to connect the door sensor to the microcontroller. Below is the wiring diagram.

![Wiring Diagram](/images/getting-started/losant-iot-dev-kits/door-sensor/wiring-diagram.png "Wiring Diagram")

1. Push the microcontroller into terminals 1-15 on either side of the center line, which are rows `b` and `i`. The USB port should be facing away from the breadboard.
1. Connect the GND pin on the microcontroller `a2` to the negative rail with a jump wire. The color of the jump wire doesn't matter.
1. Using a small screwdriver, attach the two leads of the door sensor to the screw terminal block. Which lead attaches to which terminal doesn't matter.
1. Insert the screw terminal block into the breadboard using terminals `e26` and `e28`.
1. Connect terminal `a26` to the negative rail using the resistor.
1. Connect pin D1 on the microcontroller `a14` to terminal `b26`.
1. Connect the 3V3 pin on the microcontroller `a10` to terminal `b28`.

![Wiring Image](/images/getting-started/losant-iot-dev-kits/door-sensor/wiring-image.jpg "Wiring Image")

## Flash the Firmware

Connect the microcontroller to your computer with the supplied USB cable. Make sure the device shows up under the Arduino IDE's `Tools -> Port` menu and it's selected. If the device does not show up, please review the [Environment Setup](/getting-started/losant-iot-dev-kits/door-sensor-kit/#environment-setup) guide.

With the Arduino IDE, use the `File -> Open` menu to navigate to where you downloaded and extracted the kit source code and open the file `losant-kit-door-sensor/losant-kit-door-sensor.ino`. Edit the following variables at the top of the file.

1. WIFI_SSID = the name of your WiFi network
1. WIFI_PASS = <PASSWORD>
1. LOSANT_DEVICE_ID = set this to the ID you received after adding the device to the Losant application
1. LOSANT_ACCESS_KEY = set this to the access key you generated after creating the Losant application
1. LOSANT_ACCESS_SECRET = set this to the access secret you generated after creating the Losant application

![Update Variables](/images/getting-started/losant-iot-dev-kits/door-sensor/update-variables.png "Update Variables")

Your code will automatically be saved as part of the upload process when it's flashed to the device. After editing the code, open the Arduino IDE's Serial Monitor and change the baud rate to 115200. The serial monitor can be opened using the button on the top right of the Arduino IDE or using the `Tools -> Serial Monitor` menu.

![Open Serial Monitor](/images/getting-started/losant-iot-dev-kits/door-sensor/serial-monitor-button.png "Open Serial Monitor")

![Serial Monitor Baud](/images/getting-started/losant-iot-dev-kits/builder-kit/serial-monitor-baud.png "Serial Monitor Baud")

The serial monitor won't print any information until we flash the device with the source code in the next step.

Upload the firmware to the device by clicking the `Upload` button or the `Sketch -> Upload` menu. This will take a few seconds and the microcontroller will rapidly blink a blue light while it’s uploading.

![Upload Button](/images/getting-started/losant-iot-dev-kits/door-sensor/upload-button.png "Upload Button")

After the upload is complete, the Serial Monitor should start printing information about it connecting to WiFi and Losant. It may also print some garbage, but that's just part of the board booting up. It will then print a "Door opened" and "Door closed" message whenever the door sensor parts get close together or far apart. Go ahead and give it a try. Make sure the two parts of the sensor are oriented the same way (the screw holes are facing the same direction) and then touch them together and move them apart. You should see corresponding messages on the Serial Monitor output.

![Open Close Video](/images/getting-started/losant-iot-dev-kits/door-sensor/open-close-video.gif "Open Close Video")

![Serial Output](/images/getting-started/losant-iot-dev-kits/door-sensor/serial-output.png "Serial Output")

Every time the door sensor parts come together, this firmware publishes `{ "open": 0 }` to Losant. Every time the sensor parts move away, this firmware publishes `{ "open" : 1 }`.

It’s now time to go into Losant and make use of this data.

## Setup Alerts

Now that real door sensor data is flowing into Losant, we can set up our alerts to be notified by SMS and email whenever a door opens or is left open for a certain amount of time. For this, we're going to use [Losant Workflows](/workflows/overview/).

First, create a new workflow and name it whatever you want.

{'fraction_non_alphanumeric': 0.08352457704008112, 'fraction_numerical': 0.00587073704435075, 'mean_word_length': 4.482153306026916, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5323259', 'n_tokens_mistral': 5320, 'n_tokens_neox': 5042, 'n_words': 2402}