PlatformIO Community

Can't compile examples prototyping with sonar proximity sensor on ESP32

Wanted to try this example Prototyping with Sonar Proximity Sensor - MATLAB & Simulink - MathWorks Australia on an ESP32. I’m building this in PlatformIO (VSC) and get various error messages. When I first imported the code to VSC it complained that it couldn’t find WiFi.h so I included it in platformio.ini as follows.

lib_deps =
     arduino-libraries/WiFi101 @ 0.16.1

NOTE: I have not compiled this in the Arduino IDE before so don’t know whether this code compiles or it’s an issue with my PlatformIO install/setup

Building in release mode
Compiling .pio\build\nodemcu-32s\src\watertanksensor.ino.cpp.o
Generating partitions .pio\build\nodemcu-32s\partitions.bin
Compiling .pio\build\nodemcu-32s\lib375\WiFi101\WiFi.cpp.o
Compiling .pio\build\nodemcu-32s\lib375\WiFi101\WiFiClient.cpp.o
Compiling .pio\build\nodemcu-32s\lib375\WiFi101\WiFiMDNSResponder.cpp.o
Compiling .pio\build\nodemcu-32s\lib375\WiFi101\WiFiSSLClient.cpp.o
Compiling .pio\build\nodemcu-32s\lib375\WiFi101\WiFiServer.cpp.o
Compiling .pio\build\nodemcu-32s\lib375\WiFi101\WiFiUdp.cpp.o
Compiling .pio\build\nodemcu-32s\lib375\WiFi101\bsp\source\nm_bsp_arduino.c.o
In file included from C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/stdio.h:29:0,
                 from C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\cores\esp32/Arduino.h:27,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\utility/WiFiSocket.h:28,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiClient.cpp:20:
C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/sys/unistd.h:30:9: error: conflicting declaration of C function 'int close(int)'
 int     _EXFUN(close, (int __fildes ));
         ^
In file included from .pio\libdeps\nodemcu-32s\WiFi101\src\utility/WiFiSocket.h:24:0,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiClient.cpp:20:
.pio\libdeps\nodemcu-32s\WiFi101\src/socket/include/socket.h:1839:15: note: previous declaration 'sint8 close(SOCKET)'
 NMI_API sint8 close(SOCKET sock);
               ^
In file included from C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/time.h:10:0,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFi.cpp:28:
C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/sys/unistd.h:30:9: error: conflicting declaration of C function 'int close(int)'
 int     _EXFUN(close, (int __fildes ));
         ^
In file included from .pio\libdeps\nodemcu-32s\WiFi101\src\utility/WiFiSocket.h:24:0,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFi.cpp:36:
.pio\libdeps\nodemcu-32s\WiFi101\src/socket/include/socket.h:1839:15: note: previous declaration 'sint8 close(SOCKET)'
 NMI_API sint8 close(SOCKET sock);
               ^
In file included from .pio\libdeps\nodemcu-32s\WiFi101\src/WiFiClient.h:28:0,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src/WiFi101.h:35,
                 from C:/Users/tfmei/OneDrive/Documents/PlatformIO/Projects/210903-000154-nodemcu-32s/src/watertanksensor.ino:4:
.pio\libdeps\nodemcu-32s\WiFi101\src/socket/include/socket.h:1839:32: error: conflicting declaration of C function 'sint8 close(SOCKET)'
 NMI_API sint8 close(SOCKET sock);
                                ^
In file included from C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/stdio.h:29:0,
                 from C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\cores\esp32/Arduino.h:27,
                 from C:\Users\tfmei\AppData\Local\Temp\tmp5ukpniga:1:
C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/sys/unistd.h:30:9: note: previous declaration 'int close(int)'
 int     _EXFUN(close, (int __fildes ));
         ^
C:/Users/tfmei/OneDrive/Documents/PlatformIO/Projects/210903-000154-nodemcu-32s/src/watertanksensor.ino:17:21: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
 char* writeAPIKey = "T3G1AGMZ7E70FLTX";
                     ^
C:/Users/tfmei/OneDrive/Documents/PlatformIO/Projects/210903-000154-nodemcu-32s/src/watertanksensor.ino:18:20: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
 char* readAPIKey = "8GN3TDS2B6OLS646";
                    ^
