Revision 7d332357

View differences:

PiBox/imlightsw.ino
41 41
/*
42 42
 * Blink Patterns
43 43
 */
44
#define SPIFFMOUNT_FAIL         blinkPattern(3,250)
45
#define SPIFFOPEN_FAIL          blinkPattern(4,150)
44
#define BP_CONNECTED            blinkPattern(4,1000)
45
#define BP_CONFIG_MODE          blinkPattern(2,500)
46
#define BP_CONNECT_MODE         blinkPattern(5,125)
47
#define BP_PAIR1_MODE           blinkPattern(4,150)
48
#define BP_PAIR2_MODE           blinkPattern(4,750)
46 49

  
47 50
/*
48 51
 * Filename for saving credentials.
......
138 141
 */
139 142
char flags = 0;
140 143
#define FL_REGISTERED       0x01   // If this flag is set then we're paired already.
144
#define FL_IN_REGISTRATION  0x02   // If this flag is set then we're currently trying to register.
141 145

  
142 146
/*
143 147
 * GPIO Pins
......
183 187
{
184 188
    boolean pairMode = false;
185 189

  
190
    /* Set pin modes */
191
    pinMode(0, OUTPUT);
192
    pinMode(2, INPUT);
193

  
186 194
    /* Set GPIO0 output low */
187 195
    // PRINTLN("PAIR Mode: Set GPIO0 low.");
188
    pinMode(0, OUTPUT);
189 196
    digitalWrite(0, LOW);
190 197

  
191 198
    /* check GPIO2 input to see if push button pressed connecting it to GPIO0 */
......
489 496
        flags |= FL_REGISTERED;
490 497

  
491 498
        /* Stop registration multicast */
499
        flags &= FL_IN_REGISTRATION;
492 500
        t.stop(registerID);
493 501
    }
494 502
}
......
761 769
 */
762 770
void blinkPattern(int count, int interval)
763 771
{
764
    /* All blink patterns repeat 5 times, with 2 seconds between patterns. */
765
    for(int i=0; i<5; i++)
766
    {
767
        /* Three fast blinks of the LED */
768
        for(int j=0; j<count; j++)
769
        {
770
            SETBLUELED(true); delay(interval); SETBLUELED(false);
771
            PRINT(".");
772
        }
773
        delay(2000);
774
    }
775
}
776

  
777
/*
778
 * ========================================================================
779
 * Credentials Handlers
780
 * ========================================================================
781
 */
782

  
783
/*
784
 * Max lengths:
785
 * SSID: 32 characters
786
 * PW: 64 characters
787
 */
