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

SimpleIDE WiFi

12346

Comments

  • What you could do is just us a single URL for all of your commands and parse the command keyword from the URL arguments. Add an argument like "cmd=solar" and look for that when you receive a request. The WX firmware actually has a feature that makes parsing arguments easy but for some reason it is not used by the library. In any case, I think you could use wifi_scan to find these parameters easily enough.
  • What you could do is just us a single URL for all of your commands and parse the command keyword from the URL arguments.
    I think I need a small code example for what you just described. I had a hard enough time just trying to figure the GET and POST usage.

    Ray
  • Rsadeika wrote: »
    What you could do is just us a single URL for all of your commands and parse the command keyword from the URL arguments.
    I think I need a small code example for what you just described. I had a hard enough time just trying to figure the GET and POST usage.

    Ray
    I'll have to get up to speed on Andy's library before I can provide sample code. All of the sample code I wrote during the WX firmware development used a different library.
  • This will necessitate a library modification to make it work well. It's probably not hard to do, but I have some deadlines that need to be completed first.
  • I was looking through the Wi-Fi Module WX library, and came across this:
    wifi_poll(int *event, int *id, int *handle)
    I am not sure what the elements like event,id, and handle are referring too. In the context of my application, which I presented a a couple of posts back, what names would I be using for the wifi_poll() elements?

    Since I will be reworking my application, I guess I need some better examples of the Wi-Fi Module WX library commands that are available.

    Ray
  • After looking at the WiFi functions, I have not come across a function to deactivate a wifi_listen() function.

    My thoughts, so far are, in the while(1) loop, I could activate a wifi_listen(), if it meets the criteria in the html, then proceed with an activity ending with a deactivation of the wifi_listen(). Repeat the process until you cycled through all of the necessary wifi_listen() requests. I guess the Propeller, while in the while(1) loop could be fast enough too cycle through maybe eight or more wifi_listen() requests?

    I am not exactly sure of the coding aspect for this, but it would seem that it would have to rely on the processor speed of the Propeller chip very heavily. At this point, I am not sure of what I can do different to increase the wifi_listen() limits to work in my application. Anybody have any more ideas?

    Ray
  • Rsadeika wrote: »
    After looking at the WiFi functions, I have not come across a function to deactivate a wifi_listen() function.

    My thoughts, so far are, in the while(1) loop, I could activate a wifi_listen(), if it meets the criteria in the html, then proceed with an activity ending with a deactivation of the wifi_listen(). Repeat the process until you cycled through all of the necessary wifi_listen() requests. I guess the Propeller, while in the while(1) loop could be fast enough too cycle through maybe eight or more wifi_listen() requests?

    I am not exactly sure of the coding aspect for this, but it would seem that it would have to rely on the processor speed of the Propeller chip very heavily. At this point, I am not sure of what I can do different to increase the wifi_listen() limits to work in my application. Anybody have any more ideas?

    Ray
    I don't think that is going to work. If there is no listen in effect when an HTTP request comes to the WX module the request will result in an error being returned to the browser. I think your best bet is to use only a few listeners and distinguish different commands by the URL parameters as I suggested before.

  • I think that I tried something like you are suggesting, but it did not work as expected. I think I had a big problem in the html program, as to how to deal with some specific data that I needed.

    Again, an example of what you are thinking could be useful. Maybe what I am thinking and needing, are two different requirements.

    Ray
  • Rsadeika wrote: »
    I think that I tried something like you are suggesting, but it did not work as expected. I think I had a big problem in the html program, as to how to deal with some specific data that I needed.

    Again, an example of what you are thinking could be useful. Maybe what I am thinking and needing, are two different requirements.

    Ray
    Yes, I know an example would help. I'm still trying to wade through the WX library to try to understand how it works. It's a lot different than the library I wrote while developing the WX code.
  • Yep, there is only one expert on this, Andy, and he is spread way to thin, plus he said he has a couple deadlines that he has to meet. The other thing that I noticed is, on the Learn site, new activity for the WX WiFi has gone silent, or maybe I am looking in the wrong place.

    So, maybe it is SOM, buy the product, read the datasheet, and???...

    Ray
  • Rsadeika wrote: »
    Yep, there is only one expert on this, Andy, and he is spread way to thin, plus he said he has a couple deadlines that he has to meet. The other thing that I noticed is, on the Learn site, new activity for the WX WiFi has gone silent, or maybe I am looking in the wrong place.

    So, maybe it is SOM, buy the product, read the datasheet, and???...

    Ray
    The WX data sheet and documentation cover the serial protocol used to talk to the WX module. It has the capabilities you need. It's just that the sample code doesn't yet show their use. You could try using the serial protocol directly. It's pretty simple. It was designed so it could be used by the BASIC Stamp so it's all text oriented.

  • Since most all of the examples have been POST and GET, although I did see something using serial, I am not sure what the html code would look like to access and use the data. I am still trying to get a firm grip on the html side, since most of the examples on line do not necessarily deal with communicating with the Propeller chip.
    It's pretty simple. It was designed so it could be used by the BASIC Stamp so it's all text oriented.
    So, PropGCC is a redheaded stepchild, in this case. Why is C always getting left behind?

    Ray
  • Rsadeika wrote: »
    Since most all of the examples have been POST and GET, although I did see something using serial, I am not sure what the html code would look like to access and use the data. I am still trying to get a firm grip on the html side, since most of the examples on line do not necessarily deal with communicating with the Propeller chip.
    It's pretty simple. It was designed so it could be used by the BASIC Stamp so it's all text oriented.
    So, PropGCC is a redheaded stepchild, in this case. Why is C always getting left behind?

    Ray
    It works perfectly well with the Propeller and C. My point was that the API is pretty easy to use.

  • As per David's suggestion, I am looking at the "Test Serial Commands" program in the examples folder. It looks pretty complicated, but it might be just what I need. The only problem is, I cannot seem to find an accompanying html program for that. Was it overlooked, or is there a different named html that you are suppose to use with that. Or is it a "It's your turn" suggestion?

    It seems that since all the other examples are very much POST and GET, I am not sure about how to transition from that to what the "Test Serial Commands" program is suggesting.

    Ray
  • So, I am moving on. Below, the code works as expected, I have a reboot button, which I tested, and it works as expected. I will probably revisit the WiFi part of this application at some very later point, when there is a whole bunch more of relevant examples.

    The next step of this application is data acquisition and manipulation, and for that I will be adding a Raspberry Pi to do the heavy lifting. Plus I probably will be exploring the Telnet connection aspect for another way of viewing some data.

    I will be keeping the datetime functionality alive, and will probably figure out a way of having the Raspberry Pi do some auto datetime updates via a serial link. I should have enough COGs left to do this in its own COG.

    I really had some high hopes for the WiFi aspect, but that did not happen. Maybe in a year or two when things solidify for the WX WiFi, then I will revisit, but for now other things have to be made functional.

    Ray

    /*
      wifi_solar.c
      
      Feb 25, 2017
      WiFi Solar Station
      
    */
    #include "simpletools.h"
    #include "wifi.h"
    #include "adcDCpropab.h"
    #include "simpletext.h"
    #include "fdserial.h"
    #include "datetime.h"
    
    #define reboot() __builtin_propeller_clkset(0x80)
    
    serial *rpi;
    
    volatile float vx,vy,vz;  // vx=solar panel, vy=batteries, vz=car battery
    volatile float tempf,humid,baro;
    
    datetime dt = {2017, 1, 1, 0, 0, 0};
    
    int event, id, handle;
    int solarId,batteryId,battery2Id,timeId,dateId,resetId;
    char sysr;
    
    char times[9];
    char dates[9];
    
    
    /* COGed Functions*/
    void pwrmon();
    void datamon();
    /******************************/
    
    int main()
    {
      // Add startup code here.
    /*                    Rx Tx      BAUD   */
      //rpi = fdserial_open(31, 30, 0, 115200);
      dt_run(dt);  // Start clock  
      pause(50);
      cog_run(pwrmon,128);
      pause(50);
      //cog_run(datamon,128);
      //pause(50);
      wifi_start(31, 30, 115200, WX_ALL_COM);
      solarId = wifi_listen(HTTP, "/solar");
      batteryId = wifi_listen(HTTP, "/battery");
      battery2Id = wifi_listen(HTTP, "/battery2");
    //  timeId = wifi_listen(HTTP, "/times");
    //  dateId = wifi_listen(HTTP, "/date");
      resetId = wifi_listen(HTTP, "/reset");
      
    
      int DO = 22, CLK = 23, DI = 24, CS = 25;
      sd_mount(DO, CLK, DI, CS);  // Mount SD
      
      char inBuff[40];
      int sysh,sysm,syss,sysy,sysmo,sysd;
      //int val = 0;
      while(1)
      {
        // Add main loop code here.
        wifi_poll(&event, &id, &handle);
        if(event == 'P')
        {
          /*if(id == timeId)
          {
            wifi_scan(POST, handle, "go%d", &sysh);
            dt.h = sysh;
            pause(50);
            wifi_scan(POST, handle, "go%d", &sysm);
            dt.m = sysm;
            pause(50);
            wifi_scan(POST, handle, "go%d", &syss);
            dt.s = syss;
            dt_set(dt);
          }
          if(id == dateId)
          {
            wifi_scan(POST, handle, "go%d", &sysy);
            dt.y = sysy;
            pause(50);
            wifi_scan(POST, handle, "go%d", &sysmo);
            dt.mo=sysmo;
            pause(50);
            wifi_scan(POST, handle, "go%d", &sysd);
            dt.d=sysd;        
            dt_set(dt);
          } */
          if(id == resetId)
          {
            wifi_scan(POST, handle, "go%c", &sysr);
          }                        
        }
        else if(event == 'G')
        {
         /* if(id == dateId)
          {
            //get_Date();
            print("'G' /date \n");
            dt = dt_get();
            dt_toDateStr(dt, dates);
            //dt_fromDateStr(dt, dates);
            wifi_print(GET, handle, "%s\r", dates);
          }
          if(id == timeId)
          {
            print("'G' /time \n");
            //get_Time();
            dt = dt_get();
            dt_toTimeStr(dt, times);
            wifi_print(GET, handle, "%s\r", times);
          } */
          if(id == solarId)
          {
            wifi_print(GET, handle, "%.2f\r", vx);
          }
          if(id == batteryId)
          {
            wifi_print(GET, handle, "%.2f\r", vy);  // Send to html
          }
          if(id == battery2Id)
          {
            wifi_print(GET, handle, "%.2f\r", vz);
          }        
        }
        if(sysr != 0)
        {
          switch(sysr)
          {
            case 'R':
              reboot();
              break;
          }        
        }      
    
        //print("%cSolar panel: %.2f",HOME,vx);     // Show on local terminal          
        //pause(50);
        //print("  Batteries: %.2f Car bat: %.2f%c",vy,vz,CLREOL); // Show on local terminal
        //val++;
        //print("%c Val = %d%c",HOME,val,CLREOL);   // Test to see if reboot works
      }  
    }
    /******************************/
    /* COGed Functions */
    void pwrmon()
    {
      adc_init(21, 20, 19, 18);
      float v0,v1,v2;
      while(1)
      {
        v0 = adc_volts(0);   // Solar panel
        pause(100);
        vx = (v0*4.9295);
        v1 = adc_volts(1);   // Battery array
        pause(100);
        vy = (v1*4.7869);
        pause(50);
        v2 = adc_volts(2);
        vz = (v2*4.9317);    
      }      
    }
    
    void datamon()
    {
    
    }    
    /******************************/
    [/CODE
    
    [code]
    <!-- wifisolar.html -->
    <!DOCTYPE HTML>
    <html>
    	<head>
    		<style>
    			a{
    				background-color:#637aad;
    				color:white;
    				font-size:23px;
    				margin:5px;
    				width:100px;
    				height:55px;
    				cursor:pointer;
    				padding-top:4px;
    				padding-bottom:4px
    			}
    			a:hover{backgrond-color:white;color:navy;}
    		</style>
    	</head>
    	<body bgcolor=3b5898>
    		<div align="center">
    			<font face="Arial" size=6 color="red">
    			Solar Station Report
    			</font>
    		</div>
    	<br>
    	<font face="Arial" size= 2 color="cyan">
    	Voltage for Solar Panel
    	<br>
    	 or Battery Array or Battery2
    	</font>
    	<br>
    	<br>
    	<!--<input type="radio" name="choices" onclick="getFromMcuS();"> Solar -->
    	<!--<input type="radio" name="choices" onclick="getFromMcuB();"> Battery -->
    	<div>
    	<a onclick="getFromMcuS();">Solar</a>
    	<a onclick="getFromMcuB();">Battery</a>
    	<a onclick="getFromMcuB2();">Battery2</a>
    	</div>
    	<p id="value">Value</p>
    	<br>
    	<div>
    	<font face="Arial" size= 2 color="cyan">
    	Update AB WX+ 
    	<br>
    	 System  Reboot
    	</font>
    	<br>
    	<br>
    
    	<a onclick="resetBtn('R');">Reboot</a>
    	</div>
    	</body>
    	
    	<script>
    	function resetBtn(command)
    	{
    		httpPost("/reset", "go=" + command);
    	}
    	function useMcuReply(response)
    	{
    		var val = document.getElementById("value");
    		val.innerHTML = "Value: " + response;
    	}
    	function getFromMcuS()
    	{
    		httpGet("/solar", useMcuReply);
    	}
    	function getFromMcuB()
    	{
    		httpGet("/battery", useMcuReply);
    	}
    	function getFromMcuB2()
    	{
    		httpGet("/battery2", 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 sleep(milliseconds)
    	  {
    		  var start = new Date().getTime();
    		  for(var i = 0; i < 1e7; i++)
    		  {
    			  if((new Date().getTime() - start) > milliseconds)
    			  {
    				  break;
    			  }
    		  }
    	  }
    	</script>
    </html>
    
  • For my application, I am now in the process of counting how many COGs I have left to work with. So, how may COGs are being used for the WiFi experience. I looked through the docs and it makes mention of serial. Does that mean that it is using at least one COG?

    Would this be correct:
    wifi_start() - 1 COG
    wifi_listen() - 1 COG???

    Since I am using four COGs now, I hope the wifi stuff only uses one, otherwise I will be in a tight spot, as far as the COG count goes. I forgot one thing, does the sd_mount() use a COG?

    Ray
  • Rsadeika wrote: »
    For my application, I am now in the process of counting how many COGs I have left to work with. So, how may COGs are being used for the WiFi experience. I looked through the docs and it makes mention of serial. Does that mean that it is using at least one COG?

    Would this be correct:
    wifi_start() - 1 COG
    wifi_listen() - 1 COG???

    Since I am using four COGs now, I hope the wifi stuff only uses one, otherwise I will be in a tight spot, as far as the COG count goes. I forgot one thing, does the sd_mount() use a COG?

    Ray
    The test code that I wrote for the WX module used two COGs, one to run the main code and one to run the full duplex serial driver. I suspect Andy's code does the same thing but I'm not sure.

  • Making an assumption that the wifi is using two COGs, my count now is seven COGs. Plus I am not sure about sd_mount() and the adcDCpropab.h.

    Their could be a major problem with my application in terms of, the not accounted for COGs, in some of lib functions that I am using.

    Now the problem that I am referring to is the compiler does not tell you, via a warning or error, that you have exceeded the available COGs, the program just runs, but it does not run the exceeded COGs. Talk about a debugging headache.

    I have to sit back, take a breath, and figure out whether I am using the correct tool for the project. And if I do go ahead and find out that I am running out of memory, then their would of been a lot of time and money wasted, which is probably the bigger loss.

    Ray
  • kwinnkwinn Posts: 8,697
    Why would you need multiple cogs for for both WiFi and SD. Wouldn't wifi_start() and wifi_listen() along with any other wifi commands be executed sequentially by a single cog? Wouldn't the same be true for the cog running the SD card?
  • edited 2017-03-26 19:38
    The Wi-Fi library only launches one additional cog for full duplex serial. As in, one cog for main, and one one for full duplex serial.
  • Rsadeika wrote: »
    Making an assumption that the wifi is using two COGs, my count now is seven COGs. Plus I am not sure about sd_mount() and the adcDCpropab.h.

    Their could be a major problem with my application in terms of, the not accounted for COGs, in some of lib functions that I am using.

    Now the problem that I am referring to is the compiler does not tell you, via a warning or error, that you have exceeded the available COGs, the program just runs, but it does not run the exceeded COGs. Talk about a debugging headache.

    I have to sit back, take a breath, and figure out whether I am using the correct tool for the project. And if I do go ahead and find out that I am running out of memory, then their would of been a lot of time and money wasted, which is probably the bigger loss.

    Ray
    You need to check the return values of the functions you call. That will tell you whether a call to start a COG was successful or not. The same code doesn't do that but it is really best programming practice. This is true for pretty much any function that allocates scarce resources like COGs or memory or wi-fi handles.

  • edited 2017-03-26 19:21
    About sorting POST/GET requests by path instead of by ID, here is an example. With this, you can have lots of different GET/POST requests come through on a single listener. The JavaScript uses different paths depending on which button gets clicked. In the Propeller C code, the listener has "/tpfm*". The * makes it so that the listener accepts any path starting with "/tpfm", but you can append it with one or more characters. Some examples: "/tpfm", "/tpfm/A", "/tpfm/B", "/tpfm/multiCharacterOkToo".

    BTW, wifi_command can be used with the Wi-Fi module's API:
    https://www.parallax.com/downloads/parallax-wx-wi-fi-module-firmware-guide
    In this case, it was used to check the path the JavaScript made the browser send and use it to decide how to reply.

    Propeller C Code
    #include "simpletools.h"
    #include "wifi.h"
    
    int event, id, handle;
    int getFromPageId;
    int valA, valB;
    char cmdStr[64];
    char *path;
    
    int main()
    {
      wifi_start(31, 30, 115200, WX_ALL_COM);
    
      getFromPageId = wifi_listen(HTTP, "/tpfm*");
      print("getFromPageId = %d\n", getFromPageId);
    
      while(1)
      {
        valA++;
        valB -= 2;
        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, "/A") != 0)
            {
              print("Incoming GET request from path A, sending %d\r", valA);
              wifi_print(GET, handle, "%d", valA);
            }
            else if(strstr(path, "/B") != 0)
            {
              print("Incoming GET request from path B, sending %d\r", valB);
              wifi_print(GET, handle, "%d", valB);
            }                    
          }        
        }
        pause(500);
      }    
    }
    

    HTML + JavaScript
    <!DOCTYPE html>
    <html>
      <body>
      
        <H2>Value from Microcontroller</H2> 
    
        <p>Click Update to see number from Micro:</p> 
        <button onclick="getFromMcu('A')">Update</button>
        <p id="valueA">Waiting...</p>
    
        <p>Click Update to see a different number from Micro:</p> 
        <button onclick="getFromMcu('B')">Update</button>
        <p id="valueB">Waiting...</p>
    
        <script>
          var getPathExt;
    
          function useMcuReply(response)
          {
            if(getPathExt == 'A')
            {
              var val = document.getElementById("valueA");
              val.innerHTML = "ValueA: " + response;
            }  
            else if(getPathExt == 'B')
            {
              var val = document.getElementById("valueB");
              val.innerHTML = "ValueB: " + response;
            }
          }
    
          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);
          }
    
        </script>
      </body>
    </html>
    

  • Thank You Andy, I will have to check the code out, and see how it will work with my application.

    Ray
  • The application below is a more generic concept of running the softRTC (datetime), in PropGCC, and then being able to view and update the softRTC, with a real time and date, from the browser.

    I sort of got the "view" the softRTC date and time working, but I cannot get the "update" the softRTC part to work correctly. It seems like when I do the update the date part, the softRTC gets a date, but it is the wrong date. Now I am not sure if the problem is in the PropGCC code or the POST code, in the html, or both. Hopefully a fresh set of eyes might reveal the problem. After that problem is resolved, maybe the "update" the time will be a little easier to implement.

    Ray

    /*
      dt_update.c
    
      March 27, 2017
      
    */
    #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 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, "=%d%d%d", &day,&month,&year);
                dt.d=day;
                dt_set(dt);
                dt.mo=month;
                dt_set(dt);
                dt.y=year;
                dt_set(dt);
                print("?? %s %s %s\n",day,month,year);
             }            
           }          
         }                
        
        pause(700);
        
      }  
    }
    
    <!-- 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>Click Time to update system Time</p>
        <button onclick="timeBtn()">Time</button>
    
        <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();
    		  var day = dt.getDate();
    		  var dt1 = new Date();
    		  var month = dt1.getMonth();
    		  var dt2 = new Date();
    		  var year = dt2.getFullYear();
    
    		  httpPost("/tpfm/date", "=" + day + month + 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()
    	  {
    	  }
    
        </script>
      </body>
    </html>
    
  • In my dt_update application, I am still having problems with the POST aspect.

    In the javascript:
    var dt1 = new Date();
    month = dt1.getMonth();
    
    var send = month;
    httpPost("/tpfm/date",send);
    
    This should be sending a '2', 2 = March.

    And in my C code:
    wifi_scan(POST, handle, "%d",&month);
                
    print("%d\n",month);
    
    In this particular run, on the C side, I should be getting a '2', but I keep getting a month = '1'. It does not matter what integer value I assign to 'send', in the javascript code.

    I guess I just do not understand the proper usage for the wifi_scan() function. Anybody have a more understandable explanation?

    Ray
  • edited 2017-03-29 15:38
    Hi Ray,

    The upper two buttons seem to be working for me. See attached. The only "1" I'm seeing is the listener ID. Can you describe how to replicate the error?

    Andy

    P.S. Do you know how to open the developer tools window in the lower-left of the attachment?
    1920 x 1200 - 250K
  • Below is the code that I am working with and having the problem.

    In the C code, for the 'P' section, I have:
    wifi_scan(POST, handle, "%d",&month);
    print("%d\n",month);
    The month value is supposed to be showing a '2', in this particular case. That is the value that is being sent by the html.

    In the html I have changed the integer value, to see if another value would work, but in the C program, it is always showing a one. I think my problem is with understanding how to send the values, so the C program can work with it correctly, or at least I think that is the problem.

    Ray

    /*
      dt_update.c
    
      March 27, 2017
      
    */
    #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;
    //char day,month,year;
    
    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, "%d",&month);
                
                print("%d\n",month);
                //dt.y=year;
                //dt_set(dt);
                //dt.mo=month;
                //dt_set(dt);
                //dt.d=day;
                //dt_set(dt);
                //pause(50);
                //dt = dt_get();
                //dt_toTimeStr(dt, times);
                //dt = dt_get();
                //dt_toDateStr(dt, dates);
                //print("?? %s\n",dates);
             }            
           }          
         }                
        
        pause(700);
        
      }  
    }
    
    <!-- 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();
    		  
    		 var dt2 = new Date();
    		 year = dt2.getFullYear();
    		 
    		  var send = month;
    		  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()
    	  {
    	  }
    
        </script>
      </body>
    </html>
    
  • edited 2017-03-30 14:44
    Hi Ray,

    In your HTML, make sure to send a string with a name=value pair.
    var send = "month=" + month;
    

    In your Propeller C code, the wifi_scan function needs to state the name part of the name=value string before the formatting flag on how to convert the value part. For example:
    wifi_scan(POST, handle, "month%d%d%d", &day,&month,&year);
    

    Andy
  • I made your recommended changes for the HTML:
    var send = "month=" + month + day + year;
    

    And the Propeller C code:
    wifi_scan(POST, handle, "month%d%d%d",&month,&day,&year);
    print("%d/%d/%d\n",month,day,year);
    
    When I run the program, in the terminal window I see:
    '3302017/0/0', for the print("%d/%d/%d\n",month,day,year).
    It seems like the &month variable is getting most of the values.

    When I did it with just the &month, then the value was correct. When I added the &day and &year, everything falls apart.

    Ray
  • edited 2017-03-30 22:31
    That does make sense. We were focusing on getting the month to work. If you want to add the other parts of a date and time, it would probably be better to switch to JavaScript that makes the browser transmit a name=value string that looks like this:

    date=month3day30year2017

    The format string in the wifi_scan Propeller C function running in the Activity Board WX code would then look like this:

    "date%d%d%d"

    The reason I expect this to work is because the %d flag discards non-numeric characters until it starts to pick up digits, and terminates when it gets a non-numeric character.

    Also, this is a lot like the Web Page Controlled LED Lights example. The JavaScript made the browser send led=pin26state1, which was picked up by "led%d%d" in the wifi_scan function.
Sign In or Register to comment.