MQTT client test with W5200 ethernet shield

Dependents:   IBMIoTClientEthernetExample_W5200

Fork of W5500Interface by W5500-Ethernet-Interface Makers

Committer:
kaizen
Date:
Mon Sep 29 04:56:56 2014 +0000
Revision:
6:677dfa3984d1
Parent:
5:8aefaef88f79
Modified for using MQTT Protocol

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bongjun 0:e11e8793c3ce 1 // EthernetInterface for W5500 2014/8/20
Bongjun 1:8f4374f932b4 2 /*
Bongjun 1:8f4374f932b4 3 // sample usgae.
Bongjun 1:8f4374f932b4 4 // copy below code block to main code.
Bongjun 1:8f4374f932b4 5
Bongjun 1:8f4374f932b4 6 #if defined(TARGET_LPC1114)
Bongjun 1:8f4374f932b4 7 SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
Bongjun 1:8f4374f932b4 8 EthernetInterface eth(&spi, dp25, dp26); // spi, cs, reset
Bongjun 1:8f4374f932b4 9 wait(1); // 1 second for stable state
Bongjun 1:8f4374f932b4 10 #elif defined(TARGET_LPC1768)
Bongjun 1:8f4374f932b4 11 SPI spi(p11, p12, p13); // mosi, miso, sclk
Bongjun 1:8f4374f932b4 12 EthernetInterface eth(&spi, p14, p15); // spi, cs, reset
Bongjun 1:8f4374f932b4 13 wait(1); // 1 second for stable state
Bongjun 1:8f4374f932b4 14 #elif defined(TARGET_LPC11U68)
Bongjun 1:8f4374f932b4 15 SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk
Bongjun 1:8f4374f932b4 16 EthernetInterface eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
Bongjun 1:8f4374f932b4 17 spi.format(8,0); // 8bit, mode 0
Bongjun 1:8f4374f932b4 18 spi.frequency(7000000); // 7MHz
Bongjun 1:8f4374f932b4 19 wait(1); // 1 second for stable state
Bongjun 1:8f4374f932b4 20 #endif
Bongjun 1:8f4374f932b4 21
Bongjun 1:8f4374f932b4 22 eth.init(); //Use DHCP
Bongjun 1:8f4374f932b4 23 dbg.printf("init\r\n");
Bongjun 1:8f4374f932b4 24 eth.connect();
Bongjun 1:8f4374f932b4 25 dbg.printf("IP address: %s\r\n", eth.getIPAddress());
Bongjun 1:8f4374f932b4 26
Bongjun 1:8f4374f932b4 27 */
Bongjun 0:e11e8793c3ce 28
Bongjun 0:e11e8793c3ce 29 #include "EthernetInterface.h"
Bongjun 0:e11e8793c3ce 30 #include "DHCPClient.h"
Bongjun 0:e11e8793c3ce 31
Bongjun 0:e11e8793c3ce 32 EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
Bongjun 0:e11e8793c3ce 33 WIZnet_Chip(mosi, miso, sclk, cs, reset)
Bongjun 0:e11e8793c3ce 34 {
Bongjun 0:e11e8793c3ce 35 ip_set = false;
Bongjun 0:e11e8793c3ce 36 }
Bongjun 0:e11e8793c3ce 37
Bongjun 0:e11e8793c3ce 38 EthernetInterface::EthernetInterface(SPI* spi, PinName cs, PinName reset) :
Bongjun 0:e11e8793c3ce 39 WIZnet_Chip(spi, cs, reset)
Bongjun 0:e11e8793c3ce 40 {
Bongjun 0:e11e8793c3ce 41 ip_set = false;
Bongjun 0:e11e8793c3ce 42 }
Bongjun 0:e11e8793c3ce 43
kaizen 5:8aefaef88f79 44
Bongjun 0:e11e8793c3ce 45 int EthernetInterface::init()
Bongjun 0:e11e8793c3ce 46 {
Bongjun 0:e11e8793c3ce 47 dhcp = true;
Bongjun 0:e11e8793c3ce 48 //
Bongjun 0:e11e8793c3ce 49 //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 50 //
Bongjun 0:e11e8793c3ce 51 reset();
Bongjun 0:e11e8793c3ce 52 return 0;
Bongjun 0:e11e8793c3ce 53 }
Bongjun 0:e11e8793c3ce 54
Bongjun 0:e11e8793c3ce 55 int EthernetInterface::init(uint8_t * mac)
Bongjun 0:e11e8793c3ce 56 {
Bongjun 0:e11e8793c3ce 57 dhcp = true;
Bongjun 0:e11e8793c3ce 58 //
Bongjun 0:e11e8793c3ce 59 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 60 //
Bongjun 0:e11e8793c3ce 61 reset();
Bongjun 0:e11e8793c3ce 62 setmac();
Bongjun 0:e11e8793c3ce 63 return 0;
Bongjun 0:e11e8793c3ce 64 }
Bongjun 0:e11e8793c3ce 65
Bongjun 0:e11e8793c3ce 66 // add this function, because sometimes no needed MAC address in init calling.
Bongjun 0:e11e8793c3ce 67 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway)
Bongjun 0:e11e8793c3ce 68 {
Bongjun 0:e11e8793c3ce 69 dhcp = false;
Bongjun 0:e11e8793c3ce 70 //
Bongjun 0:e11e8793c3ce 71 //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 72 //
Bongjun 0:e11e8793c3ce 73 this->ip = str_to_ip(ip);
Bongjun 0:e11e8793c3ce 74 strcpy(ip_string, ip);
Bongjun 0:e11e8793c3ce 75 ip_set = true;
Bongjun 0:e11e8793c3ce 76 this->netmask = str_to_ip(mask);
Bongjun 0:e11e8793c3ce 77 this->gateway = str_to_ip(gateway);
Bongjun 0:e11e8793c3ce 78 reset();
Bongjun 0:e11e8793c3ce 79
Bongjun 0:e11e8793c3ce 80 // @Jul. 8. 2014 add code. should be called to write chip.
Bongjun 0:e11e8793c3ce 81 setip();
Bongjun 0:e11e8793c3ce 82
Bongjun 0:e11e8793c3ce 83 return 0;
Bongjun 0:e11e8793c3ce 84 }
Bongjun 0:e11e8793c3ce 85
Bongjun 0:e11e8793c3ce 86 int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
Bongjun 0:e11e8793c3ce 87 {
Bongjun 0:e11e8793c3ce 88 dhcp = false;
Bongjun 0:e11e8793c3ce 89 //
Bongjun 0:e11e8793c3ce 90 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 91 //
Bongjun 0:e11e8793c3ce 92 this->ip = str_to_ip(ip);
Bongjun 0:e11e8793c3ce 93 strcpy(ip_string, ip);
Bongjun 0:e11e8793c3ce 94 ip_set = true;
Bongjun 0:e11e8793c3ce 95 this->netmask = str_to_ip(mask);
Bongjun 0:e11e8793c3ce 96 this->gateway = str_to_ip(gateway);
Bongjun 0:e11e8793c3ce 97 reset();
Bongjun 0:e11e8793c3ce 98
Bongjun 0:e11e8793c3ce 99 // @Jul. 8. 2014 add code. should be called to write chip.
Bongjun 0:e11e8793c3ce 100 setmac();
Bongjun 0:e11e8793c3ce 101 setip();
Bongjun 0:e11e8793c3ce 102
Bongjun 0:e11e8793c3ce 103 return 0;
Bongjun 0:e11e8793c3ce 104 }
Bongjun 0:e11e8793c3ce 105
Bongjun 0:e11e8793c3ce 106 // Connect Bring the interface up, start DHCP if needed.
Bongjun 0:e11e8793c3ce 107 int EthernetInterface::connect()
Bongjun 0:e11e8793c3ce 108 {
Bongjun 0:e11e8793c3ce 109 if (dhcp) {
Bongjun 0:e11e8793c3ce 110 int r = IPrenew();
Bongjun 0:e11e8793c3ce 111 if (r < 0) {
Bongjun 0:e11e8793c3ce 112 return r;
Bongjun 0:e11e8793c3ce 113 }
Bongjun 0:e11e8793c3ce 114 }
Bongjun 0:e11e8793c3ce 115
Bongjun 0:e11e8793c3ce 116 if (WIZnet_Chip::setip() == false) return -1;
Bongjun 0:e11e8793c3ce 117 return 0;
Bongjun 0:e11e8793c3ce 118 }
Bongjun 0:e11e8793c3ce 119
Bongjun 0:e11e8793c3ce 120 // Disconnect Bring the interface down.
Bongjun 0:e11e8793c3ce 121 int EthernetInterface::disconnect()
Bongjun 0:e11e8793c3ce 122 {
Bongjun 0:e11e8793c3ce 123 if (WIZnet_Chip::disconnect() == false) return -1;
Bongjun 0:e11e8793c3ce 124 return 0;
Bongjun 0:e11e8793c3ce 125 }
Bongjun 0:e11e8793c3ce 126
Bongjun 0:e11e8793c3ce 127 char* EthernetInterface::getIPAddress()
Bongjun 0:e11e8793c3ce 128 {
Bongjun 0:e11e8793c3ce 129 uint32_t ip = reg_rd<uint32_t>(SIPR);
Bongjun 0:e11e8793c3ce 130 snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 0:e11e8793c3ce 131 return ip_string;
Bongjun 0:e11e8793c3ce 132 }
Bongjun 0:e11e8793c3ce 133
Bongjun 0:e11e8793c3ce 134 char* EthernetInterface::getNetworkMask()
Bongjun 0:e11e8793c3ce 135 {
Bongjun 0:e11e8793c3ce 136 uint32_t ip = reg_rd<uint32_t>(SUBR);
Bongjun 0:e11e8793c3ce 137 snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 0:e11e8793c3ce 138 return mask_string;
Bongjun 0:e11e8793c3ce 139 }
Bongjun 0:e11e8793c3ce 140
Bongjun 0:e11e8793c3ce 141 char* EthernetInterface::getGateway()
Bongjun 0:e11e8793c3ce 142 {
Bongjun 0:e11e8793c3ce 143 uint32_t ip = reg_rd<uint32_t>(GAR);
Bongjun 0:e11e8793c3ce 144 snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 0:e11e8793c3ce 145 return gw_string;
Bongjun 0:e11e8793c3ce 146 }
Bongjun 0:e11e8793c3ce 147
Bongjun 0:e11e8793c3ce 148 char* EthernetInterface::getMACAddress()
Bongjun 0:e11e8793c3ce 149 {
Bongjun 0:e11e8793c3ce 150 uint8_t mac[6];
Bongjun 0:e11e8793c3ce 151 reg_rd_mac(SHAR, mac);
Bongjun 0:e11e8793c3ce 152 snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Bongjun 0:e11e8793c3ce 153 return mac_string;
Bongjun 0:e11e8793c3ce 154 }
Bongjun 0:e11e8793c3ce 155
Bongjun 0:e11e8793c3ce 156 int EthernetInterface::IPrenew(int timeout_ms)
Bongjun 0:e11e8793c3ce 157 {
Bongjun 0:e11e8793c3ce 158 // printf("DHCP Started, waiting for IP...\n");
Bongjun 0:e11e8793c3ce 159 DHCPClient dhcp;
Bongjun 0:e11e8793c3ce 160 int err = dhcp.setup(timeout_ms);
Bongjun 0:e11e8793c3ce 161 if (err == (-1)) {
Bongjun 0:e11e8793c3ce 162 // printf("Timeout.\n");
Bongjun 0:e11e8793c3ce 163 return -1;
Bongjun 0:e11e8793c3ce 164 }
Bongjun 0:e11e8793c3ce 165 // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
Bongjun 0:e11e8793c3ce 166 ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
Bongjun 0:e11e8793c3ce 167 gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
Bongjun 0:e11e8793c3ce 168 netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
Bongjun 0:e11e8793c3ce 169 dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
Bongjun 0:e11e8793c3ce 170 return 0;
Bongjun 0:e11e8793c3ce 171 }