Need help for this i got an error for this one

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

// Instantiate trig and echo pin for ultrasonic sensor
const int trigPin = D1;
const int echoPin = D2;

const char* ssid = "PLDT_Home_222B2";
const char* password = "pldthome";
const char* HOST = "http://192.168.1.197/myproject/post_sensor_data.php";

String sensorName = "HCSR04";
String sensorLocation = "Home";
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
WiFiClient client;
http.begin(client, HOST);

http.addHeader("Content-Type", "application/x-www-form-urlencoded");

// Prepare your HTTP POST request data
String httpRequestData = "&sensordata=" + String(sensorName) + "&sensorlocation=" + String(sensorLocation) + "&sensordistance=" + String(void (ultrasonic()) + "";
                         Serial.print("httpRequestData: ");
                         Serial.println(httpRequestData);
                         // Send HTTP POST request
                         int httpResponseCode = http.POST(httpRequestData);

if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
  Serial.println(httpResponseCode);
}
else {
  Serial.print("Error code: ");
  Serial.println(httpResponseCode);
}
// Free resources
http.end();

}
else {
Serial.println("WiFi Disconnected");
}
//Send an HTTP POST request every 20 seconds
delay(1000);
}

void ultrasonic()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
percentage = (duration / 2) / 29.1 / Limit * 100;
range = 100 - percentage;

}

the error is on this

// Prepare your HTTP POST request data
String httpRequestData = "&sensordata=" + String(sensorName) + "&sensorlocation=" + String(sensorLocation) + "&sensordistance=" + String(void (ultrasonic()) + "";
                         Serial.print("httpRequestData: ");
                         Serial.println(httpRequestData);
                         // Send HTTP POST request
                         int httpResponseCode = http.POST(httpRequestData);

the ultrasonic

edit your post put the code in code tags.

Is the error a secret or will you be sharing the error you are getting?

1 Like

Please read the forum guidelines to see how to properly post code and some information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

What did you intend for that to do? For one thing, 'ultrasonic()' doesn't return a value so you can't convert that value to a String. And converting the non-value (a 'void') to type 'void' isn't going to help any. And you seem to be one close-paren short of a set (three opens and two closes).

Did you want to print one of the RESULTS from a call to ultrasonic()? That would be the globals 'duration' or 'percentage' or 'range'.

If you don't want to call ultrasonic() before you create the String 'httpRequestData' you may be able to cram it in as part of a comma-expression:

String httpRequestData = 
"&sensordata=" + 
String (sensorName) + 
"&sensorlocation=" + 
String (sensorLocation) + 
"&sensordistance=" + 
String ((ultrasonic(), percentage)) + 
"";

In a comma expression, the two expressions separated by a comma are executed and the one on the right is returned. In this case 'ultrasonic() is called and then 'String (percentage)' is executed.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.