Android studio学习笔记【结合硬件做一些简单通信】——MQTT.jar包的实战

实战:手机控制esp8266

0.配置

arduino下位机

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient client(espClient);
const char* wifissid = "CMCC-wenzheng"; //改成自己家wifi
const char* password = "wenzhengspace666"; //改成自己家wifi
const char* mqtt_server = "106.13.150.28";
const char* mqtt_id = "565402462_ESP";   //改成自己的QQ号+_ESP
const char* Mqtt_sub_topic = "565402462_ESP";   //改成自己的QQ号+_ESP
const char* Mqtt_pub_topic = "565402462";  //  上报消息给  手机APP的TOPIC  //改成自己的QQ号
long lastMsg = 0; //定时用的

app上位机

 private String host = "tcp://106.13.150.28:1883";
    private String userName = "android";
    private String passWord = "android";
    private String mqtt_id = "565402462"; //定义成自己的QQ号  切记!不然会掉线!!!
    private String mqtt_sub_topic = "565402462"; //为了保证你不受到别人的消息  哈哈
    private String mqtt_pub_topic = "565402462_ESP"; //为了保证你不受到别人的消息  哈哈  自己QQ好后面加 _PC
    private int led_flag =1;

1.手机控制esp8266小灯亮灭

app上位机

  image_1 =findViewById(R.id.image_1);
        image_1.setOnClickListener(new View.OnClickListener() {
            @Override
			
            public void onClick(View v) {
                if(led_flag == 0)
                {
                    publishmessageplus(mqtt_pub_topic,"{\"set_led\":1}");
                    led_flag =1;
                }else{
                    publishmessageplus(mqtt_pub_topic,"{\"set_led\":0}");
                    led_flag =0;
                }
            }	

arduino下位机

arduino下位机
void callback(char* topic, byte* payload, unsigned int length) {
  String msg="";
  String LED_set = "";
  
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  
  for (int i = 0; i < length; i++)
  {
    msg+= (char)payload[i];
  }
  Serial.println(msg);
  if(msg.indexOf("led"))  //判断是否是要设置LED灯
  {
    //取出LED_set数据 并执行
    LED_set = msg.substring(msg.indexOf("led\":")+5,msg.indexOf("}")); //起点,终点
    digitalWrite(2,!LED_set.toInt()); 
  }
}

2.esp8266上报adc模拟量

app上位机处理数据

  case 3:  //MQTT 收到消息回传   UTF8Buffer msg=new UTF8Buffer(object.toString());

                        String T_val = msg.obj.toString().substring(msg.obj.toString().indexOf("temperature\":")+13,msg.obj.toString().indexOf("}"));
                        String text_val = "温度:"+T_val;
                        text_test.setText(text_val);
                        break;			

arduino下位机

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  long now = millis();
  if (now - lastMsg > 2000) 
  {
    lastMsg = now;
    String json = "{\"temperature\":"+String(analogRead(A0))+"}";
     //温度上报使用ADC模拟随机数
    client.publish(Mqtt_pub_topic,json.c_str());
  }
}

注意:

  • String json = “{“temperature”:”+String(analogRead(A0))+"}";
    之所以用很多转义符“\”是因为:
    转义字符""的使用技巧(谢谢这个博主的回答)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值