MQTT Send and Receive

22 views
Skip to first unread message

James Dy

unread,
Jun 30, 2025, 1:33:33 AMJun 30
to GAMA
i am doing a test where i am sending a json data to the topic for the machine species in gama platform to get and display, it works but when i am trying to send it back to the mqtt, it just shows random letters. I am using ActiveMQ with MQTT Explorer. 

this is the received data in mqtt explorer from gama

factory/machine1/data
server/machine1/data
+ ‹ ÿc`4(ÍÍÍ,NÎ×KOÌMÔËK-)Ï/ÊÖKÎÏÍÍÏÓsÎÏ-È/Î,Iu Jú¦ '¦§6ˆý Ðf`”ïæ`ƒÿÿ ~'G¯
™ ?aæš”¥!s¢B¦E-™?{Æœ‰3§g­˜?)bÒŠ%‹Cb&ÌšÅ4ÅÌì‚1C‘wP1wƒ ±Àz1†SYY§ìA¦ü )N-*K-ÒÏMLÎÈÌK5ÔOI,Iü#š–˜\’_T‰*Ì žT»¸-ÒÏMLÎÈÌK5ÔOI,Iü#š–˜\’_T‰*Ì žT»¸

this is the code

/**

* Name: mqttpractice

* Based on the internal empty template.

* Author: James Dy

* Tags:

*/



model mqttpractice


/* Insert your model definition here */


global {

float machine1_speed <- 0.0;

float machine1_temp <- 0.0;

string machine1_last_message <- "";

init {

create machine1 {

}

}

}


species machine1 skills: [network] {

string my_topic <- "factory/machine1/data";

string server_topic <- "server/machine1/data";

string last_message <- "";

float speed <- machine1_speed;

float temp <- machine1_temp;

init {

do connect to:"localhost" login:"admin" password:"admin" port:1883 with_name:my_topic;

write "connected";

}


reflex fetch {

write "fetch ran";

loop while: has_more_message() {

message mess <- fetch_message();

last_message <- mess.contents;

machine1_last_message <- last_message;

write "message is: " + mess.contents;

map<string, string> content <- from_json(mess.contents);

write "content is : " + content;

machine1_speed <- content["speed"] as float;

speed <- machine1_speed;

write "speed is: " + speed;

machine1_temp <- content["temp"] as float;

temp <- machine1_temp;

write "temp is: " + temp;

}

}

reflex send {

write "send ran";

map<string, string> msg <- [

"topic"::server_topic,

"speed"::(speed + 5 as string),

"temp"::(temp + 5 as string)

];

do send to: server_topic contents: to_json(msg);

write "msg sent is: " + msg;

write "server_topic is: " + server_topic;

}


}


experiment start type: gui {

output {

monitor "Speed" value: machine1_speed;

monitor "Temperature" value: machine1_temp;

display machine_data type: 2d{

chart "Speed and Temp over time" type: series {

data "speed" value: machine1_speed;

data "temp" value: machine1_temp;

}

}

}

}


James Dy

unread,
Jul 11, 2025, 10:22:34 AMJul 11
to GAMA
i fixed this by reading another conversation here and setting the raw value to true in the do connect action
Reply all
Reply to author
Forward
0 new messages