Download demo program folder and unzip into Documents/arduino/..

https://iotleaf.co.uk/download/training1f.zip

 

/*
 version 1f
 changing from dots to underlines in fieldnames
  '.d.08' ---> '_d_08'
*/
// *********************************************************
// No need to change this part!
// scroll down to 'Your own code' section..
// *********************************************************
#include "kicchip.h"
#include "include_me.h"
// *********************************************************

// *********************************************************
// Your own code from here down..
// *********************************************************

// ----------------------------------  
// Pin connections
// ----------------------------------  
// built in led
// xaio c++ version too old in C++98 
//'my_kicchip_serial_scan' must be initialized by constructor, not by '{...}'
/*
  pin connections
  ===
  13  ->> led (due)
  12  ->> led (nano)
  10  ->> btn_a
   9  ->> btn_gnd  
   8  ->> btn_b
   6  ->> led (mkr1000)
   5  ->> pwm_ground
   3  ->> pwm_supply
   2  ->> led xaio (probably).
  A5  ->> input by mcp9700 
  A1  ->> input by user
  A0  ->> input by user
 */
constexpr as int k_pin_in_10         {10} ;
constexpr as int k_pin_ground        { 9} ;
constexpr as int k_pin_in_08         { 8} ;
constexpr as int k_pin_ground_pwm    { 5} ; 
constexpr as int k_pin_supply_pwm    { 3} ;
constexpr as int k_pin_mcp700_input  {A5} ;
// ----------------------------------  

// ----------------------------------
// Labels used in iotleaf stream
// ----------------------------------  
constexpr dim as t_const_label k_label_in_08         { "_d_08"      } ; 
constexpr dim as t_const_label k_label_in_10         { "_d_10"      } ; 
constexpr dim as t_const_label k_label_adc_0         { "_a_00"      } ; 
constexpr dim as t_const_label k_label_adc_1         { "_a_01"      } ; 
constexpr dim as t_const_label k_label_led_built_in  { "_ledblt_"   } ; 
constexpr dim as t_const_label k_label_pwm_demo      { "_pwm_03"    } ; 
constexpr dim as t_const_label k_label_mcp9700       { "_mcp9700_"  } ; 
// ----------------------------------  

// ----------------------------------
// storage values used when user interacts via internet
// ----------------------------------
dim as byte                    my_val_led_built_in   {       0 } ; //led on off
dim as word16                  my_pwm_demo           {      20 } ; //pwm output
// ----------------------------------

// ===============================================
// setup routine
// ===============================================
subroutine setup ( ) 
begin_block

  // ----------------------------------
  // serial setup 
  // ----------------------------------
  Serial.begin(115200);
  while ( !kic_serial_cable ){} // wait for serial port to be ready
  kic_serial_cable.println(""); // get past garbage in serial monitor
  kic_serial_cable.println("******* BOOT ***********") ;
  // ----------------------------------
  
  // ----------------------------------
  //digital input
  // ----------------------------------
  pinMode      (  k_pin_in_10         , INPUT_PULLUP ) ; //SWITCH A
  pinMode      (  k_pin_ground        , OUTPUT       ) ;
  digitalWrite (  k_pin_ground        , LOW          ) ;
  pinMode      (  k_pin_in_08         , INPUT_PULLUP ) ; //SWITCH B
  // ----------------------------------

  // ----------------------------------
  // pwm output
  // 2 pins are used, one for supply , the other for GND
  // ----------------------------------
  // -->supply side
  pinMode      ( k_pin_supply_pwm , OUTPUT      ) ; 
  analogWrite  ( k_pin_supply_pwm , my_pwm_demo ) ; // set to initial value
  // <--gnd side
  pinMode      ( k_pin_ground_pwm , OUTPUT      ) ; // will act as a ground pin 
  digitalWrite ( k_pin_ground_pwm , LOW         ) ;
  // ----------------------------------

  // ----------------------------------
  // digital output
  // ----------------------------------
  pinMode      ( LED_BUILTIN , OUTPUT ) ;
  // ----------------------------------

  // ----------------------------------
  // analogue input
  // ----------------------------------
  // internal pullup of adc pin (works for avr)
  pinMode( A0, INPUT_PULLUP ) ;
  // ----------------------------------

  // ----------------------------------
  // configure callback for serial scan
  // This initialises serial coming into the device for turning led on/off
  let my_kicchip_serial_scan.on_received_string = /* ns_actions::*/ do_json  ;
  // ----------------------------------
end_block
// ===============================================


// ===============================================
// Perform action based on data received from internet
// ===============================================
//unshared
fn as boolean do_action ( as JsonDocument refer_to  a_json_actions )
begin_block
  /*
   Example input to set pwm3 to a value of 22
   <{"val":{".pwm.03":22}}>

   Example of malformed input (e.g. missing value) to generate error message..
   <{"val":{".pwm.03": }>
   Generate this error message..
   <{"leaf":{"err_msg":"InvalidInput","chrs":"val"}}>
  */

  // -----------------------
  // verify the json content
  /*
  if ( !ns_kicchip_leaf_helper::get_json_doc_header ( a_json_actions )  ) 
    return false ;
  */
  // -----------------------
  
  // digital on /off (k_label_led_built_in) aka .ledblt.
  // check if user wanted to change k_label_led_built_in
  if ( is_action_wanted ( a_json_actions ,k_label_led_built_in ) )
  begin_block
    // get users value
    let my_val_led_built_in = get_action_value<as int>( a_json_actions ,k_label_led_built_in) ;
    // and do what they want
    if ( my_val_led_built_in > 0 ) { digitalWrite ( LED_BUILTIN , HIGH ) ; } 
    else                           { digitalWrite ( LED_BUILTIN , LOW  ) ; } 
  end_block

  //pwm out (k_label_pwm_demo) aka .pwm.03
  // check if user wanted to change k_label_pwm_demo
  if ( is_action_wanted ( a_json_actions ,k_label_pwm_demo) )
  begin_block
    // get users value
    let my_pwm_demo = get_action_value<as int>(a_json_actions ,k_label_pwm_demo) ;
    // and do what they want
    analogWrite( k_pin_supply_pwm, my_pwm_demo ) ;
  end_block

  return true ;
end_block
// ===============================================

// ===============================================
// ===============================================
subroutine get_readings_settings()
begin_block
  // ******************************
  // send to iotleaf
  // ******************************
  send_to_iotleaf_generic( k_label_led_built_in , my_val_led_built_in ) ;
  send_to_iotleaf_generic( k_label_pwm_demo     , my_pwm_demo         ) ;
  // ******************************
end_block
// ===============================================


// ===============================================
// ===============================================
// array index counter
//dim as byte my_counter   {0}    ;
subroutine get_readings_pins ( )
begin_block
  // get .mcp9700. value
  dim as integer16 my_celcius { } ;
  let my_celcius = mcp9700::get_value( analogRead ( k_pin_mcp700_input )  ) ;

  // ******************************
  // send to iotleaf
  // ******************************
  send_to_iotleaf_generic( k_label_mcp9700  , my_celcius                 ) ;
  send_to_iotleaf_generic( k_label_in_10    , digitalRead( k_pin_in_10)  ) ;
  send_to_iotleaf_generic( k_label_in_08    , digitalRead( k_pin_in_08)  ) ;
  send_to_iotleaf_generic( k_label_adc_0    , analogRead ( A0         )  ) ;
  send_to_iotleaf_generic( k_label_adc_1    , analogRead ( A1         )  ) ;
  // ******************************
  
end_block
// ===============================================


// ===============================================
// ===============================================
subroutine loop ( ) 
begin_block
  constexpr dim as unsigned long my_interval          {                200 } ; 
  constexpr dim as byte          my_values_per_second { 1000 / my_interval } ;
  noreset   dim as unsigned long previousMillis { 0 } ;

  // get current timer counter
  dim as unsigned long currentMillis = millis ( ) ;

  // check if timer has reached the interval we wanted
  if ( currentMillis - previousMillis >= my_interval ) 
  begin_block  

    // reset our timer counter
    let previousMillis = currentMillis ;

    // get fast changing data
    get_readings_pins ()               ;

    // check if arrays are full of data yet
    if ( my_values_per_second == ++my_counter )
    begin_block
      // arrays are full so prepare to send

      // reset array counter
      let my_counter = 0      ;
      // get latest user settings
      get_readings_settings() ;

      // send data to internet
      data_send            () ;    
    end_block
  end_block

  // check for incoming requests  
  my_kicchip_serial_scan.execute ( ) ; 
end_block
// ===============================================