Thanks to encouragement and tutelage from Gfvalvo and in0, I have some time to start experimenting / learning how to put my multiple ino tabs into cpp/h files.
Of course, it's mostly greek to me. Everything I know about C++ has been self-taught using the Arduino IDE (with lots of help from this forum), so namespace and classes is a somewhat foreign concept.
My first tab on this learning experience is wifi.ino. I use it to connect to my wifi and set up some global variables. I am getting a 'not in scope' error for a #define in the main ino file. I am probably missing something simple. Any tips would be appreciated.
Here is my main ino file:
#define SKETCH "cppTest2"
/*
* Experiment with changing my multiple ino files into cpp/h files.
*/
#include "setupWiFi.h"
void setup(){
Serial.begin(115200);
Serial.println("\nTest begins");
setup_wifi(); //Connect to my WiFi
}
void loop(){}
Here is my .cpp file:
//setupWiFi.cpp
#include "setupWiFi.h"
#include <Arduino.h> //needed for Serial.println()
// setup_wifi() function
// ============== Connect the ESP to the router ==============
// Connect to WiFi network so we can reach the MQTT broker and
// publish messages to topics.
void setup_wifi() {
byte mac[6]; // The MAC address of your Wifi
Serial.println(F("\n"));
Serial.print(F("Connecting to "));
Serial.println(MY_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(MY_SSID, MY_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(WiFi.status()); Serial.print(F(" "));
delay(500);
}
Serial.println(F("\nWiFi connected, "));
Serial.print(F("MAC Address: "));
Serial.println(WiFi.macAddress());
Serial.print(F("IP address: "));
Serial.println(WiFi.localIP());
Serial.print(F("RSSI: "));
Serial.println(WiFi.RSSI());
Serial.println();
// Get the last three numbers of the mac address.
// "4C:11:AE:0D:83:86" becomes "0D8386" in macBuffer.
WiFi.macAddress(mac);
snprintf(macBuffer, sizeof(macBuffer), "%02X%02X%02X", mac[3], mac[4], mac[5]);
// Build hostName from prefix + last three bytes of the MAC address.
strcpy(hostName, nodeName);
strcat(hostName, "-");
strcat(hostName, macBuffer);
WiFi.hostname(hostName);
Serial.print(F("hostname= "));
Serial.println(hostName);
}
And finally the .h file:
//setupWiFi.h
#ifndef _setupWiFi_h
#define _setupWiFi_h
//Prototype of function in the .cpp file
void setup_wifi();
//--------------- WiFi declarations ---------------
// WiFi declarations
#include <ESP8266WiFi.h> // Not needed if also using the Arduino OTA Library...
#include <Kaywinnet.h> // WiFi credentials
char macBuffer[24]; // Holds the last three digits of the MAC, in hex.
char hostName[24]; // Holds nodeName + the last three bytes of the MAC address.
char nodeName[] = SKETCH; // (SKETCH is a #define at the top of my main ino file)
#endif // _setupWiFi_h
And, of course, the errors:
Arduino: 1.8.15 (Windows 10), Board: "LOLIN(WEMOS) D1 R2 & mini, 80 MHz, Flash, Disabled (new aborts on oom), Disabled, All SSL ciphers (most compatible), 32KB cache + 32KB IRAM (balanced), Use pgm_read macros for IRAM/PROGMEM, 4MB (FS:2MB OTA:~1019KB), v2 Lower Memory, Disabled, None, Only Sketch, 921600"
In file included from C:\Users\steve\Documents\Arduino\cppTest2\setupWiFi.cpp:3:
setupWiFi.h:16:19: error: 'SKETCH' was not declared in this scope
16 | char nodeName[] = SKETCH; // (SKETCH is a #define at the top of my main ino file)
| ^~~~~~
exit status 1
'SKETCH' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.