PlatformIO Community

Function calling.. where am i wrong?

Hi, i’m a new user of platformIO and i have a problem already.

i am trying to create a remoted controlled arduino with some input-output and some readings accessible via web pages.
i am in the early stage of building the things together but i’m already at a stop.
here the source code and then the errors in compiling!

#include <Arduino.h>

// send the state of the switch to the web browser
void ajaxRequest(EthernetClient client)
{
  for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
    int sensorReading = analogRead(analogChannel);
    client.print("analog input ");
    client.print(analogChannel);
    client.print(" is ");
    client.print(sensorReading);
    client.println("<br />");
  }
}

void ledChangeStatus(EthernetClient client)
{
  int state = digitalRead(RED);
  Serial.println(state);
  if (state == 1) {
    digitalWrite(RED, LOW);
    client.print("OFF");
  }
  else {
    digitalWrite(RED, HIGH);
    client.print("ON");
  }
}






#include <Wire.h>
#include <INA226.h>
#include "BlueDot_BME280.h"
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
BlueDot_BME280 bme280 = BlueDot_BME280();
INA226 ina(Wire);


byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);

EthernetServer server(80);

File webPage;
int RED = 13;              //LED to turn on/off
String HTTP_req;          // stores the HTTP request

void setup() {
    Ethernet.begin(mac, ip);  // initialize Ethernet device
  server.begin();           // start to listen for clients
  Serial.begin(9600);       // for debugging
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // initialize SD card
  Serial.println("Checking SD card is accessible...");
  if (!SD.begin(4)) {
    Serial.println("ERROR - SD card initialization failed!");
    return;    // init failed
  }
  Serial.println("SUCCESS - SD card initialized.");
  HTTP_req = "";
  //Set LED to output
  pinMode(RED, OUTPUT);

 


 
    bme280.parameter.communication = 0;                  //Choose communication protocol
    bme280.parameter.I2CAddress = 0x76;                  //Choose I2C Address
    bme280.parameter.sensorMode = 0b01;                   //Choose sensor mode 0b11
    bme280.parameter.IIRfilter = 0b100;                    //Setup for IIR Filter
    bme280.parameter.humidOversampling = 0b101;            //Setup Humidity Oversampling
    bme280.parameter.tempOversampling = 0b101;             //Setup Temperature Ovesampling
    bme280.parameter.pressOversampling = 0b101;            //Setup Pressure Oversampling 
    bme280.parameter.pressureSeaLevel = 1013.25;           //default value of 1013.25 hPa
    bme280.parameter.tempOutsideCelsius = 15;              //default value of 15°C
  //bme280.parameter.tempOutsideFahrenheit = 59;           //default value of 59°F
    
bme280.init(); 
Wire.begin();

ina.begin(); 

  // Configure INA226
  ina.configure(INA226_AVERAGES_4, INA226_BUS_CONV_TIME_2116US, INA226_SHUNT_CONV_TIME_1100US, INA226_MODE_BUS_CONT);

  // Calibrate INA226. Rshunt = 0.01 ohm, Max excepted current = 4A
  ina.calibrate(0.01, 4);

  // Enable Power Over-Limit Alert
  ina.enableOverPowerLimitAlert();
  ina.setPowerLimit(0.130);
  ina.setAlertLatch(true);


}

