
load('api_config.js');
load('api_events.js');
load('api_gpio.js');
load('api_mqtt.js');
load('api_net.js');
load('api_sys.js');
load('api_timer.js');
load('api_uart.js');

let led = Cfg.get('pins.led');
//let button = Cfg.get('pins.button');
//let topic = '/devices/' + Cfg.get('device.id') + '/events';
let is_wifi_connect = false;
let outTopic = Cfg.get('device.id')+'/Odemo';
let inTopic = Cfg.get('device.id')+'/Idemo';
let counter = 10;
let hudi_val = 30;
let uartNo = 0;   // Uart number used for this example
let rxAcc = '';   // Accumulated Rx data, will be echoed back to Tx
let value = false;

//print('LED GPIO:', led, 'button GPIO:', button);

// Configure UART at 115200 baud
UART.setConfig(uartNo, {
  baudRate: 115200,
  esp8266: {
    gpio: {
      rx: 03,
      tx: 01,
    },
  },
});


// Set dispatcher callback, it will be called whenver new Rx data or space in
// the Tx buffer becomes available
UART.setDispatcher(uartNo, function(uartNo) {
  let ra = UART.readAvail(uartNo);
  if (ra > 0) {
    // Received new data: print it immediately to the console, and also
    // accumulate in the "rxAcc" variable which will be echoed back to UART later
    let data = UART.read(uartNo);
    print('Received UART data:', data);
    rxAcc += data;
  }
}, null);


// Enable Rx
UART.setRxEnabled(uartNo, true);

let getInfo = function() {
  return JSON.stringify({
    total_ram: Sys.total_ram(),
    free_ram: Sys.free_ram()
  });
};

// Blink built-in LED every second
GPIO.set_mode(led, GPIO.MODE_OUTPUT);
Timer.set(1000 /* 1 sec */, Timer.REPEAT, function() {
  if(is_wifi_connect){
    GPIO.write(led,0);
  }else{
    let value = GPIO.toggle(led);
  }
    //print(value ? 'Tick' : 'Tock', 'uptime:', Sys.uptime(), getInfo());

}, null);
Timer.set(4000 /* 1 sec */, Timer.REPEAT, function() {
    if(!is_wifi_connect)
      return;

    let message = 'Temp:';
    counter++;
    if(counter >100)
      counter = 10;
    if(counter < 100){
      message = message+'0'+JSON.stringify(counter);
    }else{
    
      message = message+JSON.stringify(counter);
    }
    message = message + ',Humi:';
    hudi_val ++;
    if(hudi_val >100)
      hudi_val = 10;
    if(hudi_val < 100){
      message = message+'0'+JSON.stringify(hudi_val);
    }else{
    
      message = message+JSON.stringify(hudi_val);
    } 
    let ok = MQTT.pub(outTopic, message, 1);
    print('Published:', ok ? 'yes' : 'no', 'topic:', outTopic, 'message:', message);   
  
  
}, null);
// Publish to MQTT topic on a button press. Button is wired to GPIO pin 0
/*
GPIO.set_button_handler(button, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
  let message = getInfo();
  let ok = MQTT.pub(topic, message, 1);
  print('Published:', ok, topic, '->', message);
}, null);
*/

MQTT.sub(inTopic, function(conn, topic, msg) {
  print('Topic:', inTopic, 'message:', msg);
}, null);



// Monitor network connectivity.
Event.addGroupHandler(Net.EVENT_GRP, function(ev, evdata, arg) {
  let evs = '???';
  if (ev === Net.STATUS_DISCONNECTED) {
    evs = 'DISCONNECTED';
    is_wifi_connect = false;
  } else if (ev === Net.STATUS_CONNECTING) {
    evs = 'CONNECTING';
  } else if (ev === Net.STATUS_CONNECTED) {
    evs = 'CONNECTED';
    is_wifi_connect = true;
  } else if (ev === Net.STATUS_GOT_IP) {
    evs = 'GOT_IP';
  }
  print('== Net event:', ev, evs);
}, null);
