Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ESP8266WebServer.h>
- #define HTTP_REST_PORT 80
- #define WIFI_RETRY_DELAY 500
- #define MAX_WIFI_INIT_RETRY 50
- struct Pulsanti {
- byte id;
- byte status1;
- byte status2;
- } pulsanti_resource;
- const char* wifi_ssid = "xxx";
- const char* wifi_passwd = "xx";
- ESP8266WebServer http_rest_server(HTTP_REST_PORT);
- void init_pulsanti_resource()
- {
- pulsanti_resource.id = 1;
- pulsanti_resource.status1 = LOW;
- pulsanti_resource.status2 = LOW;
- }
- int init_wifi() {
- int retries = 0;
- Serial.println("Connecting to WiFi AP..........");
- WiFi.mode(WIFI_STA);
- WiFi.begin(wifi_ssid, wifi_passwd);
- // check the status of WiFi connection to be WL_CONNECTED
- while ((WiFi.status() != WL_CONNECTED) && (retries < MAX_WIFI_INIT_RETRY)) {
- retries++;
- delay(WIFI_RETRY_DELAY);
- Serial.print("#");
- }
- return WiFi.status(); // return the WiFi connection status
- }
- void get_pulsanti() {
- if (pulsanti_resource.id == 0)
- http_rest_server.send(204);
- else {
- http_rest_server.send(200, "text/html", String(pulsanti_resource.status1) + " " + String(pulsanti_resource.status2));
- }
- }
- void config_rest_server_routing() {
- http_rest_server.on("/", HTTP_GET, []() {
- http_rest_server.send(200, "text/html",
- "Welcome to the ESP8266 REST Web Server");
- });
- http_rest_server.on("/pulsanti", HTTP_GET, get_pulsanti);
- }
- void setup(void) {
- Serial.begin(115200);
- init_pulsanti_resource();
- if (init_wifi() == WL_CONNECTED) {
- Serial.print("Connetted to ");
- Serial.print(wifi_ssid);
- Serial.print("--- IP: ");
- Serial.println(WiFi.localIP());
- }
- else {
- Serial.print("Error connecting to: ");
- Serial.println(wifi_ssid);
- }
- config_rest_server_routing();
- http_rest_server.begin();
- Serial.println("HTTP REST Server Started");
- }
- void loop(void) {
- http_rest_server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement