-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenedetelli-nxt2wifi-config.c
136 lines (116 loc) · 3.1 KB
/
benedetelli-nxt2wifi-config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#pragma config(Sensor, S4, NXT2WIFI, sensorHighSpeed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*
* $Id: benedetelli-nxt2wifi-config.c 133 2013-03-10 15:15:38Z xander $
*/
/**
* benedettelli-nxt2wifi.h provides an API for Daniele's NXT2WiFi Sensor. This program
* allows you to configure the NXT2WIFI using the provided SSID and passphrase
*
* Changelog:
* - 0.1: Initial release
*
* Credits:
* - Big thanks to Daniele Benedettelli for providing me with the hardware necessary to write and test this.
*
* License: You may use this code as you wish, provided you give credit where it's due.
*
* THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER.
* Xander Soldaat (xander_at_botbench.com)
* 22 July 2012
* version 0.1
*/
#define __RS485_DEBUG__
#include "drivers/common.h"
#include "drivers/benedettelli-nxt2wifi.h"
// Use DHCP? You should.
bool useDHCP = true;
// Leave this to false, unless you know what you're doing.
bool useAdHoc = false;
// Set one of the security options below to true
bool useWPA = false;
bool useWPA2 = true;
bool useWEP104 = false;
bool useOpen = false;
string ipaddress = "192.168.0.55";
string netmask = "255.255.255.0";
string DNS1 = "192.168.0.1";
string DNS2 = "192.168.0.2";
string gateway = "192.168.0.1";
string ssid = "Bazinga!";
char *passphrase = "xammy4ever";
task main ()
{
// initialise the port, etc
RS485initLib();
nxtDisplayTextLine(0, "Stat: disconnected");
nxtDisplayTextLine(2, "-------------------");
N2WchillOut();
N2WsetDebug(true);
N2WchillOut();
// Disconnect if already connected
N2WDisconnect();
wait1Msec(100);
// Delete any pre-existing custom profiles and reset the device
N2WDelete();
N2WchillOut();
N2WReset();
wait1Msec(4000);
// enable DHCP
if (useDHCP)
{
N2WsetDHCP(true);
}
else
{
N2WsetDHCP(false);
N2WchillOut();
N2WsetIPAddress(ipaddress);
N2WchillOut();
N2WsetMask(netmask);
N2WchillOut();
N2WsetDNS1(DNS1);
N2WchillOut();
N2WsetDNS2(DNS2);
N2WchillOut();
N2WsetGateway(gateway);
}
wait1Msec(100);
// Enable or disable AdHoc
N2WsetAdHoc(useAdHoc);
wait1Msec(100);
// SSID to connect to
N2WsetSSID(ssid);
wait1Msec(100);
if (useWPA)
N2WSecurityWPAPassphrase(passphrase);
else if (useWPA2)
N2WSecurityWPA2Passphrase(passphrase);
else if (useWEP104)
N2WSecurityWEP104(passphrase);
else if (useOpen)
N2WSecurityOpen();
nxtDisplayTextLine(0, "Stat: Calculating");
nxtDisplayTextLine(3, "This can take up");
nxtDisplayTextLine(4, "to 30 seconds");
wait1Msec(100);
// Save this profile to the custom profile
N2WSave();
wait1Msec(100);
// Load the custom profile
N2WLoad();
wait1Msec(100);
N2WConnect(true);
nxtDisplayTextLine(0, "Stat: Connecting");
while (!N2WConnected())
wait1Msec(500);
wait1Msec(3000);
N2WgetIP(ipaddress);
nxtDisplayTextLine(3, "My IP address is");
nxtDisplayTextLine(4, ipaddress);
nxtDisplayTextLine(0, "Stat: Configured");
N2WchillOut();
N2WSave();
PlaySound(soundBeepBeep);
while(true) EndTimeSlice();
}