diff --git a/basics/language/README.md b/basics/language/README.md new file mode 100644 index 0000000..fc8e681 --- /dev/null +++ b/basics/language/README.md @@ -0,0 +1,15 @@ +# Fibonacci example + +This example shows how to calculate a specific number in the fibonacci sequence using recursion. + +## Running the example + +To run the example on your ESP32, execute the following command: + +```bash +toit run fibonacci.toit +``` + +## License + +[BSD0](https://choosealicense.com/licenses/0bsd/) diff --git a/basics/language/fibonacci.toit b/basics/language/fibonacci.toit new file mode 100644 index 0000000..c5d27a9 --- /dev/null +++ b/basics/language/fibonacci.toit @@ -0,0 +1,16 @@ +/** +This example defines a top-level function called fib that is not a member of any class. +It prints the 10th number in the fibonacci sequence. + +The fib function is recursive, calling itself, and also makes use of a few new features. +The if-statement is well known from other languages. In Toit it works by taking an expression +and conditionally evaluating a block. Like other blocks we could have used indentation to group +multiple lines. +*/ + +fib n: + if n <= 1: return n + return (fib n-1) + (fib n-2) + +main: + print "The 10th Fibonacci number is $(fib 10)" diff --git a/net/README.md b/net/README.md new file mode 100644 index 0000000..17d965d --- /dev/null +++ b/net/README.md @@ -0,0 +1,15 @@ +# TLS example + +This example sends a request to Google.com and prints the size of the response. + +## Running the example + +To run the example on your ESP32, execute the following command: + +```bash +toit run tls.toit +``` + +## License + +[BSD0](https://choosealicense.com/licenses/0bsd/) diff --git a/net/tls.toit b/net/tls.toit new file mode 100644 index 0000000..a0f9aa7 --- /dev/null +++ b/net/tls.toit @@ -0,0 +1,75 @@ +/** +TLS example code. + +This example sends a request to Google.com and prints the size of the response. +*/ + +import net +import net.x509 as net +import http +import tls + +main: + // Open the default network interface (typically WiFi). + network_interface := net.open + + host := "www.google.com" + // Establish a TCP connection with Google.com. + tcp := network_interface.tcp_connect host 443 + + // Upgrade the TCP socket to a secure TLS socket. + socket := tls.Socket.client tcp + --server_name=host + --root_certificates=[GLOBALSIGN_ROOT] + + // Instantiate an HTTP connection object, on which we can do requests. + connection := http.Connection socket host + // Create and send the request. + request := connection.new_request "GET" "/" + // Before sending the request, we could tweak the headers of the request here. + response := request.send + + bytes := 0 + while data := response.read: + bytes += data.size + + print "Read $bytes bytes from https://$host/" + +// To find trusted roots like this: +// $ openssl s_client -showcerts -verify 5 -connect example.com:443 | grep i: +// The last line with i: shows the name of the root cert. You can find the +// trusted root certificate in Chrome at chrome://settings/certificates (the +// Authorities tab) and export it to a text file. + +GLOBALSIGN_ROOT ::= net.Certificate.parse """\ +-----BEGIN CERTIFICATE----- +MIIFYjCCBEqgAwIBAgIQd70NbNs2+RrqIQ/E8FjTDTANBgkqhkiG9w0BAQsFADBX +MQswCQYDVQQGEwJCRTEZMBcGA1UEChMQR2xvYmFsU2lnbiBudi1zYTEQMA4GA1UE +CxMHUm9vdCBDQTEbMBkGA1UEAxMSR2xvYmFsU2lnbiBSb290IENBMB4XDTIwMDYx +OTAwMDA0MloXDTI4MDEyODAwMDA0MlowRzELMAkGA1UEBhMCVVMxIjAgBgNVBAoT +GUdvb2dsZSBUcnVzdCBTZXJ2aWNlcyBMTEMxFDASBgNVBAMTC0dUUyBSb290IFIx +MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAthECix7joXebO9y/lD63 +ladAPKH9gvl9MgaCcfb2jH/76Nu8ai6Xl6OMS/kr9rH5zoQdsfnFl97vufKj6bwS +iV6nqlKr+CMny6SxnGPb15l+8Ape62im9MZaRw1NEDPjTrETo8gYbEvs/AmQ351k +KSUjB6G00j0uYODP0gmHu81I8E3CwnqIiru6z1kZ1q+PsAewnjHxgsHA3y6mbWwZ +DrXYfiYaRQM9sHmklCitD38m5agI/pboPGiUU+6DOogrFZYJsuB6jC511pzrp1Zk +j5ZPaK49l8KEj8C8QMALXL32h7M1bKwYUH+E4EzNktMg6TO8UpmvMrUpsyUqtEj5 +cuHKZPfmghCN6J3Cioj6OGaK/GP5Afl4/Xtcd/p2h/rs37EOeZVXtL0m79YB0esW +CruOC7XFxYpVq9Os6pFLKcwZpDIlTirxZUTQAs6qzkm06p98g7BAe+dDq6dso499 +iYH6TKX/1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