// TD-ARDUINO // stocker sur flash #include #include #include #define MAGIC_NUMBER 0x7423 // arbitrary number to double check the saved SSID #define MaxNet 30 // max amount of network to be saved int PosToBeSaved = 0; // Variable used to navigate the array of networks int daily_amount_of_food = 12; // The amount of food per day needed to survive // Struct of variable to be saved in flash memory typedef struct { int magic; boolean valid[MaxNet]; char SSIDs[MaxNet][100]; } Networks; FlashStorage(my_flash_store, Networks); Networks values; void setup() { Serial.begin(115200); while(!Serial); // wait until the Serial montior has be opened delay(2000); values = my_flash_store.read(); // Read values from flash memory if (values.magic == MAGIC_NUMBER) { // If token is correct print saved networks Serial.println("saved data:"); Serial.println(""); for (int a = 0; a < MaxNet; a++) { if (values.valid[a]) { Serial.println(values.SSIDs[a]); } else { PosToBeSaved = a; } } } } void loop() { // Temporarly save the number of networks int networks_already_saved = PosToBeSaved; getNetwork(); if (PosToBeSaved >= daily_amount_of_food) { Serial.println("Enough food for today"); } delay(5000); } // Feed the Nerd with networks's SSID void getNetwork() { // scan for nearby networks: Serial.println("\n*Scan Networks*\n"); int numSsid = WiFi.scanNetworks(); delay(1000); if (numSsid == -1) { Serial.println("There are no WiFi networks here.."); } else { Serial.print("number of available networks: "); Serial.println(numSsid); // print the network number and name for each network found: for (int thisNet = 0; thisNet < numSsid; thisNet++) { Serial.print("SSID: "); Serial.println(WiFi.SSID(thisNet)); delay(500); char* net = WiFi.SSID(thisNet); bool canBeSaved = true; // check if the network has already been saved for (int a = 0; a < PosToBeSaved ; a++) { if (values.valid[a]) { if (strncmp(net, values.SSIDs[a], 100) == 0 || strnlen(net, 100) == 0) { Serial.println("Not saved"); canBeSaved = false; } } } // Store ssid name if (canBeSaved && PosToBeSaved < MaxNet) { if (strlen(net) + 1 < 100 && strlen(net) > 0) { // check if the SSID name fits 100 bytes memset(values.SSIDs[PosToBeSaved], 0, sizeof(values.SSIDs[PosToBeSaved])); // set all characters to zero memcpy(values.SSIDs[PosToBeSaved], net, strlen(net) + 1); // copy "net" to values.SSDs[thisNet] values.valid[PosToBeSaved] = true; values.magic = MAGIC_NUMBER; my_flash_store.write(values); Serial.println(String(values.SSIDs[PosToBeSaved]) + " saved in position " + String(PosToBeSaved)); PosToBeSaved ++; } else { Serial.println(" network skipped"); } } } } }