User Tools

Site Tools


esp32

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
esp32 [2025/03/06 20:15] brunoesp32 [2025/03/06 20:28] (current) bruno
Line 87: Line 87:
  
  
-==== Thread ==== 
- 
-<code c> 
-#include <WiFi.h> 
-#include <WebServer.h> 
-#include <HTTPClient.h> 
-#include <ArduinoJson.h> 
-#include <Thread.h> 
- 
-const char *ssid = "....."; 
-const char *password = "....."; 
- 
-float btc; 
- 
-Thread myThread = Thread(); 
- 
-void getBtcFromKraken() { 
- 
-  HTTPClient http; 
-   
-  http.begin("https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR"); 
- 
-  int httpCode = http.GET(); 
-   
-  if (httpCode == HTTP_CODE_OK) { 
-    String response = http.getString(); 
- 
-    JsonDocument doc; 
-    DeserializationError error = deserializeJson(doc, response); 
-    if (!error) { 
-      btc = doc["result"]["XXBTZEUR"]["c"][0].as<float>(); 
-      Serial.print("BTC: "); 
-      Serial.println(btc); 
-    } 
-     
-  } 
- 
-  http.end(); 
-} 
- 
- 
-WebServer server(80); 
- 
- 
- 
-void handleRoot() { 
-  String html = "BTC = " + String(btc); 
-  server.send(200, "text/plain", html); 
-} 
- 
-void setup(void) { 
-  Serial.begin(115200); 
- 
-  WiFi.begin(ssid, password); 
-  while (WiFi.status() != WL_CONNECTED) delay(500); 
- 
-  Serial.print("Connected to "); 
-  Serial.println(ssid); 
-  Serial.print("IP address: "); 
-  Serial.println(WiFi.localIP()); 
- 
-  //if (MDNS.begin("esp32")) { 
-  //  Serial.println("MDNS responder started"); 
-  //} 
- 
- 
-  myThread.onRun(getBtcFromKraken); 
-  myThread.setInterval(10000); 
-  myThread.run(); 
- 
-  server.on("/", handleRoot); 
- 
-  server.onNotFound([]() { 
-    server.send(404, "text/plain", "Not Found"); 
-  }); 
- 
-  server.begin(); 
-  Serial.println("HTTP server started"); 
-} 
- 
-void loop(void) { 
-  if (myThread.shouldRun()) myThread.run(); 
-  Serial.print("."); 
-  server.handleClient(); 
-  delay(500);  //allow the cpu to switch to other tasks 
-} 
- 
-</code> 
esp32.1741292114.txt.gz · Last modified: 2025/03/06 20:15 by bruno