Shop Learn P1 Docs P2 Docs Events
SimpleIDE WiFi - Page 7 — Parallax Forums

SimpleIDE WiFi

123457»

Comments

  • The datetime (softRTC) update application below, works as expected. So, all you need is an internet access that keeps your desktop computer clock up to date, and you should be able to have the correct date and time on your Propeller board applications.

    Now I will try to implement this to my solar windowsill project, and start to think about some data logging with real times and dates. This is easier said then done proposal, but if you keep jabbing at it, it will submit to your will.

    Ray

    /*
      dt_update.c
    
      March 27, 2017
      Check and update the datetime (softRTC) with the html browser.
    */
    #include "simpletools.h"
    #include "wifi.h"
    #include "datetime.h"
    
    datetime dt = {2017, 1, 1, 0, 0, 0};
    
    char dates[9];
    char times[9];
    
    int event, id, handle;
    int getFromPageId;
    
    char cmdStr[64];
    char *path;
    
    int day,month,year;
    int hour,minutes,seconds;
    
    int main()
    {
      // Add startup code here.
      dt_run(dt);
      pause(50);
      wifi_start(31, 30, 115200, WX_ALL_COM);
    
      getFromPageId = wifi_listen(HTTP, "/tpfm*");
      //print("getFromPageId = %d\n", getFromPageId);
      
      while(1)
      {
        // Add main loop code here.
        dt = dt_get();
        dt_toTimeStr(dt, times);
        dt = dt_get();
        dt_toDateStr(dt, dates);
        
        wifi_poll(&event, &id, &handle); 
        print("event = %c, id = %d, handle = %d\r", event, id, handle);
    
        if(event == 'G')
        {
          if(id == getFromPageId)
          {
            //print("Check path\r");
            memset(cmdStr, 0, sizeof(cmdStr));
            sprint(cmdStr, "PATH:%d\r", handle);
            path = wifi_command(cmdStr);
            //print("path: %s\r", path);
            
            if(strstr(path, "/Dates") != 0)   // Upload system Date
            {
              //print("Incoming GET request from path A, sending %s\r", dates);
              wifi_print(GET, handle, "%s", dates);
            }
            else if(strstr(path, "/Times") != 0)  // Upload system Time
            {
              //print("Incoming GET request from path B, sending %s\r", times);
              wifi_print(GET, handle, "%s", times);
            }                    
          }
         }
         if(event == 'P')
         {
           //print("debug 1\n");
           if(id == getFromPageId)
           {
              //print("Check path\r");
              memset(cmdStr, 0, sizeof(cmdStr));
              sprint(cmdStr, "PATH:%d\r", handle);
              path = wifi_command(cmdStr);
              //print("path: %s\r", path);
    
             if(strstr(path, "/date") !=0)
             {
                //print("debug 2\n");
                wifi_scan(POST, handle, "month%d%d%d",&month,&day,&year);
                
                //print("%d/%d/%d\n",month,day,year);
                dt.y=year; 
                dt.mo=month;
                dt.d=day;
                dt_set(dt);
                pause(50);
                dt = dt_get();
                dt_toDateStr(dt, dates);
                //print("?? %s\n",dates);
             }
             else if(strstr(path, "/time") !=0)
             {
               wifi_scan(POST, handle, "hour%d%d%d",&hour,&minutes,&seconds);
               
               //print("%d:%d:%d\n",hour,minutes,seconds);
               dt.h=hour;
               dt.m=minutes;
               dt.s=seconds;
               dt_set(dt);
               pause(50);
               dt = dt_get();
               dt_toTimeStr(dt, times);
               //print("?? %s\n",times);
             }                       
           }          
         }                
        
        //pause(2000);  // Slows the terminal screen output.
      }  
    }
    
    <!-- dt_update.html -->
    <!DOCTYPE html>
    <html>
      <body>
      
        <H2>System Time and Date from Microcontroller</H2> 
    
        <p>Click Date to see system Date from Micro:</p> 
        <button onclick="getFromMcu('Dates')">Date</button>
        <p id="valueD">Waiting...</p>
    
        <p>Click Time to see system Time from Micro:</p> 
        <button onclick="getFromMcu('Times')">Time</button>
        <p id="valueT">Waiting...</p>
    
        <H3>Update System Time and Date on Microcontroller</H3>
        
        <p>Click Date to update system Date</p>
        <button onclick="dateBtn()">Date</button> 
        <p>Date splash screen</p>
        <p id = "dateTxt">Waiting...</p> 
        
        <p>Click Time to update system Time</p>
        <button onclick="timeBtn()">Time</button>
        <p>Time splash screen</p>
        <p id = "timeTxt">Waiting...</p> 
    
        <script>
          var getPathExt;
    
          function useMcuReply(response)
          {
            if(getPathExt == 'Dates')
            {
              var val = document.getElementById("valueD");
              val.innerHTML = "ValueD: " + response;
            }  
            else if(getPathExt == 'Times')
            {
              var val = document.getElementById("valueT");
              val.innerHTML = "ValueT: " + response;
            }
          }
    	  function dateBtn()
    	  {
    		 var dt = new Date();
    		  day = dt.getDate();
    		  
    		  var dt1 = new Date();
    		   month = dt1.getMonth();
    		   month = month + 1;
    		  
    		 var dt2 = new Date();
    		 year = dt2.getFullYear();
    		 
    		  var send = "month=" + month + "day=" + day + "year=" + year;
    		  httpPost("/tpfm/date",send);
    		  
    		  document.getElementById("dateTxt").innerHTML = "To micro: "
    		   + month + "/"+ day + "/" + year;		  		  
    
    	  }
          function getFromMcu(pathExt)
          {
            getPathExt = pathExt;
            httpGet("/tpfm/" + pathExt, useMcuReply);
          }
    
          function httpGet(path, callback)
          {
            var req = new XMLHttpRequest();
            req.open("GET", path, true); 
            req.onreadystatechange = function() 
            { 
              if (req.readyState == 4)
                if(req.status == 200)
                  callback(req.responseText);
                else
                  callback("Waiting...");
            }
            req.send(null);
          }
          function httpPost(path,param)
          {
    		  var req = new XMLHttpRequest();
    		  req.open("POST", path, true);
    		  req.setRequestHeader("Content-type",
    		  "application/x-www-form-urlencoded");
    		  req.send(param);
    	   }
    
    
    	  function timeBtn()
    	  {		  
    		  var dt = new Date();
    		  var hours = dt.getHours();
    		  var dt1 = new Date();
    		  var minutes = dt1.getMinutes();
    		  var dt2 = new Date();
    		  var seconds = dt2.getSeconds();
    		  
    		  var send = "hour=" + hours + "minutes=" + minutes + 
    		  "seconds=" + seconds;
    		  
    		  httpPost("/tpfm/time",send);		  
    		  document.getElementById("timeTxt").innerHTML = "To micro: "
    		  + hours + ":"+ minutes + ":" + seconds;		  		  
    	  }
    
        </script>
      </body>
    </html>
    
    
  • Just an update, I applied the sRTC (dt_update) to my solar windowsill project, which runs 24/7, and the sRTC keeps very good time. It also makes it very convenient for keeping the sRTC updated after a reboot.

    In another thread there was mention of a FLIP module that will be available soon. I wonder if WX WiFi SIP module will be a plug and play with the FLIP module?

    I can see an interesting application using the FLIP+WX WiFi and the Telnet. Sometimes I like to have a command line UI instead of a GUI (browser) approach.

    Ray

  • Ray, thanks for following through with your project and posting this code here. I've been trying to get the web page to send the date and time to my project but I kept hitting road blocks. You code looks easy to follow and I think it's just what I need.

    Thanks again.

  • RsadeikaRsadeika Posts: 3,693

    I have been away for a few months, decided to do some programming again. Below is code for a very simple program using the Parallax Activity WX board and an html browser program. Next, I will try to add a digital clock to the html, and maybe have it update the rtc code on the Activity board.

    So far it is a good start. If anybody wants to join in, feel free to upgrade the code below.

    Any chance that Parallax will be adding P2 to the SimpleIDE program?

    Ray

    /*
      cr2_html_5.c
    
    */
    #include "simpletools.h"
    #include "wifi.h"
    #include "adcDCpropab.h"
    #include "dht22.h"
    
    int event, id, handle;
    //int getFromPageId;
    int cr2Volts, cr2Temp, cr2Humid;
    int val;
    
    volatile float cr_bat;
    static volatile float gtemp1,ghumid1;
    
    
    
    void x_sense();
    
    int main()
    {
      cog_run(x_sense,128);
      pause(150);
      wifi_start(31, 30, 115200, WX_ALL_COM);
    
      //getFromPageId = wifi_listen(HTTP, "/tpfm1");
      cr2Volts = wifi_listen(HTTP, "/cr2volts");
      cr2Temp = wifi_listen(HTTP, "/cr2temp");
      cr2Humid = wifi_listen(HTTP, "/cr2humid");
      //print("getFromPageId = %d\n", getFromPageId);
    
      while(1)
      {
        val++;
        wifi_poll(&event, &id, &handle); 
        //print("event = %c, id = %d, handle = %d\r", event, id, handle);
        if(event == 'G')
        {
          //if(id == getFromPageId)
          if(id == cr2Volts)
          {
            //print("Incoming GET request, sending %.02f\r",cr_bat );
            wifi_print(GET, handle, "%.02f", cr_bat);
          }
          if(id == cr2Temp)
          {
            wifi_print(GET, handle, "%.02f", gtemp1);
          }
          if(id == cr2Humid)
          {
            wifi_print(GET, handle, "%.02f", ghumid1);
          }                        
        }
        if(event == 'P')
        {
    
        }      
        pause(200);
      }    
    }
    
    
    void x_sense()
    {
      adc_init(21, 20, 19, 18);
      float b1;
      float t1,h1;
    
    while(1)
      {
        b1=adc_volts(0);
        pause(150);
        cr_bat=(b1*4.96853);
        //cr_bat=(b1);
     /* Temperature */
        dht22_read(2);
        pause(150);
        t1 = dht22_getTemp(1);
        gtemp1 = (t1*.10);
        /* Humidity */
        dht22_read(2);
        pause(150);
        h1 = dht22_getHumidity();
        ghumid1 = (h1*.10);
        pause(150);   
      }  
    }  
    
    

    The html code.

    <!DOCTYPE HTML>
    <html>
    
      <body>
    
        <H2>Value from Microcontroller</H2> 
    
        <p>Click Button to see value from Micro:</p> 
        <button onclick="getFromMcuVolts()">Cr2 Volts</button>
        <button onclick="getFromMcuTemp()">Cr2 Temp</button>
        <button onclick="getFromMcuHumid()">Cr2 Humidity</button>
        <p id="value">Volts...</p>    
        <p id="value1">Temp...</p>   
        <p id="value2">Humidity...</p>
    
        <script>
    
          function useMcuReplyVolts(response)
          {
            var val = document.getElementById("value");
            val.innerHTML = "Volts: " + response;
          }
          function useMcuReplyTemp(response)
          {
            var val = document.getElementById("value1");
            val.innerHTML = "Temp: " + response;
          }
          function useMcuReplyHumid(response)
          {
            var val = document.getElementById("value2");
            val.innerHTML = "Humidity: " + response;
          }
    
          function getFromMcuVolts()
          {
            httpGet("/cr2volts", useMcuReplyVolts);
          }
          function getFromMcuTemp()
          {
             httpGet("/cr2temp", useMcuReplyTemp); 
          }
          function getFromMcuHumid()
          {
             httpGet("/cr2humid", useMcuReplyHumid); 
          }
    
          function httpGet(path, callback)
          {
            var req = new XMLHttpRequest();
            req.open("GET", path, true); 
            req.onreadystatechange = function() 
            { 
              if (req.readyState == 4)
                if(req.status == 200)
                  callback(req.responseText);
                else
                  callback("Waiting...");
            }
            req.send(null);
          }
    
        </script>
      </body>
    </html>
    
    
  • RsadeikaRsadeika Posts: 3,693

    I just installed SimpleIDE for Linux and seems that it is not locating my Activity WX board, wifi of course. Am I missing something during the installation?

    Ray

  • iseriesiseries Posts: 1,311

    To find the WiFi board it sends out a broadcast UDP packet which the WiFi board sees and responds back to the IP address that sent it. If the WiFi board is not on the same network it will not find it.

    The P2 will not be supported with SimpleIDE. I have switched to FlexProp which works just fine. Some small changes are required to make P1 code work on the P2 with FlexProp.

    Mike

  • RsadeikaRsadeika Posts: 3,693

    It kind looks like Parallax will not be doing anything more with SimpleIDE, let alone installing the P2. I am now using the P1 more, so I like SimpleIDE because it has most of the drivers for all their little devices.

    I like flexprop, but you have to create all the device drivers, for C. I do not do drivers, sometimes I have a heck of a time just trying to program in C.

    Ray

  • That's a really good point there Ray.
    I wonder if there's an existing way (or set of instructions) on how to leverage all those SimpleIDE libraries in FlexProp?

    It feels like there could be. Maybe @ersmith would know ?

  • RsadeikaRsadeika Posts: 3,693

    Some time ago I looked at the SimpleIDE libraries, and it looks like all of the code is interdependent. Not sure if you could just take one lib and have it work in flexprop.

    Ray

  • iseriesiseries Posts: 1,311

    All the library functions are compiled into a library object matching the library type. Also the library code is there as well so you can modify it if you like and then build a new library from it.

    I have several libraries that I have built for the P1 and also converted most of them for use with FlexProp on the P2.
    At this point there is no home for sharing C library code.

    SimpleIDE is still my choice for building program on the P1. I only use FlexProp for the P2.

    Here are my libraries for the P1: Custom P1 Libraries
    Here are my libraries for the P2 that work with FlexProp: P2 Custom Libraries

    Mike

  • ersmithersmith Posts: 5,618

    @VonSzarvas said:
    That's a really good point there Ray.
    I wonder if there's an existing way (or set of instructions) on how to leverage all those SimpleIDE libraries in FlexProp?

    It feels like there could be. Maybe @ersmith would know ?

    @"Roy Eltham" ported a number of the simple libraries to FlexProp, and those are in the standard FlexProp distribution. Some examples are in samples/simpletools.

    @Rsadeika if you have specific drivers you're interested in you should start a thread about them, perhaps someone else will also be interested and want to work with you to port them.

    Alternatively, you can almost always use Spin2 drivers from FlexC, and there are a variety of those already existing for the P2.

  • RsadeikaRsadeika Posts: 3,693

    A couple of things, concerning the use of FlexC as an alternative to SimpleIDE. SimpleIDE has wifi.h and some code for using the ADC. Not sure how many forum members are making use of P1/WEB page posibilities, but I am starting to use that more and more for some of my projects.

    I noticed, in another thread, that jroark made some significant progress with the bmp680. That would be an interesting project, have the information displayed on a WEB page for all of your devices using FlexProp. Just a few things that I am thinking about.

    Ray

  • iseriesiseries Posts: 1,311

    Something like this page? Home Weather Station

    This is using a WiFi module to send weather information to a web server.

    BME280, AS5048A, Magnetic switches.

    Mike

  • I love SimpleIDE and P1. And I'm disappointed that SimpleIDE won't be upgraded to support the P2. I'm exploring Catalina for P2 C programming, but in the mean time, P1 it is!

    I like using the Activity Board WX with HTML, JavaScript and I've just started exploring APIs and Postman. So much power! So many possibilities!

    I just discovered the Circuit Overlay Board (p/n 32999). I think these will help make my experiments more permanent.

    Currently I'm building a garden monitoring station (multiple moisture meters, temperature, humidity, data-logging, and eventually including weather forecasting and a drip system.)

    HTML / JavaSript / APIs add so much more to a real world P1 project, it may be a long time before I need a P2.

  • iseriesiseries Posts: 1,311

    @TommieBoy ,

    I do a lot of Plug and Play. It simplifies moving from prop to prop and building things.
    Plug And Play

    Mike

  • RsadeikaRsadeika Posts: 3,693

    Below is a test program to see if the digital clock and the buttons all work. I grabbed some html code that runs a digital clock, the only problem is that it always shows the 'PM'. Could not figure out how to make it switch between AM and PM.

    Because the digital clock runs constantly, I wanted to make sure that the buttons would work also. It seems that javascript has a tasking feature, I think, not sure how many separate tasks it could run.

    I was considering adding an RTC module to the Activity board, but since it is possible to set a two-way conversation between the html and the propeller, I could just use the html clock code. Now I have to double check to see how much memory the WiFi module has, I can see the html program growing in size, very quickly.

    Not sure why the Learn site is not devoting more time to show more examples for using the html stuff.

    Ray

    /*
      cr2_html_5.c
    
    */
    #include "simpletools.h"
    #include "wifi.h"
    #include "adcDCpropab.h"
    #include "dht22.h"
    serial *cr2;
    
    static volatile int speed_right = 75;
    static volatile int speed_left = 75;
    static volatile int turn_left = 35;
    static volatile int turn_right = 35;
    static volatile int highbyte, lowbyte;
    
    
    int event, id, handle;
    int getFromPageId;
    int cr2Volts, cr2Temp, cr2Humid;
    int val;
    
    volatile float cr_bat;
    static volatile float gtemp1,ghumid1;
    
    char buttonCmd;
    
    int gled=0;
    int rled=0;
    
    void crStop();
    void crFore();
    void crBack();
    void crLeft();
    void crRite();
    
    void crBase();
    void crSafe();
    void crRestart();
    
    
    void x_sense();
    
    int main()
    {
      cog_run(x_sense,128);
      pause(150);
      wifi_start(31, 30, 115200, WX_ALL_COM);
      pause(150);
      cr2 =fdserial_open(0, 1, 0, 115200);
      pause(150);
      getFromPageId = wifi_listen(HTTP, "/bot");
      cr2Volts = wifi_listen(HTTP, "/cr2volts");
      cr2Temp = wifi_listen(HTTP, "/cr2temp");
      cr2Humid = wifi_listen(HTTP, "/cr2humid");
      //print("getFromPageId = %d\n", getFromPageId);
      fdserial_txChar(cr2,128);
      pause(1000);
      fdserial_txChar(cr2,131);
      high(4);
      gled=1;
    
      while(1)
      {
        val++;
        wifi_poll(&event, &id, &handle); 
        //print("event = %c, id = %d, handle = %d\r", event, id, handle);
        if(event == 'G')
        {
          //if(id == getFromPageId)
          if(id == cr2Volts)
          {
            //print("Incoming GET request, sending %.02f\r",cr_bat );
            wifi_print(GET, handle, "%.02f", cr_bat);
          }
          if(id == cr2Temp)
          {
            wifi_print(GET, handle, "%.02f", gtemp1);
          }
          if(id == cr2Humid)
          {
            wifi_print(GET, handle, "%.02f", ghumid1);
          }                        
        }
        if(event == 'P')
        {
          if(id == getFromPageId)
          {
            wifi_scan(POST, handle, "go%c", &buttonCmd);
          }
    
        }
        if(buttonCmd != 0)
        {
          switch(buttonCmd)
          {
            case 'F':  // Forward
              crFore();
              break;
            case 'L':  // Left
              crLeft();
              break;
            case 'R':  // Right
              crRite();
              break;
            case 'B':  // Backward
              crBack();
              break;
            case 'S':  // Stop
              crStop();
              break;
            case 'H':  // Homebase
              crBase();
              break;
            case 'Z':  // Safe Mode
              crSafe();
              break;
            case 'Y':  // Restart
              crRestart();
              break;
          }             
         buttonCmd = 0; 
        }      
        pause(200);
      }    
    }
    
    
    /* Robot movement*/
    void crStop()
    {
      fdserial_txChar(cr2,145);
      fdserial_txChar(cr2,0);
      fdserial_txChar(cr2,0);
      fdserial_txChar(cr2,0);
      fdserial_txChar(cr2,0);
    }
    
    
    void crFore()
    {
      fdserial_txChar(cr2,145);
      fdserial_txChar(cr2,((speed_right)>>8)&0xFF);
      fdserial_txChar(cr2,((speed_right)&0xFF));
      fdserial_txChar(cr2,((speed_left)>>8)&0xFF);
      fdserial_txChar(cr2,((speed_left)&0xFF));
    }
    
    
    void crBack()
    {
      fdserial_txChar(cr2,145);
      fdserial_txChar(cr2,((-speed_right)>>8)&0xFF);
      fdserial_txChar(cr2,((-speed_right)&0xFF));
      fdserial_txChar(cr2,((-speed_left)>>8)&0xFF);
      fdserial_txChar(cr2,((-speed_left)&0xFF));
    }
    
    
    void crLeft()
    {
      fdserial_txChar(cr2,145);
      fdserial_txChar(cr2,((speed_right)>>8)&0xFF);
      fdserial_txChar(cr2,((speed_right)&0xFF));
      fdserial_txChar(cr2,((-speed_left)>>8)&0xFF);
      fdserial_txChar(cr2,((-speed_left)&0xFF));
    }
    
    
    void crRite()
    {
      fdserial_txChar(cr2,145);
      fdserial_txChar(cr2,((-speed_right)>>8)&0xFF);
      fdserial_txChar(cr2,((-speed_right)&0xFF));
      fdserial_txChar(cr2,((speed_left)>>8)&0xFF);
      fdserial_txChar(cr2,((speed_left)&0xFF));
    }  
    
    
    void crBase()
    {
      if(gled == 1)
      {
        low(4);
      }    
      fdserial_txChar(cr2,143);
    }
    
    void crSafe()
    {
      fdserial_txChar(cr2,128);
      pause(700);
      fdserial_txChar(cr2,131);
      if(gled == 0)
      {
        high(4);
      }    
    }    
    
    void crRestart()
    {
      fdserial_txChar(cr2,7);
    }  
    
    
    /******************************/
    
    void x_sense()
    {
      adc_init(21, 20, 19, 18);
      float b1;
      float t1,h1;
    
    while(1)
      {
        b1=adc_volts(0);
        pause(150);
        cr_bat=(b1*4.96853);
        //cr_bat=(b1);
        if(cr_bat <= 13.00)
        {
          //bat_msg="Low Battery";
          high(5); // Big red LED
          pause(150);
          low(4);  // Green LED is now off.
          pause(150);
          fdserial_txChar(cr2,128); // Set to start mode, charge battery.
        }
        if(cr_bat >= 19.00)
        {
          low(5);  // Big red LED
        }
     /* Temperature */
        dht22_read(2);
        pause(150);
        t1 = dht22_getTemp(1);
        gtemp1 = (t1*.10);
        /* Humidity */
        dht22_read(2);
        pause(150);
        h1 = dht22_getHumidity();
        ghumid1 = (h1*.10);
        pause(150);   
      }  
    }  
    
    
    

    html code

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <title>CR2 with digiclock</title>
      <style>
        body {
          font-family: sans-serif;
          margin: 0;
          padding: 0;
          height: 100vh;
        }
    
        .clock {
          width: 40%;   <!-- 100% if you want full screen.  -->
          height: 10%;  <!-- 100% if you want full screen.  -->
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          background-color: #424558;
        }
    
        .time {
          font-size: 80px;
          font-weight: bold;
          color: #fff;
        }
    
        .date-time {
          font-size: 20px;
          color: #fff;
        }
    
        @media screen and (max-width: 768px) {
          .time {
            font-size: 50px;
          }
        }
      </style>
    </head>
    
    <body>
      <div class="clock">
        <div class="time"></div>
        <div class="date-time"></div>
      </div>
    
      <!-- This is the P1 code.  
        <p>Click Button to see value from Micro:</p> 
        <button onclick="getFromMcuVolts()">Cr2 Volts</button>
        <button onclick="getFromMcuTemp()">Cr2 Temp</button>
        <button onclick="getFromMcuHumid()">Cr2 Humidity</button>
        <p id="value">Volts...</p>    
        <p id="value1">Temp...</p>   
        <p id="value2">Humidity...</p>
        -->
        <head>
        <style>
          a{
            background-color:#637aad;
            border:1px solid navy;
            display:inline-block;
            color:white;
            font-size:18px;
            margin:20px;
            width:100px;
            height:45px;
            font-family:Arial;
            cursor:pointer;
            padding-top:37px
          }
          a:hover{background-color:white;color:navy;}
        </style>
      </head>
        <p>Click Button to see value from Micro:</p> 
        <button onclick="getFromMcuVolts()">Cr2 Volts</button>
        <button onclick="getFromMcuTemp()">Cr2 Temp</button>
        <button onclick="getFromMcuHumid()">Cr2 Humidity</button>
        <p id="value">Volts...</p>    
        <p id="value1">Temp...</p>   
        <p id="value2">Humidity...</p>
    
        <div align="center">
        <font face="Arial" size=6 color="cyan">
    
            Create 2 Robot
    
          <br>
          <a onclick="navBtns('F');">Forward</a>
          <br>
          <a onclick="navBtns('L');">Left</a>
          <a onclick="navBtns('S');">Stop</a>
          <a onclick="navBtns('R');">Right</a>
          <br>
          <a onclick="navBtns('B');">Reverse</a>
          <br>
          <a onclick="navBtns('H');">HomeBase</a>
          <a onclick="navBtns('Z');">Safe Mode</a>
          <a onclick="navBtns('Y');">Restart</a>
          <br>
    
        </div>
       <!-- End of P1 code. -->
    
      <script>
        var time = document.querySelector('.time');
        var dateTime = document.querySelector('.date-time');
    
        function updateClock() {
          // Get the current time, day , month and year
          var now = new Date();
          var hours = now.getHours();
          var minutes = now.getMinutes();
          var seconds = now.getSeconds();
          var day = now.getDay();
          var date = now.getDate();
          var month = now.getMonth();
          var year = now.getFullYear();
    
          // store day and month name in an array
          var dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
          var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    
          // format date and time
          hours = hours % 12 || 12;
          minutes = minutes < 10 ? '0' + minutes : minutes;
          seconds = seconds < 10 ? '0' + seconds : seconds;
          date = date < 10 ? '0' + date : date;
    
          // display date and time
          var period = hours >= 12 ? 'AM' : 'PM';
          //var period = hours >= 12 ? 'PM' : 'AM'
          time.innerHTML = hours + ':' + minutes + ':' + seconds + ' ' + period;
          dateTime.innerHTML = dayNames[day] + ', ' + monthNames[month] + ' ' + date + ', ' + year;
        }
    
        updateClock();
        setInterval(updateClock, 1000);
    
        // This is the P1 code.
          function useMcuReplyVolts(response)
          {
            var val = document.getElementById("value");
            val.innerHTML = "Volts: " + response;
          }
          function useMcuReplyTemp(response)
          {
            var val = document.getElementById("value1");
            val.innerHTML = "Temp: " + response;
          }
          function useMcuReplyHumid(response)
          {
            var val = document.getElementById("value2");
            val.innerHTML = "Humidity: " + response;
          }
    
          function getFromMcuVolts()
          {
            httpGet("/cr2volts", useMcuReplyVolts);
          }
          function getFromMcuTemp()
          {
             httpGet("/cr2temp", useMcuReplyTemp); 
          }
          function getFromMcuHumid()
          {
             httpGet("/cr2humid", useMcuReplyHumid); 
          }
    
          function httpGet(path, callback)
          {
            var req = new XMLHttpRequest();
            req.open("GET", path, true); 
            req.onreadystatechange = function() 
            { 
              if (req.readyState == 4)
                if(req.status == 200)
                  callback(req.responseText);
                else
                  callback("Waiting...");
            }
            req.send(null);
          }
    
        function navBtns(direction) 
        {
          httpPost("/bot", "go=" + direction);
        }
        function httpPost(path, param)
        {
          var req = new XMLHttpRequest();
          req.open("POST", path, true);
          req.setRequestHeader("Content-type",
          "application/x-www-form-urlencoded");
          req.send(param);
        }
        function useMcuReply(response)
        {
            var val = document.getElementById("value");
            val.innerHTML = "Value: " + response;
        }
        function getFromMcu()
        {
            httpGet("/bot", useMcuReply);
        }
    
      </script>
    </body>
    
    </html>
    <!-- HTML
    ▶ Run the code -->
    
Sign In or Register to comment.