Using Feather MO with WINC1500 and PlatformIO

As the title says I am trying to use the wifi module by importing arduino’s WiFi101 library. The problem is that I am getting a lot if weird import errors, where the compiler is highlighting things that should have been already imported when writing #include<WiFi101.h>. Furthermore I am getting an error at the library lever, more precisely in the WiFiServer.h file line 31 2h3n talking about the socket.

This runs with no problem on the Arduino IDE.

this is the code I a trying to run.

#ifndef WIFI_H
#define WIFI_H

#include <Arduino.h>
#include <WiFi101.h>
#define WINC_CS   8
#define WINC_IRQ  7
#define WINC_RST  4
#define WINC_EN   2

// Feather M0 WiFi (WINC1500) pins
//const int WINC_CS  = 8, WINC_IRQ = 7, WINC_RST = 4, WINC_EN = 2;

const char ssid[] = "FeatherAP";
const char pass[] = "test1234";     // >= 8 chars for WPA2

String ipToString(const IPAddress& ip) {
  return String(ip[0]) + "." + String(ip[1]) + "." + String(ip[2]) + "." + String(ip[3]);
}

void handleRoot(WiFiClient& client){
  // Minimal well-formed HTTP response
  const char body[] = "Initial Page";

  client.print("HTTP/1.1 200 OK\r\n");
  client.print("Content-Type: text/html\r\n");
  client.print("Connection: close\r\n");
  client.print("Content-Length: "); client.print(sizeof(body) - 1); client.print("\r\n\r\n");
  client.print(body);
    delay(1);
}

void handleForward(WiFiClient& client){
  moveForward();
    // Minimal well-formed HTTP response
  const char body[] = "Moved Forward";

  client.print("HTTP/1.1 200 OK\r\n");
  client.print("Content-Type: text/html\r\n");
  client.print("Connection: close\r\n");
  client.print("Content-Length: "); client.print(sizeof(body) - 1); client.print("\r\n\r\n");
  client.print(body);
    delay(1);
}

void handleBackward(WiFiClient& client){
  moveBackward();
    // Minimal well-formed HTTP response
  const char body[] = "Moved Backward";

  client.print("HTTP/1.1 200 OK\r\n");
  client.print("Content-Type: text/html\r\n");
  client.print("Connection: close\r\n");
  client.print("Content-Length: "); client.print(sizeof(body) - 1); client.print("\r\n\r\n");
  client.print(body);
    delay(1);
}

void handleMoveLeft(WiFiClient& client){
  moveLeft();
    // Minimal well-formed HTTP response
  const char body[] = "Moved Left";

  client.print("HTTP/1.1 200 OK\r\n");
  client.print("Content-Type: text/html\r\n");
  client.print("Connection: close\r\n");
  client.print("Content-Length: "); client.print(sizeof(body) - 1); client.print("\r\n\r\n");
  client.print(body);
    delay(1);
}

void handleMoveRight(WiFiClient& client){
  moveRight();
    // Minimal well-formed HTTP response
  const char body[] = "Moved Right";

  client.print("HTTP/1.1 200 OK\r\n");
  client.print("Content-Type: text/html\r\n");
  client.print("Connection: close\r\n");
  client.print("Content-Length: "); client.print(sizeof(body) - 1); client.print("\r\n\r\n");
  client.print(body);
    delay(1);
}

void handleTurnRight(WiFiClient& client){
  turnRight();
    // Minimal well-formed HTTP response
  const char body[] = "Turned Right";

  client.print("HTTP/1.1 200 OK\r\n");
  client.print("Content-Type: text/html\r\n");
  client.print("Connection: close\r\n");
  client.print("Content-Length: "); client.print(sizeof(body) - 1); client.print("\r\n\r\n");
  client.print(body);
    delay(1);
}

void handleTurnLeft(WiFiClient& client){
  turnLeft();
    // Minimal well-formed HTTP response
  const char body[] = "Turned Left";

  client.print("HTTP/1.1 200 OK\r\n");
  client.print("Content-Type: text/html\r\n");
  client.print("Connection: close\r\n");
  client.print("Content-Length: "); client.print(sizeof(body) - 1); client.print("\r\n\r\n");
  client.print(body);
    delay(1);
}

