STM32+W5500+MQTT+Android实现远程数据采集及控制

2015-09-30 09:22:29   来源:eefocus   

关键字: STM32  W5500  MQTT  Android  远程数据采集

下面我们看下主函数的代码,思路也比较清晰:

int main(void)
{
static char meassage[200];
int rc;
char *led;
char led_value;
float temperature,humidity,light,pressure;
srand(0);
//配置LED灯引脚
LED_Config();
//初始化配置网络
network_init();
while(1){
memset(meassage,0,sizeof(meassage));
//订阅消息
rc = mqtt_subscrib(“pyboard_led”,meassage);
printf(“rc = %d ”,rc);
if(rc >= 0){
printf(“meassage = %s ”,meassage);
//解析JSON格式字符串并点亮相应的LED灯
cJSON *root = cJSON_Parse(meassage);
if(root != NULL){
led = cJSON_GetObjectItem(root,”led”)->valuestring;
printf(“led = %s ”,led);
led_value = cJSON_GetObjectItem(root,”value”)->valueint;
if(!strcmp(led,”red”)){
if(led_value){
LED_On(LED_RED);
}else{
LED_Off(LED_RED);
}
}else if(!strcmp(led,”green”)){
if(led_value){
LED_On(LED_GREEN);
}else{
LED_Off(LED_GREEN);
}
}else if(!strcmp(led,”blue”)){
if(led_value){
LED_On(LED_BLUE);
}else{
LED_Off(LED_BLUE);
}
}else if(!strcmp(led,”yellow”)){
if(led_value){
LED_On(LED_YELLOW);
printf(“Yellow On ”);
}else{
LED_Off(LED_YELLOW);
printf(“Yellow Off ”);
}
}
// 释放内存空间
cJSON_Delete(root);
}else{
printf(“Error before: [%s] ”,cJSON_GetErrorPtr());
}
}
delay_ms(500);
//获取传感器测量数据,该示例使用随机数
temperature = rand()P;
humidity = rand()0;
light = rand()00;
pressure = rand()00;
//将数据合成为JSON格式数据
sprintf(meassage,”{”temperature”:%.1f,”humidity”:%.1f,”light”:%.1f,”pressure”:%.1f}”,temperature,humidity,light,pressure);
//将数据发送出去
mqtt_publish(“pyboard_value”,meassage);
}
}

完整工程代码可在后面的附件下载。

2 手机端代码实现
手机端我们也使用官方提供的Java库Java client and utilities,下载地址:
http://www.eclipse.org/paho/
将jar文件添加到工程中即可,程序界面如下所示:
\
上面4个条目分别显示STM32单片机通过W5500发送到服务器端的传感器测量数据;
下面4个图片分别控制板子上的4个LED灯;
消息发送我们采用线程的方式发送,接收采用回调函数方式接收消息。

2.1 实现消息发送
发送消息的代码如下所示:


class PublishThread extends Thread {
String topic;
MqttMessage message;
int qos = 0;
MemoryPersistence persistence = new MemoryPersistence();
PublishThread(String topic,String message){
this.topic = topic;
this.message = new MqttMessage(message.getBytes());
}
public void sendMessage(String topic,String message){
this.topic = topic;
this.message = new MqttMessage(message.getBytes());
run();
}
@Override
public void run() {
try {
MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setKeepAliveInterval(1);
System.out.println(“Connecting to broker: ” + broker);
sampleClient.connect(connOpts);
System.out.println(“Connected”);
System.out.println(“Publishing message: ” + message.toString());
message.setQos(qos);
sampleClient.publish(topic, message);
System.out.println(“Message published”);
sampleClient.disconnect();
System.out.println(“Disconnected”);
}catch(MqttException me) {
System.out.println(“reason “+me.getReasonCode());
System.out.println(“msg “+me.getMessage());
System.out.println(“loc “+me.getLocalizedMessage());
System.out.println(“cause “+me.getCause());
System.out.println(“excep “+me);
me.printStackTrace();
}
}
}

2.2 实现消息接收
接收消息的代码如下所示:


class SubscribeThread extends Thread{
final String topic;
MemoryPersistence persistence = new MemoryPersistence();
SubscribeThread(String topic){
this.topic = topic;
}
@Override
public void run(){
try {
final MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
final MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
System.out.println(“Connecting to broker: ” + broker);
connOpts.setKeepAliveInterval(5);
sampleClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
System.out.println(“connectionLost”);
try {
sampleClient.connect(connOpts);
sampleClient.subscribe(topic);
}catch (MqttException e){
e.printStackTrace();
}
}

@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
System.out.println(“messageArrived:”+mqttMessage.toString());
System.out.println(topic);
System.out.println(mqttMessage.toString());
try {
JSONTokener jsonParser = new JSONTokener(mqttMessage.toString());
JSONObject person = (JSONObject) jsonParser.nextValue();
temperature = person.getDouble(“temperature”);
humidity = person.getDouble(“humidity”);
light = person.getDouble(“light”);
pressure = person.getDouble(“pressure”);
System.out.println(“temperature = ” + temperature);
System.out.println(“humidity = ” + humidity);
runOnUiThread(new Runnable() {
@Override
public void run() {
temperatureTextView.setText(String.format(“%.1f”, temperature));
humidityTextView.setText(String.format(“%.1f”, humidity));
lightTextView.setText(String.format(“%.1f”, light));
pressureTextView.setText(String.format(“%.1f”, pressure));
}
});
} catch (JSONException ex) {
ex.printStackTrace();
}
}

@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
System.out.println(“deliveryComplete”);
}
});
sampleClient.connect(connOpts);
sampleClient.subscribe(topic);
} catch(MqttException me) {
System.out.println(“reason “+me.getReasonCode());
System.out.println(“msg “+me.getMessage());
System.out.println(“loc “+me.getLocalizedMessage());
System.out.println(“cause “+me.getCause());
System.out.println(“excep “+me);
me.printStackTrace();
}
}
}

3 实测效果
1,单片机端定时更新传感器数据,手机端也会同步更新;
2,手机端点击4个LED控制的按钮,板子上也会点亮或者熄灭对应的LED;

4 源码下载


4.1 STM32端源码下载
\ MQTT_STM32_W5500.rar
4.2 手机端源码下载
\ MQTT_Android.rar
4.3 手机端apk下载
\ stm32_w5500_mqtt_app.rar

[1] [2]
404-电子工程世界
编辑:什么鱼
本文引用地址: http://www.eeworld.com.cn/mcu/2015/0930/article_22632_2.html
[发表评论]
[加入收藏]
[打印本页]
[关闭窗口]
[返回顶部]
[RSS订阅]
小广播
每日新闻

404-电子工程世界
最热点击
本周热门资源推荐
EEWORLD独家
404-电子工程世界
论坛精华
精选博文
大学堂关键字
404-电子工程世界

EEWORLD首页
EEWORLD论坛
EEWORLD下载中心
EEWORLD博客
EEWORLD白皮书
EEWORLD精彩专题