NetBurner Simple JSON HTML Example
An example application demonstrating various methods for generating JSON responses from a NetBurner embedded web server.
Overview
This application showcases multiple approaches to serve JSON data from a NetBurner device, including static files, dynamic generation, and configuration-based responses. It provides a practical reference for developers working with NetBurner's web capabilities.
Shows several methods to have a NetBurner respond to web requests with JSON objects:
- Respond and generate the text directly.
- Respond and generate the response with a JSON lever object.
- Respond and generate the response using configuration variables.
- Respond and generate the response using a pre-built page stored in with web pages and using the dynamic web page capabilities.
Features
The application demonstrates several JSON response methods:
1. Static JSON Files
- plain.json: Pre-formatted static JSON content
- Simple text-based JSON stored as a file
2. Variable-Based Dynamic JSON
- ut.json: Template-based JSON with embedded variables
- Uses NetBurner's variable substitution system
- Displays runtime information like uptime, build date/time, and NNDK version
3. Callback-Based Dynamic JSON
- callback.json: Uses C++ callback functions to generate JSON
- Demonstrates the
<!--CPPCALL DoGeneerateJson -->
mechanism
- Generates JSON content programmatically at request time
4. Function-Based JSON Responses
- GetJson: Direct HTTP callback without associated HTML file
- GetLexerJson: Uses NetBurner's JSON lexer for structured generation
- Provides complete HTTP response handling in C++ code
5. Configuration System Integration
- Config: Exposes the entire configuration tree as JSON
- Config/AppData/MyOwnValue: Access specific configuration values
- Requires enabling the configuration mirror
File Structure
/
main.cpp # Main application code
htmlvar.h # Variable declarations for web pages
index.html # Main navigation page
plain.json # Static JSON example
ut.json # Variable-based JSON template
callback.json # Callback-based JSON page
Key Components
Main Application (main.cpp)
- Initializes network stack and HTTP server
- Defines global variables for web page access
- Implements callback functions for dynamic JSON generation
- Sets up configuration system integration
JSON Generation Methods
Method 1: Direct Text Output
{
writestring(sock,
"HTTP/1.0 200 OK\r\nPragma: no-cache\r\nContent-Type: application/json\r\n\r\n");
return 1;
}
int writestring(int fd, const char *str)
Write a null terminated ascii string to the stream associated with a file descriptor (fd)....
HTTP Request Structure.
Definition http.h:83
Method 2: JSON Lexer
{
pjs.
Add(
"ANumber", 1234);
pjs.
Add(
"AString",
"Testing123");
return 1;
}
A class to create, read, and modify a JSON object.
Definition json_lexer.h:535
ParsedJsonDataSet * AddMyMac(const char *name)
Add the device MAC address to the JSON data set.
ParsedJsonDataSet * Add(const char *name, int i)
Add a name/value pair to the JSON data set where the value is an int.
int PrintObjectToFd(int fd, bool pretty=false)
Prints the JSON data set to a specified file descriptor.
Definition json_lexer.h:1603
ParsedJsonDataSet * StartBuilding()
Use to start building the JSON data set.
ParsedJsonDataSet * DoneBuilding()
Add an end JSON data set and finish building.
Method 3: Callback Function
void DoGeneerateJson(int sock, PCSTR url)
{
fdprintf(sock,
"{\r\n\"Generated\":\"ByCallback\",\r\n");
fdprintf(sock,
"\"Uptime\":%ld\r\n}", Secs);
}
int fdprintf(int fc, const char *format,...)
printf to a file descriptor
Usage
- Build and Deploy: Compile the application and deploy to your NetBurner device
- Access Web Interface: Navigate to your device's IP address (default port 80)
- Test JSON Endpoints: Use the provided links to test different JSON generation methods
Example URLs
Assuming your device IP is 10.1.2.3
:
Configuration
The application includes a configuration variable example:
Signed 32-bit Integer Configuration Variable.
Definition config_obj.h:700
This demonstrates how to expose application-specific configuration data through the web interface.
Build Requirements
- NetBurner NNDK (NetBurner Network Development Kit)
- C++ compiler with NetBurner libraries
- Standard NetBurner build environment
Key Features Demonstrated
- HTTP server initialization and setup
- Multiple JSON response generation techniques
- Variable substitution in web pages
- Callback-based dynamic content generation
- Configuration system integration
- Proper HTTP header handling
- Network stack initialization
Notes
- The application enables system diagnostics (should be removed for production)
- Configuration mirror must be enabled for config-based endpoints
- Default HTTP port is 80
- Application waits for active network connection before proceeding
Learning Objectives
This example is ideal for developers who want to understand:
- NetBurner web server capabilities
- JSON API development on embedded systems
- Dynamic content generation techniques
- Configuration system integration
- HTTP request handling in embedded C++