Introduction: Phidgets WhatsApp - Ask the Status of Your Sensors

Picture of Phidgets WhatsApp - Ask the Status of Your Sensors

In the previous tutorial, we have prepared the PhidgetSBC to send a WhatsApp message when the state of a sensor changes.
In this tutorial we ask the status of the sensors via a WhatsApp message, for example, we may ask for the value of temperature and humidity of the apartment.

We need:

1073_0 - PhidgetSBC3

1125_0 - Humidity/Temperature Sensor

The 1125_0 board combines an ambient temperature sensor and a humidity sensor and connects to two analog inputs, I connect Temperature to A0 and Humidity to A1.

Install Yowsup and configure PhidgetSBC with your WhatsApp account.

Step 1: How Does This Work

The principle of operation is as follows, I send a message via WhatsApp to the number configured on the PhidgetSBC, the PhidgetSBC reads temperature and humidity, and sends me a message with their value.

Yowsup is a library, but it contains some sample files that allow us to test its functioning. These files are located in the directory /yowsup/src/Examples.
To intercept a new message we need to edit the file /yowsup/src/Examples/ListenerClient.py adding the line

os.system("python /yowsup/src/ifkit.py")

onMessageReceived

def onMessageReceived(self, messageId, jid, messageContent, timestamp, wantsReceipt, pushName, isBroadCast):
          formattedDate = datetime.datetime.fromtimestamp(timestamp).strftime('%d-%m-%Y %H:%M')
          print("%s [%s]:%s"%(jid, formattedDate, messageContent))
          if wantsReceipt and self.sendReceipts:
              self.methodsInterface.call("message_ack", (jid, messageId))
              os.system("python /yowsup/src/ifkit.py")

This line calls file ifkit.py that reads the status of the sensors.

Step 2: Prepare the System to Work With Python

The file ifkit.py is a modified version of the file InterfaceKit-simple.py included in the examples released by Phidgets, then download the library and examples from Phidgets.

Download the Python libraries:

wget http://www.phidgets.com/downloads/libraries/PhidgetsPython.zip

Because the libraries come as a zipped archive, we’ll likely need to install zip support

apt-get install zip unzip
unzip PhidgetsPython.zip
cd PhidgetsPython/

Now we can install the Python libraries:

python setup.py install

And download the examples:

wget http://www.phidgets.com/downloads/examples/Python.zip
unzip Python.zip
cd Python/
cp InterfaceKit-simple.py /yowsup/src/ifkit.py

Edit the file that will have this content

