Raspberry Pi 4 and ServiceNow

Community Alums
Not applicable

Hi Team,

 

I would like to connect Raspberry Pi 4 to ServiceNow through REST API (Input from GPIO pins to ServiceNow). How can I do this?

 

Thank you.

3 REPLIES 3

VernaTek
Giga Contributor

From the question assuming Option 1 (below), either one requires custom code on the PI and in ServiceNow:

1) Raspberry Pi Code reads GPIO and connects to a ServiceNow public instance using REST API
There may be all-in-one canned PI installs of this now. AdaFruit.com is a good source for the latest for that platform.
This link however, gives a lot of info on PI GPIO and at the end How To install what is needed to read the GPIO pins from Python running your custom python code. For other languages (Java? PHP?) you'd run those language installs after the OS is up and run your custom code in one of those languages. https://raspberrytips.com/raspberry-pi-gpio-pins/ 

2) Raspberry Pi Code reads GPIO and connects to a MID server for REST API Integration
Assuming optimally using (1) PI4 or "server" to run it all. But using a MID server on a PI requires a CentOS or RHEL 64 bit OS for the MID server to run. Your custom code reads the local GPIO pins (as in Option 1) and integrates with ServiceNow using your local MID server. 
I have found Centos image installs for PI3+, but not a new enough version (>=2022), nor in 64 bit that my PI4 8GB requires for an OS to run to have a CentOS7+ or RHEL OS that a MID server requires.
Found your question searching for how to get the OS on it. I'm on a deadline and on my own now, so I will post the steps I used and links to get the OS installed.
I'm actually shocked there is no article on this yet, or that average people can find 🙂
At K22 a train demo was running that used PI MID servers?  I'd be surprised if they didn't post the HOW TO for that somewhere?

VernaTek
Giga Contributor

Update: I found that Ubuntu 22 installs and meets the requirements for MID servers and was successful with it.
Only ever used RHEL and CentOs with Tokyo to do that. Must have glossed over that.
But you inspired me to write a detailed article on this, it has value for IOT and other use-cases I have planned.
"HOW TO: Raspberry PI4 Tokyo MID Server Install for GPIO and IOT Use" and will post once I can get a PID back.
Today even with my Personal or Customer DEV account, "No Instances Available" 😞

For now, follow the Ubuntu 22 (Desktop/Server) Install on PI4 for Opt 1 or Op2 (Use Desktop if your not good with Linux Cmd Line) https://ubuntu.com/tutorials/how-to-install-ubuntu-on-your-raspberry-pi#1-overview
You should be able to follow the MID server install steps from that OS if you have an instance to connect to then the GPIO stuff from my earlier post.
Cheers.

Oddeoh
Tera Contributor

Your answer can be done the quickest with direct API web-hooks that call your Service Now Instance directly to POST parameters from a Pi code process. This will Update data on a CMDB CI or Custom Table. 
But a MID-server and a CMDB Custom Class is the best-practices/scalable way especially if you want two-way IoT control or have a lot of telemetry calls coming in and don’t want to drop data, or want to reliably interface and control the outside world.


The challenge and why we have a community, is sometimes knowing where to look, to not re-invent the wheel and get other’s experiences.

PI4 PROJECTS FOR GPIO

Assuming you’re just reading the GPIO pins from code on the PI4, then take the many example projects for PI’s at Raspberry PI Org and follow them.

  • Patrick at RaspberryTips has some great free resources you can download too. Even working with Maker Teenagers with a STEM trajectory, they find these cheat sheets valuable and easy to consume to “make” things happen and what command line calls to focus on.

Essentially modify the working PI Org code example you picked to include a snippet (below) for once you have the sensor or GPIO Pin state data collected and validated, and pass that parameter to your PID (Personal Dev Instance) as you need.

  • Keep it 2000ms (2 sec) max intervals for posting the data. GPIO pin states are real-time-esque, but most sensors have nothing new at faster intervals than 800ms anyway.

