├── README.md └── Simple_REST_ESP8266.ino /README.md: -------------------------------------------------------------------------------- 1 | # Simple_REST_ESP8266 2 | 3 | Tihs is a very simple implementation of a HTTP REST Server on ESP8266. 4 | 5 | It shows how to implement GET, POST and PUT method 6 | 7 | It should be considered just as a based for further development 8 | 9 | 10 | -------------------------------------------------------------------------------- /Simple_REST_ESP8266.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define HTTP_REST_PORT 80 6 | #define WIFI_RETRY_DELAY 500 7 | #define MAX_WIFI_INIT_RETRY 50 8 | 9 | struct Led { 10 | byte id; 11 | byte gpio; 12 | byte status; 13 | } led_resource; 14 | 15 | const char* wifi_ssid = "YOUR_SSID"; 16 | const char* wifi_passwd = "YOUR_PASSWD"; 17 | 18 | ESP8266WebServer http_rest_server(HTTP_REST_PORT); 19 | 20 | void init_led_resource() 21 | { 22 | led_resource.id = 0; 23 | led_resource.gpio = 0; 24 | led_resource.status = LOW; 25 | } 26 | 27 | int init_wifi() { 28 | int retries = 0; 29 | 30 | Serial.println("Connecting to WiFi AP.........."); 31 | 32 | WiFi.mode(WIFI_STA); 33 | WiFi.begin(wifi_ssid, wifi_passwd); 34 | // check the status of WiFi connection to be WL_CONNECTED 35 | while ((WiFi.status() != WL_CONNECTED) && (retries < MAX_WIFI_INIT_RETRY)) { 36 | retries++; 37 | delay(WIFI_RETRY_DELAY); 38 | Serial.print("#"); 39 | } 40 | return WiFi.status(); // return the WiFi connection status 41 | } 42 | 43 | void get_leds() { 44 | StaticJsonDocument<200> jsonDocument; 45 | char JSONmessageBuffer[200]; 46 | 47 | if (led_resource.id == 0) 48 | http_rest_server.send(204); 49 | else { 50 | jsonDocument["id"] = led_resource.id; 51 | jsonDocument["gpio"] = led_resource.gpio; 52 | jsonDocument["status"] = led_resource.status; 53 | serializeJsonPretty(jsonDocument, JSONmessageBuffer, sizeof(JSONmessageBuffer)); 54 | http_rest_server.send(200, "application/json", JSONmessageBuffer); 55 | } 56 | } 57 | 58 | void json_to_resource(JsonDocument& jsonDocument) { 59 | int id, gpio, status; 60 | 61 | id = jsonDocument["id"]; 62 | gpio = jsonDocument["gpio"]; 63 | status = jsonDocument["status"]; 64 | 65 | Serial.println(id); 66 | Serial.println(gpio); 67 | Serial.println(status); 68 | 69 | led_resource.id = id; 70 | led_resource.gpio = gpio; 71 | led_resource.status = status; 72 | } 73 | 74 | void post_put_leds() { 75 | StaticJsonDocument<500> jsonDocument; 76 | String post_body = http_rest_server.arg("plain"); 77 | Serial.println(post_body); 78 | Serial.print("HTTP Method: "); 79 | Serial.println(http_rest_server.method()); 80 | 81 | DeserializationError error = deserializeJson(jsonDocument, http_rest_server.arg("plain")); 82 | 83 | if (error) { 84 | Serial.println("error in parsin json body"); 85 | http_rest_server.send(400); 86 | } 87 | else { 88 | if (http_rest_server.method() == HTTP_POST) { 89 | if ((jsonDocument["id"] != 0) && (jsonDocument["id"] != led_resource.id)) { 90 | json_to_resource(jsonDocument); 91 | http_rest_server.sendHeader("Location", "/leds/" + String(led_resource.id)); 92 | http_rest_server.send(201); 93 | pinMode(led_resource.gpio, OUTPUT); 94 | } 95 | else if (jsonDocument["id"] == 0) 96 | http_rest_server.send(404); 97 | else if (jsonDocument["id"] == led_resource.id) 98 | http_rest_server.send(409); 99 | } 100 | else if (http_rest_server.method() == HTTP_PUT) { 101 | if (jsonDocument["id"] == led_resource.id) { 102 | json_to_resource(jsonDocument); 103 | http_rest_server.sendHeader("Location", "/leds/" + String(led_resource.id)); 104 | http_rest_server.send(200); 105 | digitalWrite(led_resource.gpio, led_resource.status); 106 | } 107 | else 108 | http_rest_server.send(404); 109 | } 110 | } 111 | } 112 | 113 | void config_rest_server_routing() { 114 | http_rest_server.on("/", HTTP_GET, []() { 115 | http_rest_server.send(200, "text/html", 116 | "Welcome to the ESP8266 REST Web Server"); 117 | }); 118 | http_rest_server.on("/leds", HTTP_GET, get_leds); 119 | http_rest_server.on("/leds", HTTP_POST, post_put_leds); 120 | http_rest_server.on("/leds", HTTP_PUT, post_put_leds); 121 | } 122 | 123 | void setup(void) { 124 | Serial.begin(115200); 125 | 126 | init_led_resource(); 127 | if (init_wifi() == WL_CONNECTED) { 128 | Serial.print("Connetted to "); 129 | Serial.print(wifi_ssid); 130 | Serial.print("--- IP: "); 131 | Serial.println(WiFi.localIP()); 132 | } 133 | else { 134 | Serial.print("Error connecting to: "); 135 | Serial.println(wifi_ssid); 136 | } 137 | 138 | config_rest_server_routing(); 139 | 140 | http_rest_server.begin(); 141 | Serial.println("HTTP REST Server Started"); 142 | } 143 | 144 | void loop(void) { 145 | http_rest_server.handleClient(); 146 | } 147 | --------------------------------------------------------------------------------