/*Pi Pico W WiFi Station Demo picow-wifi-station.ino Use WiFi library to connect Pico W to WiFi in Station mode DroneBot Workshop 2022 https://dronebotworkshop.com */ // Include the WiFi Library #include #include "strava.h" // Replace with your network credentials const char* ssid = "MasarahHS"; const char* password = "testsetest"; // Set web server port number to 80 WiFiServer server(80); // Variable to store the HTTP request String header; String clientId; String clientSecret; String code; // Variable to store onboard LED state String picoLEDState = "off"; // Current time unsigned long currentTime = millis(); // Previous time unsigned long previousTime = 0; // Define timeout time in milliseconds (example: 2000ms = 2s) const long timeoutTime = 2000; void setup() { // Start Serial Monitor Serial.begin(115200); // Initialize the LED as an output pinMode(LED_BUILTIN, OUTPUT); // Set LED off digitalWrite(LED_BUILTIN, LOW); setup_wifi(); } void setup_wifi() { // Connect to Wi-Fi network with SSID and password WiFi.begin(ssid, password); // Display progress on Serial monitor while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.print("WiFi connected at IP Address "); Serial.println(WiFi.localIP()); // To-do: Print the IP to the Screen // Start Server server.begin(); } void loop() { WiFiClient client = server.available(); // Listen for incoming clients if (client) { Serial.println("New client found"); bool state = make_server(client); // Call make_server with the client if (state) { Serial.println("FOUND a CLIENT ID AND SECRET"); sendTokenRequest(clientId, clientSecret, code); } } } bool make_server(WiFiClient client) { if (client) { // If a new client connects, currentTime = millis(); previousTime = currentTime; Serial.println("New Client."); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected currentTime = millis(); if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // Switch the LED on and off if (header.indexOf("GET /led/on") >= 0) { Serial.println("LED on"); picoLEDState = "on"; digitalWrite(LED_BUILTIN, HIGH); } else if (header.indexOf("GET /led/off") >= 0) { Serial.println("LED off"); picoLEDState = "off"; digitalWrite(LED_BUILTIN, LOW); } // Display the HTML web page client.println(""); client.println(""); client.println(""); // CSS to style the on/off buttons and input fields client.println(""); // Web Page Heading client.println("

Strava Authentication

"); client.println("

1. Use the following Link https://www.strava.com/oauth/authorize?client_id=YOUR_ID&redirect_uri=http://localhost&response_type=code&scope=activity:write

"); // Input boxes for user input (client ID and client secret) client.println("

Enter Client ID:

"); client.println("

Enter Client Secret:

"); client.println("

Enter Code:

"); // Submit button for user input client.println("

"); client.println(""); client.println(""); // The HTTP response ends with another blank line client.println(); // Break out of the while loop break; } else { // if you got a newline, then clear currentLine currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } // Handle input request if (header.indexOf("GET /config") >= 0) { int clientIdPos = header.indexOf("clientId=") + 9; int clientIdEndPos = header.indexOf("&", clientIdPos); clientId = header.substring(clientIdPos, clientIdEndPos); int clientSecretPos = header.indexOf("clientSecret=") + 13; int clientSecretEndPos = header.indexOf("&", clientSecretPos); clientSecret = header.substring(clientSecretPos, clientSecretEndPos); int codePos = header.indexOf("code=") + 5; int codeEndPos = header.indexOf("HTTP/1.1"); code = header.substring(codePos, codeEndPos); Serial.println("Client ID: " + clientId); Serial.println("Client Secret: " + clientSecret); Serial.println("Code: " + code); // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); // Clear the header variable header = ""; return true; } // Clear the header variable header = ""; } // Close the connection if no valid data was received client.stop(); Serial.println("Client disconnected."); Serial.println(""); return false; }