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 fire with a sensor


If there's a fire in our home, it's vital to detect it; so in the next section, we will create a project that detects fire with a sensor.

In the following image, we see of the fire sensor module:

You can now either copy the code inside a file called Sensor_fire.ino, or just get the complete code from the folder for this project.

We define the variables for our program at the beginning:

int ledPin = 13;             
int inputPin= 2; 
int val = 0;                    

We define the output signals and the serial communication:

void setup() { 
pinMode(ledPin, OUTPUT);       
pinMode(inputPin, INPUT);      
Serial.begin(9600); 
} 

Now we display the value of the digital signal:

void loop(){ 
val = digitalRead(inputPin); 
Serial.print("val : ");   
Serial.println(val); 
digitalWrite(ledPin, HIGH);  // turn LED ON 

Then we compare: If the value detects a high logic state, it turns off the output; if it reads...