Cloud Scale Link API
v. 1.2.
Cloud Scale Link API
v. 1.0 Page 2 of 7
Index
1. Structure of commands and responses........................................................................................... 3
2. Module initialization – CSL-INIT....................................................................................................... 3
2.1. General .................................................................................................................................... 3
2.2. FDA – factory data ................................................................................................................... 3
2.3. FIN – factory initialization command....................................................................................... 4
3. Cloud management......................................................................................................................... 4
3.1. Cloud management protocol................................................................................................... 5
3.1.1. Read module data – RMD command............................................................................... 5
3.1.2. Save module data – SMD command................................................................................ 6
3.1.3. Reboot module – REB command..................................................................................... 6
3.1.4. Initialize module – INI command..................................................................................... 6
3.1.5. Enable/disable debug mode – DEB command ................................................................ 6
3.1.6. Ping request/response – PRQ command........................................................................ 7
3.1.7. Blink – module identification – BLI command................................................................. 7
Cloud Scale Link API
v. 1.0 Page 3 of 7
1. Structure of commands and responses
Commands for CSL configuration are composed of ASCII characters. Commands are composed of
parameters and value in the following form:
Parameter=value for example MID=1234
Parameters and values are separated by semicolon (;).
First three characters always define command type for instance FDA, RMD, SMD etc.
Please note that due to the CSL modules architecture maximum length of sent string is 255 characters
including terminators.
2. Module initialization – CSL-INIT
2.1. General
When you connect new module by default it will connect to MQTT broker to topic CSL-INIT.
Default data are:
Attribute Meaning
Topic CSL-INIT-RX – topic where module will receive commands.
CSL-INIT-TX – topic to which module will publish responses to commands.
MQTT IP Depends on the current server
Port Depends on the current server
Username Depends on the current server
Password Depends on the current server
Please note that module will connect to CSL-INIT only until it was initialized. After that module will
not connect to CSL-INIT.
2.2. FDA – factory data
New module will publish it’s data to CSL-INIT topic in the following format:
FDA;MID=1234;MOD=CSL-ODIN;VER=1.4.0;MCE=6.54.3.2.1;MCW=1.2.3.4.5.6
FDA has the following attributes:
Attribute Meaning
FDA Command type
MID Module ID – this random generated serial number which will change every time
module boots until it is initialized.
MOD Model
VER Firmware version
MCE Mac address of Ethernet (if not present MAC will 0.0.0.0.0.0) – note MAC will be
written in decimal
Cloud Scale Link API
v. 1.0 Page 4 of 7
MCW Mac address of WIFI (if not present MAC will 0.0.0.0.0.0) – note MAC will be written
in decimal
2.3. FIN – factory initialization command
To initialize module with serial number and PIN you must send following command:
FIN;MID=1234;NID=4321;NPI=8765432[;CUR=cslc.scale-monitor.com;CPO=20884]
FIN command has the following attributes:
Attribute Meaning
FIN Command type
MID Module ID (current)
NID New module ID
NPI New module PIN
CUR* Cloud URL (MQTT)
CPO* Cloud port (MQTT)
* - optional parameter
When module receive command, it will set new module ID and new PIN and will respond with return
answer of:
FIN;MID=4321;RES=1
Response has following attributes:
Attribute Meaning
FIN Command type
MID Module ID – please note that response if success will include new module ID.
RES Status of response: 1 – success, 0 – failure
Please note that module ID and PIN can be programmed only once.
Module ID must be 6 characters which must consists of uppercase characters and numbers.
PIN must be 8 characters consisting of upper and lowercase characters and numbers.
3. Cloud management
If module has activated cloud management it will connect to the following topics:
Attribute Meaning
$CSL/MID/RX This is topic where commands are accepted and are send to RS-232.
Module does subscribe to this topic. This topic is omnidirectional –
module will not publish any data on this topic.
$CSL/ MID/TX This where module will publish data coming from RS-232. This topic
is omnidirectional – module does not subscribe to this topic.
$CSL/MID/CMP/RX This topic is intended for configuration of module. Module
subscribes to this topic. This topic is omnidirectional – module will
not publish any data on this topic.
$CSL/MID/CMP/TX On this topic module will publish answer to commands recieved on
CMP/RX topic. This topic is omnidirectional – module does not
subscribe to this topic.
Cloud Scale Link API
v. 1.0 Page 5 of 7
$CSL/MID/RX/BC This is topic where commands are accepted and are send to TCP/IP
client (client is when CSL module is connecting to TCP/IP server).
Module does subscribe to this topic. This topic is omnidirectional –
module will not publish any data on this topic.
$CSL/MID/CMP/TX/BC This where module will publish data coming from TCP/IP client. This
topic is omnidirectional – module does not subscribe to this topic.
$CSL/MID/CMP/RX/BS This is topic where commands are accepted and are send to TCP/IP
server (server is when other device or program connects to CSL
TCP/IP server port). Module does subscribe to this topic. This topic is
omnidirectional – module will not publish any data on this topic.
$CSL/MID/CMP/TX/BS This where module will publish data coming from TCP server. This
topic is omnidirectional – module does not subscribe to this topic.
$CSL/MID/STATUS This is topic where status of module will be published – when
connected message will be published by module, when disconnected
message will be published by the broker (last will message).
Status message has following form:
Connected:
STA;S=C;MID=
Disconnected
STA;S=D;MID=
$CSL/MID/DEBUG On this topic messages for debugging communication will be
published.
3.1. Cloud management protocol
In order to use cloud management protocol you must always send authentication in command you
are sending. Authentication is done by adding module ID (MID=XXXX) and pin (P=1234) otherwise
module will not accept and execute command.
In case authentication fails you will get the following response:
RS;MID=XXXX;CMD=*;R=ASCII(24)
ASCII 24 which is cancel.
3.1.1. Read module data – RMD command
In order to read module data you must send RMD command in the following format:
RMD;MID=XXXX;P=1234
Response will be all module data where response will start with RDM. Since there is a lot of data
there will be multiple messages sent as cloud scale link can not send messages longer than 255 bytes.
Example of response:
RMD;V=2.9.0;DN=defa;DM=CSLTHOR;SN=KCFY9M;ED=1;EI=192.168.50.220;EM=255.255.255.0;EG=192.168.50.1;S=;R=0;WI=0.
0.0.0;WM=0.0.0.0;WG=0.0.0.0;WD=0;WS=4;WE=4;WB=5;CSL=1;CSI=1;S0CS=1
RMD;MID=KCFY9M;S0C=10010;S0D=10011;S0B=9600;S0I=1;S0P=0;S0R=0;S0N=;S1C=10020;S
1B=9600;S1I=1;S1P=0;CTY=0;SIP=;SPO=;ST=1310
Cloud Scale Link API
v. 1.0 Page 6 of 7
RMD;MQI=0;MQS=;MQP=19883;MQC=MQTT-PCS232;MQT=;MQQ=0;MQU=;MSU=1;MRJ=0;MRE=0;MLT=;MLM=dis;MCM=;MKA=10;HTP=80;HRJ=0
RMD;VK1=;VV1=;VK2=;VV2=;VK3=;VV3=;DNS=8.8.8.8;MBM=0;MBA=0
3.1.2. Save module data – SMD command
In order to save module data you must send SMD command in the following format:
SMD;MID=XXXX;P=1234;DN=test;EI=10.10.10.10
Response will contain ACK or NAK as response. If ACK is sent than settings were successfully saved. In
case of NAK settings were not saved successfully.
Example of response:
RS;MID=XXXX;CMD=SMD;PR=1;R=ASCII(6)
RS;MID=XXXX;CMD=SMD;R=ASCII(21) – NAK it can mean something is wrong with module memory or
setting does not exists.
PR parameter report, if PIN is required. This can be ignored as configuring module over cloud always
requires PIN.
3.1.3. Reboot module – REB command
In order to reboot module you must send REB command in the following format:
REB;MID=XXXX;P=1234
If reboot command was accepted you will get response in the following format:
RS;MID=XXXX;CMD=REB;R=ASCII(6)
If you do not receive any response, then command was not received.
3.1.4. Initialize module – INI command
In order to initialize module (factory reset) you must send INI command in the following format:
INI;MID=XXXX;P=1234
If initialization command was accepted you will get response in the following format:
RS;MID=XXXX;CMD=INI;R=ASCII(6)
If you do not receive any response, then command was not received.
3.1.5. Enable/disable debug mode – DEB command
You can enable/disable debugging on topic $CSL/MID/DEBUG by sending command DEB in the
following format:
DEB;MID=XXXX;P=1234;DBM=0|1
Cloud Scale Link API
v. 1.0 Page 7 of 7
If command was accepted, you will get response in the following format:
RS;MID=XXXX;CMD=DEB;DBM=0|1;CSL=0|1
DBM parameter is used to enable or disable debugging. If value of DBM is 1 than debugging is
enabled, if 0 it is disabled. Please note that after every reboot of module DBM value is set to 0 so you
have to re-enable debugging, if you need it.
CSL parameter shows, cloud management is enabled. If cloud management is not enabled (CSL=0)
than you will not be able to enable debugging and response will always give you DBM=0.
3.1.6. Ping request/response – PRQ command
Ping command is used to check, if client is alive.
Request command:
PRQ;MID=XXXX
Module will response with command:
RS;MID=XXXX;CMD=PRQ;WI=10.10.10.10;R=198;RBR=0;B=0;OTP=YYYY
WI/EI=module IP address
R=WIFI signal
RBR=reboot required 0-no, 1-yes
B=blinking activated 0-no, 1-yes
OTP=One Time Password
3.1.7. Blink – module identification – BLI command
This command is used to activate LED blinking on module to identify module.
Command:
BLI;MID=XXXX;[B=0|1]
Optional parameter B is used to activate – deactivate blinking – if 0 – then blinking is deactivated, 1 –
blinking is activated. If empty module will respond with current status.
RS;MID=XXXX;CMD=BLI;B=0|1
When user presses reset button the following message will be published on CMP-TX topic:
RS;MID=2Y1KX9;CMD=BLI;BP=1

