This example project explains the following: * Reading and writing NFC tag * Use Flash storage to store Javascript source code which can be executed when the board is turned on. * Downloading JavaScript program from url. Project demo: Writes and runs JS program from Flash storage by downloading from internet using URL from NFC tag. NFC tag must contain the following info: 1- Password 2- URL separated by: , Note: This project is based on Mbed JavaScript and hence cannot be compiled using MBed Compiler. Read Readme.md for information about compiling and running this project.

Files at this revision

API Documentation at this revision

Comitter:
akhtar.syedzeeshan@gmail.com
Date:
Sun Aug 19 21:39:08 2018 +0500
Commit message:
First commit

Changed in this revision

.DS_Store Show annotated file Show diff for this revision Revisions of this file
Gulpfile.js Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
js_manager.js Show annotated file Show diff for this revision Revisions of this file
main.js Show annotated file Show diff for this revision Revisions of this file
package.json Show annotated file Show diff for this revision Revisions of this file
Binary file .DS_Store has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Gulpfile.js	Sun Aug 19 21:39:08 2018 +0500
@@ -0,0 +1,5 @@
+'use strict';
+
+const gulp = require('gulp');
+
+require('mbed-js-gulp')(gulp);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Sun Aug 19 21:39:08 2018 +0500
@@ -0,0 +1,126 @@
+# mbed-js-st-fw-nfc02a1-example
+Example project for using Flash storage to store JS code and NFC tag reading and writing.
+
+## Description
+This example project explains the following:
+* Reading and writing NFC tag
+* Use Flash storage to store Javascript source code which can be executed when the board is turned on.
+* Downloading JavaScript program from url.
+
+## To build:
+
+```
+git clone https://github.com/STMicroelectronics-CentralLabs/mbed-js-st-examples
+cd mbed-js-st-examples/mbed-js-st-fw-nfc02a1-example
+npm install
+gulp --target=NUCLEO_F429ZI
+```
+See build/out/NUCLEO_F429ZI/mbedos5.bin.
+
+## Errors building:
+When you will try to build, you will receive errors building the project, it is because you have not defined a few parameters. Try the following tasks:
+### * Configuration error
+
+Open build/jerryscript/targets/mbedos5/mbed_app.json and modify the file as follows:
+
+```
+{
+    "config": {
+        "network-interface": {
+            "help": "options are ETHERNET,WIFI_ESP8266,WIFI_ODIN,MESH_LOWPAN_ND,MESH_THREAD",
+            "value": "ETHERNET"
+        },
+        "mesh_radio_type": {
+        	"help": "options are ATMEL, MCR20",
+        	"value": "ATMEL"
+        },
+        "esp8266-tx": {
+            "help": "Pin used as TX (connects to ESP8266 RX)",
+            "value": "D1"
+        },
+        "esp8266-rx": {
+            "help": "Pin used as RX (connects to ESP8266 TX)",
+            "value": "D0"
+        },
+        "esp8266-debug": {
+            "value": false
+        },
+        "wifi-ssid": {
+            "value": "\"SSID\""
+        },
+        "wifi-password": {
+            "value": "\"Password\""
+        }
+    },
+    "macros": ["MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES",
+               "JERRY_JS_PARSER 1", "JSMBED_USE_RAW_SERIAL", "JSMBED_OVERRIDE_JERRY_PORT_CONSOLE"],
+    "target_overrides": {
+        "*": {
+            "target.features_add": ["NANOSTACK", "LOWPAN_ROUTER"],
+            "mbed-mesh-api.6lowpan-nd-channel-page": 0,
+            "mbed-mesh-api.6lowpan-nd-channel": 12,
+            "mbed-trace.enable": 0,
+            "mbed-http.http-buffer-size": 2048,
+            "platform.stdio-baud-rate": 115200,
+            "platform.stdio-convert-newlines": true
+        },
+        "NUCLEO_F429ZI": {
+            "target.restrict_size": "0x80000"
+        }
+    }
+}
+
+```
+where target.restrict_size is the size reserved for binary, the rest of the space will be used for Flash storage.
+
+### * Pin generation error
+For some boards e.g. NUCLEO_F429ZI, pin generation is failing in Jerryscript. For the time being, you can skip some pin expressions in configuration to build for most boards by following these steps:
+
+Open build/jerryscript/targets/mbedos5/tools/generate_pins.py
+
+Find this line:
+```
+        pins[pin.name] = evaluator.eval(expr.strip())
+```
+Put this line in try statement:
+```
+        try:
+            pins[pin.name] = evaluator.eval(expr.strip())
+        except:
+            print("[Warning] Skipping pin name: " + expr.strip())
+```
+
+### * Easy-connect error
+If easy-connect fails, try updating mbed-os library.
+
+Open terminal (in Linux, Mac OS X) or command prompt (in Windows) and run the following commands:
+```
+cd build/jerryscript/targets/mbedos5/mbed-os
+mbed update ca661f9d28526ca8f874b05432493a489c9671ea
+```
+
+### * Error initializing X-NUCLEO-NFC02A1 expansion board
+if you see this error on serial terminal:
+```
+Failed to init XNucleoNFC02A1 expansion board!
+Error:0x1
+```
+
+You have to move the 'X_NUCLEO_NFC02A1' library from 'mbed-js-st-fw-nfc02a1-example/node_modules/mbed-js-st-x-nucleo-nfc02a1/NFC02A1/X_NUCLEO_NFC02A1' to 'mbed-js-st-fw-nfc02a1-example/build/jerryscript/targets/mbedos5/X_NUCLEO_NFC02A1'
+
+Remember to Move, not Copy.
+
+## Serial port Testing
+Open any serial port terminal application of your choice using settings of 115200-8N1 and try the following functions:
+```js
+// To load a js source code from HTTP url and save in Flash storage, type the following command and press Ctrl+R to run.
+load_http_program('http://mysite.com/source.js');
+
+// To erase the Flash storage, type the following command and press Ctrl+R to run.
+erase_flash();
+
+// To reboot device,, type the following command and press Ctrl+R to run.
+reboot();
+```
+## Writing to flash from terminal
+If you want to write a JS program to Flash storage offline. Open any serial port terminal application of your choice and write JS code as you like and then press Ctrl+F to flash the code. If you want to test the code only, press Ctrl+R.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/js_manager.js	Sun Aug 19 21:39:08 2018 +0500
@@ -0,0 +1,121 @@
+var js_manager = new JSManager();
+
+//print("\r\njs_manager.js has finished executing.");
+
+run_program_from_flash = function(){
+    print('\33[36mRunning js from flash...\33[0m'); // color back to normal
+    var result = js_manager.run_js_flash();
+    
+    if(result == 0){
+        print("\33[32mProgram execution successful!\33[0m");
+    }
+    else if(result == 1){
+        print("\33[35mFlash is empty!\33[0m");
+    }
+    else if(result == 2){
+        print("\33[31mProgram execution failed! Error parsing code.\33[0m");
+    }
+    else if(result == 3){
+        print("\33[31mProgram execution failed! Error in execution.\33[0m");
+    }
+    
+}
+
+load_http_program = function(url){
+    var result = js_manager.load_http_program(url);
+
+    if(result == 0){
+        print("\33[32mProgram downloaded successfully!\33[0m");
+    }
+    else if(result == 1){
+        print("\33[31mServer could not be located!\33[0m");
+    }
+    else if(result == 2){
+        print("\33[31mProgram not found!\33[0m");
+    }
+    else if(result == 3){
+        print("\33[31mInvalid Program file! Contains errors.\33[0m");
+    }
+    else if(result == 4){
+        print("\33[31mNetwork could not be initalized!\33[0m");
+    }
+    return result;
+};
+
+load_nfc_program = function(file){
+    var s = String.fromCharCode(47);
+    
+    var protocol =  'http:'; //http:' + '/' + '/' + 'www' + '.indigost' + '.com' + '/' + 'code.js';
+    var div = s + s;
+    var website = 'www.indigost.com' + s + 'cloud' + s + 'index.php' + s + 's';
+
+    var link = protocol + div + website + s + file + s + 'download';
+    
+    print(link);
+    
+    var result = js_manager.load_http_program(link);
+
+    if(result == 0){
+        print("\33[32mProgram downloaded successfully!\33[0m");
+    }
+    else if(result == 1){
+        print("\33[31mServer could not be located!\33[0m");
+    }
+    else if(result == 2){
+        print("\33[31mProgram not found!\33[0m");
+    }
+    else if(result == 3){
+        print("\33[31mInvalid Program file! Contains errors.\33[0m");
+    }
+    else if(result == 4){
+        print("\33[31mNetwork could not be initalized!\33[0m");
+    }
+    return result;
+};
+
+gg = function(){
+    // Symbol / is causing problems sometimes in a string so we get it this way 
+    var s = String.fromCharCode(47);
+    
+    var protocol =  'http:'; //http:' + '/' + '/' + 'www' + '.indigost' + '.com' + '/' + 'code.js';
+    var div = s + s;
+    var website = 'www.indigost.com';
+    var file = 'code.js';
+    var link = protocol + div + website + s + file;
+
+    print('Opening: ' + link);
+    var result = js_manager.load_http_program(link);
+
+    if(result == 0){
+        print("\33[32mProgram downloaded successfully!\33[0m");
+    }
+    else if(result == 1){
+        print("\33[31mServer could not be located!\33[0m");
+    }
+    else if(result == 2){
+        print("\33[31mProgram not found!\33[0m");
+    }
+    else if(result == 3){
+        print("\33[31mInvalid Program file! Contains errors.\33[0m");
+    }
+    else if(result == 4){
+        print("\33[31mNetwork could not be initalized!\33[0m");
+    }
+    return result;
+};
+
+erase_flash = function(){
+    var result = js_manager.erase_flash();
+    if(result == 0 ){
+        return ('Flash erased successfully!');
+    }
+    else{
+        return ('Flash erasing failed with error code: ' + result);
+    }
+};
+
+reboot = function(){
+    return js_manager.reboot();
+}
+
+module.exports = js_manager;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.js	Sun Aug 19 21:39:08 2018 +0500
@@ -0,0 +1,47 @@
+var js_manager = require('./js_manager');
+var serial_interface = new SerialInterface();
+
+var button = InterruptIn(BUTTON1);
+
+var security_key = 'F38sZ2a9';
+
+write_nfc_tag = function(){
+    var i2c = DevI2C(D14, D15);
+    var nfc = NFC02A1();
+    nfc.init(i2c);
+    nfc.write_tag("F38sZ2a9,F4Jq6yc2D6yxKqW");
+}
+
+process_nfc = function(){
+    var i2c = DevI2C(D14, D15);
+    var nfc = NFC02A1();
+    nfc.init(i2c);
+    tag_data = nfc.read_tag();
+    tag_array = tag_data.split(",");
+    if(security_key.localeCompare(tag_array[0]) == 0){
+        print("\33[32mSecurity key matched!\33[0m");
+        print('\33[32mDownloading file: ' + tag_array[1] + '\33[0m');
+        load_nfc_program(tag_array[1]);
+    }
+    else{
+        print("\33[31mSecurity key mismatched!\33[0m");
+    }    
+}
+
+button.fall(function() {
+    process_nfc();
+    //gg();
+});
+
+print('Running in 5 seconds...')
+setTimeout(function(){
+    print('')
+    run_program_from_flash();
+    button.fall(function() {
+        process_nfc();
+        //gg();
+    });
+}, 5000);
+
+print("Press button for demo.\r");
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/package.json	Sun Aug 19 21:39:08 2018 +0500
@@ -0,0 +1,21 @@
+{
+  "name": "mbed-js-st-fw-nfc02a1-example",
+  "version": "1.0.0",
+  "description": "Example of loading JS firmware from flash using nfc authentication",
+  "main": "main.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "ST",
+  "license": "Apache-2.0",
+  "dependencies": {
+    "mbed-js-st-js-manager": "^1.0.0",
+    "mbed-js-st-serial-interface": "^1.0.0",
+    "mbed-js-st-network-interface": "^1.0.0",
+    "mbed-js-st-devi2c": "^1.0.0",
+    "mbed-js-st-x-nucleo-nfc02a1": "^1.0.0"
+  },
+  "devDependencies": {
+    "mbed-js-gulp": "github:ARMmbed/mbed-js-gulp"
+  }
+}