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

Calculating flow and volume of water:


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

In this part, we calculate the flow and volume from the sensor:

int pin = 2; 
volatile unsigned int pulse; 
float volume = 0; 
floatflow_rate =0; 
constintpulses_per_litre = 450; 

We set up the interrupt:

void setup() 
{ 
Serial.begin(9600); 
pinMode(pin, INPUT); 
attachInterrupt(0, count_pulse, RISING); 
} 

Start the interrupt:

void loop() 
{ 
pulse=0; 
interrupts(); 
delay(1000); 
noInterrupts(); 

Then we display the flow rate of the sensor:

Serial.print("Pulses per second: "); 
Serial.println(pulse); 
 
flow_rate = pulse * 1000/pulses_per_litre; 

We calculate the volume of the sensor:

Serial.print("Water flow rate: "); 
Serial.print(flow_rate); 
Serial.println(" milliliters per second"); 
...