Code Monkey home page Code Monkey logo

provision-wifi's Introduction

Mongoose OS Wifi Provision Library

This library is now DEPRECATED in favor of the Wifi Captive Portal library I created for Mongoose OS. I will still accept PR and fix any bugs that are reported, but as this was my first ever lib for Mongoose OS, I now realize there was a lot of things probably not necessary in this lib. It should still work fine, and could be a great resource for those learning mos, but I will personally no longer be using this lib.

I strongly recommend taking a look at the WiFi Captive Portal library instead:

https://github.com/tripflex/wifi-captive-portal

Table of Contents

Author

  • Myles McNamara
  • You!

Overview

This library is meant to help with provisioning (setting up) a device with WiFi Credentials, handling timeouts, max connection attempts, and letting you know whether or not the SSID/Password are correct.

Are you familiar with Mongoose OS and/or C programming?

Why?

I created this library because the default/core Mongoose OS WiFi library, which this library does depend on, does not do that good of a job when it comes to validating WiFi values that are set. If correct SSID and Password are set, it works great ... but as we all know with IoT devices, the end user will be the one setting it up, and chances are there will be multiple instances where the type the SSID or Password incorrectly, and there's no real easy way of dealing with that.

This library is my solution to this problem, not only solving it, but also adding additional features and handling for provisioning WiFi on a device.

Features

  • MJS Support
  • Test WiFi settings on device boot (when provision.wifi.boot.enable is set true)
  • Testing on boot delay in seconds from provision.wifi.boot.delay when existing STA is connected (test is immediate when no existing STA connected)
  • Fail test after total connection attempts (provision.wifi.attempts) and timeout provision.wifi.timeout (in seconds)
  • Reconnects to existing station (if one was connected) after testing, when provision.wifi.reconnect is true (default: true)
  • Test STA values are stored separate from WiFi Library STA values (in provision.wifi.sta - matches wifi lib structure)
  • Automatically copy test STA values to wifi.sta after succesful connection test, when provision.wifi.success.copy is true (default: true)
  • Disable AP on successful connection test provision.wifi.success.disable_ap when true (default: false)
  • Reboot device on successful connection test provision.wifi.success.reboot when true (default: false)
  • Reboot device on failed connection test provision.wifi.fail.reboot when true (default: false)
  • Clear test STA values on success test provision.wifi.success.clear, or fail provision.wifi.fail.clear, when true (default: false)

Install/Use

To use this library in your Mongoose OS project, just add it under the libs in your mos.yml file:

  - origin: https://github.com/tripflex/provision-wifi

Configurations

This library adds a provision.wifi object to the mos configuration, with the following settings:

To see all of the available configurations please look at the mos.yml file. There are descriptions for each one of the settings.

MJS

api_provision_wifi.js

To use this library in your JavaScript files, make sure to load it at top of the file:

load('api_provision_wifi.js');

You will then have access to the ProvisionWiFi javascript object, with the following:

ProvisionWiFi.run();
  • Run WiFi test based on values previously set in provision.wifi.sta (no callback)
ProvisionWiFi.Test.run( callback_fn, userdata );
  • Run WiFi test based on values previously set in provision.wifi.sta, calling callback_fn after completion
ProvisionWiFi.Test.SSIDandPass( ssid, pass, callback_fn, userdata );
  • Set provision.wifi.sta.ssid and provision.wifi.sta.pass, calling callback_fn after completion
ProvisionWiFi.onBoot.enable();
  • Enable testing on device boot
ProvisionWiFi.onBoot.disable();
  • Disable testing on device boot
ProvisionWiFi.STA.copy();
  • Copy test station values to wifi.sta
ProvisionWiFi.STA.clear();
  • Clear any existing station values in provision.wifi.sta
ProvisionWiFi.Results.success();
  • Returns whether or not last test was a success
ProvisionWiFi.Results.ssid();
  • Returns last SSID that was tested
ProvisionWiFi.Results.isRunning();
  • Returns whether or not test is currently running

Callback Parameters If you use a callback function (ProvisionWiFi.Test.run or ProvisionWiFi.Test.SSIDandPass) the callback function will be passed 3 arguments: success, ssid, userdata

success will be an integer (since boolean not supported in mjs), 1 means succesful connection test, 0 means failed. ssid will be the SSID that was tested against userdata is whatever you passed in userdata

Examples

