Book Image

Internet of Things Programming with JavaScript

Book Image

Internet of Things Programming with JavaScript

Overview of this book

The Internet of Things is taking the tech world by storm, and JavaScript is at its helm. This book will get you to grips with this exciting new technology. Where do Node.js, HTML5 and Windows 10 IoT Core come in with JavaScript and IoT? Why Raspberry Pi Zero rather than Arduino? How do you configure and build an IoT network from scratch? All your IoT JavaScript questions are answered in this book.
Table of Contents (15 chapters)
Internet of Things Programming with JavaScript
Credits
About the Author
www.packtpub.com
Customer Feedback
Preface

Detecting if the door is open with a reed switch


An example has been added as an option to implement a magnetic sensor in order to detect when a door or window is open or closed.

The sensor outputs a 0 when it detects the magnetic field and when the field is far away the output would be a 1; so you can determine when the door is open or closed.

The program in the Arduino is performed as follows:

We define the input signal of the sensor, and configure the serial communication:

void setup() { 
  pinMode(sensor, INPUT_PULLUP); 
  Serial.begin(9600); 
} 

We read the state of the sensor:

void loop() { 
state = digitalRead(sensor); 

It compares the digital input and displays the status of the door in the serial interface:

  if (state == LOW){ 
    Serial.println("Door Close"); 
  } 
  if (state == HIGH){ 
    Serial.println("Door Open"); 
  } 
}