C:/Users/tfmei/OneDrive/Documents/PlatformIO/Projects/210903-000154-nodemcu-32s/src/watertanksensor.ino: In function 'int connectWifi()':
C:/Users/tfmei/OneDrive/Documents/PlatformIO/Projects/210903-000154-nodemcu-32s/src/watertanksensor.ino:170:3: warning: no return statement in function returning non-void [-Wreturn-type]
   }
   ^
*** [.pio\build\nodemcu-32s\src\watertanksensor.ino.cpp.o] Error 1
In file included from C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/stdio.h:29:0,
                 from C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\cores\esp32/Arduino.h:27,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\utility/WiFiSocket.h:28,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiServer.cpp:20:
C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/sys/unistd.h:30:9: error: conflicting declaration of C function 'int close(int)'
 int     _EXFUN(close, (int __fildes ));
         ^
In file included from .pio\libdeps\nodemcu-32s\WiFi101\src\utility/WiFiSocket.h:24:0,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiServer.cpp:20:
.pio\libdeps\nodemcu-32s\WiFi101\src/socket/include/socket.h:1839:15: note: previous declaration 'sint8 close(SOCKET)'
 NMI_API sint8 close(SOCKET sock);
               ^
In file included from C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/string.h:10:0,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiUdp.cpp:20:
C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/sys/unistd.h:30:9: error: conflicting declaration of C function 'int close(int)'
 int     _EXFUN(close, (int __fildes ));
         ^
In file included from .pio\libdeps\nodemcu-32s\WiFi101\src\utility/WiFiSocket.h:24:0,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiUdp.cpp:22:
.pio\libdeps\nodemcu-32s\WiFi101\src/socket/include/socket.h:1839:15: note: previous declaration 'sint8 close(SOCKET)'
 NMI_API sint8 close(SOCKET sock);
               ^
*** [.pio\build\nodemcu-32s\lib375\WiFi101\WiFiClient.cpp.o] Error 1
.pio\libdeps\nodemcu-32s\WiFi101\src\WiFi.cpp: In member function 'int WiFiClass::init()':
.pio\libdeps\nodemcu-32s\WiFi101\src\WiFi.cpp:303:46: error: invalid conversion from 'void (*)(uint8_t*, uint32_t) {aka void (*)(unsigned char*, unsigned int)}' to 'tpfAppResolveCb {aka void (*)(unsigned char*, long unsigned int)}' [-fpermissive]
  registerSocketCallback(socket_cb, resolve_cb);
                                              ^
In file included from .pio\libdeps\nodemcu-32s\WiFi101\src\utility/WiFiSocket.h:24:0,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFi.cpp:36:
.pio\libdeps\nodemcu-32s\WiFi101\src/socket/include/socket.h:1047:14: note:   initializing argument 2 of 'void registerSocketCallback(tpfAppSocketCb, tpfAppResolveCb)'
 NMI_API void registerSocketCallback(tpfAppSocketCb socket_cb, tpfAppResolveCb resolve_cb);
              ^
.pio\libdeps\nodemcu-32s\WiFi101\src\WiFi.cpp: At global scope:
.pio\libdeps\nodemcu-32s\WiFi101\src\WiFi.cpp:1133:10: error: prototype for 'uint32_t WiFiClass::getTime()' does not match any in class 'WiFiClass'
 uint32_t WiFiClass::getTime()
          ^
In file included from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFi.cpp:38:0:
.pio\libdeps\nodemcu-32s\WiFi101\src\WiFi101.h:157:16: error: candidate is: long unsigned int WiFiClass::getTime()
  unsigned long getTime();
                ^
In file included from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiClient.h:28:0,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiSSLClient.h:23,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiSSLClient.cpp:20:
.pio\libdeps\nodemcu-32s\WiFi101\src\socket/include/socket.h:1839:32: error: conflicting declaration of C function 'sint8 close(SOCKET)'
 NMI_API sint8 close(SOCKET sock);
                                ^
