ESP32_MQTT_SEND.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. require('../vendor/autoload.php');
  3. use \PhpMqtt\Client\MqttClient;
  4. use \PhpMqtt\Client\ConnectionSettings;
  5. use think\facade\Cache;
  6. date_default_timezone_set("PRC");
  7. define('HOST', '127.0.0.1');
  8. define('PORT', '6379');
  9. define('PASSWORD', '123456');
  10. define('DATABASE', 2);
  11. // define('HOST', 'r-bp1eebab79320044pd.redis.rds.aliyuncs.com');
  12. // define('PORT', '6379');
  13. // define('PASSWORD', '7e2b5c91e438be3c!');
  14. // define('DATABASE', 4);
  15. use \think\facade\Db;
  16. function app_redis()
  17. {
  18. static $redis = null;
  19. static $conn = false;
  20. if (!$conn) {
  21. connect: //定义标签
  22. $redis = new Redis();
  23. try {
  24. //建立的Redis短连接,在请求结束后不会自动关闭,相当于持久连接.
  25. $conn = $redis->connect(HOST, PORT);
  26. $conn = $redis->auth(PASSWORD);
  27. $conn = $redis->select(DATABASE);
  28. // 连接成功,返回$redis对象,连接失败,返回false.
  29. return ($conn === true) ? $redis : false;
  30. } catch (Exception $e) {
  31. return false;
  32. }
  33. } else {
  34. // 这里假设PHP-FPM在处理一个请求的时间内,Redis连接都是可用的.
  35. // 所以只在PHP-CLI下检查Redis连接的状态,进行断线重连.
  36. if (php_sapi_name() === 'cli') {
  37. try {
  38. // ping用于检查当前连接的状态,成功时返回+PONG,失败时抛出一个RedisException对象.
  39. // ping失败时警告:
  40. // Warning: Redis::ping(): connect() failed: Connection refused
  41. // var_dump('AAAAAAAAA', $redis);
  42. // echo 'Redis 连接状态' . $redis->ping() . PHP_EOL;
  43. @$redis->ping();
  44. if (!$redis->ping()) {
  45. goto connect; //跳转到标签出继续执行连接操作
  46. }
  47. } catch (Exception $e) {
  48. // 信息如 Connection lost 或 Redis server went away
  49. echo $e->getMessage();
  50. echo 'Redis 连接失败 重新连接:' . PHP_EOL;
  51. // 断线重连
  52. goto connect;
  53. }
  54. }
  55. return $redis;
  56. }
  57. }
  58. function sendConfig($topic,$config)
  59. {
  60. $server = '116.62.220.88';
  61. $port = 1883;
  62. $clientId = 'mqtt_esp32_elevator_send';
  63. $username = 'rl517';
  64. $password = "rlian2022";
  65. $clean_session = false;
  66. $connectionSettings = new ConnectionSettings();
  67. $connectionSettings = $connectionSettings
  68. ->setUsername($username)
  69. ->setPassword($password)
  70. ->setKeepAliveInterval(60)
  71. // Last Will 设置
  72. // ->setLastWillTopic('emqx/test/last-will')
  73. // ->setLastWillMessage('client disconnect')
  74. // ->setLastWillQualityOfService(1)
  75. ;
  76. $mqtt = new MqttClient($server, $port, $clientId);
  77. $mqtt->connect($connectionSettings, $clean_session);
  78. echo 'connect OK'.PHP_EOL;
  79. echo 'topic:'.$topic.PHP_EOL;
  80. echo 'config:'.$config.PHP_EOL;
  81. $res=$mqtt->publish(
  82. $topic,
  83. $config,
  84. 1
  85. );
  86. echo 'publish end'.PHP_EOL;
  87. $mqtt->loop(true,true);
  88. $mqtt->disconnect();
  89. return $res;
  90. }
  91. function updateSendResult($msgid){
  92. // $conn = new mysqli('rm-bp1h3uqkzy66ckt8yro.mysql.rds.aliyuncs.com', 'dev', '711e7D69f9d0c3f1', 'smart_elevator');
  93. $conn = new mysqli('127.0.0.1', 'root', 'root', 'smart_elevator');
  94. if ($conn -> connect_errno) {
  95. printf("Connect failed: %s\n", $conn->connect_error);
  96. exit();
  97. }
  98. $sql = "UPDATE send_config_log SET result='1' WHERE id=".$msgid;
  99. if ($conn->query($sql) === TRUE) {
  100. echo "send_config_log update success";
  101. } else {
  102. echo "更新失败: " . $conn->error;
  103. }
  104. }
  105. while (1) {
  106. $jsonData= app_redis()->rpop("device_elevator_mqtt_config_list");
  107. if(!$jsonData){
  108. sleep(3);
  109. continue;
  110. }
  111. $data=json_decode($jsonData,true);
  112. var_dump($data);
  113. $topic="ESP/OTHER/".$data['device_id'];
  114. $config=[];
  115. if($data['type']=='audio'){
  116. $config=$data['config'];
  117. }
  118. $config_json=json_encode($config);
  119. // var_dump($config_json);
  120. $res=sendConfig($topic,$config_json);
  121. if(isset($data['msgid'])){
  122. updateSendResult($data['msgid']);
  123. }
  124. // if($res){
  125. // Db::table('send_config_log')->where('id',$data['msgid'])->update(['result'=>'1']);
  126. // }
  127. // var_dump($res);
  128. }