Changes for page 2 Script

Last modified by Devin Chen on 2025/06/06 14:03

From version 5.1
edited by Hunter
on 2022/07/08 14:11
Change comment: There is no comment for this version
To version 7.1
edited by Jim(Forgotten)
on 2022/07/09 16:46
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.Hunter
1 +XWiki.Jim
Content
... ... @@ -702,4 +702,334 @@
702 702  
703 703  == **2.5 V-Box connect with AWS platform** ==
704 704  
705 -[[https:~~/~~/ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/AWS/AWS.zip>>https://ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/AWS/AWS.zip]]
705 +=== **Log in AWS** ===
706 +
707 +Login aws account and click“Connect an IoT device”
708 +
709 +[[image:image-20220709164650-1.png]]
710 +
711 +
712 +[[image:image-20220709163750-4.png]]
713 +
714 +=== **Create policy** ===
715 +
716 +Click “Secure”~-~-->“Policies”~-~-->“Create policy”~-~-->Click “Create”
717 +
718 +[[image:image-20220709163750-5.png]]
719 +
720 +Name the policy~-~-->Click “JSON”~-~-->Copy the following content~-~-->Click “Create”
721 +
722 +[[image:image-20220709163750-7.png]]
723 +
724 +[[image:image-20220709163750-6.png]]
725 +
726 +{
727 +
728 + "Version": "2012-10-17",
729 +
730 + "Statement": [
731 +
732 + {
733 +
734 + "Effect": "Allow",
735 +
736 + "Action": [
737 +
738 + "iot:Connect",
739 +
740 + "iot:Publish",
741 +
742 + "iot:Subscribe",
743 +
744 + "iot:Receive",
745 +
746 + "greengrass:Discover"
747 +
748 + ],
749 +
750 + "Resource": "*"
751 +
752 + }
753 +
754 + ]
755 +
756 +}
757 +
758 +=== **Create things** ===
759 +
760 +Click “Manage”~-~-->“Things”~-~-->“Create things”~-~-->“Create single thing”
761 +
762 +[[image:image-20220709163750-8.png]]
763 +
764 +[[image:image-20220709163750-9.png]]
765 +
766 +Name the thing~-~-->Click “Next”
767 +
768 +[[image:image-20220709163750-10.png]]
769 +
770 +Select the way to create certificate
771 +
772 +[[image:image-20220709163750-11.png]]
773 +
774 +Select policy
775 +
776 +
777 +
778 +
779 +
780 +
781 +
782 +
783 +
784 +
785 +
786 +
787 +|
788 +| |[[image:image-20220709163750-13.png]]
789 +
790 +
791 +
792 +1. **Test with MQTT.fx tool**
793 +
794 +Click “View Setting” to get the “Broker Adress”
795 +
796 +
797 +|
798 +| |[[image:image-20220709163750-14.png]]
799 +
800 +|
801 +| |[[image:image-20220709163750-15.png]]
802 +
803 +
804 +
805 +
806 +
807 +|
808 +| |[[image:image-20220709163750-16.png]]
809 +
810 +Create one connection in MQTT.fx tool, set broker port as 8883.
811 +
812 +Upload the CA File, Client Certificate File, Client Key File
813 +
814 +
815 +|
816 +| |[[image:image-20220709163750-17.png]]
817 +
818 +Publish message to topic “TEST”
819 +
820 +
821 +|
822 +| |[[image:image-20220709163750-18.png]]
823 +
824 +|
825 +| |[[image:image-20220709163750-19.png]]
826 +
827 +Click”Test”~-~-->”MQTT test client”~-~-->”Subscrible to a topic”, to get message publish from MQTT.fx tool.
828 +
829 +And we can also send message form AWS platform to MQTT.fx tool.
830 +
831 +
832 +|
833 +| |[[image:image-20220709163750-20.png]]
834 +
835 +1. **Configurate in CloudTool**
836 +
837 +Copy the same setting in MQTT.fx to MQTT configuration
838 +
839 +
840 +|
841 +| |[[image:image-20220709163750-21.png]]
842 +
843 + Add a lua script and copy the lua demo into it.
844 +
845 +
846 +|
847 +| |[[image:image-20220709163750-22.png]]
848 +
849 +sprint = print
850 +
851 +~-~-Cloud mode interface to obtain the MQTT information configured by the cloud platform: (5 returns, namely the server address, client ID, connection table, last word table, certificate table)
852 +
853 +local MQTT_URL, MQTT_CLIENTID, MQTT_CFG, MQTT_LWT, MQTT_CART = mqtt.setup_cfg()
854 +
855 +~-~-publish to topics
856 +
857 +local pub_RE_TOPIC = string.format('TEST')
858 +
859 +~-~-Subscribe topics
860 +
861 +local Subscribe_RE_TOPIC1 = string.format('TEST')
862 +
863 +~-~-variable
864 +
865 +local last_time = 0
866 +
867 +~-~-Timing main function
868 +
869 +function aws.main()
870 +
871 + sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main start")
872 +
873 + if g_mq then
874 +
875 + if g_mq:isconnected() then
876 +
877 + send_Data()
878 +
879 + else
880 +
881 + if os.time() - last_time > 5 then
882 +
883 + last_time = os.time()
884 +
885 + mymqtt_connect()
886 +
887 + end
888 +
889 + end
890 +
891 + else
892 +
893 + mymqtt_init()
894 +
895 + end
896 +
897 + sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main end")
898 +
899 +end
900 +
901 +
902 +~-~- Initialize MQTT
903 +
904 +function mymqtt_init()
905 +
906 + sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID))
907 +
908 + g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENTID) ~-~- Create the object and declare it as a global variable
909 +
910 + if g_mq then
911 +
912 + g_mq:on("message", mymqtt_msg_callback) ~-~- Register to receive message callbacks
913 +
914 + sprint("mqtt init success")
915 +
916 + else
917 +
918 + sprint("mqtt init failed:", err)
919 +
920 + end
921 +
922 +end
923 +
924 +~-~- Connect to MQTT server
925 +
926 +function mymqtt_connect()
927 +
928 + sprint("mqtt connecting...")
929 +
930 + local stat, err = g_mq:connect(MQTT_CFG,MQTT_LWT, MQTT_CART)
931 +
932 + if stat == nil then
933 +
934 + sprint("mqtt connect failed:", err)
935 +
936 + return
937 +
938 + else
939 +
940 + sprint("mqtt connected")
941 +
942 + end
943 +
944 + g_mq:subscribe(TEST, 0)
945 +
946 +end
947 +
948 +~-~- Receive MQTT message callback function
949 +
950 +function mymqtt_msg_callback(topic, msg)
951 +
952 + print("topic:",topic)
953 +
954 + print("revdata:",msg)
955 +
956 + local revData = json.decode(msg)
957 +
958 + print (revData)
959 +
960 + if topic == Subscribe_RE_TOPIC1 then ~-~-Process topic information subscribed from the cloud
961 +
962 +if string.match(topic,Subscribe_RE_TOPIC1) then
963 +
964 + ~-~-if revData ~~= nil then
965 +
966 + for k,v in pairs (revData) do
967 +
968 + print("printing revdata after kv here")
969 +
970 + print (k,v)
971 +
972 + end
973 +
974 + print ("current state is",fanstate)
975 +
976 + ~-~-end
977 +
978 +end
979 +
980 +end
981 +
982 +end
983 +
984 +
985 +~-~-Get real-time data
986 +
987 +function getData()
988 +
989 + local jdata = {}
990 +
991 + local addr = bns_get_alldata()
992 +
993 + print(json.encode(addr))
994 +
995 + for i,v in pairs(addr) do
996 +
997 + if v[2] == 1 then
998 +
999 + jdata[v[3]] = v[4]
1000 +
1001 + end
1002 +
1003 + end
1004 +
1005 + return jdata
1006 +
1007 +end
1008 +
1009 +~-~-send data
1010 +
1011 +function send_Data()
1012 +
1013 + local pub_data =
1014 +
1015 + {
1016 +
1017 +123
1018 +
1019 +}
1020 +
1021 +sprint(json.encode(pub_data))
1022 +
1023 +print("..........",pub_RE_TOPIC)
1024 +
1025 + return g_mq:publish(pub_RE_TOPIC, json.encode(pub_data), 0, 0)
1026 +
1027 +end
1028 +
1029 +
1030 +
1031 +Get message in AWS
1032 +
1033 +
1034 +|
1035 +| |[[image:image-20220709163750-23.png]]