Making WIFI version of intelligent socket easily -- Software

Keywords: PHP socket network Mobile

Refer to the first part for hardware construction: https://blog.csdn.net/qq_34472145/article/details/94561226

I. Preface

In the previous part, the socket receives messages from MQTT server, while the server receives instructions from APP and converts them into customized messages and sends them to WiFi module. The system architecture is as follows:
See the first part for the running code of WIFI module. Next, the server provides MQTT and WEB service programs, adhering to the simple principle, which are written in PHP.

II. Preparation environment

(I) hardware environment:
For a server with public IP, cloud server (Baidu, Alibaba, Tencent or others) is recommended.
(Note: it's OK to use a virtual machine, but it should be under the local area network and accessible by the mobile phone.)
(II) software environment:
LAMP environment: linux(ubuntu)+apache2+mysql+php5
Necessary library: MQTT class code https://segmentfault.com/a/1190000014031341?utm_source=tag-newest

III. implementation steps

(1) create three new files: Mqtt.class.php, mymqtt.php and processControl.php.
Mqtt.class.php: MQTT class code above
mymqtt.php: encapsulate the parameters and function to be used by mqtt
processControl.php: get method receives the parameters sent by the user and performs the message publishing action
1) the function of mymqtt.php mainly includes encapsulating data and publishing messages.

<?php
        require_once "Mqtt.class.php";
        class MyMQTT
        {
                public $server_ip = "134.175.231.46"; //Server ip address
                private $server = "j8home.cn";     // Service agent address (mqtt server address)
                private $port = 1883;                     // Communication port
                private $username = "admin";       // User name (if required)
                private $password = "123456";      // Password (if required
                private $client_id = "server_smho001"; // Set your connection client id

				//Publish news
                function pubmsg($topic,$msg){
                        $mqtt = new Mqtt($this->server, $this->port, $this->client_id); //Instantiate MQTT class
                         if ($mqtt->connect(true, NULL, $this->username, $this->password)) {
                            //If the link is created successfully
                           	   $mqtt->publish($topic, $msg, 0);     //Release
                          	   $mqtt->close();    //Close link after sending
                         } else {
                             echo "Time out!\n";
                         }
                }
                
                //Subscription message
                function submsg($topic,$msg){
                        $mqtt = new Mqtt($this->server, $this->port, $this->client_id);
                        if(!$mqtt->connect(true, NULL, $this->username, $this->password)) { //If the link is unsuccessful, repeat the listening connection.
                            exit(1);
                        }
                        $topics[$topic] = array("qos" => 0, "function" => "procmsg");
                        // Subscription subject, qos is 0
                        $mqtt->subscribe($topics, 0);
                        while($mqtt->proc()){
                        }
                        //Dead cycle monitoring
                        $mqtt->close();
                }

                //Processing message
                function procmsg($topic, $msg){ //Information callback function print information
                       echo "Msg Recieved: " . date("r") . "\n";
                          echo "Topic: {$topic}\n\n";
                          echo "\t$msg\n\n";
                          $a = json_decode($msg);
                          var_dump($a);
                          die;
                }
                
                //Encapsulate message content in json format
                function makepacket($id,$status){
                        return  '{"id":"'.$id.'","status":"'.$status.'"}';
                }
        }
?>

2) processControl.php sends a message in the form of a url link (insecure), as follows:

//processControl.php
<?php
        header("Content-type:text/html;charset=utf-8");
        require_once "mymqtt.php";
        $topic ="devices/socket/relay1";
        $id = isset($_GET['id'])? strval($_GET['id']):"";  //Socket ID
        $status = isset($_GET['status'])?  strval($_GET['status']):"";   //Socket state
        $mqtt = new MyMQTT();   //Create a custom class object
        $msg = $mqtt-> makepacket($id,$status);   //Encapsulated data
        $mqtt->pubmsg($topic,$msg);   //Publish news
?>

3) processControl.php sends a message in the form of a url link (insecure), as follows:

Posted by stelthius on Wed, 30 Oct 2019 12:22:15 -0700