Ok as you see this document i made  node.js code for it. Did i catch every detail correctly? 


// raspberry-pi-client.js
const mqtt = require('mqtt');
const crypto = require('crypto');

const brokerUrl = 'mqtt://pi-OptiPlex-3020M';
const username = 'client';
const password = 'clientpass';

let client;
let moduleId = '';
let modulePin = '';

function startClient() {
  console.log('Attempting to connect to MQTT broker...');
  client = mqtt.connect(brokerUrl, {
    username: username,
    password: password
  });

  client.on('connect', () => {
    console.log('Client connected to MQTT broker');
    subscribeToTopics();
    sendFactoryData();
  });

  client.on('message', (topic, message) => {
    console.log(`Received message on ${topic}: ${message.toString()}`);
    handleMessage(topic, message.toString());
  });

  client.on('error', (error) => {
    console.error('Connection error:', error);
  });

  client.on('close', () => {
    console.log('Connection to MQTT broker closed');
  });
}

function subscribeToTopics() {
  const topics = ['CSL-INIT-RX', '$CSL/+/CMP/RX', '$CSL/+/RX', '$CSL/+/RX/BC', '$CSL/+/RX/BS'];
  client.subscribe(topics, (err) => {
    if (!err) {
      console.log(`Subscribed to topics: ${topics.join(', ')}`);
    } else {
      console.error('Error subscribing to topics:', err);
    }
  });
}

