with some critical off line (not shown) help from kh.
Let me post this help here publicly in case anyone needs it. But do try and avoid getting into this situation (i.e. never fully erase the flash on your Oak or overwrite the config sectors) because it's difficult to restore, especially on a Windows system.
Here is a procedure to restore an Oak that has been completely wiped or had its config and backup config sectors overwritten. It writes the bootloader, then sets the device id (you need to have a record of what it was), sets the Oak's copy of the Particle cloud public key, and finally sends the Oak's auto-generated public key to Particle. See
here for some additional info.
I don't know for sure that all of these steps are necessary - setting the server key in step 2 and sending the Oak key to Particle in step 3 might not be needed (if you test this, please let me know) - but they have worked successfully for both exeng and me.
Required software (on a Windows system):- Git for Windows
- Python 2.7. I use the Anaconda Python distribution as it makes installing the necessary Python packages easy.
- The pyserial python module - if you installed Anaconda, open a command prompt and type "conda install pyserial" to install it.
- The Particle CLI. This is a bit of a pain as it requires several prerequisites - see the installation instructions here.
- OakRestore from Digistump. Open a Git Bash Shell (installed with Git for Windows above), cd to the directory where you want to put it, then type "git clone https://github.com/digistump/OakRestore.git" to download it. Once it's done, type "cd OakRestore" and keep the window open to use for the commands below...
Procedure:1. Write the firmware and update imagesGet the Oak bootloader:
curl -O https://github.com/digistump/OakBoot/raw/master/oakboot.binWrite the bootloader, config and update firmware:
python esptool.py -p YOUR_COM_PORT -b 115200 write_flash -fs 32m 0x0000 oakboot.bin 0x2000 oaksetup_restore.bin 0x81000 oakupdate_restore.bin
2. Set the device id and the server public keyGet the Particle server public key:
curl -O https://s3.amazonaws.com/spark-website/cloud_public.derConnect to the Oak's AP and then send http requests to set these parameters. You can use a tool like the Chrome Postman (
https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en) extension, or my Python script:
import urllib2
import json
import binascii
import sys
data={'device-id':'xxxxxxxxxxxxxx'} # *** Insert the device id you noted earlier here
with open('cloud_public.der','rb') as f:
data['server-public-key']=binascii.hexlify(f.read())
response=urllib2.urlopen(urllib2.Request('http://192.168.0.1/set',json.dumps(data,separators=(',',':')),{'Content-Type': 'application/json'})).read()
if json.loads(response)['r']==0:
print 'Successfully set device id and server public key'
else:
print 'Failed to set device id and server public key'
To run this Python script, and the one below, save the code as a file in the current directory, e.g. script.py, then run it by typing "python script.py" in the Git Bash Shell.
3. Get the new public key your Oak generated and send it to the Particle CloudMy Python script will automate fetching the key from your device, but you can also do it manually.
import urllib2
import json
import os
response=urllib2.urlopen('http://192.168.0.1/device-id').read()
id=json.loads(response)['id']
response=urllib2.urlopen('http://192.168.0.1/public-key').read()
pubkey=json.loads(response)['b']
os.system('echo "{}" | xxd -r -p - | openssl rsa -inform der -pubin -pubout -out {}.pub.pem'.format(pubkey,id))
Then connect back to your normal WiFi and send the key with the Particle cli (replacing 'xxxxxx' with your device id)
particle keys send xxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxx.pub.pem4. Update your Oak to the current firmwareUse the
standard update procedure, or the
forced update method.