Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <stdio.h>
- #include <ESP8266WebServer.h>
- #include <ArduinoJson.h>
- #define HTTP_REST_PORT 80
- #define WIFI_RETRY_DELAY 500
- #define MAX_WIFI_INIT_RETRY 50
- const char* wifi_ssid = "MALWARE";
- const char* wifi_passwd = "fikri007";
- const char * headerKeys[] = {"Authorization"} ;
- const size_t numberOfHeaders = 1;
- ESP8266WebServer http_rest_server(HTTP_REST_PORT);
- 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_req()
- {
- StaticJsonBuffer<200> jsonBuffer;
- JsonObject& jsonObj = jsonBuffer.createObject();
- char JSONmessageBuffer[200];
- http_rest_server.collectHeaders(headerKeys, numberOfHeaders);
- jsonObj["id"] = "445";
- jsonObj["gpio"] = "kepos";
- jsonObj["status"] = "good";
- //print header request
- jsonObj["header_"] = http_rest_server.header("authorization");
- jsonObj.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
- http_rest_server.send(200, "application/json", JSONmessageBuffer);
- }
- 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("/req", HTTP_GET, get_req);
- }
- void setup(void) {
- // put your setup code here, to run once:
- Serial.begin(9600);
- 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) {
- // put your main code here, to run repeatedly:
- http_rest_server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement