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 8.1
edited by Jim(Forgotten)
on 2022/07/09 16:48
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,328 @@
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:图片1.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 +1. **Test with MQTT.fx tool**
790 +
791 +Click “View Setting” to get the “Broker Adress”
792 +
793 +
794 +|
795 +| |[[image:image-20220709163750-14.png]]
796 +
797 +|
798 +| |[[image:image-20220709163750-15.png]]
799 +
800 +
801 +|
802 +| |[[image:image-20220709163750-16.png]]
803 +
804 +Create one connection in MQTT.fx tool, set broker port as 8883.
805 +
806 +Upload the CA File, Client Certificate File, Client Key File
807 +
808 +
809 +|
810 +| |[[image:image-20220709163750-17.png]]
811 +
812 +Publish message to topic “TEST”
813 +
814 +
815 +|
816 +| |[[image:image-20220709163750-18.png]]
817 +
818 +|
819 +| |[[image:image-20220709163750-19.png]]
820 +
821 +Click”Test”~-~-->”MQTT test client”~-~-->”Subscrible to a topic”, to get message publish from MQTT.fx tool.
822 +
823 +And we can also send message form AWS platform to MQTT.fx tool.
824 +
825 +
826 +|
827 +| |[[image:image-20220709163750-20.png]]
828 +
829 +1. **Configurate in CloudTool**
830 +
831 +Copy the same setting in MQTT.fx to MQTT configuration
832 +
833 +
834 +|
835 +| |[[image:image-20220709163750-21.png]]
836 +
837 + Add a lua script and copy the lua demo into it.
838 +
839 +
840 +|
841 +| |[[image:image-20220709163750-22.png]]
842 +
843 +sprint = print
844 +
845 +~-~-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)
846 +
847 +local MQTT_URL, MQTT_CLIENTID, MQTT_CFG, MQTT_LWT, MQTT_CART = mqtt.setup_cfg()
848 +
849 +~-~-publish to topics
850 +
851 +local pub_RE_TOPIC = string.format('TEST')
852 +
853 +~-~-Subscribe topics
854 +
855 +local Subscribe_RE_TOPIC1 = string.format('TEST')
856 +
857 +~-~-variable
858 +
859 +local last_time = 0
860 +
861 +~-~-Timing main function
862 +
863 +function aws.main()
864 +
865 + sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main start")
866 +
867 + if g_mq then
868 +
869 + if g_mq:isconnected() then
870 +
871 + send_Data()
872 +
873 + else
874 +
875 + if os.time() - last_time > 5 then
876 +
877 + last_time = os.time()
878 +
879 + mymqtt_connect()
880 +
881 + end
882 +
883 + end
884 +
885 + else
886 +
887 + mymqtt_init()
888 +
889 + end
890 +
891 + sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main end")
892 +
893 +end
894 +
895 +
896 +~-~- Initialize MQTT
897 +
898 +function mymqtt_init()
899 +
900 + sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID))
901 +
902 + g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENTID) ~-~- Create the object and declare it as a global variable
903 +
904 + if g_mq then
905 +
906 + g_mq:on("message", mymqtt_msg_callback) ~-~- Register to receive message callbacks
907 +
908 + sprint("mqtt init success")
909 +
910 + else
911 +
912 + sprint("mqtt init failed:", err)
913 +
914 + end
915 +
916 +end
917 +
918 +~-~- Connect to MQTT server
919 +
920 +function mymqtt_connect()
921 +
922 + sprint("mqtt connecting...")
923 +
924 + local stat, err = g_mq:connect(MQTT_CFG,MQTT_LWT, MQTT_CART)
925 +
926 + if stat == nil then
927 +
928 + sprint("mqtt connect failed:", err)
929 +
930 + return
931 +
932 + else
933 +
934 + sprint("mqtt connected")
935 +
936 + end
937 +
938 + g_mq:subscribe(TEST, 0)
939 +
940 +end
941 +
942 +~-~- Receive MQTT message callback function
943 +
944 +function mymqtt_msg_callback(topic, msg)
945 +
946 + print("topic:",topic)
947 +
948 + print("revdata:",msg)
949 +
950 + local revData = json.decode(msg)
951 +
952 + print (revData)
953 +
954 + if topic == Subscribe_RE_TOPIC1 then ~-~-Process topic information subscribed from the cloud
955 +
956 +if string.match(topic,Subscribe_RE_TOPIC1) then
957 +
958 + ~-~-if revData ~~= nil then
959 +
960 + for k,v in pairs (revData) do
961 +
962 + print("printing revdata after kv here")
963 +
964 + print (k,v)
965 +
966 + end
967 +
968 + print ("current state is",fanstate)
969 +
970 + ~-~-end
971 +
972 +end
973 +
974 +end
975 +
976 +end
977 +
978 +
979 +~-~-Get real-time data
980 +
981 +function getData()
982 +
983 + local jdata = {}
984 +
985 + local addr = bns_get_alldata()
986 +
987 + print(json.encode(addr))
988 +
989 + for i,v in pairs(addr) do
990 +
991 + if v[2] == 1 then
992 +
993 + jdata[v[3]] = v[4]
994 +
995 + end
996 +
997 + end
998 +
999 + return jdata
1000 +
1001 +end
1002 +
1003 +~-~-send data
1004 +
1005 +function send_Data()
1006 +
1007 + local pub_data =
1008 +
1009 + {
1010 +
1011 +123
1012 +
1013 +}
1014 +
1015 +sprint(json.encode(pub_data))
1016 +
1017 +print("..........",pub_RE_TOPIC)
1018 +
1019 + return g_mq:publish(pub_RE_TOPIC, json.encode(pub_data), 0, 0)
1020 +
1021 +end
1022 +
1023 +
1024 +
1025 +Get message in AWS
1026 +
1027 +
1028 +|
1029 +| |[[image:image-20220709163750-23.png]]