{"id":73,"date":"2017-04-01T21:09:47","date_gmt":"2017-04-01T20:09:47","guid":{"rendered":"http:\/\/wp.bizoir.dk\/?p=73"},"modified":"2025-06-11T20:03:17","modified_gmt":"2025-06-11T19:03:17","slug":"maginon-sp-2-smart-plug-final-part","status":"publish","type":"post","link":"https:\/\/wp.bizoir.dk\/?p=73","title":{"rendered":"Maginon SP-2 Smart Plug \u2013 Final part"},"content":{"rendered":"<p>The only thing that remains is to create the code to make the initial setup and some code to turn the plug on and off.<\/p>\n<p>Although I have been a software developer for many years, I have never really made much socket-programming, so I tried to find something on the internet to build on.<\/p>\n<p>After quite some searching, I found this discussion on <a href=\"http:\/\/stackoverflow.com\/questions\/30849655\/linux-sendto-implementation-possibly-adding-extra-padding-to-udp-message\">stackoverflow<\/a>.<\/p>\n<p>This actually issues the correct commands to make the initial setup of a Maginon SP-2 smart plug. However, it needs a small correction to make it work with the Maginon plug &#8211; the same that I mentioned earlier with regards to the Orvibo socket. Alle replies from the socket will be received on UDP port 48899.<\/p>\n<p>I have made a corrected version of the register.c program <a href=\"wp-content\/uploads\/2018\/04\/register.c\">here<\/a>.<\/p>\n<p>Before you compile the program, change these two lines to match the SSID and Wifi password to your own router\u00a0 (change <span style=\"color: #3366ff;\">DLINK<\/span> to your SSID and the <span style=\"color: #3366ff;\">xxxPASS_WPA_PASSxxx<\/span> to your Wifi password.<\/p>\n<pre>static const char *ssid = \"AT+WSSSID=DLINK\\r\";\r\nstatic const char *sec_settings = \"AT+WSKEY=WPA2PSK,TKIP,xxxPASS_WPA_PASSxxx\\r\";<\/pre>\n<p>The program is compiled with:<\/p>\n<pre>gcc -O0 -o register register.c<\/pre>\n<p>Before you run the program, be sure to assign the socket a static ip-address in your router. To do this, you need to get the MAC address of the socket, and then in your router, assign a static ip-address to that MAC address.<\/p>\n<p>Now run the program<\/p>\n<pre>register<\/pre>\n<p>The program sends these commands to the plug to make it connect to your router &#8211; on UDP port 48899<\/p>\n<pre>\r\nHF-A11ASSISTHREAD\r\n+ok\r\nAT+WSSSID=xxxx\\r\r\nAT+WSKEY=WPA2PSK,TKIP,xxxx\\r\r\nAT+WMODE=STA\\r\r\nAT+Z\\r\r\n<\/pre>\n<p>When the socket reboots, it should connect to you own WIFI router. Check the router to see if it has done so.<\/p>\n<p>When it is connected, these two python scripts to turn the socket<\/p>\n<p>On:<\/p>\n<pre>#!\/usr\/bin\/env python\r\n\r\nfrom socket import *\r\nfrom datetime import datetime\r\n\r\nHOST = 'plug1'\r\nPORT = 8899\r\nBUFSIZ = 1024\r\nADDR = (HOST, PORT)\r\n\r\ntcpCliSock = socket(AF_INET, SOCK_STREAM)\r\ntcpCliSock.connect(ADDR)\r\n\r\ndata2 = 'AT+YZSWITCH=1,ON,'+datetime.now().strftime('%Y%m%d%H%M')\r\ntcpCliSock.send(data2)\r\ndata1 = tcpCliSock.recv(BUFSIZ)\r\nprint data1\r\n\r\ntcpCliSock.close()<\/pre>\n<p>and Off:<\/p>\n<pre>#!\/usr\/bin\/env python\r\n\r\nfrom socket import *\r\nfrom datetime import datetime\r\n\r\nHOST = 'plug1'\r\nPORT = 8899\r\nBUFSIZ = 1024\r\nADDR = (HOST, PORT)\r\n\r\ntcpCliSock = socket(AF_INET, SOCK_STREAM)\r\ntcpCliSock.connect(ADDR)\r\ndata2 = 'AT+YZSWITCH=1,OFF,'+datetime.now().strftime('%Y%m%d%H%M')\r\ntcpCliSock.send(data2)\r\ndata1 = tcpCliSock.recv(BUFSIZ)\r\nprint data1\r\ntcpCliSock.close()<\/pre>\n<p>In both cases, you should modify the HOST line in both scripts and set it to the DNS name or the ip-adress of your plug<\/p>\n<p>When you can turn the socket on and off, you can also try to use the delay command<\/p>\n<pre>AT+YZDELAY=1,OFF,5,201701232127<\/pre>\n<p>It can be used for both ON and OFF. The number after ON\/OFF is the delay in minutes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The only thing that remains is to create the code to make the initial setup and some code to turn the plug on and off. Although I have been a software developer for many years, I have never really made much socket-programming, so I tried to find something on the internet to build on. After &hellip; <a href=\"https:\/\/wp.bizoir.dk\/?p=73\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Maginon SP-2 Smart Plug \u2013 Final part&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-hardware"],"_links":{"self":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=73"}],"version-history":[{"count":9,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":1153,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions\/1153"}],"wp:attachment":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}