OTA esp8266 error

Hello,
Idk wht to do please help!

code:

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>

const char* ssid = "Test net";
const char* password = "Abc112233Abc";

//OTA
const int OTA_port = 8266;
const char* OTA_password = "admin";
ESP8266WebServer server;
bool OTA_update = false;
uint16_t OTA_time = 0;
//OTA

//JSON
//CHANGE BOARD_number AND BOARD_name
const int BOARD_number = 1;
const String BOARD_hardware = "Gun";
const String BOARD_name = BOARD_hardware + BOARD_number;
const float JSON_software_version = 0.5;
int BULLETS_shot = 0;
int BULLETS_hit = 0;
int HP = 0;
//JSON

//ip

//ip

//GAME
const int IR_recieve = 10;
IRrecv irrecv(IR_recieve);
decode_results IR_results;
const char* IR_results_hex = "";
bool BOARD_TAGGED = false;
uint16_t TAGGED_dellay_millis = 0;
int TAGGED_dellay = 5000;
//GAME

void setup() {
  Serial.begin(115200);
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  pinMode(IR_recieve, INPUT);
  irrecv.enableIRIn();
  WIFI_check();
  OTA_setup();
  SERVER_web();
}

void loop() {
  WIFI_check();
  OTA();
  server.handleClient();
  JSON_parser();
}





String JSON_board(){
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root_board = jsonBuffer.createObject();
  root_board["Software Version"] = JSON_software_version;
  root_board["Name"] = BOARD_name;
  root_board["Hardware"] = BOARD_hardware;
  root_board["Number"] = BOARD_number;
  root_board["Bullets Shot"] = BULLETS_shot;
  root_board["Bullets Hits"] = BULLETS_hit;
  root_board["HP"] = HP;

  String JSON_board_json;
  root_board.prettyPrintTo(JSON_board_json);
  return JSON_board_json;
}

void JSON_parser(){
    HTTPClient http;  
    http.begin("http://http://192.168.0.105/game");
    int JSON_http = http.GET();                                                              
    if (JSON_http > 0) {
      // Assistant code
      const size_t JSON_capacity = JSON_OBJECT_SIZE(3) + 60;
      DynamicJsonBuffer jsonBuffer(JSON_capacity);
      const char* JSON_web = "{\"GAME_mode\":\"team_vs_team\",\"GAME_status\":\"idle\",\"GAME_TEAM\":\"1\"}";
      JsonObject& root = jsonBuffer.parseObject(JSON_web);
      const char* GAME_mode = root["GAME_mode"]; 
      const char* GAME_status = root["GAME_status"]; 
      const char* GAME_timer = root["GAME_timer"];
    }
    http.end();
}