void loop() 
{ 
  
   
   Serial.print(F("Duration in Seconds:\t\t"));
   Serial.println(float(millis())/1000);
 
   Serial.print(F("Temperature in Celsius:\t\t")); 
   Serial.println(bme280.readTempC());
 
   Serial.print(F("Temperature in Fahrenheit:\t")); 
   Serial.println(bme280.readTempF());
   
   Serial.print(F("Humidity in %:\t\t\t")); 
   Serial.println(bme280.readHumidity());

   Serial.print(F("Pressure in hPa:\t\t")); 
   Serial.println(bme280.readPressure());

   Serial.print(F("Altitude in Meters:\t\t")); 
   Serial.println(bme280.readAltitudeMeter());

   Serial.print(F("Altitude in Feet:\t\t")); 
   Serial.println(bme280.readAltitudeFeet());
   
   Serial.println();
   Serial.println();

   delay(1000);  

  Serial.print("Bus voltage:   ");
  Serial.print(ina.readBusVoltage(), 5);
  Serial.println(" V");

  Serial.print("Bus power:     ");
  Serial.print(ina.readBusPower(), 5);
  Serial.println(" W");


  Serial.print("Shunt voltage: ");
  Serial.print(ina.readShuntVoltage(), 5);
  Serial.println(" V");

  Serial.print("Shunt current: ");
  Serial.print(ina.readShuntCurrent(), 5);
  Serial.println(" A");

  if (ina.isAlert())
  {
    // Latch will be removed here
    Serial.println("ALERT");
  }

  Serial.println("");
  delay(1000); 


 // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read(); // read 1 byte (character) from client
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if ( HTTP_req.length() < 80)
          HTTP_req += c;  // save the HTTP request 1 char at a time
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();
          // send web page
          if (HTTP_req.indexOf("ajaxrefresh") >= 0 ) {
            ajaxRequest(client);  //update the analog values
            break;
          }
          else if (HTTP_req.indexOf("ledstatus") >= 0 ) {
            ledChangeStatus(client); //change the LED state
            break;
          }
          else {
            webPage = SD.open("index.htm");        // open web page file
            if (webPage) {
              while (webPage.available()) {
                client.write(webPage.read()); // send web page to client
              }
              webPage.close();
            }
            break;
          }
          if (c == '\n') {
            // you're starting a new line
            currentLineIsBlank = true;
          } else if (c != '\r') {
            // you've gotten a character on the current line
            currentLineIsBlank = false;
          }
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    HTTP_req = "";
    Serial.println("client disconnected");
  } // end if (client)






}

Processing uno (platform: atmelavr; board: uno; framework: arduino)
--------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/uno.html
PLATFORM: Atmel AVR (4.0.0) > Arduino Uno
HARDWARE: ATMEGA328P 16MHz, 2KB RAM, 31.50KB Flash
DEBUG: Current (avr-stub) On-board (avr-stub, simavr)
PACKAGES: 
 - framework-arduino-avr @ 5.1.0 
 - toolchain-atmelavr @ 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 9 compatible libraries
Scanning dependencies...
Dependency Graph
|-- INA226Lib @ 1.1.3
|   |-- Wire @ 1.0
|-- BlueDot BME280 Library @ 1.0.9
|   |-- SPI @ 1.0
|   |-- Wire @ 1.0
|-- Ethernet @ 2.0.1
|   |-- SPI @ 1.0
|-- SD @ 1.2.4
|   |-- SPI @ 1.0
|-- SPI @ 1.0
|-- Wire @ 1.0
Building in release mode
Compiling .pio/build/uno/src/main.cpp.o
src/main.cpp:4:18: error: variable or field 'ajaxRequest' declared void
 void ajaxRequest(EthernetClient client)
                  ^~~~~~~~~~~~~~
src/main.cpp:4:18: error: 'EthernetClient' was not declared in this scope
src/main.cpp:4:18: note: suggested alternative: 'ethernet_h_'
 void ajaxRequest(EthernetClient client)
                  ^~~~~~~~~~~~~~
                  ethernet_h_
src/main.cpp:16:22: error: variable or field 'ledChangeStatus' declared void
 void ledChangeStatus(EthernetClient client)
                      ^~~~~~~~~~~~~~
src/main.cpp:16:22: error: 'EthernetClient' was not declared in this scope
src/main.cpp:16:22: note: suggested alternative: 'ethernet_h_'
 void ledChangeStatus(EthernetClient client)
                      ^~~~~~~~~~~~~~
                      ethernet_h_
src/main.cpp: In function 'void loop()':
src/main.cpp:187:13: error: 'ajaxRequest' was not declared in this scope
             ajaxRequest(client);  //update the analog values
             ^~~~~~~~~~~
src/main.cpp:191:13: error: 'ledChangeStatus' was not declared in this scope
             ledChangeStatus(client); //change the LED state
             ^~~~~~~~~~~~~~~
*** [.pio/build/uno/src/main.cpp.o] Error 1
=============================================== [FAILED] Took 0.63 seconds ===============================================

what can i do to solve the problem of those 2 functions?
Thank you so much

You need to move your header file include lines to the top. Before ajaxRequest function.

1 Like

You use the type EthernetClient in a function before the #include <Ethernet.h> line, @ajaybhargav is right.

Great!! Thank you so much!
so the rule is first all the #define then the extra functions and then the setup and loop functions!
A big Thank you all!