gpt4 book ai didi

java - mqtt 与 java 的集成

转载 作者:行者123 更新时间:2023-11-30 11:05:36 25 4
gpt4 key购买 nike

我想将 Mqtt 程序与 java 代码集成。以下是我的 java 代码,我需要通过 MQTT 代理连接向设备发送消息....因此,我需要发送消息,例如:3G SETT; 56:1024;通过 mqtt broker 连接到设备。所以我需要在程序中调用 mqtt 连接方法...

告诉我具体在哪里编写和编辑代码以及在哪里集成mqtt代理代码

import java.util.Scanner;

public class message implements MqttCallback
{

static int option;
static Scanner sc = new Scanner(System.in);
static boolean go = true; // for starting main outer loop
static boolean run = true;
static String ip;
static String entire_address,topic;
static int port;
static String apn;
static String msg;



static void operator_apn() {

System.out.println("Enter operator APN");
apn = sc.next();
msg = "3gc SETT;" + 0 + 1 + ":" + apn + ";";
System.out.println("Your message Format is:" + msg);


System.out.println("Enter Topic Name");
topic=sc.next();
System.out.println("Enter Modem Ip address");
ip=sc.next();
System.out.println("Enter Modem Port Number");
port=sc.nextInt();
entire_address=ip+":"+port;
System.out.print("Entire Address is:"+entire_address);

}

static void apn_user_id() {
String username, msg;
System.out.println("Enter APN UserName");
username = sc.next();
msg = "3gc SETT;" + 0 + 2 + ":" + username + ";";
System.out.println("Your APN UserName Format is:" + msg);
}

static void apn_user_password() {
String password, msg;
System.out.println("Enter APN Password");
password = sc.next();
msg = "3gc SETT;" + 0 + 3 + ":" + password + ";";
System.out.println("Your APN Password Format is:" + msg);
}

static void primary_server_ip() {
String ip, msg;
System.out.println("Enter Server primary ip");
ip = sc.next();
msg = "3gc SETT;" + 4 + ":" + ip + ";";
System.out.println("Your Primary Server ip Format is:" + msg);
}

static void primary_server_port() {
String msg;
int port;
System.out.println("Enter Primary server port");
port = sc.nextInt();
msg = "3g SETT;" + 5 + ":" + port + ";";
System.out.println("Your Primary Server port Format is:" + msg);
}

static void secondary_server_ip() {
String ip, msg;
System.out.println("Enter Server secondary ip");
ip = sc.next();
msg = "3gc SETT;" + 6 + ":" + ip + ";";
System.out.println("Your Secondary Server ip Format is:" + msg);
}

static void secondary_server_port() {
String msg;
int port;
System.out.println("Enter Secondary server port");
port = sc.nextInt();
msg = "3g SETT;" + 7 + ":" + port + ";";
System.out.println("Your Secondary Server port Format is:" + msg);
}

static void fota_apn() {
String apn, msg;
System.out.println("Enter FOTA APN");
apn = sc.next();
msg = "3gc SETT;" + 8 + ":" + apn + ";";
System.out.println("Your FOTA APN Format is:" + msg);
}

static void fota_server_ip() {
String ip, msg;
System.out.println("Enter FOTA Server ip");
ip = sc.next();
msg = "3gc SETT;" + 9 + ":" + ip + ";";
System.out.println("Your FOTA Server ip Format is:" + msg);
}

static void fota_server_port() {
String msg;
int port;
System.out.println("Enter FOTA server port");
port = sc.nextInt();
msg = "3g SETT;" + 10 + ":" + port + ";";
System.out.println("Your FOTA Server port Format is:" + msg);
}

static void connection_retry_time() {
String msg;
int time;
System.out.println("Enter Connection Retry Time");
time = sc.nextInt();
msg = "3g SETT;" + 11 + ":" + time + ";";
System.out.println("Your Connection Retry Time Format is:" + msg);
}

static void keep_alive_time() {
String msg;
int time;
System.out.println("Enter Keep Alive Time");
time = sc.nextInt();
msg = "3g SETT;" + 12 + ":" + time + ";";
System.out.println("Your Keep Alive Time Format is:" + msg);
}

static void mp_size() {
String msg;
int size;
System.out.println("Enter MP Size");
size = sc.nextInt();
msg = "3g SETT;" + 13 + ":" + size + ";";
System.out.println("Your MP Size Format is:" + msg);
}

static void mh_size() {
String msg;
int size;
System.out.println("Enter MH Size");
size = sc.nextInt();
msg = "3g SETT;" + 14 + ":" + size + ";";
System.out.println("Your MH Size Format is:" + msg);
}

static void message_queing_username() {
String username, msg;
System.out.println("Enter Message Queing UserName");
username = sc.next();
msg = "3gc SETT;" + 22 + ":" + username + ";";
System.out.println("Your Message Queing UserName Format is:" + msg);
}

static void message_queing_password() {
String password, msg;
System.out.println("Enter Message Queing password");
password = sc.next();
msg = "3gc SETT;" + 23 + ":" + password + ";";
System.out.println("Your Message Queing password Format is:" + msg);
}

static void data_center_smsc_number() {
long number;
String msg;
System.out.println("Enter Data Center SMSC Mobile Number");
number = sc.nextLong();
msg = "3gc SETT;" + 24 + ":" + number + ";";
System.out.println("Your Data Center SMSC Mobile Number Format is:"
+ msg);
}

static void application_instant_time() {
String msg;
int time;
System.out.println("Enter Application Instant Time");
time = sc.nextInt();
msg = "3g SETT;" + 51 + ":" + time + ";";
System.out.println("Your Application Instant Time Setting Format is:"
+ msg);
}

static void app_billing_time() {
String msg;
int time;
System.out.println("Enter Application Billing Time");
time = sc.nextInt();
msg = "3g SETT;" + 52 + ":" + time + ";";
System.out.println("Your Application Billing Time Setting Format is:"
+ msg);
}

static void app_load_time() {
String msg;
int time;
System.out.println("Enter Application Load Time");
time = sc.nextInt();
msg = "3g SETT;" + 53 + ":" + time + ";";
System.out.println("Your Application Load Time Setting Format is:"
+ msg);
}

static void app_event_time() {
String msg;
int time;
System.out.println("Enter Application Event Time");
time = sc.nextInt();
msg = "3g SETT;" + 54 + ":" + time + ";";
System.out.println("Your Application Event Time Setting Format is:"
+ msg);
}

static void identify_time() {
String msg;
int time;
System.out.println("Enter Identify Time");
time = sc.nextInt();
msg = "3g SETT;" + 55 + ":" + time + ";";
System.out.println("Your Identifyt Time Setting Format is:" + msg);
}

static void app_identify_log() {
String msg;
int log;
System.out.println("Enter Application Identify Log");
log = sc.nextInt();
msg = "3g SETT;" + 56 + ":" + log + ";";
System.out.println("Your Application Identify Log Format is:" + msg);
}

static void app_instant_log() {
String msg;
int log;
System.out.println("Enter Application Instant log");
log = sc.nextInt();
msg = "3g SETT;" + 57 + ":" + log + ";";
System.out.println("Your Application Instant log Setting Format is:"
+ msg);
}

static void app_bill_log() {
String msg;
int log;
System.out.println("Enter Application Bill Log");
log = sc.nextInt();
msg = "3g SETT;" + 58 + ":" + log + ";";
System.out.println("Your Application Instant Bill Log Format is:" + msg);
}

static void app_load_log() {
String msg;
int log;
System.out.println("Enter Application Load Log");
log = sc.nextInt();
msg = "3g SETT;" + 59 + ":" + log + ";";
System.out.println("Your Application Load Log Format is:" + msg);
}

static void app_event_log() {
String msg;
int log;
System.out.println("Enter Application Event Log");
log = sc.nextInt();
msg = "3g SETT;" + 60 + ":" + log + ";";
System.out.println("Your Application Event Log Format is:" + msg);
}

static void load_survey_days() {
String msg;
int days;
System.out.println("Enter Load Survey Days");
days = sc.nextInt();
msg = "3g SETT;" + 61 + ":" + days + ";";
System.out.println("Your Load Survey Days Format is:" + msg);
}

static void schedule_interval() {
String msg;
int interval;
System.out.println("Enter Schedule Interval");
interval = sc.nextInt();
msg = "3g SETT;" + 62 + ":" + interval + ";";
System.out.println("Your Schedule Interval Format is:" + msg);
}

static void dlms_password() {
String password, msg;
System.out.println("Enter DLMS Password");
password = sc.next();
msg = "3g SETT;" + 63 + ":" + password + ";";
System.out.println("Your DLMS Password Format is:" + msg);

}

static void dia_time() {
int time;
String msg;
System.out.println("Enter DIA Time");
time = sc.nextInt();
msg = "3g SETT;" + 64 + ":" + time + ";";
System.out.println("Your DIA Time Format is:" + msg);

}

static void dia_sample() {
int sample;
String msg;
System.out.println("Enter DIA Sample");
sample = sc.nextInt();
msg = "3g SETT;" + 65 + ":" + sample + ";";
System.out.println("Your DIA Time Sample is:" + msg);

}

static void schedule_enable() {
int schedule;
String msg;
System.out.println("Enter Schedule Enable");
schedule = sc.nextInt();
msg = "3g SETT;" + 66 + ":" + schedule + ";";
System.out.println("Your Schedule Enable Format is:" + msg);

}

static void schedule_hour() {
int time;
String msg;
System.out.println("Enter Schedule Hour");
time = sc.nextInt();
msg = "3g SETT;" + 67 + ":" + time + ";";
System.out.println("Your Schedule Hour Format is:" + msg);

}

static void schedule_mins() {
int time;
String msg;
System.out.println("Enter Schedule Mins");
time = sc.nextInt();
msg = "3g SETT;" + 68 + ":" + time + ";";
System.out.println("Your Schedule Mins Format is:" + msg);

}

static void event_order_request() {
int order_request;
String msg;
System.out.println("Enter Event Order Request");
order_request = sc.nextInt();
msg = "3g SETT;" + 69 + ":" + order_request + ";";
System.out.println("Your Event Order request Format is:" + msg);

}

static void event_fifo() {
int fifo;
String msg;
System.out.println("Enter Event FIFO");
fifo = sc.nextInt();
msg = "3g SETT;" + 70 + ":" + fifo + ";";
System.out.println("Your Event FIFO Format is:" + msg);

}

static void app_send_load_select() {
int load;
String msg;
System.out.println("Enter App Send Load Select");
load = sc.nextInt();
msg = "3g SETT;" + 71 + ":" + load + ";";
System.out.println("Your App Send Load Select Format is:" + msg);
}







public static void main(String[] args) throws MqttException {

while (go == true) {
while (run == true) {

System.out.println("\n 1.Enter operator APN \n 2.Enter APN userId \n 3.Enter APN password \n 4.Enter primary server ip \n 5.Enter primary server port \n"
+ " 6.Enter Secondary Server IP \n 7.Enter Secondary Server Port \n 8.Enter FOTA APN \n 9.Enter FOTA Server IP \n 10.Enter FOTA server Port \n"
+ " 11.Enter Connection Retry Time \n 12.Enter Keep Alive Time \n 13.Enter MP Size \n 14.Enter MH Size \n"
+ " 15.Enter Message Queing UserName \n 16.Enter Message Queing Password \n 17.Enter Data Center SMSC Number \n 18.Enter App Instant Time \n"
+ " 19.Enter App Billing Time \n 20.Enter App Load Time \n 21.Enter App Event Time \n 22.Enter Identify Time \n 23.Enter App Identify Log \n"
+ " 24.Enter App Instant Time \n 25.Enter App Bill Log \n 26.Enter App Load Log \n 27.Enter App Event Log \n 28.Enter Load Survey Days \n"
+ " 29.Enter Schedule Interval \n 30.Enter DLMS Password \n 31.Enter DIA Time \n 32.Enter DIA Sample \n 33.Enter Schedule Enable \n"
+ " 34.Enter Schedule Hour \n 35.Enter Schedule Mins \n 36.Enter Event Order Request \n 37.Enter Event FIFO \n 38.Enter App Send Load Select \n"
+ " 39.Change Meter \n 40.Restart Meter ");

System.out.println("Enter your option:");
option = sc.nextInt();

switch (option) {
case 1:
operator_apn();
sendMessage();
break;
case 2:
apn_user_id();
message.sendMessage();
break;
case 3:
apn_user_password();
message.sendMessage();
break;
case 4:
primary_server_ip();
message.sendMessage();
break;
case 5:
primary_server_port();
message.sendMessage();
break;
case 6:
secondary_server_ip();
message.sendMessage();
break;
case 7:
secondary_server_port();
message.sendMessage();
break;
case 8:
fota_apn();
message.sendMessage();
break;
case 9:
fota_server_ip();
message.sendMessage();
break;
case 10:
fota_server_port();
message.sendMessage();
break;
case 11:
connection_retry_time();
message.sendMessage();
break;
case 12:
keep_alive_time();
message.sendMessage();
break;
case 13:
mp_size();
message.sendMessage();
break;
case 14:
mh_size();
message.sendMessage();
break;
case 15:
message_queing_username();
message.sendMessage();
break;
case 16:
message_queing_password();
message.sendMessage();
break;
case 17:
data_center_smsc_number();
message.sendMessage();
break;
case 18:
application_instant_time();
message.sendMessage();
break;
case 19:
app_billing_time();
message.sendMessage();
break;
case 20:
app_load_time();
message.sendMessage();
break;
case 21:
app_event_time();
message.sendMessage();
break;
case 22:
identify_time();
message.sendMessage();
break;
case 23:
app_identify_log();
message.sendMessage();
break;
case 24:
app_instant_log();
message.sendMessage();
break;
case 25:
app_bill_log();
message.sendMessage();
break;
case 26:
app_load_log();
message.sendMessage();
break;
case 27:
app_event_log();
message.sendMessage();
break;
case 28:
load_survey_days();
message.sendMessage();
break;
case 29:
schedule_interval();
message.sendMessage();
break;
case 30:
dlms_password();
message.sendMessage();
break;
case 31:
dia_time();message.sendMessage();
break;
case 32:
dia_sample();
message.sendMessage();
break;
case 33:
schedule_enable();
message.sendMessage();
break;
case 34:
schedule_hour();
message.sendMessage();
break;
case 35:
schedule_mins();
message.sendMessage();
break;
case 36:
event_order_request();
message.sendMessage();
break;
case 37:
event_fifo();
message.sendMessage();
break;
case 38:
app_send_load_select();
message.sendMessage();
break;
case 39:
System.out.println("Change Meter");
break;
case 40:
System.out.println("Restart");
break;

default:
System.err.println("Invalid Option");

}

run = false;
}

if (run == false) {
System.out.println("\nWould you like to run again? Y/N");
char again = sc.next().charAt(0);
again = Character.toUpperCase(again);
if (again == 'Y') {
run = true;
} else if (again == 'N') {
System.out.println("Exit.");
go = false;
} else {
System.err.println("Invalid entry. Try again.");
}
}

最佳答案

使用 Java Paho MQTT 库,您应该查看示例以了解如何实现发布到 MQTT 代理的客户端。

通常首先你应该实现 .connect 方法到 Broker,然后你使用 .publish 方法发送包含 payload< 的消息 到 MQTT Broker 中的特定 topic

你应该看看 sample Paho app .

还有实现的Paho client tests .

关于java - mqtt 与 java 的集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29599146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com