Skip to content

Commit

Permalink
mqtt host and port configuration when in Access Point mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sblantipodi committed Aug 11, 2020
1 parent 10882bf commit 31d26b3
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CMakeListsUser.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ add_custom_target(
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

add_custom_target(
ERASE_ALL
COMMAND platformio.exe run --target erase
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "git",
"url": "https://github.com/sblantipodi/arduino_bootstrapper.git"
},
"version": "1.5.0",
"version": "1.6.0",
"examples": "examples/*.cpp",
"exclude": "tests",
"frameworks": "arduino",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Bootstrapper
version=1.5.0
version=1.6.0
author=Davide Perini <[email protected]>
maintainer=Davide Perini <[email protected]>
sentence=A client library for MQTT messaging.
Expand Down
8 changes: 4 additions & 4 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ build_flags =
'-D WIFI_DEVICE_NAME="ArduinoBootstrapper"'
'-D MICROCONTROLLER_OTA_PORT=8199'
'-D WIFI_SIGNAL_STRENGTH=0'
'-D GATEWAY_IP=192,168,1,1'
'-D DNS_IP=192,168,1,1'
'-D MICROCONTROLLER_IP=192,168,1,99'
'-D MQTT_SERVER_IP="192.168.1.3"'
'-D GATEWAY_IP="192.168.1.1"'
'-D DNS_IP="192.168.1.1"'
'-D MICROCONTROLLER_IP="192.168.1.99"'
'-D MQTT_SERVER_IP="192.168.1.3"'
'-D MQTT_SERVER_PORT=1883'
'-D MAX_RECONNECT=500'
'-D MAX_JSON_OBJECT_SIZE=50'
Expand Down
10 changes: 8 additions & 2 deletions src/BootstrapManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ void BootstrapManager::getMicrocontrollerInfo() {

helper.smartPrint(F("Flash: ")); helper.smartPrint(ESP.getFlashChipSize()/1024); helper.smartPrintln(F(" KB"));
helper.smartPrint(F("Sketch: ")); helper.smartPrint(ESP.getSketchSize()/1024); helper.smartPrintln(F(" KB"));
helper.smartPrint(F("IP: ")); helper.smartPrintln(IP);
helper.smartPrint(F("IP: ")); helper.smartPrintln(microcontrollerIP);
helper.smartPrintln(F("MAC: ")); helper.smartPrintln(WiFi.macAddress());
helper.smartPrint(F("SDK: ")); helper.smartPrintln(ESP.getSdkVersion());
// helper.smartPrint(F("Arduino Core: ")); helper.smartPrintln(ESP.getCoreVersion());
Expand Down Expand Up @@ -246,7 +246,7 @@ void BootstrapManager::drawInfoPage(String softwareVersion, String author) {
void BootstrapManager::sendState(const char *topic, JsonObject objectToSend, String version) {

objectToSend["Whoami"] = WIFI_DEVICE_NAME;
objectToSend["IP"] = IP;
objectToSend["IP"] = microcontrollerIP;
objectToSend["MAC"] = MAC;
objectToSend["ver"] = version;
objectToSend["time"] = timedate;
Expand Down Expand Up @@ -412,9 +412,12 @@ DynamicJsonDocument BootstrapManager::readSPIFFS(String filename) {
bool BootstrapManager::isWifiConfigured() {

if (wifiManager.isWifiConfigured()) {
microcontrollerIP = IP_MICROCONTROLLER;
qsid = SSID;
qpass = PASSWORD;
OTApass = OTAPASSWORD;
mqttIP = MQTT_SERVER;
mqttPort = MQTT_PORT;
mqttuser = MQTT_USERNAME;
mqttpass = MQTT_PASSWORD;
return true;
Expand All @@ -426,9 +429,12 @@ bool BootstrapManager::isWifiConfigured() {
#endif
if (mydoc.containsKey("qsid")) {
Serial.println("Storage OK, restoring WiFi and MQTT config.");
microcontrollerIP = helper.getValue(mydoc["microcontrollerIP"]);
qsid = helper.getValue(mydoc["qsid"]);
qpass = helper.getValue(mydoc["qpass"]);
OTApass = helper.getValue(mydoc["OTApass"]);
mqttIP = helper.getValue(mydoc["mqttIP"]);
mqttPort = helper.getValue(mydoc["mqttPort"]);
mqttuser = helper.getValue(mydoc["mqttuser"]);
mqttpass = helper.getValue(mydoc["mqttpass"]);
return true;
Expand Down
12 changes: 6 additions & 6 deletions src/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@ const double WIFI_POWER = WIFI_SIGNAL_STRENGTH;

// GATEWAY IP
#ifndef GATEWAY_IP
#define GATEWAY_IP 192,168,1,1
#define GATEWAY_IP "192.168.1.1"
#endif
const IPAddress IP_GATEWAY = IPAddress(GATEWAY_IP);
const char* const IP_GATEWAY = GATEWAY_IP;

// DNS IP
#ifndef DNS_IP
#define DNS_IP 192,168,1,1
#define DNS_IP "192.168.1.1"
#endif
const IPAddress IP_DNS = IPAddress(DNS_IP);
const char* const IP_DNS = DNS_IP;

// STATIC IP FOR THE MICROCONTROLLER
#ifndef MICROCONTROLLER_IP
#define MICROCONTROLLER_IP 192,168,1,99
#define MICROCONTROLLER_IP "192.168.1.99"
#endif
const IPAddress IP_MICROCONTROLLER = IPAddress(MICROCONTROLLER_IP);
const char* const IP_MICROCONTROLLER = MICROCONTROLLER_IP;

// MQTT server IP
#ifndef MQTT_SERVER_IP
Expand Down
3 changes: 3 additions & 0 deletions src/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ int yoffset = 150;
String haVersion = "";
String IP = "";
String MAC = "";
String microcontrollerIP = "XXX";
String qsid = "XXX";
String qpass = "XXX";
String OTApass = "XXX";
String mqttIP = "XXX";
String mqttPort = "XXX";
String mqttuser = "XXX";
String mqttpass = "XXX";

Expand Down
4 changes: 3 additions & 1 deletion src/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ extern bool lastPageScrollTriggered;
extern int yoffset;

extern String haVersion;
extern String IP;
extern String MAC;
extern String microcontrollerIP;
extern String qsid;
extern String qpass;
extern String OTApass;
extern String mqttIP;
extern String mqttPort;
extern String mqttuser;
extern String mqttpass;

Expand Down
5 changes: 4 additions & 1 deletion src/QueueManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ PubSubClient mqttClient(espClient);
/********************************** SETUP MQTT QUEUE **********************************/
void QueueManager::setupMQTTQueue(void (*callback)(char*, byte*, unsigned int)) {

mqttClient.setServer(MQTT_SERVER, MQTT_PORT);
mqttClient.setServer(IPAddress(helper.getValue(mqttIP,'.',0).toInt(),
helper.getValue(mqttIP,'.',1).toInt(),
helper.getValue(mqttIP,'.',2).toInt(),
helper.getValue(mqttIP,'.',3).toInt()), mqttPort.toInt());
mqttClient.setCallback(callback);
mqttClient.setBufferSize(MQTT_MAX_PACKET_SIZE);

Expand Down
44 changes: 35 additions & 9 deletions src/WifiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,19 @@ void WifiManager::setupWiFi(void (*manageDisconnections)(), void (*manageHardwar

WiFi.mode(WIFI_STA); // Disable AP mode
//WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.setAutoConnect(true);
WiFi.config(IP_MICROCONTROLLER, IP_DNS, IP_GATEWAY);
WiFi.setAutoConnect(true);
WiFi.config(IPAddress(helper.getValue(microcontrollerIP,'.',0).toInt(),
helper.getValue(microcontrollerIP,'.',1).toInt(),
helper.getValue(microcontrollerIP,'.',2).toInt(),
helper.getValue(microcontrollerIP,'.',3).toInt()),
IPAddress(helper.getValue(IP_GATEWAY,'.',0).toInt(),
helper.getValue(IP_GATEWAY,'.',1).toInt(),
helper.getValue(IP_GATEWAY,'.',2).toInt(),
helper.getValue(IP_GATEWAY,'.',3).toInt()),
IPAddress(helper.getValue(IP_DNS,'.',0).toInt(),
helper.getValue(IP_DNS,'.',1).toInt(),
helper.getValue(IP_DNS,'.',2).toInt(),
helper.getValue(IP_DNS,'.',3).toInt()));
#if defined(ESP8266)
WiFi.hostname(WIFI_DEVICE_NAME);
// Set wifi power in dbm range 0/0.25, set to 0 to reduce PIR false positive due to wifi power, 0 low, 20.5 max.
Expand Down Expand Up @@ -112,9 +123,9 @@ void WifiManager::setupWiFi(void (*manageDisconnections)(), void (*manageHardwar
}

helper.smartPrintln(F("WIFI CONNECTED"));
IP = WiFi.localIP().toString();
microcontrollerIP = WiFi.localIP().toString();
MAC = WiFi.macAddress();
helper.smartPrintln(IP);
helper.smartPrintln(microcontrollerIP);

delay(DELAY_1500);

Expand Down Expand Up @@ -286,9 +297,12 @@ void WifiManager::createWebServer() {
content += "</h1>";
content += htmlString;
content += "<br><br><form method='get' action='setting' id='form1'>";
content += "<label for='microcontrollerIP'>IP ADDRESS</label><input type='text' id='microcontrollerIP' name='microcontrollerIP'>";
content += "<label for='ssid'>SSID</label><input type='text' id='ssid' name='ssid'>";
content += "<label for='pass'>WIFI PASSWORD</label><input type='password' id='pass' name='pass'>";
content += "<label for='OTApass'>OTA PASSWORD</label><input type='password' id='OTApass' name='OTApass'>";
content += "<label for='mqttIP'>MQTT SERVER IP</label><input type='text' id='mqttIP' name='mqttIP'>";
content += "<label for='mqttPort'>MQTT SERVER PORT</label><input type='text' id='mqttPort' name='mqttPort'>";
content += "<label for='mqttuser'>MQTT USERNAME</label><input type='text' id='mqttuser' name='mqttuser'>";
content += "<label for='mqttpass'>MQTT PASSWORD</label><input type='password' id='mqttpass' name='mqttpass'>";
content += "</form><br><br><button type='submit' form='form1' value='Submit' class='button button3'>STORE CONFIG</button><br><br><br></div></body>";
Expand All @@ -297,30 +311,42 @@ void WifiManager::createWebServer() {
});

server.on("/setting", []() {
String microcontrollerIP = server.arg("microcontrollerIP");
String qsid = server.arg("ssid");
String qpass = server.arg("pass");
String mqttIP = server.arg("mqttIP");
String mqttPort = server.arg("mqttPort");
String OTApass = server.arg("OTApass");
String mqttuser = server.arg("mqttuser");
String mqttpass = server.arg("mqttpass");

if (qsid.length() > 0 && qpass.length() > 0 && OTApass.length() > 0 && mqttuser.length() > 0 && mqttpass.length() > 0) {

if (microcontrollerIP.length() > 0 && qsid.length() > 0 && qpass.length() > 0 && OTApass.length() > 0 && mqttIP.length() > 0 && mqttPort.length() > 0 && mqttuser.length() > 0 && mqttpass.length() > 0) {

Serial.println("microcontrollerIP");
Serial.println(microcontrollerIP);
Serial.println("qsid");
Serial.println(qsid);
Serial.println("qpass");
Serial.println(qpass);
Serial.println("OTApass");
Serial.println(OTApass);
Serial.println("mqttIP");
Serial.println(mqttIP);
Serial.println("mqttPort");
Serial.println(mqttPort);
Serial.println("mqttuser");
Serial.println(mqttuser);
Serial.println("mqttpass");
Serial.println(mqttpass);

DynamicJsonDocument doc(1024);
doc["qsid"] = qsid;
doc["microcontrollerIP"] = microcontrollerIP;
doc["qsid"] = qsid;
doc["qpass"] = qpass;
doc["OTApass"] = OTApass;
doc["mqttuser"] = mqttuser;
doc["OTApass"] = OTApass;
doc["mqttIP"] = mqttIP;
doc["mqttPort"] = mqttPort;
doc["mqttuser"] = mqttuser;
doc["mqttpass"] = mqttpass;

#if defined(ESP8266)
Expand Down

0 comments on commit 31d26b3

Please sign in to comment.