How do you convert ESP8266WiFi.h and WiFiClient.h to particle core?

I have an Arduino ESP8266 board, attach potentiometer. But I want to convert Arduino code to Particle Core. How do I do this? Build.Particle.io Community libraries can’t find ESP8266WiFi.h and WiFiClient.h.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>

const char* ssid = "ParticleRulez";
const char* password = "Core123";

WiFiServer server(26);
WiFiClient client;
int xPin = A0;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid,password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.print("Connected to "); 
  Serial.println(ssid);
  Serial.print("IP Address: "); 
  Serial.println(WiFi.localIP());
  // Start the TCP server
  server.begin();
}

void loop() {
  client = server.available();
  if (client){
    Serial.println("Client connected");
    while (client.connected()){
        int xValue = analogRead( xPin );
        Serial.println( xValue );
        client.print(xValue);
        client.print('\r');
        delay(10);
    }
  }
 }

You don’t need any extra libraries tu use WiFi on a Core. (is it actually a Core or are you using a Photon?)

Have a look at these inbuilt objects
https://docs.particle.io/reference/firmware/core/#wifi
https://docs.particle.io/reference/firmware/core/#tcpserver
https://docs.particle.io/reference/firmware/core/#tcpclient

This should be all you need to adapt your code.

On Particle devices you’d only set the WiFi credentials once and the device will remember them (7 sets for Core and 5 for Photon) to automatically reconnect (no need for WiFi.on()/WiFi.connect() in default AUTOMATIC mode.

So this should work

TCPServer server(26);
TCPClient client;
int xPin = A0;

void setup() {
  Serial.begin(115200);
  
  // credentials should already be stored and
  // auto-connect already have finished
  
  Serial.print("Connected to "); 
  Serial.println(WiFi.SSID());
  Serial.print("IP Address: "); 
  Serial.println(WiFi.localIP());
  // Start the TCP server
  server.begin();
}

void loop() {
  client = server.available();
  if (client){
    Serial.println("Client connected");
    while (client.connected()){
        int xValue = analogRead( xPin );
        Serial.println( xValue );
        client.print(xValue);
        client.print('\r');
        delay(10);
    }
  }
 }
2 Likes