Skip to Content
Technical Articles
Author's profile photo Nabheet Madan

Moisture Sensor using SAPUI5, Raspberry PI as MQTT and what not….

Motivation – What we are Planning to do?

I have been greatly fascinated by the recent advancement in world of IoT  and more by how cheaply and easily it solves the problem. One of the problem which I always have at home is a delay in moisturizing the plants? So in this first blog post we will integrate the hardware with the software basically get the soil moisture content into our system

image courtesy

What do we need and how they will interact

We can monitor soil moisture of the soil via N number of ways, so this is not the only way. So what do we need

  1. A Moisture Sensor
  2. Some way of sending the sensor data may be an ESP8266 board which can send data over wifi
  3. Where to receive so I could have directly sent it to the SAP Cloud but I wanted to use Raspberry PI. So Raspberry PI act as MQTT message broker.
  4. A SAPUI5 application running on websockets to display the data

Lets do it!

We will do it in steps.

Get our Raspberry PI MQTT up and SAPUI5 set up done

We want Raspberry PI to be able to receive the sensor moisture data so to act as message broker.  For RPI we can use Mosquitto as a message broker, followed this article to install on our RPI. Now we have the broker installed next step was run it in order to listen to messages posted for a particular topic in our case Moisture

mosquitto_sub -d -t Moisture

Now we are listening to any messages posted on topic Moisture, next step was we wanted an app to display them at a basic level. What is better than our SAPUI5 app with local tooling running on RPI. I could have used NodeRed also but thought of using our SAPUI5.  Copied the sample app and modified it. I wanted our app to get updated in real time so thought of using Web sockets. Good thing Mosquitto also support websockets apart from MQTT. So added the websocket code to our app as shown below.

Added bootstrap for Mosquitto

<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js" type="text/javascript"></script>

Created the connection and added callbacks, used the bind method from previous experience for context

		onInit: function() {
			this.aSearchFilters = [];
			this.aTabFilters = [];
			this.client = new Paho.MQTT.Client("192.168.29.3", 9001, "nabheet");
			this.client.onConnectionLost = this.onConnectionLost;
			this.client.onMessageArrived = this.onMessageArrived.bind(this);
			this.client.connect({
				useSSL: false,
				onSuccess: this.onConnect.bind(this)
			});

 

What to do when message arrives, update the model simple.

		 onMessageArrived:function(message) {
			try {
				console.log("Recieved Message from server");
				var value = message.payloadString;
				var datastream = message.destinationName;
				console.log("datastream: " + datastream + ", value: " + value);
				var oModel = this.getView().getModel();
				var aTodos = oModel.getProperty("/todos").map(function (oTodo) { return Object.assign({}, oTodo); });
	             // Use of Date.now() function 
  				var d = Date(Date.now()); 
  
  				// Converting the number of millisecond in date string 
  				var a = d.toString() 
				aTodos.push({
					title: value,
					completed: false,
					time:a
				});
	
				oModel.setProperty("/todos", aTodos);
				oModel.setProperty("/newTodo", "");
			} catch (e) {
				console.log("exception in onMessageArrived: " + e);
				return false;
			}
		},
		onConnect: function () {
			console.log("Connected to server");   
		  this.client.subscribe("Moisture");
		},

In order to test I just pushed a message to topic manually in RPI and bingo our app shows it instantly.

mosquitto_pub -d -t Mositure -m "Hello World!"

 

Connecting our ESP8266 to Moisture Sensor

This is the most easiest part all you need is few wires. This is how my set up look like

Deploying the code to ESP8266 to read moisture sensor and post the data to MQTT server

Now we have everything set all that is remaining is some way to read sensor data and publish to the RPI MQTT broker.  All you need to do install  Arduino IDE installed which will be used to deploy the code to the ESP8266. In my sample code I am first connecting to my wifi

  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

Once connected we are creating a PubSub client

// Make sure to update this for your own MQTT Broker!
const char* mqtt_server = "192.168.29.3";
const char* mqtt_topic = "Moisture";
const char* clientID = "M1";
PubSubClient client(mqtt_server, 1883, wifiClient); // 1883 is the listener port for the Broker

 

Once we are connected to WIFI and registered as MQTT client all that is left is to read the moisture percentage and send the message

 float moisture_percentage;
  moisture_percentage = ( 100.00 - ( (analogRead(sensor_pin)/1023.00) * 100.00 ) );
    String msg = String(moisture_percentage);
   if (client.publish(mqtt_topic, msg.c_str())) {
      Serial.println("Message Sent!");
      Serial.println(msg);
      Serial.println(msg.c_str());
    }

All set lets see the demo now?

Live Demo

What is next?

So we have now got the moisture data intent now is to integrate with SAP cloud , Raise Alarms via emails/whatsapp for decreased moisture content, enhanced SAPUI5 dashboard and automatic or manual watering of plants via the SAPUI5 app. Sky is the limit I will say. Feel free to provide your feedback and ideas.

 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.