In file included from C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/stdio.h:29:0,
                 from C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\cores\esp32/Arduino.h:27,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiClient.h:23,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiSSLClient.h:23,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiSSLClient.cpp:20:
C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/sys/unistd.h:30:9: note: previous declaration 'int close(int)'
 int     _EXFUN(close, (int __fildes ));
         ^
*** [.pio\build\nodemcu-32s\lib375\WiFi101\WiFi.cpp.o] Error 1
*** [.pio\build\nodemcu-32s\lib375\WiFi101\WiFiSSLClient.cpp.o] Error 1
In file included from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiClient.h:28:0,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFi101.h:35,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiMDNSResponder.h:27,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiMDNSResponder.cpp:32:
.pio\libdeps\nodemcu-32s\WiFi101\src\socket/include/socket.h:1839:32: error: conflicting declaration of C function 'sint8 close(SOCKET)'
 NMI_API sint8 close(SOCKET sock);
                                ^
In file included from C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/strings.h:10:0,
                 from .pio\libdeps\nodemcu-32s\WiFi101\src\WiFiMDNSResponder.cpp:28:
C:\Users\tfmei\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/sys/unistd.h:30:9: note: previous declaration 'int close(int)'
 int     _EXFUN(close, (int __fildes ));
         ^
*** [.pio\build\nodemcu-32s\lib375\WiFi101\WiFiMDNSResponder.cpp.o] Error 1
*** [.pio\build\nodemcu-32s\lib375\WiFi101\WiFiServer.cpp.o] Error 1
*** [.pio\build\nodemcu-32s\lib375\WiFi101\WiFiUdp.cpp.o] Error 1
========================================================================================== [FAILED] Took 7.40 seconds

Is this an issue with my PlatformIO environment?

No. As I’m seeing in your build output you’re using a NodeMCU 32S (ESP32 board). The WiFi101 library (https://github.com/arduino-libraries/WiFi101) is meant as a library for interacting with a ATMEL WINC1500 WiFi module. It does not apply to your ESP32 with built-in WiFi.

Carefully inspect the text in the link you posted. The code is targeting the MKR-1000 board (Arduino MKR1000 WIFI — Arduino Online Shop).

Schematic and Connections

  1. Connect VCC to 5 V on the MKR-1000.

Which is a board with a module SAMD21 processor conntected to a WINC1500 WiFi chip. Hence why they are using the WiFi101 library to be able to connect to the internet at all.

So, you’re gonna have to port the example code to work on an ESP32 instead of that Atmel SAMD + WiFi module.

As one can see from the selob/master/libraries/WiFi/examples/WiFiClient/WiFiClient.ino)) one needs to use the WiFi.h library. The WiFi.h and WiFi101.h libraries are pretty much identical with their provided classes and functions.

So I use the code you linked and just modify WiFi101.h to WiFi.h and save it as src/main.cpp

#include <ThingSpeak.h>
#include<WiFi.h>

#define triggerPin 8
#define echoPin 12
#define LEDpin 6

// Network information
const char* ssid = "SSID";
const char* password = "xxxxxxxxxx";

// ThingSpeak information
char* writeAPIKey = "XXXXXXXXXXXXXXXX";
char* readAPIKey = "YYYYYYYYYYYYYYYY"; 
const long channelID = 000000; 
const unsigned int firstReadFieldNumber = 2;
const unsigned int secondReadFieldNumber = 3; 
const unsigned int switchField = 4; // Field number (1-8) to use to change status of device.  Determines if data is read from ThingSpeak.

// Other constants
const unsigned long postingInterval = 60L * 1000L;   // Post data every 60 seconds

// Global variables
unsigned long lastConnectionTime = 0;
long lastUpdateTime = 0;
float distanceThreshold = 0;
bool getInfo = 1;  // Set this to zero if you don’t want to read data from ThingSpeak anymore (i.e. calibration complete)
int points = 7; 
WiFiClient client; 

int writeTSData(long TSChannel,unsigned int TSField,float data,char* ReadAPIKey){
  int  writeSuccess = ThingSpeak.writeField(TSChannel, TSField, data, writeAPIKey); //write the data to the channel
  return writeSuccess;
}

