-
Notifications
You must be signed in to change notification settings - Fork 2
Language and net examples #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
42c3182
a39ee98
2b12139
2e82a58
bceb4dd
e3ab23a
a1fdc38
8909dd3
f336b13
3718b09
c6d25ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/) |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -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 | ||||||||
jacobqvist marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
host := "www.google.com" | ||||||||
// Establish a TCP connection with Google.com. | ||||||||
tcp := network_interface.tcp_connect host 443 | ||||||||
jacobqvist marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
// Upgrade the TCP socket to a secure TLS socket. | ||||||||
socket := tls.Socket.client tcp | ||||||||
jacobqvist marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
--server_name=host | ||||||||
--root_certificates=[GLOBALSIGN_ROOT] | ||||||||
|
||||||||
// Instantiate an HTTP connection object, on which we can do requests. | ||||||||
connection := http.Connection socket host | ||||||||
jacobqvist marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
// 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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think our example should do more.
Suggested change
|
||||||||
|
||||||||
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/1Y7DzkvgtdizjkXPdsDtQCv9Uw+wp9U7DbGKogPeMa3Md+pvez7W35Ei | ||||||||
Eua++tgy/BBjFFFy3l3WFpO9KWgz7zpm7AeKJt8T11dleCfeXkkUAKIAf5qoIbap | ||||||||
sZWwpbkNFhHax2xIPEDgfg1azVY80ZcFuctL7TlLnMQ/0lUTbiSw1nH69MG6zO0b | ||||||||
9f6BQdgAmD06yK56mDcYBZUCAwEAAaOCATgwggE0MA4GA1UdDwEB/wQEAwIBhjAP | ||||||||
BgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBTkrysmcRorSCeFL1JmLO/wiRNxPjAf | ||||||||
BgNVHSMEGDAWgBRge2YaRQ2XyolQL30EzTSo//z9SzBgBggrBgEFBQcBAQRUMFIw | ||||||||
JQYIKwYBBQUHMAGGGWh0dHA6Ly9vY3NwLnBraS5nb29nL2dzcjEwKQYIKwYBBQUH | ||||||||
MAKGHWh0dHA6Ly9wa2kuZ29vZy9nc3IxL2dzcjEuY3J0MDIGA1UdHwQrMCkwJ6Al | ||||||||
oCOGIWh0dHA6Ly9jcmwucGtpLmdvb2cvZ3NyMS9nc3IxLmNybDA7BgNVHSAENDAy | ||||||||
MAgGBmeBDAECATAIBgZngQwBAgIwDQYLKwYBBAHWeQIFAwIwDQYLKwYBBAHWeQIF | ||||||||
AwMwDQYJKoZIhvcNAQELBQADggEBADSkHrEoo9C0dhemMXoh6dFSPsjbdBZBiLg9 | ||||||||
NR3t5P+T4Vxfq7vqfM/b5A3Ri1fyJm9bvhdGaJQ3b2t6yMAYN/olUazsaL+yyEn9 | ||||||||
WprKASOshIArAoyZl+tJaox118fessmXn1hIVw41oeQa1v1vg4Fv74zPl6/AhSrw | ||||||||
9U5pCZEt4Wi4wStz6dTZ/CLANx8LZh1J7QJVj2fhMtfTJr9w4z30Z209fOU0iOMy | ||||||||
+qduBmpvvYuR7hZL6Dupszfnw0Skfths18dG9ZKb59UhvmaSGZRVbNQpsg3BZlvi | ||||||||
d0lIKO2d1xozclOzgjXPYovJJIultzkMu34qQb9Sz/yilrbCgj8= | ||||||||
-----END CERTIFICATE-----""" |
Uh oh!
There was an error while loading. Please reload this page.