Wifi connections work!
This commit is contained in:
parent
88444d3454
commit
bb8f57b4e9
|
|
@ -223,7 +223,7 @@ void Span::pollTask() {
|
|||
readSerial(cBuf,64);
|
||||
|
||||
if(strncmp(cBuf, "IMPROV", 6) == 0) {
|
||||
processImprovCommand(cBuf);
|
||||
processImprovCommand(cBuf, this);
|
||||
} else {
|
||||
processSerialCommand(cBuf);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -271,7 +271,6 @@ class Span{
|
|||
void checkConnect(); // check WiFi connection; connect if needed
|
||||
void commandMode(); // allows user to control and reset HomeSpan settings with the control button
|
||||
void resetStatus(); // resets statusLED and calls statusCallback based on current HomeSpan status
|
||||
void reboot(); // reboots device
|
||||
|
||||
void printfAttributes(int flags=GET_VALUE|GET_META|GET_PERMS|GET_TYPE|GET_DESC); // writes Attributes JSON database to hapOut stream
|
||||
|
||||
|
|
@ -296,10 +295,10 @@ class Span{
|
|||
const char *hostNameBase=DEFAULT_HOST_NAME,
|
||||
const char *modelName=DEFAULT_MODEL_NAME);
|
||||
|
||||
void reboot(); // reboots device
|
||||
void poll(); // calls pollTask() with some error checking
|
||||
void processSerialCommand(const char *c); // process command 'c' (typically from readSerial, though can be called with any 'c')
|
||||
void processImprovCommand(const char *c); // process Improv-Serial command 'c'
|
||||
void Span::handleImprovCommand(improv::ImprovCommand cmd);
|
||||
void processImprovCommand(const char *c, Span* span); // process Improv-Serial command 'c'
|
||||
|
||||
boolean updateDatabase(boolean updateMDNS=true); // updates HAP Configuration Number and Loop vector; if updateMDNS=true and config number has changed, re-broadcasts MDNS 'c#' record; returns true if config number changed
|
||||
boolean deleteAccessory(uint32_t aid); // deletes Accessory with matching aid; returns true if found, else returns false
|
||||
|
|
|
|||
103
src/Improv.cpp
103
src/Improv.cpp
|
|
@ -4,7 +4,9 @@
|
|||
using namespace Utils;
|
||||
using namespace improv;
|
||||
|
||||
void Span::processImprovCommand(const char *c){
|
||||
#define MAX_ATTEMPTS_WIFI_CONNECTION 10
|
||||
|
||||
void Span::processImprovCommand(const char *c, Span* span){
|
||||
|
||||
uint8_t len = c[8] + 10;
|
||||
// Copy the const char *c from the argument into a new character array
|
||||
|
|
@ -12,55 +14,47 @@ void Span::processImprovCommand(const char *c){
|
|||
strcpy(buffer, c);
|
||||
buffer[len-1] = c[len-1]; // Copy the checksum bit since it falls after the null terminator
|
||||
|
||||
Serial.print("Command: ");
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
Serial.print("0x");
|
||||
Serial.print(c[i] < 16 ? "0" : "");
|
||||
Serial.print(c[i], HEX);
|
||||
Serial.print(" ");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
Serial.println("Processing Improv Command " + String(buffer) + " length " + strlen(buffer));
|
||||
Serial.print("Command: ");
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
Serial.print("0x");
|
||||
Serial.print(buffer[i] < 16 ? "0" : "");
|
||||
Serial.print(buffer[i], HEX);
|
||||
Serial.print(" ");
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
Serial.println("Length " + String(len) + " char ");
|
||||
Serial.println(buffer[len - 1], HEX);
|
||||
Serial.println("Onwards...");
|
||||
|
||||
improv::parse_improv_serial_byte(len - 1, buffer[len - 1], (uint8_t *)c, [&](ImprovCommand command) {
|
||||
improv::handleImprovCommand(command);
|
||||
improv::handleImprovCommand(command, span);
|
||||
return true;
|
||||
}, [&](Error error) {
|
||||
Serial.println("Error parsing Improv command");
|
||||
LOG0("Error parsing Improv command");
|
||||
});
|
||||
} // Span::processImprovCommand
|
||||
|
||||
|
||||
namespace improv {
|
||||
|
||||
void handleImprovCommand(improv::ImprovCommand cmd) {
|
||||
void handleImprovCommand(improv::ImprovCommand cmd, Span* span) {
|
||||
switch(cmd.command) {
|
||||
case Command::WIFI_SETTINGS:
|
||||
Serial.println("WiFi Settings: ");
|
||||
Serial.print(cmd.ssid.c_str());
|
||||
Serial.print(" ");
|
||||
Serial.println(cmd.password.c_str());
|
||||
//Span::setWifiCredentials(cmd.ssid.c_str(), cmd.password.c_str());
|
||||
|
||||
// Attempt to use our credentials so we can see if they work
|
||||
if (connectWifi(cmd.ssid.c_str(), cmd.password.c_str())) {
|
||||
sendImprovState(improv::State::STATE_PROVISIONED);
|
||||
std::vector<std::string> infos; // Empty vector, we could put an HTTP URL here as the next step for the user if we had one
|
||||
std::vector<uint8_t> data = improv::build_rpc_response(improv::WIFI_SETTINGS, infos, false);
|
||||
improv::sendImprovResponse(data);
|
||||
|
||||
span->setWifiCredentials(cmd.ssid.c_str(), cmd.password.c_str()); // Save credentials and reboot
|
||||
delay(1000); // Give the serial on the other end a moment to process
|
||||
span->reboot();
|
||||
} else {
|
||||
sendImprovState(improv::State::STATE_STOPPED);
|
||||
improv::sendImprovError(improv::Error::ERROR_UNABLE_TO_CONNECT);
|
||||
}
|
||||
break;
|
||||
|
||||
case Command::GET_CURRENT_STATE:
|
||||
if((WiFi.status() == WL_CONNECTED)) {
|
||||
sendImprovState(improv::State::STATE_PROVISIONED);
|
||||
//std::vector<uint8_t> data = improv::build_rpc_response(improv::GET_CURRENT_STATE, getLocalUrl(), false);
|
||||
//send_response(data);
|
||||
|
||||
std::vector<std::string> infos; // Empty vector, we could put an HTTP URL here as the next step for the user if we had one
|
||||
std::vector<uint8_t> data = improv::build_rpc_response(improv::GET_CURRENT_STATE, infos, false);
|
||||
improv::sendImprovResponse(data);
|
||||
} else {
|
||||
sendImprovState(improv::State::STATE_AUTHORIZED);
|
||||
}
|
||||
|
|
@ -94,6 +88,27 @@ void handleImprovCommand(improv::ImprovCommand cmd) {
|
|||
}
|
||||
}
|
||||
|
||||
bool connectWifi(const char *ssid, const char *pwd) {
|
||||
uint8_t attempts = 0;
|
||||
|
||||
WiFi.begin(ssid, pwd);
|
||||
LOG2("Attempting to connect to WiFi SSID %s", ssid);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
LOG2("Attempting to connect to WiFi SSID %s, attempt #%s", ssid, String(attempts));
|
||||
if (attempts > MAX_ATTEMPTS_WIFI_CONNECTION) {
|
||||
LOG0("Failed to connect to WiFi");
|
||||
WiFi.disconnect();
|
||||
return false;
|
||||
}
|
||||
attempts++;
|
||||
}
|
||||
|
||||
LOG0("Successfully connected to WiFi SSID %s", ssid);
|
||||
return true;
|
||||
} // connectWifi
|
||||
|
||||
void getAvailableWifiNetworks() {
|
||||
int networkNum = WiFi.scanNetworks();
|
||||
|
||||
|
|
@ -121,11 +136,35 @@ void sendImprovState(improv::State state) {
|
|||
for(uint8_t d : data)
|
||||
checksum += d;
|
||||
data[10] = checksum;
|
||||
Serial.println("Writing " + String(data.size()) + " bytes to Improv");
|
||||
|
||||
Serial.write(data.data(), data.size());
|
||||
|
||||
Serial.println("Wrote ");
|
||||
for(size_t i = 0; i < data.size(); i++) {
|
||||
Serial.print("0x");
|
||||
Serial.print(data[i] < 16 ? "0" : "");
|
||||
Serial.print(data[i], HEX);
|
||||
Serial.print(" ");
|
||||
}
|
||||
Serial.println();
|
||||
} // sendImprovState
|
||||
|
||||
void sendImprovError(improv::Error error) {
|
||||
std::vector<uint8_t> data = {'I', 'M', 'P', 'R', 'O', 'V'};
|
||||
data.resize(11);
|
||||
data[6] = improv::IMPROV_SERIAL_VERSION;
|
||||
data[7] = improv::TYPE_ERROR_STATE;
|
||||
data[8] = 1;
|
||||
data[9] = error;
|
||||
|
||||
uint8_t checksum = 0x00;
|
||||
for (uint8_t d : data)
|
||||
checksum += d;
|
||||
data[10] = checksum;
|
||||
|
||||
Serial.write(data.data(), data.size());
|
||||
}
|
||||
|
||||
void sendImprovResponse(std::vector<uint8_t> &response) {
|
||||
std::vector<uint8_t> data = {'I', 'M', 'P', 'R', 'O', 'V'};
|
||||
data.resize(9);
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "HomeSpan.h"
|
||||
|
||||
namespace improv {
|
||||
|
||||
|
|
@ -73,10 +74,11 @@ std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::
|
|||
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<String> &datum, bool add_checksum = true);
|
||||
#endif // ARDUINO
|
||||
|
||||
void handleImprovCommand(improv::ImprovCommand cmd);
|
||||
void handleImprovCommand(improv::ImprovCommand cmd, Span* span);
|
||||
void sendImprovState(improv::State state);
|
||||
void sendImprovResponse(std::vector<uint8_t> &response);
|
||||
void sendImprovRPCResponse(std::vector<uint8_t> &response);
|
||||
void sendImprovError(improv::Error error);
|
||||
void getAvailableWifiNetworks();
|
||||
bool connectWifi(const char *ssid, const char *pwd);
|
||||
|
||||
} // namespace improv
|
||||
Loading…
Reference in New Issue