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