Skip to content

Commit 264f9e4

Browse files
committed
Added IoT NodeMCU → ServiceNow REST integration snippet
1 parent 41877af commit 264f9e4

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
---
3+
4+
## ⚡ 2. NodeMCU_IoT_Sensor.ino
5+
6+
```cpp
7+
#include <ESP8266WiFi.h>
8+
#include <ESP8266HTTPClient.h>
9+
#include <DHT.h>
10+
11+
#define DHTPIN D4 // Pin connected to sensor
12+
#define DHTTYPE DHT11 // DHT 11 sensor
13+
14+
const char* ssid = "YOUR_WIFI_SSID";
15+
const char* password = "YOUR_WIFI_PASSWORD";
16+
const char* serverName = "https://YOUR_INSTANCE.service-now.com/api/x_iot/iotdeviceapi/data";
17+
const char* user = "YOUR_SERVICENOW_USER";
18+
const char* userPassword = "YOUR_SERVICENOW_PASSWORD";
19+
20+
DHT dht(DHTPIN, DHTTYPE);
21+
22+
void setup() {
23+
Serial.begin(115200);
24+
WiFi.begin(ssid, password);
25+
dht.begin();
26+
Serial.print("Connecting to WiFi");
27+
while (WiFi.status() != WL_CONNECTED) {
28+
delay(1000);
29+
Serial.print(".");
30+
}
31+
Serial.println("\nConnected!");
32+
}
33+
34+
void loop() {
35+
float temperature = dht.readTemperature();
36+
float humidity = dht.readHumidity();
37+
38+
if (isnan(temperature) || isnan(humidity)) {
39+
Serial.println("Failed to read from DHT sensor!");
40+
return;
41+
}
42+
43+
if (WiFi.status() == WL_CONNECTED) {
44+
HTTPClient http;
45+
http.begin(serverName);
46+
http.setAuthorization(user, userPassword);
47+
http.addHeader("Content-Type", "application/json");
48+
49+
String payload = "{";
50+
payload += "\"device_id\":\"PARK01\",";
51+
payload += "\"status\":\"occupied\",";
52+
payload += "\"temperature\":" + String(temperature) + ",";
53+
payload += "\"humidity\":" + String(humidity);
54+
payload += "}";
55+
56+
int httpCode = http.POST(payload);
57+
String response = http.getString();
58+
59+
Serial.println(httpCode);
60+
Serial.println(response);
61+
http.end();
62+
} else {
63+
Serial.println("WiFi not connected");
64+
}
65+
delay(10000); // send every 10 seconds
66+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# IoT NodeMCU → ServiceNow REST Integration
2+
3+
## Overview
4+
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.
5+
It shows how edge IoT devices can log events directly into ServiceNow tables such as `x_iot_device_data` or `incident`.
6+
7+
8+
9+
## Components
10+
- NodeMCU (ESP8266)
11+
- DHT11 Temperature/Humidity sensor (or IR/Ultrasonic sensor)
12+
- Wi-Fi connection
13+
- ServiceNow instance with Scripted REST API enabled
14+
15+
16+
17+
## NodeMCU Script (`NodeMCU_IoT_Sensor.ino`)
18+
Connects to Wi-Fi, reads sensor data, and sends JSON payload to ServiceNow REST endpoint.
19+
20+
21+
22+
## ServiceNow Scripted REST API (`ServiceNow_REST_Endpoint.js`)
23+
Receives JSON payload and inserts into the `x_iot_device_data` table.
24+
25+
26+
27+
## How to Use
28+
1. **ServiceNow Setup**
29+
- Navigate to **System Web Services → Scripted REST APIs**
30+
- Create a new API called `IoTDeviceAPI`
31+
- Add a resource `/data`
32+
- Paste code from `ServiceNow_REST_Endpoint.js`
33+
- Test via Postman
34+
35+
2. **NodeMCU Setup**
36+
- Update Wi-Fi credentials: `ssid` and `password`
37+
- Update ServiceNow endpoint URL, username, and password
38+
- Flash code using Arduino IDE
39+
- Open Serial Monitor to verify successful data push
40+
41+
3. **Verify in ServiceNow**
42+
- Check `x_iot_device_data` table for new records
43+
- Optional: create Flow Designer flow for automation
44+
45+
## Example Output
46+
47+
```json
48+
{
49+
"device_id": "PARK01",
50+
"status": "occupied",
51+
"temperature": 28,
52+
"humidity": 60,
53+
"timestamp": "2025-10-27T14:23:05Z"
54+
}
55+
56+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
2+
var body = JSON.parse(request.body.data);
3+
4+
var rec = new GlideRecord('x_iot_device_data');
5+
rec.initialize();
6+
rec.device_id = body.device_id;
7+
rec.status = body.status;
8+
rec.temperature = body.temperature;
9+
rec.humidity = body.humidity;
10+
rec.timestamp = new GlideDateTime();
11+
rec.insert();
12+
13+
return { message: 'Data inserted successfully' };
14+
})(request, response);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Win32",
5+
"includePath": [
6+
"${workspaceFolder}/**",
7+
"C:/Users/ameet/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/*/cores/esp8266",
8+
"C:/Users/ameet/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/*/libraries/**",
9+
"C:/Users/ameet/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/*/tools/sdk/include",
10+
"C:/Users/ameet/Documents/Arduino/libraries/**"
11+
],
12+
"defines": [
13+
"ARDUINO=10819",
14+
"ESP8266"
15+
],
16+
"compilerPath": "C:/Users/ameet/AppData/Local/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/*/bin/xtensa-lx106-elf-g++.exe",
17+
"cStandard": "c11",
18+
"cppStandard": "c++17",
19+
"intelliSenseMode": "gcc-x64"
20+
}
21+
],
22+
"version": 4
23+
}
23 KB
Loading

0 commit comments

Comments
 (0)