CupCarbon Code Examples¶
This page provides examples for creating and simulating Wireless Sensor Networks (WSNs) using the CupCarbon simulator.
Download: CupCarbon Code Examples (ZIP)
Example Categories¶
Router Node Scripts¶
These examples demonstrate how to implement router node scripts for message forwarding:
Router Node Script (atget_router.csc)¶
This script creates a router node that forwards messages between sensor nodes. It's a fundamental component in a wireless sensor network simulation.
Code Breakdown¶
Explanation¶
atget id id: Gets the node's own ID and stores it in the variableidloop: Starts an infinite loop for continuous operationwait: Pauses the script until a message is receivedread message: Reads the received message into the variablemessagerdata message rid x: Extracts data from the message - the sender ID (rid) and the payload (x)data message2 id x: Creates a new message containing the router's ID and the original payloadsend message2 * rid: Forwards the message to the node with ID matchingrid
Use Case¶
This router script is used to:
- Extend the range of a wireless sensor network
- Enable communication between nodes that are not within direct range of each other
- Create multi-hop network topologies for larger deployments
Motion Detector Script (motion_detector.csc)¶
This script implements a motion detector sensor that triggers when movement is detected in its vicinity.
Code Breakdown¶
Explanation¶
atget id id: Gets the node's own ID and stores it in the variableidloop: Starts an infinite loop for continuous monitoringareadsensor value: Reads the analog sensor value (0 = no motion, >0 = motion detected)if(value>0): Checks if motion is detecteddata msg id value: Creates a message with the node's ID and the sensor valuesend msg *: Broadcasts the message to all nodes in rangedelay 1000: Waits 1 second before checking for motion again
Use Case¶
This motion detector script is useful for:
- Security systems and intrusion detection
- Automatic lighting systems that activate when someone enters a room
- Occupancy monitoring in smart buildings
- Triggering cameras or other devices when motion is detected
Light Control Script (light_control.csc)¶
This script controls a light node based on messages received from motion sensors.
Code Breakdown¶
Explanation¶
atget id id: Gets the node's own ID and stores it in the variableidloop: Starts an infinite loop to continuously check for messageswait: Pauses the script until a message is receivedread msg: Reads the received message into the variablemsgrdata msg src value: Extracts the sender ID and sensor value from the messageif(value>0): Checks if the received value indicates motionmark 1: Turns on the light (sets the node marker to 1)delay 5000: Keeps the light on for 5 secondsmark 0: Turns off the light (sets the node marker to 0)
Use Case¶
This light control script can be used to:
- Create energy-efficient lighting systems that only turn on when needed
- Simulate smart home lighting scenarios
- Test motion-based automation systems
- Demonstrate IoT device interaction in a network
Temperature Monitoring Script (temperature_monitor.csc)¶
This script monitors temperature and sends alerts when the temperature exceeds a threshold.
Code Breakdown¶
atget id id
set threshold 25
loop
sensing
areadsensor temp
if(temp > threshold)
data tempAlert id temp
send tempAlert *
end
delay 5000
Explanation¶
atget id id: Gets the node's own ID and stores it in the variableidset threshold 25: Sets the temperature threshold to 25 degreesloop: Starts an infinite loop for continuous monitoringsensing: Activates the sensor to collect dataareadsensor temp: Reads the temperature valueif(temp > threshold): Checks if the temperature exceeds the defined thresholddata tempAlert id temp: Creates an alert message with the node ID and temperaturesend tempAlert *: Broadcasts the alert to all nodesdelay 5000: Waits 5 seconds before the next temperature check
Use Case¶
This temperature monitoring script is suitable for:
- Environmental monitoring systems
- HVAC control in smart buildings
- Fire detection and prevention systems
- Agricultural monitoring for optimal growing conditions
Gateway Node Script (gateway.csc)¶
This script implements a gateway node that collects data from sensor nodes and can communicate with external systems.
Code Breakdown¶
atget id id
loop
wait
read message
rdata message src data
data log id src data
printfile log.txt log
delay 100
Explanation¶
atget id id: Gets the node's own ID and stores it in the variableidloop: Starts an infinite loop for continuous operationwait: Waits for incoming messagesread message: Reads the received messagerdata message src data: Extracts the sender ID and payload from the messagedata log id src data: Creates a log entry with gateway ID, source ID, and the dataprintfile log.txt log: Writes the log entry to a file named "log.txt"delay 100: Brief pause before processing the next message
Use Case¶
This gateway script can be used for:
- Data collection from multiple sensors in an IoT network
- Creating a bridge between sensor networks and cloud platforms
- Logging sensor data for later analysis
- Monitoring the overall activity in a sensor network
CupCarbon Configuration Files¶
CupCarbon uses several configuration files to define node properties, radio settings, and movement patterns. Below are examples and explanations of these configuration files.
Node Configuration Files¶
These files define the properties of different sensor nodes in the simulation.
Sensor Node Configuration (sensor)¶
List of parameters
------------------------------------------
device_type:1
device_id:402
device_longitude:55.47531425209045
device_latitude:25.303690903218275
device_elevation:0.0
device_radius:0.0
device_hide:1
device_pic:0
device_draw_battery:false
device_sensor_unit_radius:25.0
device_script_file_name:atget_hello.csc
Explanation:
- device_type:1: Indicates a standard sensor node
- device_id:402: Unique identifier for this sensor
- device_longitude/latitude: Geographic coordinates of the node
- device_elevation: Height above ground level (0.0 = ground level)
- device_radius: Physical radius of the node (0.0 = default size)
- device_hide:1: Node visibility setting (1 = visible in simulation)
- device_draw_battery:false: Whether to display battery status
- device_sensor_unit_radius:25.0: Sensing range of the node
- device_script_file_name:atget_hello.csc: Script that controls this node's behavior
IoT Device Configuration (iot)¶
List of parameters
------------------------------------------
device_type:13
device_id:200
device_longitude:55.47740345516205
device_latitude:25.30307910615224
device_elevation:0.0
device_radius:0.0
device_hide:1
device_draw_battery:false
device_sensor_unit_radius:20.0
device_pic:0
device_script_file_name:atget_recive.csc
Explanation:
- device_type:13: Indicates an IoT device node
- device_id:200: Unique identifier for this IoT device
- device_sensor_unit_radius:20.0: Sensing range (smaller than regular sensor)
- device_script_file_name:atget_recive.csc: Script for receiving and processing data
Mobile Node Configuration (mobile)¶
List of parameters
------------------------------------------
device_type:6
device_id:35
device_longitude:55.47770619392395
device_latitude:25.303692700131606
device_elevation:0.0
device_radius:8.0
device_hide:1
device_gps_file_name:building.gps
Explanation:
- device_type:6: Indicates a mobile node that can move
- device_id:35: Unique identifier for this mobile node
- device_radius:8.0: Larger radius to represent a moving object (e.g., vehicle)
- device_gps_file_name:building.gps: GPS file that defines the movement path
Radio Configuration Files¶
These files define the wireless communication capabilities of sensor nodes.
Standard Sensor Radio Configuration (sensor_radios/sensor)¶
List of radio Modules for the Sensor402
------------------------------------------
current_radio_name:radio1
radio_standard:ZIGBEE
radio_my:0
radio_channel:0
radio_network_id:13108
radio_radius:30.0
radio_etx:5.92E-5
radio_erx:2.86E-5
radio_esleep:1.0E-7
radio_elisten:1.0E-6
radio_data_rate:250000
conso_tx_model:Classical (Tx)
conso_rx_model:Classical (Rx)
Explanation:
- radio_standard:ZIGBEE: Using ZigBee protocol for communication
- radio_radius:30.0: Maximum communication range in meters
- radio_network_id:13108: Network identifier for this radio
- radio_etx:5.92E-5, radio_erx:2.86E-5: Energy consumption for transmitting/receiving
- radio_data_rate:250000: Data transmission rate in bits per second
IoT Device Radio Configuration (sensor_radios/iot)¶
List of radio Modules for the Sensor392
------------------------------------------
current_radio_name:radio1
radio_standard:ZIGBEE
radio_my:0
radio_channel:0
radio_network_id:13108
radio_radius:20.0
radio_etx:5.92E-5
radio_erx:2.86E-5
radio_esleep:1.0E-7
radio_elisten:1.0E-6
radio_data_rate:250000
conso_tx_model:Classical (Tx)
conso_rx_model:Classical (Rx)
Explanation:
- Similar to the standard sensor but with radio_radius:20.0: Shorter communication range
- This creates a realistic scenario where IoT devices have more limited communication capabilities
GPS Movement File (building.gps)¶
GPS files define movement paths for mobile nodes. Below is a sample of the building.gps file:
Route name
City one
City two
false
10
0 55.47838747501373 25.303067083442635 0.0 4.0
1 55.47854874283075 25.303066780333015 0.0 4.0
1 55.478710010647774 25.303066477223396 0.0 4.0
// ...more path points...
Explanation:
- First line: Route name
- Second line: Starting location name
- Third line: Destination name
- Fourth line: Route visibility flag
- Fifth line: Number of points in the route
- Remaining lines: Path points in format [type] [longitude] [latitude] [elevation] [speed]
- Type 0 = starting point, Type 1 = path point
- Last two values define elevation and movement speed
How to Use Configuration Files¶
- Node Configuration:
- Create nodes in CupCarbon by right-clicking on the map
- Configure properties through the node dialog or directly edit configuration files
-
Assign scripts to nodes by specifying the script filename
-
Radio Configuration:
- Access through the radio tab in node properties
- Adjust range and energy parameters based on your simulation requirements
-
Match network IDs for nodes that need to communicate
-
GPS Movement:
- Create paths by clicking on the map to place waypoints
- Save as GPS files for reuse
- Assign to mobile nodes through the node configuration
- Adjust speed values for realistic movement simulation
These configuration files work together to create a realistic IoT simulation environment where: - Sensors collect and transmit data using their scripts - Radio configurations determine communication capabilities and energy consumption - Mobile nodes move along predefined paths to simulate dynamic environments
Complete IoT Simulation Setup¶
A typical CupCarbon simulation combines the script files discussed earlier with appropriate configuration settings:
- Stationary sensors (using motion_detector.csc) placed at strategic locations
- Router nodes (using atget_router.csc) positioned to ensure network coverage
- Gateway node (using gateway.csc) collecting data from the network
- Mobile nodes following paths to simulate moving objects in the environment
This creates a comprehensive IoT simulation that can model real-world applications like smart buildings, traffic monitoring, or environmental sensing.
How to Use These Scripts¶
- Open CupCarbon simulation environment
- Create new sensor nodes for each script type needed
- Assign the appropriate script to each node
- Configure the network topology by placing nodes appropriately
- Run the simulation to observe the IoT network behavior
Additional Considerations¶
- Adjust thresholds and delay times based on your specific requirements
- For larger networks, use multiple router nodes to ensure proper message propagation
- Consider energy consumption by optimizing sensing and transmission frequencies
- Test your network under different conditions to ensure reliability
- Use the CupCarbon visualization tools to monitor network activity during simulation