[zumo-cc3200/zumo-cc3200.git] / src / Energia / libraries / ZumoCC3200 / examples / MasterSlave / apLoop.ino
1 /*
2 * ======== apLoop ========
3 * This sketch waits on either a button press or a
4 *
5 * The name and password of the network and the port number of the server
6 * (always at IP address 192.168.1.1) can be changed below.
7 */
9 #include <WiFi.h>
10 #include <Pushbutton.h>
11 #include <ti/sysbios/knl/Task.h>
13 #include <string.h>
15 /* name of the network and its password */
16 char ssid[] = "zumo_group";
17 char wifipw[] = "password";
19 /* zumo command port */
20 #define CMD_PORT 8080
22 /* IMU data port */
23 #define DATA_PORT 6000
25 /* Create a Pushbutton object for pin 12 (the Zumo user pushbutton pin) */
26 Pushbutton button(ZUMO_BUTTON);
28 IPAddress broadcastIP(192,168,1,255);
31 static bool waiting = true;
33 /* 1 is master, 0 is slave, -1 is unassigned */
34 int rank = -1;
36 static void sendIMUPacket(WiFiUDP Udp, IPAddress ip, int localPort);
38 //WiFiUDP UdpRX; /* UDP connection for receiving */
39 //WiFiUDP UdpTX; /* UDP connection for transmitting */
41 WiFiUDP Udp;
43 /*
44 * ======== apSetup ========
45 */
46 void apSetup()
47 {
48 Serial.begin(9600);
50 /* set priority of this task to be lower than other tasks */
51 //Task_setPri(Task_self(), 1);
53 // attempt to connect to group network:
54 Serial.print("Attempting to connect to Network named: ");
55 Serial.println(ssid);
56 WiFi.begin(ssid, wifipw);
58 while (waiting) {
60 /* if group network is found */
61 if(WiFi.status() == WL_CONNECTED){
62 Serial.println("Connected to the group network");
63 Serial.println("Waiting for an ip address");
65 while (WiFi.localIP() == INADDR_NONE) {
66 // print dots while we wait for an ip addresss
67 Serial.print(".");
68 delay(300);
69 }
71 Serial.println("\nIP Address obtained");
72 printWifiStatus();
74 /* this zumo is a slave */
75 rank = 0;
77 waiting = false;
78 }
79 /* if zumo button is pressed */
80 else if(button.getSingleDebouncedRelease()){
81 Serial.println("Button press detected");
82 /* startup a new network and get the first IP address: 192.168.1.1 */
83 Serial.print("Starting a new network: "); Serial.println(ssid);
84 WiFi.beginNetwork((char *)ssid, (char *)wifipw);
85 while (WiFi.localIP() == INADDR_NONE) {
86 Serial.print(".");
87 delay(300);
88 }
90 /* this zumo is the master */
91 rank = 1;
93 waiting = false;
94 }
95 else{
96 WiFi.begin(ssid, wifipw);
97 Serial.print(".");
98 delay(300);
99 }
100 }
102 Udp.begin(CMD_PORT);
103 }
105 /*
106 * ======== apLoop ========
107 */
109 void apLoop()
110 {
111 int packetSize = 0;
113 switch(rank){
114 case -1: /* unassigned case: should theoretically never happen */
115 Serial.println("rank was not assigned, please exit the program and try again");
116 break;
118 case 0: /* slave case: listen for motor commands from group network */
119 // if there's data available, read a packet
120 packetSize = Udp.parsePacket();
121 if (packetSize){
122 static char buffer[64] = {0};
124 Serial.print("Received packet from ");
125 IPAddress remoteIp = Udp.remoteIP();
126 Serial.print(remoteIp);
127 Serial.print(", port ");
128 Serial.println(Udp.remotePort());
130 /* if packet is from master */
131 if(Udp.remotePort() == CMD_PORT){
132 // read the packet into packetBufffer
133 int len = Udp.read(buffer, 64);
135 if (len > 0) buffer[len] = 0;
136 Serial.print("Contents: ");
137 Serial.println(buffer);
139 motorWASD = buffer[0];
140 }
142 }
143 break;
145 case 1: /* master case: listen for motor commands from processing, and multicast them over the group network */
146 // if there's data available, read a packet
147 packetSize = Udp.parsePacket();
148 if (packetSize){
149 static char buffer[64] = {0};
151 Serial.print("Received packet from ");
152 IPAddress remoteIp = Udp.remoteIP();
153 Serial.print(remoteIp);
154 Serial.print(", port ");
155 Serial.println(Udp.remotePort());
157 /* if packet is from processing */
158 if(Udp.remotePort() == DATA_PORT){
159 // read the packet into packetBufffer
160 int len = Udp.read(buffer, 64);
162 if (len > 0) buffer[len] = 0;
163 Serial.print("Contents: ");
164 Serial.println(buffer);
166 motorWASD = buffer[0];
168 /* broadcast the command */
169 Udp.beginPacket(broadcastIP, CMD_PORT);
170 Udp.write(motorWASD);
171 Udp.endPacket();
172 }
173 }
174 /* broadcast the command */
175 Udp.beginPacket(broadcastIP, CMD_PORT);
176 Udp.write("hello slaves");
177 Udp.endPacket();
178 break;
179 }
180 }
182 /*
183 * ======== doWASD ========
184 */
185 static void sendIMUPacket(WiFiUDP Udp, IPAddress ip, int localPort)
186 {
187 static char report[80];
189 /* send current IMU data */
190 System_snprintf(report, sizeof(report),
191 "A: %6d %6d %6d G: %6d %6d %6d M: %6d %6d %6d",
192 imuCompass.a.x, imuCompass.a.y, imuCompass.a.z,
193 imuGyro.g.x, imuGyro.g.y, imuGyro.g.z,
194 imuCompass.m.x, imuCompass.m.y, imuCompass.m.z);
196 Udp.beginPacket(ip, localPort);
197 Udp.write(report);
198 Udp.endPacket();
199 }
201 void printWifiStatus() {
202 // print the SSID of the network you're attached to:
203 Serial.print("SSID: ");
204 Serial.println(WiFi.SSID());
206 // print your WiFi IP address:
207 IPAddress ip = WiFi.localIP();
208 Serial.print("IP Address: ");
209 Serial.println(ip);
211 // print the received signal strength:
212 long rssi = WiFi.RSSI();
213 Serial.print("signal strength (RSSI):");
214 Serial.print(rssi);
215 Serial.println(" dBm");
216 }