示教机械臂

Dependencies:   MQTT SDFileSystem WIZnet_Library mbed

Fork of wmx_laser_copy by w mx

Committer:
xmwmx
Date:
Fri Aug 03 10:06:33 2018 +0000
Revision:
14:350e03215b0e
Parent:
11:568789ce1397
??????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zhangyx 2:a50b794b8ede 1 #include "mbed.h"
zhangyx 2:a50b794b8ede 2 #include "WIZnetInterface.h"
zhangyx 2:a50b794b8ede 3 #include "MQTTSocket.h"
zhangyx 2:a50b794b8ede 4 #include "MQTTClient.h"
zhangyx 2:a50b794b8ede 5 #include "networking.h"
zhangyx 2:a50b794b8ede 6
zhangyx 2:a50b794b8ede 7 extern Serial pc;
zhangyx 2:a50b794b8ede 8 //W5500接线 mosi,miso,sclk,cs,reset
zhangyx 2:a50b794b8ede 9 WIZnetInterface wiz(PB_5,PB_4,PB_3,PC_14,NC);
zhangyx 2:a50b794b8ede 10 //节点名称任取
xmwmx 11:568789ce1397 11 #define NODE_NAME "mechanicalarm"
zhangyx 2:a50b794b8ede 12 //接在同一子网下的设备MAC地址必须不同
zhangyx 2:a50b794b8ede 13 uint8_t mac_addr[6]={0x50,0x51,0x50,0x00,0x00,0x01};
zhangyx 2:a50b794b8ede 14
zhangyx 2:a50b794b8ede 15 recv_ctl_cb g_recv_cb;
zhangyx 2:a50b794b8ede 16 const char* (*g_actuators)[2];
zhangyx 2:a50b794b8ede 17 static Timer g_timer;
zhangyx 2:a50b794b8ede 18
zhangyx 2:a50b794b8ede 19 void disable_LSE() //调用此函数将 PC_14, PC_15 用作普通I/O
zhangyx 2:a50b794b8ede 20 {
zhangyx 2:a50b794b8ede 21 RCC_OscInitTypeDef OscInitStruct;
zhangyx 2:a50b794b8ede 22 HAL_RCC_GetOscConfig(&OscInitStruct);
zhangyx 2:a50b794b8ede 23 // pc.printf("%u %u %u %u\r\n",OscInitStruct.HSEState,OscInitStruct.LSEState,OscInitStruct.HSIState,OscInitStruct.LSIState);
zhangyx 2:a50b794b8ede 24
zhangyx 2:a50b794b8ede 25 // Enable access to Backup domain
zhangyx 2:a50b794b8ede 26 HAL_PWR_EnableBkUpAccess();
zhangyx 2:a50b794b8ede 27 // Reset Backup domain
zhangyx 2:a50b794b8ede 28 __HAL_RCC_BACKUPRESET_FORCE();
zhangyx 2:a50b794b8ede 29 __HAL_RCC_BACKUPRESET_RELEASE();
zhangyx 2:a50b794b8ede 30 // Disable access to Backup domain
zhangyx 2:a50b794b8ede 31 HAL_PWR_DisableBkUpAccess();
zhangyx 2:a50b794b8ede 32
zhangyx 2:a50b794b8ede 33 OscInitStruct.LSEState=RCC_LSE_OFF;
zhangyx 2:a50b794b8ede 34 HAL_RCC_OscConfig(&OscInitStruct);
zhangyx 2:a50b794b8ede 35
zhangyx 2:a50b794b8ede 36 HAL_RCC_GetOscConfig(&OscInitStruct);
zhangyx 2:a50b794b8ede 37 // pc.printf("%u %u %u %u\r\n",OscInitStruct.HSEState,OscInitStruct.LSEState,OscInitStruct.HSIState,OscInitStruct.LSIState);
zhangyx 2:a50b794b8ede 38 }
zhangyx 2:a50b794b8ede 39
zhangyx 2:a50b794b8ede 40
zhangyx 2:a50b794b8ede 41 void meta_report(MClient& client, const char* ns, const char* type,
zhangyx 2:a50b794b8ede 42 const char* payload = NULL, size_t payload_len = 0,
zhangyx 2:a50b794b8ede 43 bool retain = false, MQTT::QoS qos = MQTT::QOS1){
zhangyx 2:a50b794b8ede 44 char topic[64];
zhangyx 2:a50b794b8ede 45 sprintf(topic, "/%s/" NODE_NAME "/%s", ns, type);
zhangyx 2:a50b794b8ede 46 int ret = client.publish(topic, (void*)payload, payload_len, qos, retain);
zhangyx 2:a50b794b8ede 47 //pc.printf("client.publish()=%d\r\n",ret);
zhangyx 2:a50b794b8ede 48 }
zhangyx 2:a50b794b8ede 49 void messageArrived(MQTT::MessageData& md)
zhangyx 2:a50b794b8ede 50 {
zhangyx 2:a50b794b8ede 51 MQTT::Message &message = md.message;
xmwmx 9:a3f9faa9ca1f 52 //pc.printf("messageArrived %d,%d\r\n", md.topicName.lenstring.len, message.payloadlen);
zhangyx 2:a50b794b8ede 53
zhangyx 2:a50b794b8ede 54 // char buf[64];
zhangyx 2:a50b794b8ede 55 // int value, len = sizeof(buf)-1;
zhangyx 2:a50b794b8ede 56 // if(message.payloadlen < len)
zhangyx 2:a50b794b8ede 57 // len = message.payloadlen;
zhangyx 2:a50b794b8ede 58 // memcpy(buf, message.payload, len);
zhangyx 2:a50b794b8ede 59 // buf[len] = '\0';
zhangyx 2:a50b794b8ede 60 // sscanf(buf, "%d", &value);
zhangyx 2:a50b794b8ede 61 // pc.printf("received %d\r\n", value);
zhangyx 2:a50b794b8ede 62
zhangyx 2:a50b794b8ede 63 char* payload = new char[message.payloadlen+1];
zhangyx 2:a50b794b8ede 64 if(!payload)
zhangyx 2:a50b794b8ede 65 return;
zhangyx 2:a50b794b8ede 66 memcpy(payload, message.payload, message.payloadlen);
zhangyx 2:a50b794b8ede 67 payload[message.payloadlen]='\0';
zhangyx 2:a50b794b8ede 68
zhangyx 2:a50b794b8ede 69 char* topic = new char[md.topicName.lenstring.len+1];
zhangyx 2:a50b794b8ede 70 if(!topic){
zhangyx 2:a50b794b8ede 71 delete[] payload;
zhangyx 2:a50b794b8ede 72 return;
zhangyx 2:a50b794b8ede 73 }
zhangyx 2:a50b794b8ede 74 memcpy(topic, md.topicName.lenstring.data, md.topicName.lenstring.len);
zhangyx 2:a50b794b8ede 75 topic[md.topicName.lenstring.len]='\0';
zhangyx 2:a50b794b8ede 76
zhangyx 2:a50b794b8ede 77 char *pch = strtok (topic,"/");
zhangyx 2:a50b794b8ede 78 for (int tok=0; tok<2 && pch != NULL; tok++)
zhangyx 2:a50b794b8ede 79 {
zhangyx 2:a50b794b8ede 80 // pc.printf ("%s\r\n",pch);
zhangyx 2:a50b794b8ede 81 pch = strtok (NULL, "/");
zhangyx 2:a50b794b8ede 82 }
zhangyx 2:a50b794b8ede 83 if(pch)
zhangyx 2:a50b794b8ede 84 g_recv_cb(pch, payload);
zhangyx 2:a50b794b8ede 85 delete[] topic;
zhangyx 2:a50b794b8ede 86 delete[] payload;
zhangyx 2:a50b794b8ede 87 }
zhangyx 2:a50b794b8ede 88
zhangyx 2:a50b794b8ede 89 void publish_value(MClient &client, const char *topic, const char *buf)
zhangyx 2:a50b794b8ede 90 {
zhangyx 4:ae6f380a5b41 91 meta_report(client, "values",topic,buf,strlen(buf),false);
zhangyx 2:a50b794b8ede 92 }
zhangyx 2:a50b794b8ede 93
zhangyx 2:a50b794b8ede 94 void buildCapability(char *out, const char* infoList[][2])
zhangyx 2:a50b794b8ede 95 {
zhangyx 2:a50b794b8ede 96 out[0] = '\0';
zhangyx 2:a50b794b8ede 97 for (int i = 0; infoList[i][0]; ++i)
zhangyx 2:a50b794b8ede 98 {
zhangyx 2:a50b794b8ede 99 strcat(out, infoList[i][0]);
zhangyx 2:a50b794b8ede 100 strcat(out, ",");
zhangyx 2:a50b794b8ede 101 strcat(out, infoList[i][1]);
zhangyx 2:a50b794b8ede 102 strcat(out, "\\n");
zhangyx 2:a50b794b8ede 103 }
zhangyx 2:a50b794b8ede 104 }
zhangyx 2:a50b794b8ede 105
zhangyx 2:a50b794b8ede 106 int networking_init(MQTTSocket &sock, MClient &client, const char *broker,const char* sensors[][2], const char* actuators[][2], recv_ctl_cb cb) {
zhangyx 2:a50b794b8ede 107 int ret;
zhangyx 2:a50b794b8ede 108 g_timer.start();
zhangyx 2:a50b794b8ede 109 disable_LSE(); //free LSE pins
zhangyx 2:a50b794b8ede 110 wiz.init(mac_addr);
zhangyx 2:a50b794b8ede 111 pc.printf("DHCP...\r\n");
zhangyx 2:a50b794b8ede 112 wiz.connect();
zhangyx 2:a50b794b8ede 113 pc.printf("IP: %s\r\n", wiz.getIPAddress());
zhangyx 2:a50b794b8ede 114
zhangyx 2:a50b794b8ede 115 srand(rand()^g_timer.read_us());
zhangyx 2:a50b794b8ede 116
zhangyx 2:a50b794b8ede 117 ret = sock.connect((char*)broker,1883);
zhangyx 2:a50b794b8ede 118 if(ret != 0){
zhangyx 2:a50b794b8ede 119 pc.printf("failed to connect to TCP server\r\n");
zhangyx 2:a50b794b8ede 120 return 1;
zhangyx 2:a50b794b8ede 121 }
zhangyx 2:a50b794b8ede 122 pc.printf("sock.connect()=%d\r\n",ret);
zhangyx 2:a50b794b8ede 123
zhangyx 2:a50b794b8ede 124 srand(rand()^g_timer.read_us());
zhangyx 2:a50b794b8ede 125
zhangyx 2:a50b794b8ede 126 ret = client.connect();
zhangyx 2:a50b794b8ede 127 if(ret != 0){
zhangyx 2:a50b794b8ede 128 pc.printf("MQTT connect failed\r\n");
zhangyx 2:a50b794b8ede 129 return 1;
zhangyx 2:a50b794b8ede 130 }
zhangyx 2:a50b794b8ede 131 pc.printf("client.connect()=%d\r\n",ret);
zhangyx 2:a50b794b8ede 132
zhangyx 2:a50b794b8ede 133
zhangyx 2:a50b794b8ede 134 ret = client.subscribe("/control/" NODE_NAME "/+", MQTT::QOS1, messageArrived);
zhangyx 2:a50b794b8ede 135 pc.printf("sock.subscribe()=%d\r\n", ret);
zhangyx 2:a50b794b8ede 136
zhangyx 2:a50b794b8ede 137 g_recv_cb = cb;
zhangyx 2:a50b794b8ede 138 g_actuators = actuators;
zhangyx 2:a50b794b8ede 139
zhangyx 2:a50b794b8ede 140 char * capabilities = new char[128];
zhangyx 2:a50b794b8ede 141 if(!capabilities){
zhangyx 2:a50b794b8ede 142 pc.printf("failed to alloc memory\r\n");
zhangyx 2:a50b794b8ede 143 return 1;
zhangyx 2:a50b794b8ede 144 }
zhangyx 2:a50b794b8ede 145 //for (int i = 0; actuators[i][0]; ++i){
zhangyx 2:a50b794b8ede 146 // sprintf(capabilities,"/control/" NODE_NAME "/+",actuators[i][0]);
zhangyx 2:a50b794b8ede 147 // ret = client.subscribe(capabilities, MQTT::QOS1, messageArrived);
zhangyx 2:a50b794b8ede 148 // pc.printf("sock.subscribe(%s)=%d\r\n", capabilities, ret);
zhangyx 2:a50b794b8ede 149 // }
zhangyx 2:a50b794b8ede 150
zhangyx 2:a50b794b8ede 151 //节点上线消息
zhangyx 2:a50b794b8ede 152 meta_report(client, "events","online");
zhangyx 2:a50b794b8ede 153
zhangyx 2:a50b794b8ede 154 //报告所有可接受的控制指令
zhangyx 2:a50b794b8ede 155 buildCapability(capabilities, actuators);
zhangyx 2:a50b794b8ede 156 meta_report(client, "capability","control", capabilities, strlen(capabilities), true);
zhangyx 2:a50b794b8ede 157 //报告所有的传感器
zhangyx 2:a50b794b8ede 158 buildCapability(capabilities, sensors);
zhangyx 2:a50b794b8ede 159 meta_report(client, "capability","values", capabilities, strlen(capabilities), true);
zhangyx 2:a50b794b8ede 160 delete[] capabilities;
zhangyx 2:a50b794b8ede 161
zhangyx 2:a50b794b8ede 162 pc.printf("Initialization done.\r\n");
zhangyx 2:a50b794b8ede 163
zhangyx 2:a50b794b8ede 164 return 0;
zhangyx 2:a50b794b8ede 165 }