Hello,
I have a problem with String variable during the compilation. I get the following error for every String:
"In file included from sketch\pump.cpp:1:0:
pump.h:16: error: 'String' has not been declared
Pump(int ID, String text, int DurMaxi, int Press, int TestDur, Pump * prec);"
I always have errors when i used String variable except in the Main file.
I have tried to put all the code in only one file and I can compile without any error.
Do I need to add something ?
I have searched on Google without finding any clue.
It is probably very simple but it's been a long time since the last time, i used C++.
Thank you for your help.
Main
// Libraries
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <String>
#include "pump.cpp"
#define USE_SERIAL Serial
const char * SERVERADR = "http://192.168.1.4/arduino/";
ESP8266WiFiMulti WiFiMulti;
Pump test;
HTTPClient http;
void setup() {
Serial.begin(115200); // Start Serial
USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.printf("[SETUP] WAIT %d...\n");
for(uint8_t t = 8; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFiMulti.addAP("ChinaNet-J4S5", "NA");
WiFiMulti.addAP("Router2-SSI", "Router2-password");
http.setReuse(true);
if((WiFiMulti.run() == WL_CONNECTED)) {
http.begin(String(SERVERADR)+"getpumpdata.php"); //HTTP
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET Pump Data... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed receive pump data, error: %s\n", http.errorToString(httpCode).c_str());
}
}
else {
USE_SERIAL.printf("Connexion wifi problem\n");
}
}
void loop()
{
// wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) {
//Serial.println(WiFi.localIP());
USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
char Data[30];
CreateDataString(Data, 3, 5, 8, 321, 1);
http.begin(String(SERVERADR)+"serverarduino2.php?Data="+String(Data)); //HTTP
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
delay(100); // wait a minute
}
}
void CreateDataString(char Data[30],int user, int TestbBench, int PumpType, int Duration, int valid)
{
sprintf(Data, "%.2d", user);
sprintf(Data, "%s%.2d",Data, TestbBench);
sprintf(Data, "%s%.2d",Data, PumpType);
sprintf(Data, "%s%.4d",Data, Duration);
sprintf(Data, "%s%.2d",Data, valid);
unsigned int checksum =crc32b(Data);
sprintf(Data, "%s%u",Data, checksum);
}
unsigned int crc32b( char *message) {
int i, j;
unsigned int byte, crc, mask;
i = 0;
crc = 0xFFFFFFFF;
while (message[i] != 0) {
byte = message[i]; // Get next byte.
crc = crc ^ byte;
for (j = 7; j >= 0; j--) { // Do eight times.
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i = i + 1;
}
return ~crc;
}
Pump.cpp
#include "pump.h"
Pump::Pump()
{
ID = 0;
DurationMaxi = 0;
Pressure= 0;
TestDuration= 0;
Next = NULL;
}
Pump::Pump(int IDNb, String text, int DurMaxi, int Press, int TestDur, Pump * prec)
{
ID = IDNb;
text.toCharArray(Name,20);
DurationMaxi = DurMaxi;
Pressure= Press;
TestDuration= TestDur;
//Next = NULL;
prec->SetNext(this);
}
Pump::Pump(String text, Pump * prec)
{
const char separator = '|';
int temp1, temp2;
temp1 = 0;
temp2 = text.indexOf(separator, temp1+1);
ID = text.substring(temp1, temp2 - temp1).toInt();
temp1 = temp2+1;
temp2 = text.indexOf(separator, temp1);
text.substring(temp1, temp2 - temp1).toCharArray(Name,20);
temp1 = temp2+1;
temp2 = text.indexOf(separator, temp1);
DurationMaxi = text.substring(temp1, temp2 - temp1).toInt();
temp1 = temp2+1;
temp2 = text.indexOf(separator, temp1);
Pressure= text.substring(temp1, temp2 - temp1).toInt();
temp1 = temp2+1;
temp2 = text.indexOf(separator, temp1);
TestDuration= text.substring(temp1, temp2 - temp1).toInt();
prec->SetNext(this);
}
void Pump::SetNext(Pump * Nxt)
{
Next = Nxt;
}
int Pump::GetDurationMaxi()
{
return DurationMaxi;
}
int Pump::GetPressure()
{
return Pressure;
}
int Pump::GetTestDuration()
{
return TestDuration;
}
char* Pump::GetName()
{
return Name;
}
Pump.h
#ifndef DEF_PUMP
#define DEF_PUMP
class Pump
{
private:
int ID;
char Name[20];
int DurationMaxi;
int Pressure;
int TestDuration;
Pump * Next;
public:
Pump();
Pump(int ID, String text, int DurMaxi, int Press, int TestDur, Pump * prec);
Pump(String text, Pump * prec);
int GetDurationMaxi();
void SetNext(Pump * prec);
int GetPressure();
int GetTestDuration();
char* GetName();
};
#endif