// Use this function if you want multiple fields simultaneously.
/*
int writeTDData(long TSChannel,unsigned int TSField1,float data1,unsigned int TSField2,data2,char* ReadAPIKey){
  ThingSpeak.setField(TSField1,data1);
  ThingSpeak.setField(TSField1,data2);
   
  writeSuccess = ThingSpeak.writeFields(TSChannel, writeAPIKey);
  return writeSuccess;
}
*/
 
float readTSData(long TSChannel,unsigned int TSField,char* ReadAPIKey){
  
  float data = 0;
  
  data = ThingSpeak.readFloatField(TSChannel,TSField,ReadAPIKey);
  Serial.println(" Data read from ThingSpeak "+String(data));
  return data;
}

float getDistance(int tPin,int ePin){
  
  long duration, distance;
    
  digitalWrite(tPin, LOW);  // Reset the trigger pin.
  delayMicroseconds(2); 
  digitalWrite(tPin, HIGH);  // Start a measurement.
  delayMicroseconds(10); // 
  digitalWrite(tPin, LOW);   // Complete the pulse.
  duration = pulseIn(ePin, HIGH);  // Wait for a reflection pulse.
  distance = (duration/2) / 29.1;     // Calculate the appropriate distance using the estimated speed of sound.

  // This section is useful when debugging the sensor.
/*
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
   Serial.print(distance);
   Serial.println(" cm");
  }
  */
  return distance;
}

int connectWifi(){
     
    while (WiFi.status() != WL_CONNECTED) {
       WiFi.begin(ssid, password);
       delay(2500);
       Serial.println("Connecting to WiFi");
    }
    
    Serial.println("Connected");
    ThingSpeak.begin(client);
  }

void setup() {
  Serial.begin (9600); 
  pinMode(triggerPin, OUTPUT); 
  pinMode(LEDpin, OUTPUT);
  pinMode(echoPin, INPUT);
  connectWifi();
  Serial.println("Start");

  // Get the initial parameters from ThingSpeak.
  distanceThreshold = readTSData(channelID,firstReadFieldNumber,readAPIKey); 
  points = readTSData(channelID,secondReadFieldNumber,readAPIKey);   
}

void loop() {
  
  float  distance=0;

  // Make sure there is an Internet connection.
  if(WiFi.status() != WL_CONNECTED){
    
    connectWifi();  
    }

     for (uint16_t loops = 0; loops < points; loops++){
      distance += getDistance(triggerPin,echoPin);  //make a measurement, store the sum of all measurements
      delay(5);  
     }
     
     distance = distance/points;
     
    if (distance < distanceThreshold){
      digitalWrite(LEDpin,HIGH);
      }
    
     else{
      
      digitalWrite(LEDpin,LOW); 
      }
      
     
     Serial.println("Ave: "+ String(distance)+ " cm");
      
    if (millis() - lastUpdateTime >=  postingInterval) {  
      
      lastUpdateTime = millis();
      
           if (!(getInfo==0)){
            
            distanceThreshold = readTSData(channelID,firstReadFieldNumber,readAPIKey);
            points = readTSData(channelID,secondReadFieldNumber,readAPIKey);
            getInfo = (bool)readTSData(channelID,switchField,readAPIKey);
            }
            
       if (distance < distanceThreshold){
        
      // Write data to ThingSpeak channel.
       writeTSData(channelID, 1, distance, writeAPIKey);
       }
       
      }
      
    delay(500);    // Provide some delay between measurements.
    
}

and use the platformio.ini that just declares the dependency to the ThingSpeak library

[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
lib_deps =
     mathworks/ThingSpeak @ ^2.0.0

I get…

Dependency Graph
|-- <ThingSpeak> 2.0.0
|-- <WiFi> 1.0
Building in release mode
...
Retrieving maximum program size .pio\build\nodemcu-32s\firmware.elf
Checking size .pio\build\nodemcu-32s\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]  11.7% (used 38276 bytes from 327680 bytes)
Flash: [=====     ]  50.8% (used 666002 bytes from 1310720 bytes)
============================================= [SUCCESS] Took 2.82 seconds =============================================
1 Like

Thanks so much. That solved the issue for me. I was able to compile and upload the code