void SERVER_web(){
  server.on("/restart",[](){
    server.send(200, "text/plain", "Restarting ESP");
    delay(100);
    ESP.restart();
  });
  server.on("/update",[](){
    server.send(200, "text/plain", "Update Called \n5 seconds left");
    delay(100);
    OTA_update = true;
    OTA_time = 0;
  });
  server.on("/board",[](){
    server.send(200, "text/json", JSON_board());
    delay(100);
  });
  server.begin();
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void SHOOT(){
  
}

void TAGGED(){
  BOARD_TAGGED = true;
  if (millis() > TAGGED_dellay_millis + TAGGED_dellay){
    TAGGED_dellay_millis = millis();
    BOARD_TAGGED = false;
    irrecv.resume();
  }
}


void OTA_setup(){
  ArduinoOTA.setPort(OTA_port);
  ArduinoOTA.setHostname(BOARD_name);
  ArduinoOTA.setPassword(OTA_password);
  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_SPIFFS
      type = "filesystem";
    }
    Serial.println("OTA Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nOTA End");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("OTA Update Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("OTA Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("OTA Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("OTA Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("OTA Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("OTA Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("OTA End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("OTA Ready");
}

void WIFI_check(){
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi Connection Failed! Connecting...");
    WiFi.begin(ssid, password);
    delay(500);
  }
}

void OTA(){
  if (OTA_update == true){
    uint16_t time_start = millis();
    while(OTA_time < 5000)
    {
      ArduinoOTA.handle();
      OTA_time = millis()-time_start;
      delay(10);
    }
    delay(500);
    ESP.restart();
  }
}

error:

no matching function for call to 'ArduinoOTAClass::setHostname(const String&)'

ArduinoOTA.setHostname() only works with strings (null terminated char array), not String. So you need to make BOARD_name a string instead of a String. You can do that using c_str():

1 Like

i didnt find any good tutorials on c_str() if someone can send me a link it would be nice
Thanks

i always just do it like this:

char boardname[BOARD_name.length()+1];
  BOARD_name.toCharArray(boardname,BOARD_name.length()+1);
  ArduinoOTA.setHostname(boardname);

I dont realy understand what to do thanks

error:

'BOARD_name_array' was not declared in this scope

code:

char BOARD_number = 1;
char BOARD_name[BOARD_name_array.length()+BOARD_number];
BOARD_name_array.toCharArray(BOARD_name,BOARD_name_array.length()+BOARD_number);

I dont realy understand what to do thanks

Read the stickies, then. They CLEARLY say to post ALL of your code. EVERY time.

Trying to figure out the relationship between the snippets in your last post and the code in the first post is just too difficult.

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "Test net";
const char* password = "Abc112233Abc";

//OTA
const int OTA_port = 8266;
const char* OTA_password = "admin";
ESP8266WebServer server;
bool OTA_update = false;
uint16_t OTA_time = 0;
//OTA

//JSON
//CHANGE BOARD_number AND BOARD_name
char BOARD_number = 1;
char BOARD_name[BOARD_name_array.length()+BOARD_number];
BOARD_name_array.toCharArray(BOARD_name,BOARD_name_array.length()+BOARD_number);
const float JSON_software_version = 0.4;
const int GAME_mode = 0;
const int GAME_status = 0;
const bool GAME_timer = false;
const int GAME_timer_time = 0;
//JSON

//ip
const int GAME_people_ammount = 3;
const char GAME_people_ip_1[] = "192.168.0.104";
const char GAME_people_ip_2[] = "192.168.0.105";
const char GAME_people_ip_3[] = "192.168.0.106";
const char* GAME_people_ip[3] = {GAME_people_ip_1, GAME_people_ip_2, GAME_people_ip_3};
//ip

//COMAND
const byte COMAND_chars = 32;
char COMAND_recieved[COMAND_chars];
bool COMAND_new = false;
char COMAND_read = 0;
//COMAND

void setup() {
  Serial.begin(115200);
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  OTA_update = true;
  OTA_time = 0;
  OTA();
  WIFI_check();
  OTA_setup();
  SERVER_web();
}

void loop() {
  WIFI_check();
  server.handleClient();
  OTA();
  COMAND(); 
}

void COMAND(){
  static byte COMAND_sb = 0;
  char COMAND_marker = '\n';
  char COMAND_serial;
    
  while (Serial.available() > 0 && COMAND_new == false) {
      COMAND_serial = Serial.read();
      if (COMAND_serial != COMAND_marker) {
          COMAND_recieved[COMAND_sb] = COMAND_serial;
          COMAND_sb++;
          if (COMAND_sb >= COMAND_chars) {
              COMAND_sb = COMAND_chars - 1;
          }
      }
      else {
          COMAND_recieved[COMAND_sb] = '\0'; 
          COMAND_sb = 0;
          COMAND_new = true;
      }
  }
  if (COMAND_new == true) {
        Serial.print("This just in ... ");
        Serial.print(COMAND_recieved);
    }
}

String JSON_board(){
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root_board = jsonBuffer.createObject();
  root_board["Software Version"] = JSON_software_version;
  root_board["Name"] = BOARD_name;
  root_board["Hardware"] = BOARD_hardware;
  root_board["Number"] = BOARD_number;
  
  String JSON_board_json;
  root_board.prettyPrintTo(JSON_board_json);
  return JSON_board_json;
}

String JSON_game(){
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root_game = jsonBuffer.createObject();
  root_game["GAME_mode"] = GAME_mode;
  root_game["GAME_status"] = GAME_status;
  root_game["GAME_timer"] = GAME_timer;
  
  String JSON_game_json;
  root_game.prettyPrintTo(JSON_game_json);
  return JSON_game_json;
}

String JSON_data(){
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root_data = jsonBuffer.createObject();
  root_data["HP"] = "null";
  root_data["TEAM"] = "null";
  root_data["GAME_people_ammount"] = GAME_people_ammount;
  root_data["GAME_people_ip"] = GAME_people_ip;

  String JSON_data_json;
  root_data.prettyPrintTo(JSON_data_json);
  return JSON_data_json;
}

String JSON_comand(){
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root_comand = jsonBuffer.createObject();
  root_comand["COMAND"] = "null";

  String JSON_comand_json;
  root_comand.prettyPrintTo(JSON_comand_json);
  return JSON_comand_json;
}

void SERVER_web(){
  server.on("/restart",[](){
    server.send(200, "text/plain", "Restarting ESP");
    delay(100);
    ESP.restart();
  });
  server.on("/update",[](){
    server.send(200, "text/plain", "Update Called \n5 seconds left");
    delay(100);
    OTA_update = true;
    OTA_time = 0;
  });
  server.on("/game",[](){
    server.send(200, "text/json", JSON_game());
    delay(100);
  });
  server.on("/data",[](){
    server.send(200, "text/json", JSON_data());
    delay(100);
  });
  server.on("/comand",[](){
    server.send(200, "text/json", JSON_comand());
    delay(100);
  });
  server.on("/",[](){
    server.send(200, "text/plain", "http://192.168.0.104/restart \nhttp://192.168.0.104/update \nhttp://192.168.0.104/game \nhttp://192.168.0.104/data \nhttp://192.168.0.104/comand");
    delay(100);
  });
  server.begin();
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}





void OTA_setup(){
  ArduinoOTA.setPort(OTA_port);
  ArduinoOTA.setHostname(BOARD_name);
  ArduinoOTA.setPassword(OTA_password);
  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_SPIFFS
      type = "filesystem";
    }
    Serial.println("OTA Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nOTA End");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("OTA Update Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("OTA Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("OTA Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("OTA Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("OTA Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("OTA Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("OTA End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("OTA Ready");
}

void WIFI_check(){
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi Connection Failed! Connecting...");
    WiFi.begin(ssid, password);
    delay(500);
  }
}

void OTA(){
  if (OTA_update == true){
    uint16_t time_start = millis();
    while(OTA_time < 5000)
    {
      ArduinoOTA.handle();
      OTA_time = millis()-time_start;
      delay(10);
    }
    delay(500);
    ESP.restart();
  }
}

Sorry BTW

So, in what scope have you defined BOARD_name_array?

In the context, it looks like you are trying to call the toCharArray() method of that object, which implies that the object is supposed to be a String. A String with a name of BOARD_name_array makes no sense.

You can't call methods outside of functions.

here you define BOARD_name

const int BOARD_number = 1;
const String BOARD_hardware = "Gun";
const String BOARD_name = BOARD_hardware + BOARD_number;

but you probably mean

const int BOARD_number = 1;
const String BOARD_hardware = "Gun";
const String BOARD_name = BOARD_hardware + String(BOARD_number,DEC);

if the code from Reply#3 is then inserted & compiled there should be no error.
What do you mean to do with BOARD_name_array ?

I used the code:

const int BOARD_number = 1;
const String BOARD_hardware = "Gun";
const String BOARD_name = BOARD_hardware + String(BOARD_number,DEC);

full code:

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "Test net";
const char* password = "Abc112233Abc";

//OTA
const int OTA_port = 8266;
const char* OTA_password = "admin";
ESP8266WebServer server;
bool OTA_update = false;
uint16_t OTA_time = 0;
//OTA

//JSON
//CHANGE BOARD_number AND BOARD_name
const int BOARD_number = 1;
const String BOARD_hardware = "Gun";
const String BOARD_name = BOARD_hardware + String(BOARD_number,DEC);
const float JSON_software_version = 0.4;
const int GAME_mode = 0;
const int GAME_status = 0;
const bool GAME_timer = false;
const int GAME_timer_time = 0;
//JSON

//ip
const int GAME_people_ammount = 3;
const char GAME_people_ip_1[] = "192.168.0.104";
const char GAME_people_ip_2[] = "192.168.0.105";
const char GAME_people_ip_3[] = "192.168.0.106";
const char* GAME_people_ip[3] = {GAME_people_ip_1, GAME_people_ip_2, GAME_people_ip_3};
//ip

//COMAND
const byte COMAND_chars = 32;
char COMAND_recieved[COMAND_chars];
bool COMAND_new = false;
char COMAND_read = 0;
//COMAND

void setup() {
  Serial.begin(115200);
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  OTA_update = true;
  OTA_time = 0;
  OTA();
  WIFI_check();
  OTA_setup();
  SERVER_web();
}

void loop() {
  WIFI_check();
  server.handleClient();
  OTA();
  COMAND(); 
}

void COMAND(){
  static byte COMAND_sb = 0;
  char COMAND_marker = '\n';
  char COMAND_serial;
    
  while (Serial.available() > 0 && COMAND_new == false) {
      COMAND_serial = Serial.read();
      if (COMAND_serial != COMAND_marker) {
          COMAND_recieved[COMAND_sb] = COMAND_serial;
          COMAND_sb++;
          if (COMAND_sb >= COMAND_chars) {
              COMAND_sb = COMAND_chars - 1;
          }
      }
      else {
          COMAND_recieved[COMAND_sb] = '\0'; 
          COMAND_sb = 0;
          COMAND_new = true;
      }
  }
  if (COMAND_new == true) {
        Serial.print("This just in ... ");
        Serial.print(COMAND_recieved);
    }
}

String JSON_board(){
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root_board = jsonBuffer.createObject();
  root_board["Software Version"] = JSON_software_version;
  root_board["Name"] = BOARD_name;
  root_board["Hardware"] = BOARD_hardware;
  root_board["Number"] = BOARD_number;
  
  String JSON_board_json;
  root_board.prettyPrintTo(JSON_board_json);
  return JSON_board_json;
}

String JSON_game(){
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root_game = jsonBuffer.createObject();
  root_game["GAME_mode"] = GAME_mode;
  root_game["GAME_status"] = GAME_status;
  root_game["GAME_timer"] = GAME_timer;
  
  String JSON_game_json;
  root_game.prettyPrintTo(JSON_game_json);
  return JSON_game_json;
}

String JSON_data(){
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root_data = jsonBuffer.createObject();
  root_data["HP"] = "null";
  root_data["TEAM"] = "null";
  root_data["GAME_people_ammount"] = GAME_people_ammount;
  root_data["GAME_people_ip"] = GAME_people_ip;

  String JSON_data_json;
  root_data.prettyPrintTo(JSON_data_json);
  return JSON_data_json;
}

String JSON_comand(){
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root_comand = jsonBuffer.createObject();
  root_comand["COMAND"] = "null";

  String JSON_comand_json;
  root_comand.prettyPrintTo(JSON_comand_json);
  return JSON_comand_json;
}

void SERVER_web(){
  server.on("/restart",[](){
    server.send(200, "text/plain", "Restarting ESP");
    delay(100);
    ESP.restart();
  });
  server.on("/update",[](){
    server.send(200, "text/plain", "Update Called \n5 seconds left");
    delay(100);
    OTA_update = true;
    OTA_time = 0;
  });
  server.on("/game",[](){
    server.send(200, "text/json", JSON_game());
    delay(100);
  });
  server.on("/data",[](){
    server.send(200, "text/json", JSON_data());
    delay(100);
  });
  server.on("/comand",[](){
    server.send(200, "text/json", JSON_comand());
    delay(100);
  });
  server.on("/",[](){
    server.send(200, "text/plain", "http://192.168.0.104/restart \nhttp://192.168.0.104/update \nhttp://192.168.0.104/game \nhttp://192.168.0.104/data \nhttp://192.168.0.104/comand");
    delay(100);
  });
  server.begin();
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}





void OTA_setup(){
  ArduinoOTA.setPort(OTA_port);
  ArduinoOTA.setHostname(BOARD_name);
  ArduinoOTA.setPassword(OTA_password);
  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_SPIFFS
      type = "filesystem";
    }
    Serial.println("OTA Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nOTA End");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("OTA Update Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("OTA Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("OTA Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("OTA Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("OTA Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("OTA Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("OTA End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("OTA Ready");
}

void WIFI_check(){
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi Connection Failed! Connecting...");
    WiFi.begin(ssid, password);
    delay(500);
  }
}

void OTA(){
  if (OTA_update == true){
    uint16_t time_start = millis();
    while(OTA_time < 5000)
    {
      ArduinoOTA.handle();
      OTA_time = millis()-time_start;
      delay(10);
    }
    delay(500);
    ESP.restart();
  }
}

but i am getting an error:

v0.4:180:36: error: no matching function for call to 'ArduinoOTAClass::setHostname(const String&)'

   ArduinoOTA.setHostname(BOARD_name);

                                    ^

C:\Users\user\Desktop\project\Station\v0.4\v0.4.ino:180:36: note: candidate is:

In file included from C:\Users\Andrey_user\Desktop\project\Station\v0.4\v0.4.ino:4:0:

C:\Users\user\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.5.2\libraries\ArduinoOTA/ArduinoOTA.h:38:10: note: void ArduinoOTAClass::setHostname(const char*)

     void setHostname(const char *hostname);

          ^

C:\Users\user\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.5.2\libraries\ArduinoOTA/ArduinoOTA.h:38:10: note:   no known conversion for argument 1 from 'const String' to 'const char*'

exit status 1
no matching function for call to 'ArduinoOTAClass::setHostname(const String&)'

Sorry if i did something wrong...

Deva_Rishi:
if the code from Reply#3 is then inserted & compiled there should be no error.

So instead of ArduinoOTA.setHostname(BOARD_name);
you should use

  char boardname[BOARD_name.length()+1];
  BOARD_name.toCharArray(boardname,BOARD_name.length()+1);
  ArduinoOTA.setHostname(boardname);

I did as you told in reply #8 and did this:

const int BOARD_number = 1;
const String BOARD_hardware = "Gun";
const String BOARD_name = BOARD_hardware + String(BOARD_number,DEC);

ArduinoOTA.setHostname(BOARD_name);

a_kiselev_private:
I did as you told in reply #8 and did this:

const int BOARD_number = 1;

const String BOARD_hardware = "Gun";
const String BOARD_name = BOARD_hardware + String(BOARD_number,DEC);

ArduinoOTA.setHostname(BOARD_name);

you did half, then i told you to include the part i wrote in reply #3 as well, but you didn't.
There is of course very little point if you do not understand what you are trying to achieve (other than a compiling sketch) The function you are calling (.setHostName() ) takes a char * as argument, and since you declared BOARD_name as a String to use it you would have to convert it.
Other option is to declare BOARD_name as a char * (or char-array or c-string) with a size big enough to hold both the BOARD_hardware & BOARD_number.

const int BOARD_number = 1;
const String BOARD_hardware = "Gun";
const String BOARD_name = BOARD_hardware + String(BOARD_number,DEC);

ArduinoOTA.setHostname(BOARD_name.c_str());

Expose the underlying string to the method, not the String object.