IQRF Standard Photorezistor Sensor

This example shows how to create “Standard Photorezistor Sensor” that will be immediately recognized and manageable from IOT world thanks to gateway and standardization.

Prerequisities

Verify that network is ready

First thing is to know an IP address of your IQRF gateway. You can get it from your wifi router. Once you have it, simply type it to your web browser. this will bring you to IQRF gateway manager. Please log in with credentials you got with your gateway.

Once you are in select IQRF network / Network manager and click Discovery button any should see a symbol of your connected IQRF dev board. This must be a symbol of signal, any other or even red cross here means that your IQRF board is not connected and you should go to page where we demonstrated how to make it working as described here.

Make wiring

Unplug you IQRF dev board and ensure that JP6=OFF. Then make wiring of photorezistor as described in the picture.

Upload software to IQRF dev board

Then connect IQRF dev board with CK-USB-04 programmer. Plug CK-USB-04 to USB of your computer and also plug IQRF dev board to any other USB, just for power up. Make sure that JP6=OFF and JP1=OFF on IQRF board.

Open IDE and in status bar in the bottom you should see blinking green spot indicating that programmer is connected.

Right clik on the Source in project view and select Add/Add Existing Item and select standard-photorezistor.c source code for IQRF transceiver.

Now build it by right click on standard-photorezistor.c file and choice Build. After that you should see standard-photorezistor.hex file under Plug-ins.

Ensure that under Plug-ins you have checked DPA-Node-… , right click on .hex file and select Upload.

Unplug IQRF dev board from programmer.

Run it

Set JP1=ON and JP6=ON. Then on IQRF dev board LD1=ON. Turn also JP4=ON and LD4 indicates a status of battery.

Notice that LD5=ON indicating C2 board input. You can quickly check your photoresistor, try to shadow it and the LD5 will show measured level = measured data.

Get sensor data to your software

Now you can run Python code getting sensor data on any your laptop or any other device. The only thing you need is internet connectivity.

Open standard-photorezistor.py and ensure that variable boardAddr includes your real address of node in IQRF network. You can get it from Gateway manager. Below you see node with address 1.

Then ensure that ipAddress = ‘ws://192.168.1.227:1338’ is a real address of you gateway.

# 
# Copyright Logimic,s.r.o., www.logimic.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import asyncio
import websockets
import json
import time

# This is IQRFBB-10 node address in IQRF network
boardAddr = 1
ipAddress = 'ws://192.168.1.227:1338'

# JSON messages by "https://docs.iqrfsdk.org/iqrf-gateway-daemon/api.html"
SENSORS_ENUM = {
  "mType": "iqrfSensor_Enumerate",
  "data": {
    "msgId": "testEmbedSensor",
    "req": {
      "nAdr": boardAddr,
      "param": {}
    },
    "returnVerbose": True
  }
}

READ_TEMP = {
  "mType": "iqrfSensor_ReadSensorsWithTypes",
  "data": {
    "msgId": "testEmbedSensor",
    "req": {
      "nAdr": boardAddr,
      "param": {
        "sensorIndexes": [
          0
        ]
      }
    },
    "returnVerbose": True
  }
}


async def hello():
    # Connect websockets
    async with websockets.connect(
            ipAddress) as websocket:

        # List Lights...
        await websocket.send(json.dumps(SENSORS_ENUM))
        print(f"Sent > {SENSORS_ENUM}")

        response = await websocket.recv()
        print(f"Received < {response}")         

        data = json.loads(response)
        numOuts = data["data"]["rsp"]["result"]["sensors"]
        
        print("-------------------------------------")
        print(f"Node has implemented {len(numOuts)} sensors!")
        print("-------------------------------------")
        print("wait...")

        # Wait 2 sec
        time.sleep(2)

        count = 0        
        while (count < 20):
          print (f"The count is:{count}")
          count = count + 1

          await websocket.send(json.dumps(READ_TEMP))
          print(f"Sent > {READ_TEMP}")

          response = await websocket.recv()
          print(f"Received < {response}")           

          data = json.loads(response)
          value = data["data"]["rsp"]["result"]["sensors"][0]["value"]

          print(f"\n\n---> Measured: {value} \n")

        print("\n---> End of measurement...")


asyncio.get_event_loop().run_until_complete(hello())