Skip to content
Skip to main content

About this free course

Download this course

Share this free course

Internet of everything
Internet of everything

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

3.3.5 JavaScript programming example

To give you a better understanding of computer programs, it is useful to analyse some JavaScript code.

JavaScript is a scripting language used primarily in web applications. For example, consider a fictional web application called Cisco Coffee. This application is designed to act as a monitoring tool, or dashboard, for a coffee farm. Figure 18 shows a coffee farm.

Described image
Figure 20

In this scenario, many sensors are installed in various locations in the coffee field, close to the coffee bean plants. These sensors report data back to a central station. This station uses the Cisco Coffee web interface to allow users to monitor the field.

Three types of sensors are installed: temperature, sunlight, and soil moisture. If the temperature drops below 77°F, a warning is presented on the interface screen. If the coffee plants are exposed to too much sunlight, a different warning is presented. If the soil becomes too wet or too dry, a different warning is shown.

The following JavaScript snippets are used to implement these tests.

if (temp

document.getElementById("logArea").innerHTML =

"WARNING: Field temperature dropped below 77F.";
}

If the temperature drops below 77 degrees, then trigger an alert.

if (sun > 17000) {

document.getElementById("logArea").innerHTML =

"WARNING: There's too much sunlight on the coffee plants.";

}

If the sunlight is greater than 17000 lux, then trigger an alert.

if (if ((moist < 5) || (moist > 20)) {

document.getElementById("logArea").innerHTML =

"WARNING: Field moisture level is out of the optimal range."

}

If the moisture is less than 5 or greater than 20, then trigger an alert.

Warnings provide an opportunity for feedback loops. For example, if the soil moisture level is low, it might be necessary to activate the irrigation system and alert the farmer who may be aware of other circumstances and can make an appropriate decision. The farmer might decide to intervene and turn off the irrigation system because rain is in the forecast. Regardless of how the irrigation occurs, the sensor reporting soil moisture should begin showing more desirable levels, completing the feedback loop.