diff --git a/09_thingspeak_randomnum/.gitignore b/09_thingspeak_randomnum/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/09_thingspeak_randomnum/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/09_thingspeak_randomnum/.vscode/extensions.json b/09_thingspeak_randomnum/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/09_thingspeak_randomnum/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/09_thingspeak_randomnum/platformio.ini b/09_thingspeak_randomnum/platformio.ini new file mode 100644 index 0000000..457ca3e --- /dev/null +++ b/09_thingspeak_randomnum/platformio.ini @@ -0,0 +1,15 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32dev] +platform = espressif32 +board = esp32dev +framework = arduino +monitor_speed = 115200 diff --git a/09_thingspeak_randomnum/src/main.cpp b/09_thingspeak_randomnum/src/main.cpp new file mode 100644 index 0000000..3b31c04 --- /dev/null +++ b/09_thingspeak_randomnum/src/main.cpp @@ -0,0 +1,76 @@ +#include +#include +#include + +const char* ssid = "tksteti"; +const char* password = "ProsimTeNevim"; + +const char* serverName = "api.thingspeak.com"; +const String apiKey = "DJFN3ZJGKAO5BXMJ"; + +const int ledPin = 2; + +const char* hostname = "ESP-Thingspeak-Skrabanek"; + +//přidat deklaraci kvuli C++ pro zajištění "existence" nové metody +void sendData2ThingSpeak(int value); + +void setup() { + Serial.begin(115200); + pinMode(ledPin, OUTPUT); + + WiFi.hostname(hostname); + + WiFi.begin(ssid,password); + while (WiFi.status() != WL_CONNECTED) + { + Serial.print("."); + delay(400); + } + Serial.println("\nWiFi připojena"); + Serial.print("IP adresa: "); + Serial.println(WiFi.localIP()); + Serial.print("hostname:"); + Serial.println(WiFi.getHostname()); + +} + +void loop() { + int randomValue = random(0,101); + Serial.print("Nahodne cislo: "); + Serial.println(randomValue); + + digitalWrite(ledPin, HIGH); + sendData2ThingSpeak(randomValue); + digitalWrite(ledPin,LOW); + + delay(5000); +} + +void sendData2ThingSpeak(int value) { + if (WiFi.status() == WL_CONNECTED) { + Serial.println("WiFi připojeno"); + HTTPClient http; + String url = "http://"; + url += serverName; + url += "/update?api_key="; + url += apiKey; + url += "&field1="; //AltGR+C=& + url += value; + Serial.println(url); + http.begin(url); + int httpResponseCode = http.GET(); + if (httpResponseCode > 0) { + Serial.print("HTTP Response code:"); + Serial.println(httpResponseCode); + //vypsat odpověd + String payload = http.getString(); + Serial.println(payload); + } else { + Serial.print("Error code: "); + Serial.println(httpResponseCode); + } + } else { + Serial.println("WiFi odpojeno"); + } +} \ No newline at end of file diff --git a/09_thingspeak_randomnum/test/README b/09_thingspeak_randomnum/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/09_thingspeak_randomnum/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html