It's because the line require("ESP8266WiFi_0v25").connect is being executed when the code is uploaded to the board, rather than after save(). To fix it, just put that code into an function called onInit:
var soc;
var wifi;
function onInit() {
Serial3.setup(115200, { rx: D9, tx : D8 });
wifi = require("ESP8266WiFi_0v25").connect(Serial3, function(err) {
if (err) {digitalWrite(LED4,1);throw err;}
//wifi.at.debug();
console.log("Connecting to WiFi");
wifi.connect("Espruino",'freeWifi', function(err) {
if (err) throw err;
console.log("Connected");
require("net").createServer(function(c){
console.log("incomming connection");
c.on('data', onDataCallback);
c.on('close', function(){
console.log("bye");
});
soc = c;
}).listen(8080);
wifi.getIP(function(err,ip){
if(err === null){
console.log(ip);
serverIP = ip;
digitalWrite(LED2, 1);
}
});
});
});
}
function onDataCallback(data){
digitalWrite(LED1, !digitalRead(LED1));
console.log("Server received: " + data);
if(data !== undefined){
if(data.length >= 6 && data.substr(0, 3).toLowerCase() == "led"){
switch(data.charAt(3)){
case '1':digitalWrite(LED1, data.charAt(5));break;
case '2':digitalWrite(LED2, data.charAt(5));break;
case '3':digitalWrite(LED3, data.charAt(5));break;
case '4':digitalWrite(LED4, data.charAt(5));break;
}
}
soc.write("Response: " + data);
if(data.substr(0,4) == "exit")
soc.end();
}
}
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
It's because the line
require("ESP8266WiFi_0v25").connect
is being executed when the code is uploaded to the board, rather than aftersave()
. To fix it, just put that code into an function calledonInit
: