NetBurner 3.5.6
PDF Version
Web Socket DIP Switch

Supported Platforms: MODM7AE70, MOD54415, MOD54417

Real-Time DIP Switch States via WebSocket

Overview

This NetBurner application demonstrates real-time monitoring of DIP switch states through a web interface using WebSockets. The application continuously polls DIP switches on the development board and instantly updates a webpage whenever switch positions change, without requiring page refreshes.

The application continuously polls the DIP switches and sends the state of the DIP switches via a websocket to the client. It's not the most efficient implementation when considering CPU utilization but this app demonstrates the capabilities of a WebSocket to allow the server(NetBurner device) to send the state of a variable to the client(webpage) with low latency and minimal packet size. This app also illustrates how to use the NetBurner JSON library to build and send JSON objects from the NetBurner device to the client. In this case, JSON objects are used to pass the state of the DIP switches to the webpage.

Features

  • Real-time Updates: DIP switch state changes are immediately reflected on the connected webpage
  • WebSocket Communication: Low-latency, minimal packet size communication between device and client
  • LED Control: Interactive LED control through the web interface
  • JSON Data Exchange: Structured data transfer using NetBurner's JSON library
  • Multi-platform Support: Compatible with multiple NetBurner platforms

Supported Platforms

  • MODM7AE70
  • MOD5441x

Hardware Requirements

  • NetBurner development board (one of the supported platforms)
  • MOD-DEV-70CR carrier board
  • 8 DIP switches (connected to specific pins)
  • 8 LEDs (connected to specific pins)

Pin Configuration

LED Connections (J2/P2 Connector)

  • LED 1: Pin 15
  • LED 2: Pin 16
  • LED 3: Pin 31
  • LED 4: Pin 23
  • LED 5: Pin 37
  • LED 6: Pin 19
  • LED 7: Pin 20
  • LED 8: Pin 24

DIP Switch Connections (J2/P2 Connector)

  • Switch 1: Pin 8 (A/D channel 7)
  • Switch 2: Pin 6 (A/D channel 6)
  • Switch 3: Pin 7 (A/D channel 5)
  • Switch 4: Pin 10 (A/D channel 3)
  • Switch 5: Pin 9 (A/D channel 4)
  • Switch 6: Pin 11 (A/D channel 1)
  • Switch 7: Pin 12 (A/D channel 0)
  • Switch 8: Pin 13 (A/D channel 2)

How It Works

System Architecture

  1. Main Loop: Continuously polls DIP switch states
  2. WebSocket Server: Handles client connections and data transmission
  3. Input Task: Processes incoming WebSocket messages for LED control
  4. JSON Communication: Structures data exchange between device and web client

Data Flow

  1. Application reads DIP switch positions using analog-to-digital conversion
  2. Switch states are converted to JSON format
  3. JSON data is transmitted via WebSocket to connected web clients
  4. Web interface receives LED control commands and updates hardware accordingly

Switch State Detection

  • MCF5441X Platform: Uses analog-to-digital conversion to read switch positions
  • Other Platforms: Uses built-in getdipsw() function
  • State Logic: 0 = switch ON, 1 = switch OFF
  • Change Detection: Only transmits updates when switch states actually change

Key Components

WebSocket Endpoint

CallBackWSEndPoint ws("data", StartWS);

Creates a WebSocket endpoint at /data path for client connections.

JSON Data Structure

The application sends DIP switch states in the following JSON format:

{
"dipSwitches": {
"dip1": "On/Off",
"dip2": "On/Off",
...
"dip8": "On/Off"
}
}

LED Control

LEDs can be controlled via incoming JSON messages with the format:

{
"ledcb1": true/false,
"ledcb2": true/false,
...
}

Configuration Parameters

  • INCOMING_BUF_SIZE: 8192 bytes for incoming WebSocket data
  • REPORT_BUF_SIZE: 512 bytes for outgoing JSON reports
  • NUM_LEDS: 8 LEDs supported
  • NUM_SWITCHES: 8 DIP switches supported
  • STATE_BUF_SIZE: 8 bytes per switch state string

Usage

  1. Build and Deploy: Compile and flash the application to your NetBurner device
  2. Network Connection: Ensure the device is connected to your network
  3. Web Interface: Connect to the device's web interface through a browser
  4. Real-time Monitoring: Flip DIP switches to see immediate updates on the webpage
  5. LED Control: Use the web interface to control individual LEDs

Performance Considerations

While this implementation continuously polls the DIP switches, it's designed for demonstration purposes rather than optimal CPU utilization. The polling approach clearly shows WebSocket capabilities for real-time data transmission with minimal latency.

Development Notes

  • Uses NetBurner RTOS for task management
  • Implements proper WebSocket message parsing with JSON object boundary detection
  • Handles WebSocket connection lifecycle (connect/disconnect)
  • Includes error handling for socket operations
  • Uses semaphores for task synchronization