Internet of everything
Internet of everything

This free course is available to start right now. Review the full course description and key learning outcomes and create an account and enrol if you want a free statement of participation.

Free course

Internet of everything

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 _unit3.3.2 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.

Box _unit3.3.1

if (temp < 77) {

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 &lt; 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.


Take your learning further

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses.

If you are new to university level study, find out more about the types of qualifications we offer, including our entry level Access courses and Certificates.

Not ready for University study then browse over 900 free courses on OpenLearn and sign up to our newsletter to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus