1

Topic: ESP8266-Ultrasonic sensors-motor relay

NEED HELP - Trying to get a sketch that will drive an esp8266 fed by two sensors to measure water volume and percentage in two seperate but identical tanks and turn a pump on when tank 1 gets to 10% and off at 50% and have that info displayed on my ipad running remotexy.

Copied the scrip generated by remotexy that defines the GUI and had ChatGPT write code for the reast and then tried to combine the two.

This is the code :-

#define REMOTEXY_MODE__ESP8266WIFI_LIB_CLOUD
#include <ESP8266WiFi.h>
#include <RemoteXY.h>

// RemoteXY connection settings
#define REMOTEXY_WIFI_SSID "ORBI58"
#define REMOTEXY_WIFI_PASSWORD "freshbug844"
#define REMOTEXY_CLOUD_SERVER "cloud.remotexy.com"
#define REMOTEXY_CLOUD_PORT 6376
#define REMOTEXY_CLOUD_TOKEN "a14581acb38dab3e7980fa05c36281a5"



// RemoteXY configurate 
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =   // 69 bytes
  { 255,0,0,24,0,62,0,16,160,0,66,1,17,6,32,49,4,26,66,1,
  50,6,32,49,94,26,67,5,22,55,22,6,16,26,11,67,5,53,55,26,
  6,16,26,11,129,0,57,1,17,5,16,72,79,85,83,69,0,129,0,26,
  1,13,5,16,66,79,82,69,0 };
 
// this structure defines all the variables and events of your control interface
struct {

    // output variables
  int8_t volumeTank1; // =0..100 level position
  int8_t volumeTank2; // =0..100 level position
  char percentTank1[11];  // string UTF8 end zero
  char percentTank2[11];  // string UTF8 end zero

    // other variable
// uint8_t connect_flag;  // =1 if wire connected, else =0

} RemoteXY;
#pragma pack(pop)
#define PIN_RELAY D1
#define TANK_HEIGHT 2.75 // in meters
#define TANK_RADIUS 1.89 // in meters (diameter 3.78m / 2)
#define SENSOR_HEIGHT 0.5 // in meters
#define TANK_VOLUME (PI * pow(TANK_RADIUS, 2) * (TANK_HEIGHT -SENSOR_HEIGHT)) // in cubic meters
#define TRIG_PIN_TANK1 D2
#define ECHO_PIN_TANK1 D3
#define TRIG_PIN_TANK2 D4
#define ECHO_PIN_TANK2 D5


void setup() {
  RemoteXY_Init ();

  pinMode(PIN_RELAY, OUTPUT);
  pinMode(TRIG_PIN_TANK1, OUTPUT);
  pinMode(ECHO_PIN_TANK1, INPUT);
  pinMode(TRIG_PIN_TANK2, OUTPUT);
  pinMode(ECHO_PIN_TANK2, INPUT);
}

void loop() {
   RemoteXY_Handler ();

  float measureLevel1(int TRIG_PIN_TANK1, int ECHO_PIN_TANK1);
  digitalWrite(TRIG_PIN_TANK1, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN_TANK1, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN_TANK1, LOW);

  float duration1 = pulseIn(ECHO_PIN_TANK1, HIGH);
  float distance1 = (duration1 * 0.0343) / 2; // calculate distance based on speed of sound in air
  float level = SENSOR_HEIGHT - distance1; // calculate level of water in the tank
  return measureLevel1;

float measureLevel2(int TRIG_PIN_TANK2, int ECHO_PIN_TANK2);
  digitalWrite(TRIG_PIN_TANK2, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN_TANK2, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN_TANK2, LOW);

  float duration2 = pulseIn(ECHO_PIN_TANK2, HIGH);
  float distance2 = (duration2 * 0.0343) / 2; // calculate distance based on speed of sound in air
  float level = SENSOR_HEIGHT - distance2; // calculate level of water in the tank
  return measureLevel2;

  float levelTank1 = measureLevel1(TRIG_PIN_TANK1, ECHO_PIN_TANK1);
  float levelTank2 = measureLevel2(TRIG_PIN_TANK2, ECHO_PIN_TANK2);

  float percentTank1 = (levelTank1 / TANK_HEIGHT) * 100;
  float percentTank2 = (levelTank2 / TANK_HEIGHT) * 100;

  float volumeTank1 = (levelTank1 / TANK_HEIGHT) * TANK_VOLUME * 1000; // in liters
  float volumeTank2 = (levelTank2 / TANK_HEIGHT) * TANK_VOLUME * 1000; // in liters

  if (percentTank1 < 10) {
    digitalWrite(PIN_RELAY, HIGH); // turn on pump
  } else if (percentTank1 > 50) {
    digitalWrite(PIN_RELAY, LOW); // turn off pump
  }

  // Send data to RemoteXY
  RemoteXY.levelTank1 = percentTank1;
  RemoteXY.levelTank2 = percentTank2;
  RemoteXY.volumeTank1 = volumeTank1;
  RemoteXY.volumeTank2 = volumeTank2;
}


But it will not compile and gives these errors:-

C:\Users\User\OneDrive\Documents\Arduino\update\sketches\WAVdemo\adc_i2v56\adc_i2v56.ino: In function 'void loop()':
C:\Users\User\OneDrive\Documents\Arduino\update\sketches\WAVdemo\adc_i2v56\adc_i2v56.ino:70:10: error: return-statement with a value, in function returning 'void' [-fpermissive]
   70 |   return measureLevel1;
      |          ^~~~~~~~~~~~~
C:\Users\User\OneDrive\Documents\Arduino\update\sketches\WAVdemo\adc_i2v56\adc_i2v56.ino:81:9: error: redeclaration of 'float level'
   81 |   float level = SENSOR_HEIGHT - distance2; // calculate level of water in the tank
      |         ^~~~~
C:\Users\User\OneDrive\Documents\Arduino\update\sketches\WAVdemo\adc_i2v56\adc_i2v56.ino:69:9: note: 'float level' previously declared here
   69 |   float level = SENSOR_HEIGHT - distance1; // calculate level of water in the tank
      |         ^~~~~
C:\Users\User\OneDrive\Documents\Arduino\update\sketches\WAVdemo\adc_i2v56\adc_i2v56.ino:82:10: error: return-statement with a value, in function returning 'void' [-fpermissive]
   82 |   return measureLevel2;
      |          ^~~~~~~~~~~~~
C:\Users\User\OneDrive\Documents\Arduino\update\sketches\WAVdemo\adc_i2v56\adc_i2v56.ino:100:12: error: 'struct<unnamed>' has no member named 'levelTank1'
  100 |   RemoteXY.levelTank1 = percentTank1;
      |            ^~~~~~~~~~
C:\Users\User\OneDrive\Documents\Arduino\update\sketches\WAVdemo\adc_i2v56\adc_i2v56.ino:101:12: error: 'struct<unnamed>' has no member named 'levelTank2'
  101 |   RemoteXY.levelTank2 = percentTank2;
      |            ^~~~~~~~~~

Using library ESP8266WiFi at version 1.0 in folder: C:\Users\User\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.1\libraries\ESP8266WiFi
Using library RemoteXY at version 3.1.11 in folder: C:\Users\User\OneDrive\Documents\Arduino\libraries\RemoteXY
exit status 1

Compilation error: return-statement with a value, in function returning 'void' [-fpermissive]