|
| 1 | +/* |
| 2 | + * Socket App |
| 3 | + * |
| 4 | + * A simple socket application / DHCP example sketch for the WiShield |
| 5 | + * Sample python server script for this sketch to connect with at bottom of sketch |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +// Requires APP_SOCKAPP, APP_UDPAPP and UIP_DHCP to be defined in apps-conf.h |
| 10 | +// APP_SOCKAPP - for the TCP sockets components of the sketch |
| 11 | +// APP_UDPAPP - for the UDP/DNS components of the sketch |
| 12 | +// UIP_DHCP - for the DHCP components of the sketch |
| 13 | + |
| 14 | +#include <WiShield.h> |
| 15 | +extern "C" { |
| 16 | + #include "uip.h" |
| 17 | +} |
| 18 | + |
| 19 | +// Wireless configuration parameters ---------------------------------------- |
| 20 | +unsigned char local_ip[] = {192,168,1,2}; // IP address of WiShield |
| 21 | +unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address |
| 22 | +unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network |
| 23 | +char ssid[] = {"ASYNCLABS"}; // max 32 bytes |
| 24 | +unsigned char security_type = 0; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2 |
| 25 | +unsigned char wireless_mode = 1; // 1==Infrastructure, 2==Ad-hoc |
| 26 | +unsigned char ssid_len; |
| 27 | +unsigned char security_passphrase_len; |
| 28 | + |
| 29 | +// WPA/WPA2 passphrase |
| 30 | +const prog_char security_passphrase[] PROGMEM = {"12345678"}; // max 64 characters |
| 31 | + |
| 32 | +// WEP 128-bit keys |
| 33 | +prog_uchar wep_keys[] PROGMEM = { |
| 34 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0 |
| 35 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1 |
| 36 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2 |
| 37 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3 |
| 38 | +}; |
| 39 | +// End of wireless configuration parameters ---------------------------------------- |
| 40 | + |
| 41 | + |
| 42 | +// global data |
| 43 | +boolean connectAndSendTCP = false; |
| 44 | +uip_ipaddr_t srvaddr; |
| 45 | + |
| 46 | +void setup() |
| 47 | +{ |
| 48 | + // Enable Serial output |
| 49 | + Serial.begin(57600); |
| 50 | + |
| 51 | + WiFi.init(); |
| 52 | + |
| 53 | + Serial.println("Start the DHCP query..."); |
| 54 | + uip_dhcp_request(); |
| 55 | +} |
| 56 | + |
| 57 | +void loop() |
| 58 | +{ |
| 59 | + if(true == connectAndSendTCP) { |
| 60 | + connectAndSendTCP = false; |
| 61 | + // Address of server to connect to |
| 62 | + uip_ipaddr(&srvaddr, 192,168,1,100); |
| 63 | + uip_connect(&srvaddr, HTONS(3333)); |
| 64 | + } |
| 65 | + |
| 66 | + WiFi.run(); |
| 67 | +} |
| 68 | + |
| 69 | +extern "C" { |
| 70 | + // Process UDP UIP_APPCALL events |
| 71 | + void udpapp_appcall(void) |
| 72 | + { |
| 73 | + uip_dhcp_run(); |
| 74 | + } |
| 75 | + |
| 76 | + // DHCP query complete callback |
| 77 | + void uip_dhcp_callback(const struct dhcp_state *s) |
| 78 | + { |
| 79 | + if(NULL != s) { |
| 80 | + // Set the received IP addr data into the uIP stack |
| 81 | + uip_sethostaddr(s->ipaddr); |
| 82 | + uip_setdraddr(s->default_router); |
| 83 | + uip_setnetmask(s->netmask); |
| 84 | + |
| 85 | + // Print the received data - its quick and dirty but informative |
| 86 | + Serial.print("DHCP IP : "); |
| 87 | + Serial.print(uip_ipaddr1(s->ipaddr), DEC); |
| 88 | + Serial.print("."); |
| 89 | + Serial.print(uip_ipaddr2(s->ipaddr), DEC); |
| 90 | + Serial.print("."); |
| 91 | + Serial.print(uip_ipaddr3(s->ipaddr), DEC); |
| 92 | + Serial.print("."); |
| 93 | + Serial.println(uip_ipaddr4(s->ipaddr), DEC); |
| 94 | + |
| 95 | + Serial.print("DHCP GATEWAY: "); |
| 96 | + Serial.print(uip_ipaddr1(s->default_router), DEC); |
| 97 | + Serial.print("."); |
| 98 | + Serial.print(uip_ipaddr2(s->default_router), DEC); |
| 99 | + Serial.print("."); |
| 100 | + Serial.print(uip_ipaddr3(s->default_router), DEC); |
| 101 | + Serial.print("."); |
| 102 | + Serial.println(uip_ipaddr4(s->default_router), DEC); |
| 103 | + |
| 104 | + Serial.print("DHCP NETMASK: "); |
| 105 | + Serial.print(uip_ipaddr1(s->netmask), DEC); |
| 106 | + Serial.print("."); |
| 107 | + Serial.print(uip_ipaddr2(s->netmask), DEC); |
| 108 | + Serial.print("."); |
| 109 | + Serial.print(uip_ipaddr3(s->netmask), DEC); |
| 110 | + Serial.print("."); |
| 111 | + Serial.println(uip_ipaddr4(s->netmask), DEC); |
| 112 | + |
| 113 | + Serial.print("DHCP DNS : "); |
| 114 | + Serial.print(uip_ipaddr1(s->dnsaddr), DEC); |
| 115 | + Serial.print("."); |
| 116 | + Serial.print(uip_ipaddr2(s->dnsaddr), DEC); |
| 117 | + Serial.print("."); |
| 118 | + Serial.print(uip_ipaddr3(s->dnsaddr), DEC); |
| 119 | + Serial.print("."); |
| 120 | + Serial.println(uip_ipaddr4(s->dnsaddr), DEC); |
| 121 | + } |
| 122 | + else { |
| 123 | + Serial.println("DHCP NULL FALLBACK"); |
| 124 | + } |
| 125 | + |
| 126 | + connectAndSendTCP = true; |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + char packet[] = "SocketAppDHCP"; |
| 131 | + |
| 132 | + void socket_app_appcall(void) |
| 133 | + { |
| 134 | + if(uip_closed() || uip_timedout()) { |
| 135 | + Serial.println("SA: closed / timedout"); |
| 136 | + uip_close(); |
| 137 | + return; |
| 138 | + } |
| 139 | + if(uip_poll()) { |
| 140 | + Serial.println("SA: poll"); |
| 141 | + } |
| 142 | + if(uip_aborted()) { |
| 143 | + Serial.println("SA: aborted"); |
| 144 | + } |
| 145 | + if(uip_connected()) { |
| 146 | + Serial.println("SA: connected / send"); |
| 147 | + uip_send(packet, strlen(packet)); |
| 148 | + } |
| 149 | + if(uip_acked()) { |
| 150 | + Serial.println("SA: acked"); |
| 151 | + uip_close(); |
| 152 | + } |
| 153 | + if(uip_newdata()) { |
| 154 | + Serial.println("SA: newdata"); |
| 155 | + } |
| 156 | + if(uip_rexmit()) { |
| 157 | + Serial.println("SA: rexmit"); |
| 158 | + uip_send(packet, strlen(packet)); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + // These uIP callbacks are unused for the purposes of this simple DHCP example |
| 163 | + // but they must exist. |
| 164 | + void socket_app_init(void) |
| 165 | + { |
| 166 | + } |
| 167 | + |
| 168 | + void udpapp_init(void) |
| 169 | + { |
| 170 | + } |
| 171 | + |
| 172 | + void dummy_app_appcall(void) |
| 173 | + { |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +/* |
| 178 | +
|
| 179 | +# -- Beginning of python server script |
| 180 | +
|
| 181 | +import socket |
| 182 | +
|
| 183 | +HOST = '' # Symbolic name meaning all available interfaces |
| 184 | +PORT = 3333 # Arbitrary non-privileged port |
| 185 | +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 186 | +s.bind((HOST, PORT)) |
| 187 | +s.listen(1) |
| 188 | +
|
| 189 | +try: |
| 190 | + while 1: |
| 191 | + conn, addr = s.accept() |
| 192 | + print 'Connected by', addr |
| 193 | + data = conn.recv(1024) |
| 194 | + if not data: |
| 195 | + continue |
| 196 | + print data |
| 197 | + conn.close() |
| 198 | +except: |
| 199 | + conn.close() |
| 200 | +
|
| 201 | +# -- End of python script |
| 202 | +
|
| 203 | +*/ |
| 204 | + |
0 commit comments