Skip to content

Instantly share code, notes, and snippets.

@zst123
Last active May 27, 2023 15:39
Show Gist options
  • Save zst123/1fc6fd8286411e8c24d064744436bdc8 to your computer and use it in GitHub Desktop.
Save zst123/1fc6fd8286411e8c24d064744436bdc8 to your computer and use it in GitHub Desktop.
RT-Thread Global Tech Conference 2023: Build an embedded IoT web server quickly & easily using RT-Thread Studio. Download Slides: https://drive.google.com/file/d/1HjNO1hh-bPqBm1Z9kYkAjqO9Pfeclu5W
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <webnet.h>
#include <wn_module.h>
#ifdef BSP_USING_ADC1
#define ADC_DEV_NAME "adc1" /* ADC device name */
#define ADC_DEV_CHANNEL 5 /* ADC channel */
#define ADC_CONVERT_BITS (1 << 12) /* The number of conversion bits is 12 */
rt_adc_device_t adc_dev; /* ADC device handle */
#endif
static const char* homepage_output =
"<html><head><title>Home</title></head>"
"<body>"
"<p style=\"color:darkgreen;font-size:18px\">Welcome to home page!</p>"
"<a href=\"./sensor\">Go to sensor page</a>"
"</body></html>\r\n";
static void handler_homepage(struct webnet_session* session) {
/* set http header and write output */
const char* mimetype = mime_get_type(".html");
session->request->result_code = 200;
webnet_session_set_header(session, mimetype, 200, "Ok", rt_strlen(homepage_output));
webnet_session_write(session, (const rt_uint8_t*)homepage_output, rt_strlen(homepage_output));
}
#ifdef BSP_USING_ADC1
static const char* sensorpage_template =
"<html><head><title>Sensor</title></head>"
"<body>"
"<meta http-equiv=\"refresh\" content=\"1\" />"
"<p style=\"color:blue;font-size:18px\">This is a demo of ADC readings.</p>"
"<p>Potentiometer (%d%%)</p>"
"<progress value=\"%d\" max=\"100\"></progress>"
"<a href=\"javascript:history.go(-1);\">Go back home</a>"
"</body></html>\r\n";
char sensorpage_output[400];
#else
static const char* sensorpage_output =
"<html><head><title>Sensor</title></head>"
"<body>"
"<p style=\"color:red;font-size:18px\">There is no sensor to show.</p>"
"<a href=\"javascript:history.go(-1);\">Go back home</a>"
"</body></html>\r\n";
#endif
static void handler_sensorpage(struct webnet_session* session) {
#ifdef BSP_USING_ADC1
/* Read ADC sample value */
rt_uint32_t value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
int percent = value * 100.0 / ADC_CONVERT_BITS;
rt_kprintf("ADC: %d (%d%%) \n", value, percent);
// Parse potentiometer reading onto the webpage
snprintf(sensorpage_output, 400, sensorpage_template, percent, percent);
#endif
/* set http header and write output */
const char* mimetype = mime_get_type(".html");
session->request->result_code = 200;
webnet_session_set_header(session, mimetype, 200, "Ok", rt_strlen(sensorpage_output));
webnet_session_write(session, (const rt_uint8_t*)sensorpage_output, rt_strlen(sensorpage_output));
}
void start_webpage(void) {
// Setup webnet pages
webnet_cgi_set_root("/");
webnet_cgi_register("", handler_homepage);
webnet_cgi_register("sensor", handler_sensorpage);
webnet_init();
// Setup ADC device
#ifdef BSP_USING_ADC1
adc_dev = (rt_adc_device_t) rt_device_find(ADC_DEV_NAME);
rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
#endif
}
// Export to shell
#ifdef FINSH_USING_MSH
MSH_CMD_EXPORT(start_webpage, demo of webnet);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment