Skip to content

Commit

Permalink
simpletext support for recent wifi code examples
Browse files Browse the repository at this point in the history
For #173, added putStrWithNpcVals and sscanAfterStr functions.  Temperature from OpenWeathermap updated to search for an exact match to the JSON attribute.
  • Loading branch information
AndyLindsay committed Jul 10, 2018
1 parent c9f708d commit db3fe37
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 42 deletions.
75 changes: 34 additions & 41 deletions Learn/Examples/Network/WiFi/Temperature from OpenWeatherMap.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
match the COM control circuit you choose.
This application does not make the Wi-Fi module serve
and monitor a page. Instead, it grabs text from this
page on the Internet:
www-eng-x.llnl.gov//documents/a_document.txt
and monitor a page. Instead, it gets weather information
over the Internet from openweathermap.com
Note: This example relies on the 0.8 version of the wifi library.
Note: This example relies on the 0.82 version of the wifi library.
Updates may change some function behaviors in later releases.
*/

Expand All @@ -32,15 +31,17 @@

int event, id, handle;
char str[1024];
char wifi_event;
float temp, degC, degF;

int main()
{
wifi_start(31, 30, 115200, WX_ALL_COM);
//wifi_start(9, 8, 115200, USB_PGM_TERM);
wifi_setBuffer(str, sizeof(str));

pause(5000);
print("Waiting 10 s...");
pause(10000);
print("done!\r\r");

while(1)
{
Expand All @@ -50,7 +51,7 @@ int main()

pause(2000);

print("tcpHandle = %d\r", tcpHandle);
print("tcpHandle: %d\r", tcpHandle);

pause(2000);

Expand All @@ -65,55 +66,47 @@ int main()
"Host: api.openweathermap.org\r\n"\
"Connection: keep-alive\r\n"\
"Accept: *" "/" "*\r\n\r\n";

int size = strlen(request);

print("GET req size: %d\r", size);
int length = strlen(request);

print("GET request length: %d\r", length);
print("GET request\r======================================\r");
putStrWithNpcVals(request);
print("\r------------------------------------\rEnd of GET request\r\r\r");

pause(2000);

wifi_print(TCP, tcpHandle, "%s", request);
event = wifi_event;

pause(2000);
size = strlen(str);
print("size = %d", size);
print("WX serial response: %s\r\r", str);

pause(2000);
wifi_scan(TCP, tcpHandle, "%s", str);
for(int n = 0; n < sizeof(str); n++)
{
if(str[n] <= 'z' && str[n] >= ' ')
{
print("%c", str[n]);
}
else if(str[n] == 0)
{
print("[%d]", str[n]);
break;
}
else if(str[n] == '\n')
{
print("\r", str[n]);
}
else
{
print("[%d]", str[n]);
}
}
char *loc = strstr(str, "temp");
print("\rloc = %d\r", loc);
float temp = 0;
sscan(loc+5, "%f", &temp);
float degC = temp -273.15;

length = strlen(str);
print("Server response length: %d\r", length);
print("Server response to GET request\r");
print("======================================\r");
putStrWithNpcVals(str);
print("\r------------------------------------\r");
print( "End of server response to GET request\r\r");

temp = 0;
sscanAfterStr(str, "\"temp\":", "%f", &temp);
print("temp = %6.2f deg K\r", temp);

wifi_disconnect(tcpHandle);
degC = temp -273.15;
print("temp = %6.2f deg C\r", degC);
float degF = degC * 9.0 / 5.0 + 32.0;
degF = degC * 9.0 / 5.0 + 32.0;
print("temp = %6.2f deg C\r", degF);

wifi_disconnect(tcpHandle);

print("\rdelay...");
pause(20000);
print("done!\r\r\r");
}
}



Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ writeByte.c
putByte.c
getByte.c
readByte.c
scanAfterStr.c
sscanAfterStr.c
putStrWithNpcVals.c
>compiler=C
>memtype=cmm main ram compact
>optimize=-Os
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* @file putStrWithNpCVals.c
* Function to print a string to the terminal with non printable characters
* displayed as decimal values in brackets.
*
* Copyright (c) 2018, Parallax Inc.
* Written by Andy Lindsay
*/

#include "simpletext.h"

int putStrWithNpcVals(const char *s)
{
int size = strlen(s);
for(int n = 0; n <= size; n++)
{
if(s[n] <= '~' && s[n] >= ' ')
{
print("%c", s[n]);
}
else if(s[n] == 0)
{
print("[%d]", s[n]);
break;
}
else if(s[n] == '\n' || s[n] == '\r')
{
print("[%d]%c", s[n], s[n]);
}
else
{
print("[%d]", s[n]);
}
//pause(10);
}
//pause(10);
return size;
}

/*
+--------------------------------------------------------------------
| TERMS OF USE: MIT License
+--------------------------------------------------------------------
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+--------------------------------------------------------------------
*/


Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

33 changes: 33 additions & 0 deletions Learn/Simple Libraries/TextDevices/libsimpletext/simpletext.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,27 @@ int sprint(char *buffer, const char *format, ...) __attribute__((format (printf,
int sscan(const char *buffer, const char *fmt, ...) __attribute__((format (printf, 2, 3)));


/**
* @brief Store values represented by characters in a buffer in variable list
* using "..." args.
*
* @note See scan for format specifiers.
*
* @param *buffer Pointer to string with formatted values.
*
* @param *str String to find before scanning. Scanning starts after the last character.
* in this string.
*
* @param *fmt C printf comparable format string.
*
* @param ... Arguments where output will go and must be pointers.
*
* @returns Number of % specifiers successfully matched.
*/
int sscanAfterStr(char *buffer, char *str, char *fmt, ...) __attribute__((format (printf, 3, 4)));



/**
* @name Print/Scan for Device Communication
* @{
Expand Down Expand Up @@ -668,6 +689,18 @@ int putln(const char* str);
int putLine(const char* str);


/**
* @brief Print string to the debug port, and display all non printable characters
* (NPCs) as decimal ASCII values in brackets. For example, delete would be [127].
* This function also displays the null [0] terminator.
*
* @param *str Null terminated string to send.
*/
int putStrWithNpcVals(const char *s);




/**
* @}
*
Expand Down
53 changes: 53 additions & 0 deletions Learn/Simple Libraries/TextDevices/libsimpletext/sscanAfterStr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Super-simple text I/O for PropGCC, stripped of all stdio overhead.
* Copyright (c) 2012, Ted Stefanik. Concept inspired by:
*
* very simple printf, adapted from one written by me [Eric Smith]
* for the MiNT OS long ago
* placed in the public domain
* - Eric Smith
* Propeller specific adaptations
* Copyright (c) 2011 Parallax, Inc.
* Written by Eric R. Smith, Total Spectrum Software Inc.
*
* MIT licensed (see terms at end of file)
*/

#include <stdlib.h>
#include "simpletext.h"

//int sscanAfterStr(char *str, char *strAfter, char *fmt, ...)
int sscanAfterStr(char *buffer, char *str, char *fmt, ...){
char *loc = strstr(buffer, str);
loc += strlen(str);
va_list args;
va_start(args, fmt);
int blocks = _doscanf(loc, fmt, args);
va_end(args);
return blocks;
}

/* +--------------------------------------------------------------------
* | TERMS OF USE: MIT License
* +--------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* +--------------------------------------------------------------------
*/

2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.4.89
v1.4.94

0 comments on commit db3fe37

Please sign in to comment.