void handleStop(WiFiClient& client){
  stopAllMotors();
    // Minimal well-formed HTTP response
  const char body[] = "Stopped all motors";

  client.print("HTTP/1.1 200 OK\r\n");
  client.print("Content-Type: text/html\r\n");
  client.print("Connection: close\r\n");
  client.print("Content-Length: "); client.print(sizeof(body) - 1); client.print("\r\n\r\n");
  client.print(body);
    delay(1);
}
void route(WiFiClient& c,const String& path,const String& q){
  if(path=="/"||path=="") { handleRoot(c); return; }
  if(path=="/f")          { handleForward(c); return; }
  if(path=="/b")          { handleBackward(c); return; }
  if(path=="/ml")         { handleMoveLeft(c); return; }
  if(path=="/mr")         { handleMoveRight(c); return; }
  if(path=="/tr")         { handleTurnRight(c); return; }
  if(path=="/tl")         { handleTurnLeft(c); return; }
  if(path=="/stop")       { handleStop(c); return;  }
  // sendText(c,"404\n");
}

void serve(WiFiClient& c){
  c.setTimeout(1500);
  String rl=c.readStringUntil('\n');        // "GET /path?query HTTP/1.1"
  int sp1=rl.indexOf(' '), sp2=rl.indexOf(' ',sp1+1);
  String uri=(sp1>0&&sp2>sp1)?rl.substring(sp1+1,sp2):"/";
  int q=uri.indexOf('?'); String pth=(q>=0)?uri.substring(0,q):uri; String qry=(q>=0)?uri.substring(q+1):"";
  while(true){ String h=c.readStringUntil('\n'); if(h.length()==0||h=="\r") break; } // headers
  route(c,pth,qry);
}

void wifiSetup(){
  WiFiServer server(80);
  WiFi.setPins(WINC_CS, WINC_IRQ, WINC_RST, WINC_EN);

  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WINC1500 not detected"); while (1) {}
  }

  Serial.print("FW: "); Serial.println(WiFi.firmwareVersion());

  Serial.println("Starting AP…");
  int s = WiFi.beginAP(ssid, pass, 6);            // WPA2, ch 6
  if (s != WL_AP_LISTENING) {
    Serial.print("WPA2 AP failed ("); Serial.print(s); Serial.println("). Trying OPEN…");
    s = WiFi.beginAP(ssid, 6);                    // OPEN AP fallback
    if (s != WL_AP_LISTENING) { Serial.println("AP failed"); while (1) {} }
  }

  delay(8000); // let AP + DHCP come up
  Serial.print("AP IP: "); Serial.println(ipToString(WiFi.localIP())); // usually 192.168.1.1

}

#endif

Why use an include guard in your cpp code? That’s for headers only.

What platformio.ini are you trying to compile this with?

well because it is a header file called wifi.h used in an other cpp file

here is the cpp file.

#include <Arduino.h>
#include "motor.h"
#include "sensors.h"
#include "wifi.h"

extern WiFiServer server;


char lastMotionCmd = 'x';      // Stores the last direction command (e.g., 'f' for forward)
// --- Print Available Commands to the Serial Monitor ---
void printInstructions() {
  Serial.println("\nEnter a command:");
  Serial.println("f = forward");
  Serial.println("b = backward");
  Serial.println("l = move left");
  Serial.println("r = move right");
  Serial.println("q = rotate counter clockwise, left (CCW)");
  Serial.println("e = rotate clockwise, right (CW)");
  Serial.println("x = stop all motors");
  Serial.println("d = get distance");
  Serial.println("s### = set speed (e.g., s200 for speed = 200)");
}




// --- Arduino Setup Function ---
// Runs once when the board powers up or resets
void setup() {

  Serial.begin(115200);
  
  wifiSetup();
  

  // Initialize all four motors
  setupMotor(FL_PWM, FL_DIR);
  setupMotor(FR_PWM, FR_DIR);
  setupMotor(BL_PWM, BL_DIR);
  setupMotor(BR_PWM, BR_DIR);

  //Add your code to control the other motors.

  // Stop all motors initially
  stopAllMotors();

  // Initialize ir
  pinMode(PIN_IR_LEFT_DIGITAL, INPUT);
  pinMode(PIN_IR_RIGHT_DIGITAL, INPUT);
  pinMode(PIN_IR_LEFT_ANALOG, INPUT);
  pinMode(PIN_IR_RIGHT_ANALOG, INPUT);

  // Initialize Ultrasound
  pinMode(PIN_ULTRASOUND_TRIGGER, OUTPUT);
  pinMode(PIN_ULTRASOUND_ECHO, INPUT);

  //Initialize Encoder
  pinMode(PIN_ENC_FR_A,INPUT);
  pinMode(PIN_ENC_FR_B,INPUT);
  attachInterrupt(digitalPinToInterrupt(PIN_ENC_FR_A),updateEncoder,RISING);

  server.begin();

  delay(1000); 
}

// --- Main Loop ---
// Keeps running in a loop after setup() finishes
void loop() {
  
  int pos = 0; 
  noInterrupts();
  pos = position;
  interrupts();


  // Check if data is available from Serial (USB) input
  if (Serial.available()) {
    // Read input string from user until newline
    String input = Serial.readStringUntil('\n');
    input.trim();  // Remove whitespace and newline characters

    

    Serial.print("Command: ");
    Serial.println(input);

    // --- Handle Speed Change (e.g., "s150") ---
    if (input.charAt(0) == 's') {
      int newSpeed = input.substring(1).toInt();  // Extract number after 's'

      // Speed must be in the 0–255 range
      if (newSpeed >= 0 && newSpeed <= 255) {
        Serial.print("Received: Setting speed to ");
        Serial.println(newSpeed);
        motorSpeed = newSpeed;

        // Repeat last movement with new speed
        input = String(lastMotionCmd);  // Reuse the last motion command
      } else {
        Serial.println("Received: Invalid speed value (0–255 allowed)");
        return;
      }
    }


    
    // Extract the actual command (first character)
    char cmd = input.charAt(0);

    // Save the motion command (so we can reuse it later)
    if (cmd == 'f' || cmd == 'b' || cmd == 'l' || cmd == 'r' ||
        cmd == 'q' || cmd == 'e' || cmd == 'x' || cmd == 'd') {
      lastMotionCmd = cmd;
    }
    // --- Motion Commands Switch ---
    switch (cmd) {
      case 'f': moveForward(); break;
      case 'b': moveBackward(); break;
      case 'l': moveLeft(); break;
      case 'r': moveRight(); break;
      case 'q': turnLeft(); break;
      case 'e': turnRight(); break;
      case 'x':
        stopAllMotors();  // Stop everything
        Serial.println("Stopped.");
        break;
      case 'd': getDistance(); break; 
      default:
        Serial.println("Unknown command.");  // Unknown input
        printInstructions();                 // Show help again
        break;
    }

    if (input.length() == 0){
      updateIR();

      Serial.println();
      Serial.print("IR Left Digital : ");
      Serial.println(irLeftDigital);
      Serial.print("IR Left Analog : ");
      Serial.println(irLeftAnalog);
      Serial.print("IR Right Digital : ");
      Serial.println(irRightDigital);
      Serial.print("IR Right Analog : ");
      Serial.println(irRightAnalog);
      Serial.println();
      Serial.println("Front right encoder value :");
      Serial.println(pos);

    } // Ignore empty input
  } else{
    WiFiClient client = server.available();
    if (!client) return;

    client.setTimeout(2000); // 2s read timeout
    serve(client);
    client.stop();
  }
}


and here is the .ini file

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:adafruit_feather_m0]
platform = atmelsam
board = adafruit_feather_m0
framework = arduino
lib_deps = arduino-libraries/WiFi101
upload_port = /dev/cu.usbmodem101
monitor_speed = 115200