EXAMPLE GPIO READ AND POST CODE

As you build the layers to the final MVP. Use this sample advice on reading the column from the table (OOB ITIL role).

Once that works, now switch to the security roles and payload updates needed to update that column.  Table API Example

 

Most PI Org project samples assume Python. But this Python boiler plate should be easy to merge as a function to pass the GPIO pin and its state your reading once you have the value:

 

import requests

import json

import RPi.GPIO as GPIO


# Set the GPIO pin number you want to read from

gpio_pin = 17


# Set the ServiceNow instance URL and webhook URL


# Use ServiceNow’s API Explorer to derive and test the webook URL to use and what payloads to use. This example is assuming its

instance_url = "https://your-instance.service-now.com"

webhook_url = instance_url + "/api/your-webhook-endpoint"


def post_gpio_state(pin_number, state):

    # Set the JSON payload data (Confirm from API Explorer what to use)

    payload = {"pinState": state}


    try:

        # Send the POST request with the payload

        response = requests.post(webhook_url, json=payload)


        # Check the response status code

        if response.status_code == 200:

            print("GPIO state posted successfully.")

        else:

            print("Error occurred while posting GPIO state. Status code:", response.status_code)


    except requests.exceptions.RequestException as e:

        print("An error occurred:", e)


def monitor_gpio_state(pin_number):

    # Setup GPIO pin as input

    GPIO.setmode(GPIO.BCM)

    GPIO.setup(pin_number, GPIO.IN)


    try:

        while True:

            # Read the current state of the GPIO pin

            state = GPIO.input(pin_number)


            # Convert the state to boolean

            state = bool(state)


            # Post the GPIO state using the webhook

            post_gpio_state(pin_number, state)


    except KeyboardInterrupt:

        GPIO.cleanup()


# Usage example

monitor_gpio_state(gpio_pin)

 

 

Future Proof the PI4 Image Build

As I said, if you are not doing a lot of telemetry or data collection you don't need a MID-server. The ServiceNow Innovation demonstrations at K23 on PI’s were using that just fine, even on a hotel shared Wifi. 
But, I would still follow the PI4 64 bit MID Server Image install build (below) for the PI Org Project Builds above so that you can add MID-server support later without rebuilding, or buying another PI.

 

INSTALLING 64-bit MID-SERVER on PI4

Here’s some simple steps that will get you up quickly if you want to explore integrating with ServiceNow from a PI, with our without the MID Server:

  1.       Use a Raspberry Pi4 8GB model, 4GB will work but is pushing it in a year from now.  I recommend Parallax to buy these from and save the price gouging from some Amazon resellers while supply chains are low/slow.
  2.       On Parallax’s site, read what is says below “Out of Stock” and you can prepay and get one at 2019 prices in a few weeks time over the phone, as soon as they restock.
  3.       Download and Install the latest Raspberry Pi Imager
  4.       Get your high-speed SD Card of 16GB or higher (ideally 128GB card, they are so cheap on Amazon why wouldn’t you)
  5.       Load up Raspberry PI imager
  6.       Scroll down to select "Other general-purpose OS"
  7.       Select "Ubuntu"
  8.       Select a 64-bit flavor (I suggest Desktop for doing DEV on it or to avoid unnecessary CMD line needs)

 

MID SERVER INSTALL

  1. Follow the documented MID Server installation steps from ServiceNow for Linux
  2. Create a local ServiceNow user account for your “API” security Context (i.e. “api_rpi4_gpiosetter”).  KB – ServiceNow REST API Roles (contains KB – Security REST API Roles) and example to Grant
  3. I would recommend for starters to test the linking to just update an existing table a Boolean column with the Table API with the state value and build your App in ServiceNow off of that data. But if you delve into CMDB then a Custom Class is the right way to go for v2!
  4. OT (Operational Technology) IoT Class Model


Let me know if you have any questions. Excited for your project!