function sendFactoryData() {
  const factoryData = {
    FDA: true,
    MID: generateTempModuleId(),
    MOD: 'CSL-ODIN',
    VER: '1.4.0',
    MCE: '6.54.3.2.1.0',
    MCW: '1.2.3.4.5.6'
  };
  client.publish('CSL-INIT-TX', formatMessage(factoryData));
}

function handleMessage(topic, message) {
  if (topic === 'CSL-INIT-RX') {
    handleInitResponse(message);
  } else if (topic.includes('/CMP/RX') || topic.includes('/RX')) {
    handleCommand(topic, message);
  }
}

function handleInitResponse(message) {
  const data = parseMessage(message);
  if (data.FIN && data.NID && data.NPI) {
    moduleId = data.NID;
    modulePin = data.NPI;
    console.log(`Initialized with ID: ${moduleId} and PIN: ${modulePin}`);
    subscribeToModuleTopics();
  }
}

function subscribeToModuleTopics() {
  const topics = [
    `$CSL/${moduleId}/CMP/RX`,
    `$CSL/${moduleId}/RX`,
    `$CSL/${moduleId}/RX/BC`,
    `$CSL/${moduleId}/RX/BS`
  ];
  client.subscribe(topics, (err) => {
    if (!err) {
      console.log(`Subscribed to module-specific topics: ${topics.join(', ')}`);
    } else {
      console.error('Error subscribing to module-specific topics:', err);
    }
  });
}

