diff --git a/Learn/Examples/Network/WiFi/Temperature from OpenWeatherMap.c b/Learn/Examples/Network/WiFi/Temperature from OpenWeatherMap.c index dfd88fc8..438cf61c 100644 --- a/Learn/Examples/Network/WiFi/Temperature from OpenWeatherMap.c +++ b/Learn/Examples/Network/WiFi/Temperature from OpenWeatherMap.c @@ -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. */ @@ -32,7 +31,7 @@ int event, id, handle; char str[1024]; -char wifi_event; +float temp, degC, degF; int main() { @@ -40,7 +39,9 @@ int main() //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) { @@ -50,7 +51,7 @@ int main() pause(2000); - print("tcpHandle = %d\r", tcpHandle); + print("tcpHandle: %d\r", tcpHandle); pause(2000); @@ -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"); } } + + + diff --git a/Learn/Simple Libraries/TextDevices/libsimpletext/cmm/libsimpletext.a b/Learn/Simple Libraries/TextDevices/libsimpletext/cmm/libsimpletext.a index afeabd63..58f3c449 100644 Binary files a/Learn/Simple Libraries/TextDevices/libsimpletext/cmm/libsimpletext.a and b/Learn/Simple Libraries/TextDevices/libsimpletext/cmm/libsimpletext.a differ diff --git a/Learn/Simple Libraries/TextDevices/libsimpletext/libsimpletext.side b/Learn/Simple Libraries/TextDevices/libsimpletext/libsimpletext.side index b4eeede8..e1c907f1 100644 --- a/Learn/Simple Libraries/TextDevices/libsimpletext/libsimpletext.side +++ b/Learn/Simple Libraries/TextDevices/libsimpletext/libsimpletext.side @@ -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 diff --git a/Learn/Simple Libraries/TextDevices/libsimpletext/putStrWithNpcVals.c b/Learn/Simple Libraries/TextDevices/libsimpletext/putStrWithNpcVals.c new file mode 100644 index 00000000..78ad4d36 --- /dev/null +++ b/Learn/Simple Libraries/TextDevices/libsimpletext/putStrWithNpcVals.c @@ -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. ++-------------------------------------------------------------------- +*/ + + diff --git a/Learn/Simple Libraries/TextDevices/libsimpletext/scanAfterStr.c b/Learn/Simple Libraries/TextDevices/libsimpletext/scanAfterStr.c new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/Learn/Simple Libraries/TextDevices/libsimpletext/scanAfterStr.c @@ -0,0 +1 @@ + diff --git a/Learn/Simple Libraries/TextDevices/libsimpletext/simpletext.h b/Learn/Simple Libraries/TextDevices/libsimpletext/simpletext.h index 0c7e5324..70ff3222 100644 --- a/Learn/Simple Libraries/TextDevices/libsimpletext/simpletext.h +++ b/Learn/Simple Libraries/TextDevices/libsimpletext/simpletext.h @@ -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 * @{ @@ -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); + + + + /** * @} * diff --git a/Learn/Simple Libraries/TextDevices/libsimpletext/sscanAfterStr.c b/Learn/Simple Libraries/TextDevices/libsimpletext/sscanAfterStr.c new file mode 100644 index 00000000..f5ef7080 --- /dev/null +++ b/Learn/Simple Libraries/TextDevices/libsimpletext/sscanAfterStr.c @@ -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 +#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. + * +-------------------------------------------------------------------- + */ + diff --git a/version.txt b/version.txt index a6abb852..777e29a2 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -v1.4.89 +v1.4.94 \ No newline at end of file