Template that includes wifi manager, lambda timer, and OTA

The example displays the up time in minutes and cycles through a list of names just for a thing to do.

I made it as a template for myself but decided that new programmers might like it as a place to start

I made it in 3 files
main

#include <FS.h>                   //this needs to be first, or it all crashes and burns...

#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson
#include "globals.h"


//create timer instance
BlynkTimer timer;



void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();
  
  // these two call get the data from the SPIFFS file and use the data to provision  blynk and wifi
  restoreparameters();
  startmywifi();

  Blynk.config(blynk_token);          // config Blynk
  
  ArduinoOTA.setHostname(modulename); // give an name to our module fo OTA
  ArduinoOTA.begin();                 // OTA initialization 
    
// Timed Lambda code
  timer.setInterval(1200L, [] () { //  <<=== set how often the code should run 1200L = Run every 1.2 seconds
// START timer Function 
    ArduinoOTA.handle();                        // check for OTA update
//       <--- put code lines here that you want to run repetedly in sync
    Blynk.virtualWrite(V0, millis() / 60000);   // Display the UpTime in Minutes
    Blynk.virtualWrite(V1, String(eml[j++]));   // Display the name
    if (j >= count) j = 0;
  });  // END Timer Function

}

void loop() {
  // in most cases you should not put code here
 if (Blynk.connected()){
    Blynk.run();
  } else Blynk.connect(1000l);                // timeout 1 seconds        
  timer.run();
}

globals.h

//  Global data
char modulename[] = "lambda";   //<===== this is the name for the OTA updates using the arduino IDE

// this is the data for the loop  you can remove it when you put in your own code
int j = 0;
int count = 4;
char eml[][20] {"mike", "john", "margarete", "shannon"};

//define your default values here, if there are different values in config.json, they are overwritten.
// if you are using BLYNK you may not need the mqtt items
char mqtt_server[40] = "your mqtt server";    // <======= this is the default data 
char mqtt_port[6] = "8080";                   // <========= this also
char blynk_token[34] = "YOUR_BLYNK_TOKEN";    // <======== and this

//flag for saving data
bool shouldSaveConfig = true;

Wifi



//callback notifying us of the need to save config
void saveConfigCallback () {
  shouldSaveConfig = true;
}

void restoreparameters() {

  if (SPIFFS.begin()) {
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);
        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        //        json.printTo(Serial);
        if (json.success()) {
          // json will parse the file for the variable name and load it into the supplied variable
          strcpy(mqtt_server, json["mqtt_server"]);     //get the mqtt_server <== you need one of these for each param
          strcpy(mqtt_port, json["mqtt_port"]);         //get the mqtt port
          strcpy(blynk_token, json["blynk_token"]);     //get the blynk token
        }
        configFile.close();
      }
    }
  }
}



void startmywifi() {
  //Local intialization. Once its done, there is no need to keep it around
  WiFiManager wifiManager;

  //set config save notify callback
  wifiManager.setSaveConfigCallback(saveConfigCallback);

  // The extra parameters to be configured (can be either global or just here)
  // After connecting, parameter.getValue() will get you the configured value
  // id/name placeholder/prompt default length
  WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
  WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32);

  //set static ip if you want
  //  wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

  //  <============== add all your parameters here
  wifiManager.addParameter(&custom_mqtt_server);
  wifiManager.addParameter(&custom_mqtt_port);
  wifiManager.addParameter(&custom_blynk_token);

  //reset settings - for testing
  //    wifiManager.resetSettings();    // <========= once you have this working you can comment out this line

  //set minimu quality of signal so it ignores AP's under that quality
  //defaults to 8%
  //wifiManager.setMinimumSignalQuality();

  //sets timeout until configuration portal gets turned off
  //useful to make it all retry or go to sleep
  //in seconds
  //wifiManager.setTimeout(120);

  //fetches ssid and pass and tries to connect
  //if it does not connect it starts an access point with the specified name
  //and goes into a blocking loop awaiting configuration
  if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
    ESP.reset();    //reset and try again
  }

  //if you get here you have connected to the WiFi

  //get the custom parameters here
  strcpy(mqtt_server, custom_mqtt_server.getValue());
  strcpy(mqtt_port, custom_mqtt_port.getValue());
  strcpy(blynk_token, custom_blynk_token.getValue());

  //check the flag to see if we save the custom parameters to FS
  if (shouldSaveConfig) {
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();

    //set the custom parameters here
    json["mqtt_server"] = mqtt_server;
    json["mqtt_port"] = mqtt_port;
    json["blynk_token"] = blynk_token;

    File configFile = SPIFFS.open("/config.json", "w");
    if (configFile) {
      //      json.printTo(Serial);       // dump the data to the serial
      json.printTo(configFile);   //write the custom parameters here
      configFile.close();
    } else Serial.println("failed to open config file for writing");
    //end save
  }
}
4 Likes