Skip to content

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

atget id id
loop
wait 
read message
rdata message rid x
data message2 id x
send message2 * rid

Explanation

  • atget id id: Gets the node's own ID and stores it in the variable id
  • loop: Starts an infinite loop for continuous operation
  • wait: Pauses the script until a message is received
  • read message: Reads the received message into the variable message
  • rdata 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 payload
  • send message2 * rid: Forwards the message to the node with ID matching rid

Use Case

This router script is used to:

  1. Extend the range of a wireless sensor network
  2. Enable communication between nodes that are not within direct range of each other
  3. 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

atget id id
loop
areadsensor value
if(value>0)
    data msg id value
    send msg *
end
delay 1000

Explanation

  • atget id id: Gets the node's own ID and stores it in the variable id
  • loop: Starts an infinite loop for continuous monitoring
  • areadsensor value: Reads the analog sensor value (0 = no motion, >0 = motion detected)
  • if(value>0): Checks if motion is detected
  • data msg id value: Creates a message with the node's ID and the sensor value
  • send msg *: Broadcasts the message to all nodes in range
  • delay 1000: Waits 1 second before checking for motion again

Use Case

This motion detector script is useful for:

  1. Security systems and intrusion detection
  2. Automatic lighting systems that activate when someone enters a room
  3. Occupancy monitoring in smart buildings
  4. 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

atget id id
loop
wait
read msg
rdata msg src value
if(value>0)
    mark 1
    delay 5000
    mark 0
end

Explanation

  • atget id id: Gets the node's own ID and stores it in the variable id
  • loop: Starts an infinite loop to continuously check for messages
  • wait: Pauses the script until a message is received
  • read msg: Reads the received message into the variable msg
  • rdata msg src value: Extracts the sender ID and sensor value from the message
  • if(value>0): Checks if the received value indicates motion
  • mark 1: Turns on the light (sets the node marker to 1)
  • delay 5000: Keeps the light on for 5 seconds
  • mark 0: Turns off the light (sets the node marker to 0)

Use Case

This light control script can be used to:

  1. Create energy-efficient lighting systems that only turn on when needed
  2. Simulate smart home lighting scenarios
  3. Test motion-based automation systems
  4. 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 variable id
  • set threshold 25: Sets the temperature threshold to 25 degrees
  • loop: Starts an infinite loop for continuous monitoring
  • sensing: Activates the sensor to collect data
  • areadsensor temp: Reads the temperature value
  • if(temp > threshold): Checks if the temperature exceeds the defined threshold
  • data tempAlert id temp: Creates an alert message with the node ID and temperature
  • send tempAlert *: Broadcasts the alert to all nodes
  • delay 5000: Waits 5 seconds before the next temperature check

Use Case

This temperature monitoring script is suitable for:

  1. Environmental monitoring systems
  2. HVAC control in smart buildings
  3. Fire detection and prevention systems
  4. 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 variable id
  • loop: Starts an infinite loop for continuous operation
  • wait: Waits for incoming messages
  • read message: Reads the received message
  • rdata message src data: Extracts the sender ID and payload from the message
  • data log id src data: Creates a log entry with gateway ID, source ID, and the data
  • printfile 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:

  1. Data collection from multiple sensors in an IoT network
  2. Creating a bridge between sensor networks and cloud platforms
  3. Logging sensor data for later analysis
  4. 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

  1. Node Configuration:
  2. Create nodes in CupCarbon by right-clicking on the map
  3. Configure properties through the node dialog or directly edit configuration files
  4. Assign scripts to nodes by specifying the script filename

  5. Radio Configuration:

  6. Access through the radio tab in node properties
  7. Adjust range and energy parameters based on your simulation requirements
  8. Match network IDs for nodes that need to communicate

  9. GPS Movement:

  10. Create paths by clicking on the map to place waypoints
  11. Save as GPS files for reuse
  12. Assign to mobile nodes through the node configuration
  13. 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:

  1. Stationary sensors (using motion_detector.csc) placed at strategic locations
  2. Router nodes (using atget_router.csc) positioned to ensure network coverage
  3. Gateway node (using gateway.csc) collecting data from the network
  4. 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

  1. Open CupCarbon simulation environment
  2. Create new sensor nodes for each script type needed
  3. Assign the appropriate script to each node
  4. Configure the network topology by placing nodes appropriately
  5. 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