#!/usr/bin/env python
"""Copyright 2010 Phidgets Inc.
  This work is licensed under the Creative Commons Attribution 2.5 Canada License.
  To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ca/
"""
  __author__ = 'Adam Stelmack'
  __version__ = '2.1.8'
  __date__ = 'May 17 2010'
  #Basic imports
  from ctypes import *
  import sys
  import random
  import os
  #Phidget specific imports
  from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException
  from Phidgets.Events.Events import AttachEventArgs, DetachEventArgs, ErrorEventArgs, InputChangeEventArgs, OutputChangeEventArgs, SensorChangeEventArgs
  from Phidgets.Devices.InterfaceKit import InterfaceKit
  #Create an interfacekit object
  try:
      interfaceKit = InterfaceKit()
  except RuntimeError as e:
      print("Runtime Exception: %s" % e.details)
      print("Exiting....")
      exit(1)
  #Information Display Function
  def displayDeviceInfo():
      print("|------------|----------------------------------|--------------|------------|")
      print("|- Attached -|-              Type              -|- Serial No. -|-  Version -|")
      print("|------------|----------------------------------|--------------|------------|")
      print("|- %8s -|- %30s -|- %10d -|- %8d -|" % (interfaceKit.isAttached(), interfaceKit.getDeviceName(), interfaceKit.getSerialNum(), interfaceKit.getDeviceVersion()))
      print("|------------|----------------------------------|--------------|------------|")
      print("Number of Digital Inputs: %i" % (interfaceKit.getInputCount()))
      print("Number of Digital Outputs: %i" % (interfaceKit.getOutputCount()))
      print("Number of Sensor Inputs: %i" % (interfaceKit.getSensorCount()))
  #Event Handler Callback Functions
  def interfaceKitAttached(e):
      attached = e.device
      print("InterfaceKit %i Attached!" % (attached.getSerialNum()))
   
  def interfaceKitDetached(e):
      detached = e.device
      print("InterfaceKit %i Detached!" % (detached.getSerialNum()))
   
  def interfaceKitError(e):
      try:
          source = e.device
          print("InterfaceKit %i: Phidget Error %i: %s" % (source.getSerialNum(), e.eCode, e.description))
      except PhidgetException as e:
          print("Phidget Exception %i: %s" % (e.code, e.details))
   
  def interfaceKitInputChanged(e):
      source = e.device
  #    print("InterfaceKit %i: Input %i: %s" % (source.getSerialNum(), e.index, e.state))
   
  def interfaceKitSensorChanged(e):
      source = e.device
  #    print("InterfaceKit %i: Sensor %i: %i" % (source.getSerialNum(), e.index, e.value))
   
  def interfaceKitOutputChanged(e):
      source = e.device
  #    print("InterfaceKit %i: Output %i: %s" % (source.getSerialNum(), e.index, e.state))
  #Main Program Code
  try:
      interfaceKit.setOnAttachHandler(interfaceKitAttached)
      interfaceKit.setOnDetachHandler(interfaceKitDetached)
      interfaceKit.setOnErrorhandler(interfaceKitError)
      interfaceKit.setOnInputChangeHandler(interfaceKitInputChanged)
      interfaceKit.setOnOutputChangeHandler(interfaceKitOutputChanged)
      interfaceKit.setOnSensorChangeHandler(interfaceKitSensorChanged)
  except PhidgetException as e:
      print("Phidget Exception %i: %s" % (e.code, e.details))
      print("Exiting....")
      exit(1)
  print("Opening phidget object....")
   
  try:
      interfaceKit.openPhidget()
  except PhidgetException as e:
      print("Phidget Exception %i: %s" % (e.code, e.details))
      print("Exiting....")
      exit(1)
   
  print("Waiting for attach....")
   
  try:
      interfaceKit.waitForAttach(10000)
  except PhidgetException as e:
      print("Phidget Exception %i: %s" % (e.code, e.details))
      try:
          interfaceKit.closePhidget()
      except PhidgetException as e:
          print("Phidget Exception %i: %s" % (e.code, e.details))
          print("Exiting....")
          exit(1)
      print("Exiting....")
      exit(1)
  else:
  #    displayDeviceInfo()
         
     
     
   
  temperature=(interfaceKit.getSensorValue(0)*0.22222)-61.11
  humidity=(interfaceKit.getSensorValue(1)*0.1906)-40.2
  print("Closing...")
   
  try:
      interfaceKit.closePhidget()
  except PhidgetException as e:
      print("Phidget Exception %i: %s" % (e.code, e.details))
      print("Exiting....")
      exit(1)
   
  print("Done.")
   
  os.system('sh ./send.sh '+str(temperature)+ ' ' +str(humidity))
  exit(0)

Some of the changes made to the original code, are the reading of the analog inputs 0 and 1 relative to temperature and humidity. The conversion formulas are provided by Phidgets for each sensor type. The last line call the file send.sh that manages whatsapp.

Create a new file /yowsup/src/send.sh with this content

#!/bin/bash
   
  echo "Current temperature is $1 °C"
  echo "Current humidity is $2 %"
   
  python yowsup-cli -c config.example -w -s 39xxxxxxxxxx "Current temperature is $1 and current humidity is $2"
  python yowsup-cli -c config.example -k -a -l
  exit 0

Replace xxxxxxxx with your recipient number.

This file is responsible to send the variables temperature and humidity via WhatsApp, then puts the system in listening of new messages.

Step 3: Test the Application

Picture of Test the Application

To start the application, type the command

python yowsup-cli -c config.example -k -a -l

the system waits to receive messages

Send a message with any text

The PhidgetsSBC reads the values ​and send the message

In this tutorial we connected a sensor to read the temperature and humidity, but we could also link a specific action as a result of the received message, for example with the message, "Heat ON" could trigger a relay that turn on the heating remotely with WhatsApp.

The potential that it offers us the PhidgetSBC are so many and in the next tutorials we will try to find out more.

Follow us on social to stay informed.

www.emmeshop.eu

Comments

seamster (author)2014-11-03

Very nice!

About This Instructable

1,286views

19favorites

License:

More by emmeshop:Roast Coffee With Artisan and PhidgetsRaspberry datalogger with Mysql Highcharts Remote control with Raspberry Pi and Phidget WebService
Add instructable to: