Change Arduino with ESP8266, Credentials with DroidTerm

More a project FOR Blynk than a project made with Blynk.

Screenshot above of DroidTerm running on my Samsung S3 phone to change the Blynk access credentials of the connected Arduino (with ESP8266). You can change the routers SSID, password and Blynk token details without having access to the Arduino sketch.

So you take that Blynk connected remote control buggy that you built round to a friend’s house and want to demonstrate it, but he doesn’t have access to the Arduino IDE. Or perhaps you have built the latest ‘must have’ Arduino based device but don’t want everyone to ‘borrow’ your sketch.

As long as your friends or clients have access to a Serial connection then they are good to go. The sketch below is the basic details you need to implement this. Very crude coding from a newbie, but it works.

Inspired by recent discussions here with @Lichtsignaal and the WiFIManager built by @tzapulica for ESP8266 standalone systems etc. Please note this sketch is for Arduinos with ESP8266 but NOT standalone.

/* 
   BlynkSecureV1.0.4.ino built with Arduino 1.6.4 on the 14th Jan 2016
   
   Arduino IOT products with Blynk access via ESP8266 NOT standalone.
   
   You want users to be able to use the Arduino IOT device you made, from different routers and
   maybe even with different Blynk tokens, but you don't want users to be able to change the sketch.
   Changes are simply made via Serial, no flash webpage options and users will need terminal access to
   your device like RealTerm http://realterm.sourceforge.net/index.html or Arduino Serial Monitor etc.
   Works with DroidTerm (Pro or Free versions) https://play.google.com/store/apps/details?id=com.felhr.droidterm
   
   
   1. Connect a switch to Arduino digital pin 5 (this is to change access credentials)
   2. Wire up the ESP with a separate switch
   3. 1. and 2. could be combined because you only want one switch on at a time
   4. Normal operation ESP ON and Access OFF
   5. To change Access details boot up with Access ON and ESP OFF
   6. Maximum characters: SSID 24 Password 16 Blynk Token 32
   7. Baud Rate setting of 115200 for both console and ESP
   8. Set Input options to LF and baud of 115200 when using DroidTerm 
*/
  //#define BLYNK_DEBUG
  #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
  #include <ESP8266_HardSer.h>
  #include <BlynkSimpleShieldEsp8266_HardSer.h>
  #include <EEPROM.h>
  // Set ESP8266 Serial object
  #define EspSerial Serial
  #define SwitchPin 5 // switch on D5
  ESP8266 wifi(EspSerial);

  char auth[33]; // max 32 plus NULL
  char ssid[25]; // max 24 plus NULL
  char pwd[17];  // max 16 plus NULL     total 75 i.e. array 0 to 74

  int address = 0;  // first EEPROM address
  char newbigstring[75];
  int proceedFlag = 1;
  String inputString;  // a string to hold incoming data
  boolean stringComplete = false;  // check if data entry string is complete
  char inChar;
  byte justonce = 0;



void setup()
{

   pinMode(SwitchPin, INPUT_PULLUP); // set D5 as input but set high when not pressed
   // Set console baud rate
   Serial.begin(115200);
   delay(10);
   for (unsigned int t=0; t<74; t++){   // 0 to 74 is 75 characters which includes 3 NULL's between SSID and pwd and final NULL after auth
      newbigstring[t] = EEPROM.read(t);
    }
    Serial.println("\n Access details for this system.");
    Serial.print("\n Network SSID:   ");
    for (unsigned int t=0; t<25; t++){    //24
      ssid[t] = newbigstring[t];
    }
    char * n = strchr (ssid, '\n');  // search for space or newline or carriage return
    if (n)     // if found, truncate
       *n = 0;        
    Serial.print(" <");
    Serial.print(ssid);
    Serial.print(">");    
    Serial.print("\n Password:       ");  // miss 1 memory address for final NULL 
    for (unsigned int t=25; t<42; t++){  //16
      pwd[t-25] = newbigstring[t];
    }    
    char * q = strchr (pwd, '\n');  // search for space or newline or carriage return
    if (q)     // if found, truncate
       *q = 0;        
    Serial.print(" <");
    //Serial.print(pwd);   // remove comment and comment out row below if you wish to display router password
    Serial.print("************");
    Serial.print(">");
    Serial.print("\n Blynk Token is: ");  // miss 1 memory address for final NULL
    for (unsigned int t=42; t<74; t++){  //32
      auth[t-42] = newbigstring[t];
    }
    char * r = strchr (auth, '\n');  // search for space or newline or carriage return
    if (r)     // if found, truncate 
       *r = 0;        
    Serial.print(" <");
    Serial.print(auth);
    Serial.print(">");  
    Serial.println("\n\n Turn OFF ESP and Turn ON Access Switch");
    Serial.println(" then reboot to change the access details");    
    Serial.println();
    delay(3000);   // wait 3 seconds to read credential details

    // Set ESP8266 baud rate
    EspSerial.begin(115200);
    delay(10);
  
    Blynk.begin(auth, wifi, ssid, pwd);
}

void(* resetFunc) (void) = 0; //reset function


void loop()
{
  if (digitalRead(SwitchPin) == LOW){
    if (justonce == 0){
       ChangeDetails();
       justonce++;  // i.e. just on bootup
    }    
  }
  if (digitalRead(SwitchPin) == HIGH){
      Blynk.run();
  }
}
        
void ChangeDetails(){  // change ssid, pwd or auth
  if (digitalRead(SwitchPin) == LOW){
    Serial.print("\n\n Enter your SSID: "); // max 24 characters
    if (stringComplete) {   // print the string when a newline arrives 
      inputString = "";     // clear the string
      stringComplete = false;
    }
  }
}

void serialEvent() {
  if(digitalRead(SwitchPin) == LOW){
    while (Serial.available()) {
      inChar = (char)Serial.read();  // get the new byte:
      inputString += inChar;     // add it to the inputString:
      if (inChar == '\n') {      // if the incoming character is a newline, set a flag so the main loop can do something about it
        Serial.println(inputString);
        if (proceedFlag == 1){
          int SSIDLen = inputString.length();
          for (unsigned int t=0; t<SSIDLen; t++){
            EEPROM.update(t, inputString[t]);    
            delay(10);
          }
        }
        if (proceedFlag == 2){
          int PWDLen = inputString.length();
           for (unsigned int t=0; t<PWDLen; t++){
            EEPROM.update((25+t), inputString[t]);    
            delay(10);
          }       
        }
        if (proceedFlag == 3){
          int AUTHLen = inputString.length();
           for (unsigned int t=0; t<AUTHLen; t++){
            EEPROM.update((42+t), inputString[t]);    
            delay(10);
          }       
        }      
        proceedFlag ++;       
        if (proceedFlag == 2){
          inputString = "";
          Serial.print (F(" Enter your WiFi password: "));
        }
        if (proceedFlag == 3){
          inputString = "";
          Serial.print (F(" Enter your 32 character Blynk token: "));
        }     
        if (proceedFlag > 3){
          Serial.println(" Turn Access Switch OFF and Swtch ON ESP");
          Serial.println(" New credentials stored, rebooting .....");
          delay(10000);  
          resetFunc();  // reset Arduino
        }
        stringComplete = true;     
    }
   }
  }
}
2 Likes

That’s interesting. @Pavel, @vshymanskyy have a look.

1 Like

thanks for correcting the url @Dmitriy

nice job @Costas, you should have a blog post or something to that effect somewhere.
the whole how you can use esp8266 s thing is very very confusing for people getting started, too many options

Thanks @tzapulica, you are right about confusing, only a few weeks ago I had know idea what the ESP8266 was all about. I actually flashed the Arduino IDE to the ESP8266 and hadn’t realised that my Arduino would no longer connect to the ESP.

As you may have guessed from the sketch I am not a coder so I was quite pleased that within a couple of days I had knocked up a portable system to change the Blynk credentials. It needs a LOT more work and maybe I will return to it at some stage but at least it proves the concept works.

With your WiFi Manager would it not be possible for a connected Arduino to read the ESP8266 SSID, pwd and a Blynk token?

you could make some serial or i2c interface i guess so the arduino talks to the esp and gets anything it needs, but it seems overly convoluted.

just bare in mind that the ESP flashed from arduino IDE is in essence an Arduino in itself, faster and with more memory, with all the capabilities of arduino + WiFi, limited only by the available pins, so, at least from what I’ve seen, people tend to forego the arduino.

there s even an esp board in the arduino form factor, so you can use shields and what not (see wemos d1)

Yes I’m thinking of moving over to the ESP8266 standalone in due course when I know more about them. Been looking at the Wemos D1’s recently and they are an absolute bargain. I have a big stock of Arduino’s that I was hoping to use but at the price of the D1’s it would be daft not to move over.

Look at the wemos d1 mini as well if you don t need the arduino form factor

@tzapulica my Wemos D1 mini arrived 3 or 4 days ago and I’m very impressed. Cheap, plenty of pins, lots of memory, regular 5V micro USB, small size, reliable and 5V output available (obviously 3.3V general ESP I/O).

I understand why they are supplied without the pin headers soldered as people have different requirements for them but my soldering skills are almost none existent. 9/10 for me and 10/10 if they become available with pin headers already attached.

1 Like