function handleCommand(topic, message) {
  const data = parseMessage(message);
  if (data.MID !== moduleId || data.P !== modulePin) {
    console.log('Received command with invalid MID or PIN');
    return;
  }

  console.log(`Executing command: ${data.CMD}`);
  
  switch (data.CMD) {
    case 'RMD':
      handleReadModuleData();
      break;
    case 'SMD':
      handleSaveModuleData(data);
      break;
    case 'REB':
      handleReboot();
      break;
    case 'INI':
      handleInitialize();
      break;
    case 'DEB':
      handleDebugMode(data);
      break;
    case 'PRQ':
      handlePingRequest();
      break;
    case 'BLI':
      handleBlink(data);
      break;
    default:
      console.log(`Unknown command: ${data.CMD}`);
  }
}

function handleReadModuleData() {
  const response = {
    RMD: true,
    V: '1.4.0',
    DN: 'TestDevice',
    DM: 'CSL-ODIN',
    SN: moduleId,
    // Add more simulated data as needed
  };
  sendResponse('RMD', formatMessage(response));
}

function handleSaveModuleData(data) {
  // Simulate saving data
  console.log(`Saving module data: DN=${data.DN}, EI=${data.EI}`);
  sendResponse('SMD', String.fromCharCode(6)); // ACK
}

function handleReboot() {
  console.log('Rebooting module...');
  sendResponse('REB', String.fromCharCode(6)); // ACK
  // Simulate reboot
  setTimeout(() => {
    console.log('Module rebooted');
    sendFactoryData();
  }, 5000);
}

function handleInitialize() {
  console.log('Initializing module...');
  sendResponse('INI', String.fromCharCode(6)); // ACK
  moduleId = '';
  modulePin = '';
  sendFactoryData();
}

function handleDebugMode(data) {
  const debugMode = data.DBM === '1';
  console.log(`Setting debug mode to: ${debugMode}`);
  sendResponse('DEB', formatMessage({ DBM: data.DBM, CSL: '1' }));
}

function handlePingRequest() {
  const response = {
    CMD: 'PRQ',
    WI: '192.168.1.100', // Example IP
    R: '198',
    RBR: '0',
    B: '0',
    OTP: crypto.randomBytes(4).toString('hex').toUpperCase()
  };
  sendResponse('PRQ', formatMessage(response));
}

function handleBlink(data) {
  const blinkStatus = data.B === '1';
  console.log(`Setting blink status to: ${blinkStatus}`);
  sendResponse('BLI', formatMessage({ B: data.B }));
}

function sendResponse(command, response) {
  const topic = `$CSL/${moduleId}/CMP/TX`;
  const message = `RS;MID=${moduleId};CMD=${command};${response}`;
  client.publish(topic, message);
}

function generateTempModuleId() {
  return crypto.randomBytes(3).toString('hex').toUpperCase();
}

function formatMessage(data) {
  return Object.entries(data)
    .map(([key, value]) => `${key}=${value}`)
    .join(';');
}

function parseMessage(message) {
  return message.split(';').reduce((acc, pair) => {
    const [key, value] = pair.split('=');
    if (key && value !== undefined) {
      acc[key] = value;
    }
    return acc;
  }, {});
}

startClient();

this client in this case raspberrypi client. Do you think willthiswork?