Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Integration/IoT_NodeMCU_ServiceNow_REST/NodeMCU_IoT_Sensor.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

---

## ⚡ 2. NodeMCU_IoT_Sensor.ino

```cpp
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <DHT.h>

#define DHTPIN D4 // Pin connected to sensor
#define DHTTYPE DHT11 // DHT 11 sensor

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* serverName = "https://YOUR_INSTANCE.service-now.com/api/x_iot/iotdeviceapi/data";
const char* user = "YOUR_SERVICENOW_USER";
const char* userPassword = "YOUR_SERVICENOW_PASSWORD";

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
dht.begin();
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected!");
}

void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();

if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName);
http.setAuthorization(user, userPassword);
http.addHeader("Content-Type", "application/json");

String payload = "{";
payload += "\"device_id\":\"PARK01\",";
payload += "\"status\":\"occupied\",";
payload += "\"temperature\":" + String(temperature) + ",";
payload += "\"humidity\":" + String(humidity);
payload += "}";

int httpCode = http.POST(payload);
String response = http.getString();

Serial.println(httpCode);
Serial.println(response);
http.end();
} else {
Serial.println("WiFi not connected");
}
delay(10000); // send every 10 seconds
}
56 changes: 56 additions & 0 deletions Integration/IoT_NodeMCU_ServiceNow_REST/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# IoT NodeMCU → ServiceNow REST Integration

## Overview
This snippet demonstrates how to send real-time sensor data (e.g., parking slot status, temperature, or humidity) from a **NodeMCU (ESP8266)** board to a **ServiceNow instance** using the REST API.
It shows how edge IoT devices can log events directly into ServiceNow tables such as `x_iot_device_data` or `incident`.



## Components
- NodeMCU (ESP8266)
- DHT11 Temperature/Humidity sensor (or IR/Ultrasonic sensor)
- Wi-Fi connection
- ServiceNow instance with Scripted REST API enabled



## NodeMCU Script (`NodeMCU_IoT_Sensor.ino`)
Connects to Wi-Fi, reads sensor data, and sends JSON payload to ServiceNow REST endpoint.



## ServiceNow Scripted REST API (`ServiceNow_REST_Endpoint.js`)
Receives JSON payload and inserts into the `x_iot_device_data` table.



## How to Use
1. **ServiceNow Setup**
- Navigate to **System Web Services → Scripted REST APIs**
- Create a new API called `IoTDeviceAPI`
- Add a resource `/data`
- Paste code from `ServiceNow_REST_Endpoint.js`
- Test via Postman

2. **NodeMCU Setup**
- Update Wi-Fi credentials: `ssid` and `password`
- Update ServiceNow endpoint URL, username, and password
- Flash code using Arduino IDE
- Open Serial Monitor to verify successful data push

3. **Verify in ServiceNow**
- Check `x_iot_device_data` table for new records
- Optional: create Flow Designer flow for automation

## Example Output

```json
{
"device_id": "PARK01",
"status": "occupied",
"temperature": 28,
"humidity": 60,
"timestamp": "2025-10-27T14:23:05Z"
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var body = JSON.parse(request.body.data);

var rec = new GlideRecord('x_iot_device_data');
rec.initialize();
rec.device_id = body.device_id;
rec.status = body.status;
rec.temperature = body.temperature;
rec.humidity = body.humidity;
rec.timestamp = new GlideDateTime();
rec.insert();

return { message: 'Data inserted successfully' };
})(request, response);
23 changes: 23 additions & 0 deletions Integration/IoT_NodeMCU_ServiceNow_REST/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Users/ameet/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/*/cores/esp8266",
"C:/Users/ameet/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/*/libraries/**",
"C:/Users/ameet/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/*/tools/sdk/include",
"C:/Users/ameet/Documents/Arduino/libraries/**"
],
"defines": [
"ARDUINO=10819",
"ESP8266"
],
"compilerPath": "C:/Users/ameet/AppData/Local/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/*/bin/xtensa-lx106-elf-g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading