├── .gitignore ├── .vscode └── extensions.json ├── README.md ├── include └── README ├── lib └── README ├── platformio.ini ├── src ├── main.cpp └── ota.h └── test └── README /.gitignore: -------------------------------------------------------------------------------- 1 | .pio 2 | .vscode/.browse.c_cpp.db* 3 | .vscode/c_cpp_properties.json 4 | .vscode/launch.json 5 | .vscode/ipch 6 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See http://go.microsoft.com/fwlink/?LinkId=827846 3 | // for the documentation about the extensions.json format 4 | "recommendations": [ 5 | "platformio.platformio-ide" 6 | ], 7 | "unwantedRecommendations": [ 8 | "ms-vscode.cpptools-extension-pack" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP01-IOT-StartTemplate 2 | Template de desenvolvimento para projetos IOT com ESP01 3 | -------------------------------------------------------------------------------- /include/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for project header files. 3 | 4 | A header file is a file containing C declarations and macro definitions 5 | to be shared between several project source files. You request the use of a 6 | header file in your project source file (C, C++, etc) located in `src` folder 7 | by including it, with the C preprocessing directive `#include'. 8 | 9 | ```src/main.c 10 | 11 | #include "header.h" 12 | 13 | int main (void) 14 | { 15 | ... 16 | } 17 | ``` 18 | 19 | Including a header file produces the same results as copying the header file 20 | into each source file that needs it. Such copying would be time-consuming 21 | and error-prone. With a header file, the related declarations appear 22 | in only one place. If they need to be changed, they can be changed in one 23 | place, and programs that include the header file will automatically use the 24 | new version when next recompiled. The header file eliminates the labor of 25 | finding and changing all the copies as well as the risk that a failure to 26 | find one copy will result in inconsistencies within a program. 27 | 28 | In C, the usual convention is to give header files names that end with `.h'. 29 | It is most portable to use only letters, digits, dashes, and underscores in 30 | header file names, and at most one dot. 31 | 32 | Read more about using header files in official GCC documentation: 33 | 34 | * Include Syntax 35 | * Include Operation 36 | * Once-Only Headers 37 | * Computed Includes 38 | 39 | https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html 40 | -------------------------------------------------------------------------------- /lib/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for project specific (private) libraries. 3 | PlatformIO will compile them to static libraries and link into executable file. 4 | 5 | The source code of each library should be placed in a an own separate directory 6 | ("lib/your_library_name/[here are source files]"). 7 | 8 | For example, see a structure of the following two libraries `Foo` and `Bar`: 9 | 10 | |--lib 11 | | | 12 | | |--Bar 13 | | | |--docs 14 | | | |--examples 15 | | | |--src 16 | | | |- Bar.c 17 | | | |- Bar.h 18 | | | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html 19 | | | 20 | | |--Foo 21 | | | |- Foo.c 22 | | | |- Foo.h 23 | | | 24 | | |- README --> THIS FILE 25 | | 26 | |- platformio.ini 27 | |--src 28 | |- main.c 29 | 30 | and a contents of `src/main.c`: 31 | ``` 32 | #include 33 | #include 34 | 35 | int main (void) 36 | { 37 | ... 38 | } 39 | 40 | ``` 41 | 42 | PlatformIO Library Dependency Finder will find automatically dependent 43 | libraries scanning project source files. 44 | 45 | More information about PlatformIO Library Dependency Finder 46 | - https://docs.platformio.org/page/librarymanager/ldf.html 47 | -------------------------------------------------------------------------------- /platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:esp01_1m] 12 | platform = espressif8266 13 | board = esp01_1m 14 | framework = arduino 15 | lib_deps = 16 | me-no-dev/ESPAsyncTCP@^1.2.2 17 | me-no-dev/ESP Async WebServer@^1.2.3 18 | ;upload_port = COM19 19 | upload_port = xxx.xxx.x.xxx -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //#include 8 | #include "ota.h" 9 | 10 | 11 | 12 | #ifndef STASSID 13 | #define STASSID "login" 14 | #define STAPSK "senha" 15 | #endif 16 | 17 | const char* ssid = STASSID; 18 | const char* password = STAPSK; 19 | IPAddress ip(xxx,xxx,x,xxx); 20 | IPAddress gateway(xxx,xxx,x,xxx); 21 | IPAddress subnet(xxx,xxx,x,xxx); 22 | 23 | ESP8266WebServer server(80); 24 | 25 | const int led = 0; 26 | 27 | void handleRoot() { 28 | digitalWrite(led, 1); 29 | server.send(200, "text/plain", "hello from esp8266!\r\n"); 30 | digitalWrite(led, 0); 31 | } 32 | 33 | void handleNotFound() { 34 | digitalWrite(led, 1); 35 | String message = "File Not Found\n\n"; 36 | message += "URI: "; 37 | message += server.uri(); 38 | message += "\nMethod: "; 39 | message += (server.method() == HTTP_GET) ? "GET" : "POST"; 40 | message += "\nArguments: "; 41 | message += server.args(); 42 | message += "\n"; 43 | for (uint8_t i = 0; i < server.args(); i++) { 44 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; 45 | } 46 | server.send(404, "text/plain", message); 47 | digitalWrite(led, 0); 48 | } 49 | 50 | void setup(void) { 51 | 52 | pinMode(led, OUTPUT); 53 | digitalWrite(led, 0); 54 | Serial.begin(115200); 55 | WiFi.mode(WIFI_STA); 56 | WiFi.config(ip,gateway,subnet); 57 | WiFi.begin(ssid, password); 58 | Serial.println(""); 59 | setupOTA(); 60 | // Wait for connection 61 | while (WiFi.status() != WL_CONNECTED) { 62 | delay(500); 63 | Serial.print("."); 64 | } 65 | Serial.println(""); 66 | Serial.print("Connected to "); 67 | Serial.println(ssid); 68 | Serial.print("IP address: "); 69 | Serial.println(WiFi.localIP()); 70 | 71 | if (MDNS.begin("esp8266")) { 72 | Serial.println("MDNS responder started"); 73 | } 74 | 75 | server.on("/", handleRoot); 76 | 77 | server.on("/online", []() { 78 | server.send(200, "text/plain", "this works as well"); 79 | }); 80 | 81 | server.on("/LIGAR", []() { 82 | server.send(200, "text/plain", "ALOOOO GALERA TO VIVO"); 83 | digitalWrite(led,HIGH); 84 | delay(100); 85 | digitalWrite(led,LOW); 86 | }); 87 | 88 | server.on("/gif", []() { 89 | 90 | digitalWrite(led, 1); 91 | server.send(200, "text/plain", "LIGOU!\r\n"); 92 | delay(500); 93 | digitalWrite(led, 0); 94 | 95 | }); 96 | 97 | server.onNotFound(handleNotFound); 98 | 99 | 100 | server.begin(); 101 | Serial.println("HTTP server started"); 102 | } 103 | 104 | 105 | void loop() { 106 | ArduinoOTA.handle(); 107 | server.handleClient(); 108 | MDNS.update(); 109 | 110 | // put your main code here, to run repeatedly: 111 | } -------------------------------------------------------------------------------- /src/ota.h: -------------------------------------------------------------------------------- 1 | 2 | void setupOTA() { 3 | // Port defaults to 8266 4 | // ArduinoOTA.setPort(8266); 5 | 6 | // Hostname defaults to esp8266-[ChipID] 7 | // ArduinoOTA.setHostname("myesp8266"); 8 | 9 | // No authentication by default 10 | // ArduinoOTA.setPassword((const char *)"123"); 11 | 12 | ArduinoOTA.onStart([]() { 13 | Serial.println("Start"); 14 | }); 15 | ArduinoOTA.onEnd([]() { 16 | Serial.println("\nEnd"); 17 | }); 18 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { 19 | Serial.printf("Progress: %u%%\r", (progress / (total / 100))); 20 | }); 21 | ArduinoOTA.onError([](ota_error_t error) { 22 | Serial.printf("Error[%u]: ", error); 23 | if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); 24 | else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); 25 | else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); 26 | else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); 27 | else if (error == OTA_END_ERROR) Serial.println("End Failed"); 28 | }); 29 | ArduinoOTA.begin(); 30 | } -------------------------------------------------------------------------------- /test/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for PlatformIO Unit Testing and project tests. 3 | 4 | Unit Testing is a software testing method by which individual units of 5 | source code, sets of one or more MCU program modules together with associated 6 | control data, usage procedures, and operating procedures, are tested to 7 | determine whether they are fit for use. Unit testing finds problems early 8 | in the development cycle. 9 | 10 | More information about PlatformIO Unit Testing: 11 | - https://docs.platformio.org/page/plus/unit-testing.html 12 | --------------------------------------------------------------------------------