788
struct credentials_t {
789
    char    ssid[32];
790
    char    pw[64];
791
    char    ok[3];
792
};
793
credentials_t credentials;
794

  
795
void printCredentials(String header)
796
{
797
    PRINT(header);
798
    PRINTLN(": " );
799

  
800
    PRINT("ssid: " );
801
    PRINTLN(credentials.ssid);
802
    PRINT("pw: ");
803
    // PRINTLN(strlen(credentials.pw)>0?"********":"<no password>");
804
    PRINTLN(credentials.pw);
805
    PRINT("ok: ");
806
    PRINTLN(credentials.ok);
807
}
808

  
809
void clearCredentials()
810
{
811
    memset(credentials.ssid, 0, 32);
812
    memset(credentials.pw, 0, 64);
813
    memset(credentials.ok, 0, 3);
814
}
815

  
816
void saveCredentials()
817
{
818
#if 0
819
    sprintf(credentials.ok, "OK");
820
    printCredentials("Saving Credentials");
821

  
822
    EEPROM.begin(128);
823
    EEPROM.put(0, credentials);
824
    EEPROM.commit();
825
    EEPROM.end();
826
#endif
827

  
828
#if 1
829
    FSInfo fs_info;
830
    String ssid = "";
831
    String pw   = "";
832

  
833
    if ( !SPIFFS.begin() )
834
    {
835
        /* Three fast blinks of the LED */
836
        PRINTLN("Failed to mount SPIFFS.  Can't save SSID and PW!");
837
        while(1)
838
        {
839
            SPIFFMOUNT_FAIL;
840
        }
841
    }
842

  
843
    SPIFFS.info(fs_info);
844
    PRINTLN("FS Total bytes: ");
845
    PRINTLN(fs_info.totalBytes);
846

  
847
    /*
848
     * Now save the SSID and PW for operational mode.
849
     */
850
    if ( !SPIFFS.exists( WIFI_CREDENTIALS ) )
851
    {
852
        PRINTLN("Formatting SPIFFS for the first time.");
853
        if ( !SPIFFS.format() )
854
        {
855
            PRINTLN("SPIFFS format failed.");
856
        }
857
        else
858
        {
859
            PRINTLN("SPIFFS has been formatted.");
860
        }
861
    }
862

  
863
    File file = SPIFFS.open(WIFI_CREDENTIALS, "w");
864
    if (!file) 
865
    {
866
        /* Four fast blinks of the LED */
867
        PRINTLN("Failed to open wifi.txt.  Can't save SSID and PW!");
868
        while(1)
869
        {
870
            SPIFFOPEN_FAIL;
871
        }
872
    }
873

  
874
    ssid = WiFi.SSID();
875
    pw   = WiFi.psk();
876
    PRINTLN("Saving SSID/PW: " + ssid + " / " + pw);
877
    file.println(ssid); 
878
    file.println(pw); 
879
    file.close();
880

  
881
#endif
882
}
883

  
884
boolean loadCredentials()
885
{
886
#if 0
887
    printCredentials("Loading Credentials");
888
    EEPROM.begin(512);
889
    EEPROM.get(0, credentials);
890
    EEPROM.end();
891

  
892
    if (String(credentials.ok) != String("OK")) {
893
        PRINTLN("Failed to get credentials");
894
        return false;
895
    }
896
    printCredentials("Recovered credentials");
897
    return true;
898
#endif
899

  
900
#if 1
901
    String ssid = "";
902
    String pw   = "";
903

  
904
    /*
905
     * Open SPIFFS and read credentials.
906
     */
907
    if ( !SPIFFS.begin() )
908
    {
909
        /* Three fast blinks of the LED */
910
        PRINTLN("Failed to mount SPIFFS.  Can't read SSID and PW!");
911
        while (1)
912
        {
913
            SPIFFMOUNT_FAIL;
914
        }
915
    }
916

  
917
    File file = SPIFFS.open(WIFI_CREDENTIALS, "r");
918
    if (!file) 
919
    {
920
        /* Four fast blinks of the LED */
921
        PRINTLN("Failed to open wifi.txt.  Can't read SSID and PW!");
922
        while(1)
923
        {
924
            SPIFFOPEN_FAIL;
925
        }
926
    }
927
    while (file.available())
772
    /* Blink patterns repeat "count" times, with "interval" milliseconds between patterns. */
773
    for(int i=0; i<count; i++)
928 774
    {
929
        /* Read credentials, line by line */
930
        String line = file.readStringUntil('\n');
931
        if ( ssid.equals("") )
932
        {
933
            ssid = line;
934
        }
935
        else
936
        {
937
            pw = line;
938
            break;
939
        }
775
        SETBLUELED(true); delay(interval); SETBLUELED(false); delay(interval);
776
        PRINT(".");
940 777
    }
941
    file.close();
942
    sprintf(credentials.ssid, "%s", ssid.c_str());
943
    sprintf(credentials.pw, "%s", pw.c_str());
944
    printCredentials("Loaded credentials from SPIFF");
945
#endif
946 778
}
947 779

  
948 780
/*
......
971 803
     */
972 804
    SERIAL_BEGIN(115200);
973 805
    PRINTLN("");
806
    PRINTLN("Waiting on SDK.");
974 807
#ifdef USE_SERIAL
975 808
#ifdef DEBUG_CORE
976 809
    Serial.setDebugOutput(true);
