Problem with save()

Posted on
  • Hi guys, first of all my apologies if i ask some newbie question but i have this problem and i can't find a solution for that.
    I've flashed my esp-12 with the latest firmware (thanks tve) and and if i try to save my code adding save(), the web IDE return erasing flash... writing... compressed byte (numbers) bytes to (numbers) but it hasn't saved nothing, just erased the flash.
    I've tried only to send an the http server example:

    var http = require("http");
    http.createServer(function (req, res) {
    res.writeHead(200);
    res.end("Hello World");
    }).listen(8080);

    and it works but just adding save() at the end of the code, erase the flash but the code don't work.
    What i'm doing wrong?
    Thanks for your time guys
    Marco

  • What do you expect it to do and what are you observing?
    Also, do you perhaps get a warning about the flash size and chip not corresponding when you reset Espruino, e.g. using reset()?

  • Hi tve, digging a little bit into the documentation i've found that save() save the code on the flas after (not before) the code i wrote, is it right? Is it bettor to use E.on('init',function()) instead of save?
    What i'm expecting is to try to save the code inside the flash so next time the esp boot it will read the code i wrote (i'm a little bit confused about this, because i understand that when the esp boot read immediately the espruino firmware and the javascript interpreter so the code need to be readed after the firmware... sorry if it can be stupid but i'm trying to understand how it works)

    so now writing save(); before the code i've got this error:

    Erasing Flash.....
    Writing...
    Compressed 12276 bytes to 703
    CheckUncaught SyntaxError: Got '(' expected ID
     at line 1 col 10
    function (req, res) {
             ^
    

    and to reply to your question, if i type reset()

     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v84.tve_master_588d193 Copyright 2015 G.Williams
    WARNING: the esp8266 port is in beta!
    Flash map 4MB:512/512, manuf 0xe0 chip 0x4016
    

    another strange behave that i found is when the ide send the code to the esp (without trying to save) i receive a warning message that "the module http is not found" but the webserver work.
    Many thanks.
    Marco

  • use load(); to get your code back and then use dump(); to display saved code

  • Hi MaBe
    if i write this

    save(); var http = require("http");
    http.createServer(function (req, res) {
      res.writeHead(200);
      res.end("Hello World"); 
    }).listen(8080);
    

    i get this

     http://espruino.com
     1v84.tve_master_588d193 Copyright 2015 G.Williams
    WARNING: the esp8266 port is in beta!
    Flash map 4MB:512/512, manuf 0xe0 chip 0x4016
    >echo(0);
    Erasing Flash.....
    Writing...
    Compressed 12276 bytes to 702
    CheckUncaught ReferenceError: "s" is not defined
     at line 1 col 2
    s.writeHead(200);
     ^
    

    And after if i load()
    it stays

    >load()
    =undefined
    Loading 702 bytes from flash...
    

    without doing nothing forcing me to switch reboot the esp

  • try this

    var http = require("http");
    http.createServer(function (req, res) {
      res.writeHead(200);
      res.end("Hello World"); 
    }).listen(8080);
    save();
    
  • what about the wifi setup code ?

  • @tve thanks, I've tried to do a search before post my question but I haven't found that thread. Maybe I did a wrong search.
    @MaBe what I found is that when I do a Wifi setup (from the tve tutorial) and I save it, next time I boot the esp, the settings are not saved and I have to repeat the whole process (I've followed the tutorial step by step). How is that possible?
    Thanks guys, I hope one day I can be useful to the community too.

  • Ok, after a while i successfull built something that seems to work:

    function onInit(){
      
      var wifi = require("Wifi");
      wifi.connect("ssid", {password: "password"},
       function(err){if(err)console.log(err);
                     else {
                       console.log("connesso!");
                       var http = require("http");
                       http.createServer(function (req, res) {
                       res.writeHead(200);
                       res.end("Hello World"); 
    
                       }).listen(8080);}});
      wifi.save();
    
      }
    save();
    

    Since @tve pointed me to the good explanation of @allObjects things are slightly more clear.
    Thanks everyone!

  • No.. it works only when i'm connected to the web ide via serial.
    If i try to power the esp standalone, the router don't give me an ip? Where it will be the trick?

  • Ok, got it (honestly i haven't realized what the function E.on do, but anyway... at the moment is working).

    E.on('init', function(){
      
    var wifi = require("Wifi");
      wifi.connect("ssid", {password: "password"},
       function(err){if(err)console.log(err);
                     else {
                       console.log("connesso!");
                       var http = require("http");
                       http.createServer(function (req, res) {
                       res.writeHead(200);
                       res.end("Hello World"); 
                       }).listen(8080);}});
       });
    

    And after send the code, do a save(); to store the program from RAM to FLASH.

    Thanks to everyone as pointed me to the solution.
    Marco

  • Please check out this draft tutorial http://microcosm.app/out/uvYYg for setting up your wireless so you can remove the wifi.connect stuff from your code. Then you start you code with the require("http") and http.creaseServer. You don't need to wait for the esp to actually connect to your wifi to start the http server.

  • @tve. I tried to put

    var wifi = require("Wifi");
    wifi.connect("myssid", {password:"mypass"}, function(err){
      console.log("connected? err=", err, "info=", wifi.getIP());
    });
    wifi.stopAP();
    wifi.save();
    

    And i'm connected, but next time i do reset() cycle and i try to write a wifi.getIP() i get this response

    >wifi.getIP()
    Uncaught ReferenceError: "wifi" is not defined
     at line 1 col 5
    wifi.getIP()
        ^
    

    It looks like the code is not stored into the flash

  • The wifi code is not supposed to be stored in the flash. start with reset(), then save() to empty what you have stored. Then ensure you have a wifi connection. Then wifi.save(). Then restart and make sure it comes up again and connects. Then load your code (without wifi commands) and save() it.

  • @tve, i've tried many times but wifi.save() don't save the wifi paramenters. If power down/up again and i try wifi.getIP() i receive an "wifi is not defined". If i put a save() inside the code and do the power cycle again doing a wifi.getIP() i have a response.
    Maybe because at the moment im using an old version? (1v84.tve_master_588d193).
    I'll try with the latest and i let you know.
    Anyway thanks.

  • Upgrading is a good idea :-)

  • @MarcoM, taking a look at ...explanation what save () does may help you to sort things out.

    What I did not cover there is what has to be done to make sure that after reset / power cycle 'ALL things are there to resume normal operation'. For my stuff I use sequencer and initializer that makes that happen.

    Decent SW design tries to separate concerns. Even though all things can abstractly be looked at as resources and treated equally - also sequence / time wise - they are not equal, and on top of it they have (inter)dependencies of existence and sequence / time. As if this is not enough complex yet, asynchronousity is added to the . Promises save you from callback hell, but they are not simple heaven either... ;-)

  • Thanks @allObjects, can you explain me what you mean by "sequencer and initializer" (or point me where I can read something about its) I'm a total newbie on JavaScript (trying to learn) and on programming in general (coming from the electronic field).
    Saluti.
    Marco

  • @MarcoM. Can I help you on this? I've covered many of the steps you're talking about, but though I haven't seen those particularly obstacles maybe I can. Also do you know of the Espruino Gitter channel?

    https://gitter.im/espruino/Espruino

  • @Ollie, thanks. Now i'm struggling on how i can invoke a function (startServer) just after the wifi.connected. How can i use the callback function of wifi.connected if the wifi parameters are saved outside the lines of code but they was stored before with wifi.save()? How can i callback they? Sorry if it can be a silly question, i'm starting out now with programming.

  • @Ollie, also thanks for the tip of gitter. I think here i'm going offtopic

  • No this is good here, but gitter sometimes faster - that's the only reason I mentioned it.

    That is a good question. Using wifi.save(), puts you outside of the normal callback approach, but you can assess connected status and do your own if test with wifi.getStatus()?

    http://www.espruino.com/Reference#t_l_Wi­fi_getStatus

    I have code to refactor now that the ESP8266 port is really quite polished. The above will be my approach. We need the "station" property and will be looking for "connected" as the value.

  • @MarcoM, ...regarding sequencer...

    Think about your electronics: some things you time - delay - with an RC-circuit, and some you setup for trigger on a rising or falling edge. That sums it up quite simply... may be a bit too simply, but it is a good starting point.

    Post continues in this post as part of the conversation that deals with save() and related things in more details.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Problem with save()

Posted by Avatar for MarcoM @MarcoM

Actions