sds

simple/sensor data server
git clone git://git.hanetzok.net/sds
Log | Files | Refs | README | LICENSE

commit ff5c4205827063dd6b774d34bb2809aa68bfdbd0
parent ed00a3834b572690a239fff48f09e54e73682fcf
Author: Markus Hanetzok <markus@hanetzok.net>
Date:   Fri,  7 Nov 2025 20:47:11 +0100

add auto-refresh

Diffstat:
Msds.c | 28++++++++++++++--------------
1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/sds.c b/sds.c @@ -1,4 +1,4 @@ -/* +/* Copyright 2024 Markus Hanetzok This program is free software: you can redistribute it and/or modify it under @@ -11,7 +11,7 @@ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with - this program. If not, see <https://www.gnu.org/licenses/>. + this program. If not, see <https://www.gnu.org/licenses/>. */ #include <ctype.h> @@ -24,10 +24,10 @@ #include <sys/socket.h> -#define BUFFER_SIZE 1024 -#define OK_HEADER "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\n\r\n" -#define NOTFOUND_HEADER "HTTP/1.0 404 Not Found\r\nContent-Type:text/html\r\n\r\n" -#define NOTALLOWED_HEADER "HTTP/1.0 405 Method Not Allowed\r\nContent-Type:text/html\r\n\r\n" +#define BUFFER_SIZE 1024 +#define OK_HEADER "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\n\r\n" +#define NOTFOUND_HEADER "HTTP/1.0 404 Not Found\r\nContent-Type:text/html\r\n\r\n" +#define NOTALLOWED_HEADER "HTTP/1.0 405 Method Not Allowed\r\nContent-Type:text/html\r\n\r\n" #define INTERNALERR_HEADER "HTTP/1.0 500 Internal Server Error\r\nContent-Type:text/html\r\n\r\n" typedef struct { @@ -53,7 +53,7 @@ void writeresponse(char response[], int clientfd, size_t responselen); #include "sensors.h" void buildsite(char response[], char *name, char *val, char *unit) { - snprintf(response + strlen(response), BUFFER_SIZE - strlen(response), + snprintf(response + strlen(response), BUFFER_SIZE - strlen(response), "<li>%s: %s%s</li>", name, val, unit); } @@ -96,26 +96,26 @@ int main(int argc, char *argv[]) { struct sockaddr_in client_addr; int client_addrlen = sizeof(client_addr); - if (bind(sockfd, (struct sockaddr *)&host_addr, host_addrlen) != 0) + if (bind(sockfd, (struct sockaddr *)&host_addr, host_addrlen) != 0) die("could not bind socket"); if (listen(sockfd, 10) != 0) die("could not listen"); while(1) { - int clientfd = accept(sockfd, (struct sockaddr *)&host_addr, + int clientfd = accept(sockfd, (struct sockaddr *)&host_addr, (socklen_t *)&host_addrlen); if (clientfd < 0) { fprintf(stderr, "could not accept connection\n"); continue; } - getpeername(clientfd, (struct sockaddr *)&client_addr, + getpeername(clientfd, (struct sockaddr *)&client_addr, (socklen_t *)&client_addrlen); memset(buffer, 0, BUFFER_SIZE); read(clientfd, buffer, BUFFER_SIZE); char method[BUFFER_SIZE], uri[BUFFER_SIZE], version[BUFFER_SIZE]; sscanf(buffer, "%s %s %s", method, uri, version); - printf("[%s:%u] %s %s %s\n", inet_ntoa(client_addr.sin_addr), + printf("[%s:%u] %s %s %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), method, version, uri); memset(response, 0, BUFFER_SIZE); @@ -134,8 +134,8 @@ int main(int argc, char *argv[]) { writeresponse(response, clientfd, strlen(response)); continue; } - snprintf(response + strlen(response), BUFFER_SIZE - strlen(response), - "%s%s", OK_HEADER, "<html><h1>Sensor data</h1><ul>"); + snprintf(response + strlen(response), BUFFER_SIZE - strlen(response), + "%s%s", OK_HEADER, "<html><head><meta http-equiv=\"refresh\" content=\"30\"></head><h1>Sensor data</h1><ul>"); int sensorsc = sizeof(sensors)/sizeof(sensors[0]); int i; for (i = 0; i < sensorsc; i++) { @@ -171,7 +171,7 @@ int main(int argc, char *argv[]) { curl_easy_cleanup(curl); } - snprintf(response + strlen(response), BUFFER_SIZE - strlen(response), + snprintf(response + strlen(response), BUFFER_SIZE - strlen(response), "</ul></html>"); writeresponse(response, clientfd, strlen(response));