977 810
#endif
978
    delay(1000);
979 811
#endif
980
    PRINTLN("Configuring WifiManager.");
981

  
982
#if 0
983
    if ( !SPIFFS.begin() )
984
    {
985
        /* Three fast blinks of the LED */
986
        PRINTLN("Failed to mount SPIFFS.  Can't save SSID and PW!");
987
        while(1)
988
        {
989
            SPIFFMOUNT_FAIL;
990
        }
991
    }
992
#endif
993
    /* Wait for internal wifi to come up */
994
    PRINTLN("Waiting on SDK wifi.");
995 812
    delay(3500);
996 813

  
997 814
    /*
......
1000 817
    wifiManager.setDebugOutput(true);
1001 818
    wifiManager.setConnectTimeout(30);
1002 819

  
1003
#ifdef NOWM
1004

  
1005
    // Without WiFiManager, this would do the connection for us.
1006
    PRINTLN("No WifiManager - Using WiFi instead.");
1007
    WiFi.begin(ssid, password);
1008
    PRINTLN("");
1009

  
1010
    // Wait for connection
1011
    while (WiFi.status() != WL_CONNECTED)
1012
    {
1013
        delay(500);
1014
        PRINTLN(".");
1015
    }
1016

  
1017
    // Do we need this for IoT?
1018
    if (MDNS.begin("esp8266"))
1019
    {
1020
        PRINTLN("MDNS responder started");
1021
    }
1022

  
1023
#else // NOWM
1024

  
1025 820
    /*
1026 821
     * Set callback to be notified when connection to last configured
1027 822
     * remote AP fails.
......
1052 847
    pinMode(0, OUTPUT);
1053 848
    digitalWrite(0, LOW);
1054 849

  
1055
    /* check GPIO2 input to see if push button pressed connecting it to GPIO0 */
1056
    PRINTLN("Checking GPIO2 state.");
850
    /*
851
     * Check GPIO2 input to see if push button is pressed (connecting it to GPIO0)
852
     * placing us in ConfigMode.
853
     */
854
    PRINTLN("Checking boot state.");
1057 855
    configMode = (digitalRead(2) == LOW);
1058 856
    if (configMode)
1059 857
    {
1060 858
        PRINTLN("In Config Mode.");
1061
#if 1
859
        BP_CONFIG_MODE;
860
        delay(1000);
861
        SETBLUELED(true);
862

  
1062 863
        /*
1063
         * start AP and get ready to serve config web page
1064
         * leave relay on to indicate in config mode
864
         * Start AP and serve up web page configuration.
1065 865
         */
1066 866
        while ( 1 )
1067 867
        {
......
1070 870
            {
1071 871
                break;
1072 872
            }
1073

  
1074 873
            message = "Failed connection to ";
1075 874
            message += WiFi.SSID();
1076 875
            message += "; trying again.";
1077 876
            PRINTLN(message);
1078 877
        }
878
        BP_CONNECTED;
1079 879
        PRINTLN("");
1080 880
        PRINT("Connected to ");
1081 881
        PRINTLN(WiFi.SSID());
1082 882
        PRINT("IP address: ");
1083 883
        PRINTLN(WiFi.localIP());
1084 884
        return;
1085

  
1086
#endif
1087
#if 0
1088
        /* Save the credentials to EEPROM */
1089
        sprintf(credentials.ssid, "%s", WiFi.SSID().c_str());
1090
        sprintf(credentials.pw, "%s", WiFi.psk().c_str());
1091
        saveCredentials();
1092
        clearCredentials();
1093
        printCredentials("Cleared");
1094
        loadCredentials();
1095
#endif
1096

  
1097
#if 0
1098
        /*
1099
         * Tries to connect with last known settings.
1100
         * If connection fails, starts an access point with no password
1101
         * and waits for configuration.
1102
         */
1103
        PRINTLN("Trying WiFiManager autoconnect.");
1104
        while ( 1 )
1105
        {
1106
            wifiManager.autoConnect((char *)apname.c_str());
1107
            if (WiFi.status() == WL_CONNECTED)
1108
            {
1109
                break;
1110
            }
1111
            message = "Failed connection to ";
1112
            message += WiFi.SSID();
1113
            message += "; trying again.";
1114
            PRINTLN(message);
1115
        }
1116
#endif
1117 885
    }
1118 886
    else
1119 887
    {
......
1124 892
         * then we simply punt in this mode.
1125 893
         */
1126 894
        PRINTLN("In Operational Mode.");
1127

  
1128
        /* Get the credentials from EEPROM */
1129
        // clearCredentials();
1130
        // loadCredentials();
1131 895
        WiFi.begin();
1132

  
1133
#if 0
1134 896
        while ( 1 )
1135 897
        {
1136
            /*
1137
             * Copied from WiFiManager.connectWifi() because that function is private.
1138
             */
1139
            WiFi.disconnect();
1140
            // WiFi.begin(credentials.ssid, credentials.pw);
1141
            WiFi.begin();
1142
            WiFi.waitForConnectResult();
1143

  
1144 898
            if (WiFi.status() == WL_CONNECTED)
1145 899
            {
1146 900
                break;
1147 901
            }
1148 902
            message = "Failed connection to ";
1149
            message += credentials.ssid;
903
            message += WiFi.SSID();
1150 904
            message += "; trying again.";
1151 905
            PRINTLN(message);
906
            BP_CONNECT_MODE;
907
            delay(1500);
1152 908
        }
1153
#endif
909
        BP_CONNECTED;
910
        PRINTLN("imiot setup:");
911
        PRINT("Connected to: "); PRINTLN(WiFi.SSID());
912
        PRINT("IP address  : "); PRINTLN(WiFi.localIP());
1154 913
    }
1155 914

  
1156 915
    /* Reset GPIO0 HIGH to turn off the relay */
1157 916
    PRINTLN("Resetting GPIO0.");
1158 917
    digitalWrite(0, HIGH);
1159 918

  
1160
#endif // NOWM
1161

  
1162
    PRINTLN("imiot setup:");
1163
    PRINT("Connected to: "); PRINTLN(WiFi.SSID());
1164
    PRINT("IP address  : "); PRINTLN(WiFi.localIP());
1165

  
1166
    /*
1167
     * Reset the WebServer to use the configured IP.
1168
     * This only works with the version from GIT at the moment.
1169
     * server.reset(new ESP8266WebServer(WiFi.localIP(), 80));
1170
     */
1171

  
1172 919
    /*
1173 920
     * Setup web server URI handlers: this is done in a function to make
1174 921
     * it easy to find where device specific code will go.
......
1208 955
    {
1209 956
        return;
1210 957
    }
1211
    // PRINTLN("Not in Config Mode.");
1212 958

  
1213 959
    /*
1214 960
     * If we're not registered AND PAIR mode button is set, go into pairing mode.
1215 961
     */
1216 962
    if ( !(flags & FL_REGISTERED) && pairEnabled() )
1217 963
    {
1218
        PRINTLN("Attempting pair mode.");
1219
        SETBLUELED(true);
1220
        delay(2000);
1221

  
1222 964
        /* Check for registration events */
1223
        PRINTLN("Scheduling deviceRegister.");
1224
        registerID = t.every(3000, deviceRegister);
965
        if ( !(flags & FL_IN_REGISTRATION) )
966
        {
967
            PRINTLN("Attempting pair mode.");
968
            PRINTLN("Scheduling deviceRegister.");
969
            registerID = t.every(3000, deviceRegister);
970
        }
971
        BP_PAIR1_MODE;
972
        BP_PAIR2_MODE;
973

  
1225 974
        t.update();
1226 975
    }
1227 976
    else
1228 977
    {
1229

  
1230 978
        /*
1231 979
         * Operational Mode.
1232 980
         */

Also available in: Unified diff