ProvisionWiFi.Test.SSIDandPass( "TestSSID", "TestPassword", function( success, ssid, userdata ){

    if( success ){
        // Hey look those credentials worked!
    } else {
        // Oh no, probably wrong/bad credentials!
    }

}, null );
ProvisionWiFi.Test.run( function( success, ssid, userdata ){

    if( success ){
        // Hey look those credentials worked!
    } else {
        // Oh no, probably wrong/bad credentials!
    }

}, null );
let lastTestSuccess = ProvisionWiFi.Results.success();
let lastTestSSID = ProvisionWiFi.Results.ssid();

if( lastTestSSID == 'TestSSID' && lastTestSuccess ){
    // Hey it worked!
} else {
    // Doh! Fail!
}

C

mgos_provision_wifi.h

WiFi AP Need to Know

  • The AP should NOT go down while testing
  • If provision.wifi.reconnect is true (default), there was an existing STA that was connected to before testing, and the test failed, the AP will go down for ~5 seconds or so while wifi is reinitialized.

Roadmap

  • RPC Helper library (for making RPC calls to provision wifi)
  • Your idea!
  • Optimizing and reducing code base size

Suggestions and Code Review

PLEASE PLEASE if you're familiar with Mongoose OS (mos) or C programming in general, I need your help with this project!

My C and Mongoose OS experience is pretty limited (mos only month or two), but being a full time geek and developer I was able to figure out how to get this working (for most part) ... but i'm sure there's numerous ways to optimize the code base, or correct/better way to do things either in C or Mongoose OS specifically, and i'm completely open to constructive criticism!

Even if you're not 100% sure, neither am I, so open up an issue and let's discuss it!

License

Apache 2.0

provision-wifi's People

Contributors

tripflex avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

iim-slide

provision-wifi's Issues

Set configuration values at once instead of calling functions

Currently all of the values for STA are set using the helper functions:

  mgos_sys_config_set_wifi_sta_anon_identity( cfg->anon_identity );
  mgos_sys_config_set_wifi_sta_ca_cert( cfg->ca_cert );
  mgos_sys_config_set_wifi_sta_cert( cfg->cert );
  mgos_sys_config_set_wifi_sta_dhcp_hostname( cfg->dhcp_hostname );
  mgos_sys_config_set_wifi_sta_enable( mgos_sys_config_get_provision_wifi_success_enable() );
  mgos_sys_config_set_wifi_sta_gw( cfg->gw );
  mgos_sys_config_set_wifi_sta_ip( cfg->ip );
  mgos_sys_config_set_wifi_sta_key( cfg->key );
  mgos_sys_config_set_wifi_sta_nameserver( cfg->nameserver );
  mgos_sys_config_set_wifi_sta_netmask( cfg->netmask );
  mgos_sys_config_set_wifi_sta_pass( cfg->pass );
  mgos_sys_config_set_wifi_sta_ssid( cfg->ssid );
  mgos_sys_config_set_wifi_sta_user( cfg->user );

Need to figure out the right way to save the configuration instead of calling one by one ... looks like helper fns are just tied to fn that calls

mgos_conf_set_str(&mgos_sys_config->provision.wifi.sta.ssid, val)

Add EVENT triggers

Need to add some event triggers to trigger when testing is completed, in case user does not want to use callbacks

Library dependency problem

Hello! First of all I want to thank you for your wonderful work, I have a problem compiling "wifi-setup-web" when I add the dependency of your library.

Am I doing something wrong? I have not changed any file of the example project and when I do not add the dependency of your library compiles perfectly.

Log error:

console_log.txt

Support multiple STA configurations

Right now only the main STA is supported wifi.sta but need to add support for other station configurations, wifi.sta1 and wifi.sta2

Example use case would be if existing wifi.sta successfully connects, on test of new wifi ssid/pass, copy values from say wifi.sta to wifi.sta1 or wifi.sta2 before setting wifi.sta to values checked.

Also need to add support for setting specific sta number in yml configuration

Kernel Panic "Exception was unhandled" tcpip_adapter_set_hostname

Error:

[Jun  1 12:43:32.300] Guru Meditation Error: Core  0 panic'ed (LoadProhibited)
[Jun  1 12:43:32.302] . Exception was unhandled.

Core Dump:

#1  0x401b3d9e in tcpip_adapter_set_hostname (tcpip_if=TCPIP_ADAPTER_IF_STA,
Unmapped addr 0x1
    hostname=0x1 "")
    at /opt/Espressif/esp-idf/components/tcpip_adapter/tcpip_adapter_lwip.c:1137
#2  0x401b3df0 in tcpip_adapter_set_hostname_api (msg=0x3ffd9ac0)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.