Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ cmake/pico_sdk_import.cmake
# clangd stuff
.cache
compile_commands.json

#kate backups
*~

Comment on lines +9 to +12
Copy link
Owner

@krzmaz krzmaz Jan 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that was added by mistake

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I didn't realize that Kate was an editor 😅

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.19)
cmake_minimum_required(VERSION 3.18)

find_package(Perl)
if(NOT PERL_FOUND)
Expand Down
5 changes: 3 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ execute_process(COMMAND
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
ECHO_OUTPUT_VARIABLE
ECHO_ERROR_VARIABLE
COMMAND_ERROR_IS_FATAL ANY
)
file(RENAME fsdata.c my_fsdata.c)

add_executable(${PROGRAM_NAME}
main.cpp
ssi.cpp
cgi.cpp
)
target_compile_definitions(${PROGRAM_NAME} PRIVATE
WIFI_SSID=\"${WIFI_SSID}\"
Expand All @@ -37,7 +38,7 @@ target_link_libraries(${PROGRAM_NAME}
)

pico_enable_stdio_usb(${PROGRAM_NAME} TRUE)
pico_enable_stdio_uart(${PROGRAM_NAME} FALSE)
pico_enable_stdio_uart(${PROGRAM_NAME} TRUE)
suppress_tinyusb_warnings()

pico_add_extra_outputs(${PROGRAM_NAME})
138 changes: 138 additions & 0 deletions src/cgi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include "lwip/apps/httpd.h"
#include "pico/cyw43_arch.h"
#include "lwipopts.h"
#include "cgi.h"


static const tCGI cgi_handlers[] = {
{
/* Html request for "/leds.cgi" will start cgi_handler_basic */
"/leds.cgi", cgi_handler_basic
},
{
/* Html request for "/leds2.cgi" will start cgi_handler_extended */
"/leds_ext.cgi", cgi_handler_extended
}
};



/* cgi-handler triggered by a request for "/leds.cgi" */
const char *
cgi_handler_basic(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
{
int i=0;

/* We use this handler for one page request only: "/leds.cgi"
* and it is at position 0 in the tCGI array (see above).
* So iIndex should be 0.
*/
printf("cgi_handler_basic called with index %d\n", iIndex);

/* All leds off */
Led_Off(LED1);
Led_Off(LED2);
Led_Off(LED3);
Led_Off(LED4);

/* Check the query string.
* A request to turn LED2 and LED4 on would look like: "/leds.cgi?led=2&led=4"
*/
for (i = 0; i < iNumParams; i++){
/* check if parameter is "led" */
if (strcmp(pcParam[i] , "led") == 0){
/* look ar argument to find which led to turn on */
if(strcmp(pcValue[i], "1") == 0)
Led_On(LED1);
else if(strcmp(pcValue[i], "2") == 0)
Led_On(LED2);
else if(strcmp(pcValue[i], "3") == 0)
Led_On(LED3);
else if(strcmp(pcValue[i], "4") == 0)
Led_On(LED4);
}
}

/* Our response to the "SUBMIT" is to simply send the same page again*/
return "/cgi.html";
}

/* cgi-handler triggered by a request for "/leds_ext.cgi".
*
* It is almost identical to cgi_handler_basic().
* Both handlers could be easily implemented in one function -
* distinguish them by looking at the iIndex parameter.
* I left it this way to show how to implement two (or more)
* enirely different handlers.
*/
const char *
cgi_handler_extended(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
{
int i=0;

/* We use this handler for one page request only: "/leds_ext.cgi"
* and it is at position 1 in the tCGI array (see above).
* So iIndex should be 1.
*/
printf("cgi_handler_extended called with index %d\n", iIndex);

/* All leds off */
Led_Off(LED1);
Led_Off(LED2);
Led_Off(LED3);
Led_Off(LED4);

/* Check the query string.
* A request to turn LED2 and LED4 on would look like: "/leds.cgi?led=2&led=4"
*/
for (i = 0; i < iNumParams; i++){
/* check if parameter is "led" */
if (strcmp(pcParam[i] , "led") == 0){
/* look ar argument to find which led to turn on */
if(strcmp(pcValue[i], "1") == 0)
Led_On(LED1);
else if(strcmp(pcValue[i], "2") == 0)
Led_On(LED2);
else if(strcmp(pcValue[i], "3") == 0)
Led_On(LED3);
else if(strcmp(pcValue[i], "4") == 0)
Led_On(LED4);
}
}

/* Our response to the "SUBMIT" is to send "/ssi_cgi.shtml".
* The extension ".shtml" tells the server to insert some values
* which show the user what has been done in response.
*/
return "/ssi_cgi.shtml";
}

/* initialize the CGI handler */
void
cgi_init(void)
{
http_set_cgi_handlers(cgi_handlers, 2);

for(int i = LED1; i <= LED4; i++){
gpio_init(i);
gpio_set_dir(i, GPIO_OUT);
gpio_put(i, 0);
}
}

/* led control and debugging info */
void
Led_On(int led)
{
printf("GPIO%d on\n", led);
gpio_put(led, 1);
}

void
Led_Off(int led)
{
printf("GPIO%d off\n", led);
gpio_put(led, 0);
}


23 changes: 23 additions & 0 deletions src/cgi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef __CGI_H__
#define __CGI_H__

// GPIOs for Leds
#define LED1 18
#define LED2 19
#define LED3 20
#define LED4 21

/* initialize the CGI handler */
void cgi_init(void);

/* CGI handler for LED control */
const char * cgi_handler_basic(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]);
/* CGI handler for LED control with feedback*/
const char * cgi_handler_extended(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]);


/* led control and debugging info */
void Led_On(int led);
void Led_Off(int led);

#endif // __CGI_H__
25 changes: 25 additions & 0 deletions src/fs/cgi.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head> <title>Pico W CGI-PAGE</title> </head>
<body> <h1>Pico W</h1>
<h3>CGI-PAGE</h3>

<span>
This page allows you to control four leds: LED1, LED2, LED3 and LED4 connected to GPIO18 - 21<br><br>
To turn a led on/off check/uncheck its corresponding checkbox.<br>
Then click on the "Send" button to submit the changes.<br>
</span>
<br>
<form method="get" action="/leds.cgi">
<input value="1" name="led" type="checkbox">LED1<br>
<input value="2" name="led" type="checkbox">LED2<br>
<input value="3" name="led" type="checkbox">LED3<br>
<input value="4" name="led" type="checkbox">LED4<br>
<br>
<input value="Send" type="submit">
</form>
<br>
<br>
<a href="/index.html">Back</a>
</body>
</html>
10 changes: 8 additions & 2 deletions src/fs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
<head> <title>Pico W</title> </head>
<body> <h1>Pico W</h1>
<p>Hello World</p>
</body>
</html>
<br>
<br>
<br>
<a href="/ssi.shtml">&nbsp;ssi-page: Counter and Voltage</a><br>
<a href="/cgi.html">&nbsp;cgi-page: Ledcontrol</span></a><br>
<a href="/ssi_cgi.shtml">&nbsp;cgi-page: Ledcontrol with feedback</span></a><br>
</body>
</html>
11 changes: 8 additions & 3 deletions src/fs/ssi.shtml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<!DOCTYPE html>
<html>
<head> <title>Pico W</title> </head>
<body> <h1>Pico W</h1>
<head> <title>Pico W SSI-PAGE</title> </head>
<body>
<h1>Pico W</h1>
<h3>SSI-PAGE</h3>
<p><!--#Hello--> Times <!--#counter-->!</p>
<p>GPIO26 voltage is: <!--#GPIO-->!</p>
<br>
<br>
<a href="/index.html">Back</a>
</body>
</html>
</html>
29 changes: 29 additions & 0 deletions src/fs/ssi_cgi.shtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head> <title>Pico W CGI-SSI-PAGE</title> </head>
<body> <h1>Pico W</h1>
<h3>CGI with Feedback (ssi) PAGE</h3>

<span>
This page allows you to control four leds: LED1, LED2, LED3 and LED4 connected to GPIO18 - 21<br><br>
To turn a led on/off check/uncheck its corresponding checkbox.<br>
Then click on the "Send" button to submit the changes.<br>
</span>
<br>
<form method="get" action="/leds_ext.cgi">
<input value="1" name="led" type="checkbox" <!--#state1-->>
<span style=<!--#bg1-->>LED1</span><br>
<input value="2" name="led" type="checkbox" <!--#state2-->>
<span style=<!--#bg2-->>LED2</span><br>
<input value="3" name="led" type="checkbox" <!--#state3-->>
<span style=<!--#bg3-->>LED3</span><br>
<input value="4" name="led" type="checkbox" <!--#state4-->>
<span style=<!--#bg4-->>LED4</span><br>
<input value="Send" type="submit">
</form>
<br>
<br>
<a href="/index.html">Back</a>
</body>
</html>

1 change: 1 addition & 0 deletions src/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

#define LWIP_HTTPD 1
#define LWIP_HTTPD_SSI 1
#define LWIP_HTTPD_CGI 1
// don't include the tag comment - less work for the CPU, but may be harder to debug
#define LWIP_HTTPD_SSI_INCLUDE_TAG 0
// use generated fsdata
Expand Down
12 changes: 7 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#include "pico/cyw43_arch.h"

#include "lwipopts.h"
#include "cgi.h"
#include "ssi.h"

void run_server() {
httpd_init();
ssi_init();
cgi_init();
printf("Http server initialized.\n");
// infinite loop for now
for (;;) {}
Expand All @@ -23,9 +25,6 @@ int main() {
printf("failed to initialise\n");
return 1;
}
// turn on LED to distinguish from BOOTSEL mode
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);

cyw43_arch_enable_sta_mode();
// this seems to be the best be can do using the predefined `cyw43_pm_value` macro:
// cyw43_wifi_pm(&cyw43_state, CYW43_PERFORMANCE_PM);
Expand All @@ -43,6 +42,9 @@ int main() {
auto ip_addr = cyw43_state.netif[CYW43_ITF_STA].ip_addr.addr;
printf("IP Address: %lu.%lu.%lu.%lu\n", ip_addr & 0xFF, (ip_addr >> 8) & 0xFF, (ip_addr >> 16) & 0xFF, ip_addr >> 24);
}

// turn on LED to signal connected
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);


run_server();
}
}
Loading