Hey guys,
So I've been working on my project, and hit a small snag. This code is running on a Adafruit Feather M0 Wi-Fi. It's getting stuck on
while (client.available()) {
char c = client.read();
data = data+c;
}
and I can't figure out why. It works perfectly when the code is in setup instead of it's own function. Could I have a little help? XD
#include <SPI.h>
#include <WiFi101.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(10,13,3,201); // numeric IP for Google (no DNS)
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;
void setup() {
WiFi.setPins(8,7,4,2);
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
Serial.println("Connected to wifi");
printWiFiStatus();
activateConnection();
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
//while(true);
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void activateConnection() {
String data = "";
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
String PostData = "uuid=abcd";
if (client.connect(server, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("POST /API/activate HTTP/1.1");
client.println("User-Agent: Arduino/1.0");
client.println("Host: 10.13.3.201");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(PostData.length());
client.println();
client.println(PostData);
client.println("Connection: close");
}
while (client.available()) {
char c = client.read();
data = data+c;
}
int i = data.length();
i = i - 27;
data.remove(0, i);
Serial.println(data);
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
}
}