diff --git a/Integration/IoT_NodeMCU_ServiceNow_REST/NodeMCU_IoT_Sensor.ino b/Integration/IoT_NodeMCU_ServiceNow_REST/NodeMCU_IoT_Sensor.ino new file mode 100644 index 0000000000..19e654e030 --- /dev/null +++ b/Integration/IoT_NodeMCU_ServiceNow_REST/NodeMCU_IoT_Sensor.ino @@ -0,0 +1,66 @@ + +--- + +## ⚡ 2. NodeMCU_IoT_Sensor.ino + +```cpp +#include +#include +#include + +#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 +} diff --git a/Integration/IoT_NodeMCU_ServiceNow_REST/README.md b/Integration/IoT_NodeMCU_ServiceNow_REST/README.md new file mode 100644 index 0000000000..35ec3ad6cb --- /dev/null +++ b/Integration/IoT_NodeMCU_ServiceNow_REST/README.md @@ -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" +} + + diff --git a/Integration/IoT_NodeMCU_ServiceNow_REST/ServiceNow_REST_Endpoint.js b/Integration/IoT_NodeMCU_ServiceNow_REST/ServiceNow_REST_Endpoint.js new file mode 100644 index 0000000000..96094ff82a --- /dev/null +++ b/Integration/IoT_NodeMCU_ServiceNow_REST/ServiceNow_REST_Endpoint.js @@ -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); diff --git a/Integration/IoT_NodeMCU_ServiceNow_REST/c_cpp_properties.json b/Integration/IoT_NodeMCU_ServiceNow_REST/c_cpp_properties.json new file mode 100644 index 0000000000..38ef4829ee --- /dev/null +++ b/Integration/IoT_NodeMCU_ServiceNow_REST/c_cpp_properties.json @@ -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 +} \ No newline at end of file diff --git a/Integration/IoT_NodeMCU_ServiceNow_REST/example_output.png b/Integration/IoT_NodeMCU_ServiceNow_REST/example_output.png new file mode 100644 index 0000000000..896063ab6b Binary files /dev/null and b/Integration/IoT_NodeMCU_ServiceNow_REST/example_output.png differ