├── .gitignore ├── LICENSE ├── README.md ├── basics ├── blinking │ ├── README.md │ ├── blinking.toit │ ├── blinking_breadboard.png │ └── blinking_schematic.png ├── button │ ├── README.md │ ├── button.toit │ ├── button_breadboard.png │ └── button_schematic.png ├── fading │ ├── README.md │ ├── fading.toit │ ├── fading_breadboard.png │ └── fading_schematic.png └── hello_world │ ├── README.md │ └── hello_world.toit ├── ble ├── README.md ├── ble_advertising.toit └── ble_scanning.toit ├── communication └── pubsub │ ├── publish_cloud.toit │ ├── publish_device.toit │ ├── subscribe_cloud.toit │ ├── subscribe_cloud.yaml │ └── subscribe_device.toit ├── language ├── README.md ├── class.toit ├── fib.toit └── mandelbrot.toit ├── network ├── mqtt │ ├── README.md │ └── publish_subscribe.toit └── web_sockets │ ├── encrypted_client.toit │ ├── package.lock │ ├── package.yaml │ └── web_sockets.toit ├── sensors ├── photoresistor │ ├── README.md │ ├── photoresistor.toit │ ├── photoresistor_breadboard.png │ └── photoresistor_schematic.png └── reed │ ├── README.md │ ├── reed.toit │ ├── reed_breadboard.png │ └── reed_schematic.png └── uart ├── README.md ├── ping.toit └── pong.toit /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2021 Toitware ApS 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any 4 | purpose with or without fee is hereby granted. 5 | 6 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 7 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 8 | FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 9 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 10 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 11 | OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 12 | PERFORMANCE OF THIS SOFTWARE. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Examples 2 | 3 | A repository with Toit examples, which are also available directly in [console.toit.io](https://console.toit.io/) in the Code tab of your device. 4 | 5 | ## Running the examples 6 | 7 | ```bash 8 | toit run example_name.toit 9 | ``` 10 | 11 | You can choose a default device by running 12 | 13 | ```bash 14 | toit device use 15 | ``` 16 | 17 | The examples can also be deployed, but you will need to create a specification file for doing this. Read more at [docs.toit.io](https://docs.toit.io/platform/deploy/runordeploy/). 18 | 19 | ## Contributing 20 | 21 | Pull requests with new examples are welcome. 22 | 23 | Please make sure to follow the style of the other examples. We can create the schematic and breadboard overview for you, if you dont have access to drawing tools. 24 | 25 | ## Comments in code 26 | 27 | Remember to keep the line length of comments in the code below approx. 90 chars for easy readability in the Code tab of the Toit console. 28 | 29 | ## License 30 | 31 | [0BSD](https://choosealicense.com/licenses/0bsd/) 32 | -------------------------------------------------------------------------------- /basics/blinking/README.md: -------------------------------------------------------------------------------- 1 | # Blinking example 2 | 3 | This example prints 'blink' and makes the LED blink with a 1 second frequency. 4 | 5 | ## Running the example 6 | 7 | Wire your ESP32 and the LED according to: 8 | 9 | ![Alt text](./blinking_breadboard.png "Blinking example - breadboard overview") 10 | 11 | ![Alt text](./blinking_schematic.png "Blinking example - schematic circuit") 12 | 13 | To run the example on your ESP32, execute the following command: 14 | 15 | ```bash 16 | toit run blinking.toit 17 | ``` 18 | -------------------------------------------------------------------------------- /basics/blinking/blinking.toit: -------------------------------------------------------------------------------- 1 | /** 2 | Blinking LED example code. 3 | 4 | This example prints 'blink' and makes the LED blink with a 1 second frequency. 5 | 6 | How to wire the LED: 7 | - Place a 220ohm resistor in between the anode of the LED and GPIO19 and connect the cathode to ground. 8 | - The resistor limits the current that flows through the LED and the ESP32, increasing their life. 9 | */ 10 | 11 | import gpio 12 | 13 | /// The GPIO pin the LED is connected to. 14 | LED ::= 19 15 | 16 | main: 17 | led := gpio.Pin LED --output 18 | while true: 19 | print "blink" 20 | led.set 1 21 | sleep --ms=500 22 | led.set 0 23 | sleep --ms=500 24 | -------------------------------------------------------------------------------- /basics/blinking/blinking_breadboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toitware/examples/95d57e65aaf137a9d236b7acb56f8f57cbcf7031/basics/blinking/blinking_breadboard.png -------------------------------------------------------------------------------- /basics/blinking/blinking_schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toitware/examples/95d57e65aaf137a9d236b7acb56f8f57cbcf7031/basics/blinking/blinking_schematic.png -------------------------------------------------------------------------------- /basics/button/README.md: -------------------------------------------------------------------------------- 1 | # Button example 2 | 3 | This example prints 'Button clicked', when you press the connected button. 4 | 5 | ## Running the example 6 | 7 | Wire your ESP32 and the button according to: 8 | 9 | ![Alt text](./button_breadboard.png "Button example - breadboard overview") 10 | 11 | ![Alt text](./button_schematic.png "Button example - schematic circuit") 12 | 13 | To run the example on your ESP32, execute the following command: 14 | 15 | ```bash 16 | toit run button.toit 17 | ``` 18 | -------------------------------------------------------------------------------- /basics/button/button.toit: -------------------------------------------------------------------------------- 1 | /** 2 | Button example code. 3 | 4 | This example prints 'Button clicked', when you press click the button. 5 | 6 | How to wire the button: 7 | - See breadboard or schematic image. 8 | */ 9 | 10 | import gpio 11 | 12 | /// The GPIO pin the button is connected to. 13 | BUTTON ::= 21 14 | 15 | main: 16 | button ::= gpio.Pin BUTTON --input 17 | while true: 18 | button.wait_for 1 19 | print "Button clicked" 20 | sleep --ms=100 // Debounce. 21 | // Wait for the button to be released. 22 | button.wait_for 0 23 | sleep --ms=100 // Debounce. 24 | -------------------------------------------------------------------------------- /basics/button/button_breadboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toitware/examples/95d57e65aaf137a9d236b7acb56f8f57cbcf7031/basics/button/button_breadboard.png -------------------------------------------------------------------------------- /basics/button/button_schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toitware/examples/95d57e65aaf137a9d236b7acb56f8f57cbcf7031/basics/button/button_schematic.png -------------------------------------------------------------------------------- /basics/fading/README.md: -------------------------------------------------------------------------------- 1 | # Fading example 2 | 3 | This example increases and decreases the lighting level of a LED over and over again. 4 | 5 | ## Running the example 6 | 7 | Wire your ESP32 and the LED according to: 8 | 9 | ![Alt text](./fading_breadboard.png "Fading example - breadboard overview") 10 | 11 | ![Alt text](./fading_schematic.png "Fading example - schematic circuit") 12 | 13 | To run the example on your ESP32, execute the following command: 14 | 15 | ```bash 16 | toit run fading.toit 17 | ``` 18 | -------------------------------------------------------------------------------- /basics/fading/fading.toit: -------------------------------------------------------------------------------- 1 | /** 2 | Fading LED example code. 3 | 4 | This example increases and decreases the lighting level of a LED over and over again. 5 | 6 | How to wire the LED: 7 | - Place a 220ohm resistor in between the anode of the LED and GPIO19 and connect the cathode to ground. 8 | - The resistor limits the current that flows through the LED and the ESP32, increasing their life. 9 | */ 10 | 11 | import gpio.pwm as gpio 12 | import gpio 13 | 14 | /// The GPIO pin for the LED. 15 | LED ::= 19 16 | 17 | /// The configured range of the PWM. 18 | DUTY_MIN ::= 0.0 19 | DUTY_MAX ::= 1.0 20 | CHANGE_RATE ::= 0.01 // The amount of change every 5ms. 21 | 22 | main: 23 | led_pwm ::= gpio.Pwm --frequency=100 24 | led := gpio.Pin LED 25 | fading_led := led_pwm.start led 26 | 27 | step := CHANGE_RATE 28 | duty := DUTY_MIN 29 | while true: 30 | fading_led.set_duty_factor(duty) 31 | duty += step 32 | if not DUTY_MIN <= duty <= DUTY_MAX: 33 | step = -step 34 | duty += step 35 | sleep --ms=5 36 | -------------------------------------------------------------------------------- /basics/fading/fading_breadboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toitware/examples/95d57e65aaf137a9d236b7acb56f8f57cbcf7031/basics/fading/fading_breadboard.png -------------------------------------------------------------------------------- /basics/fading/fading_schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toitware/examples/95d57e65aaf137a9d236b7acb56f8f57cbcf7031/basics/fading/fading_schematic.png -------------------------------------------------------------------------------- /basics/hello_world/README.md: -------------------------------------------------------------------------------- 1 | # Hello World example 2 | 3 | This example prints 'Hello, World!'. 4 | 5 | ## Running the example 6 | 7 | To run the example on your ESP32, execute the following command: 8 | 9 | ```bash 10 | toit run hello_world.toit 11 | ``` 12 | -------------------------------------------------------------------------------- /basics/hello_world/hello_world.toit: -------------------------------------------------------------------------------- 1 | /** 2 | Hello World example code. 3 | 4 | This example prints 'Hello, World!' 5 | */ 6 | 7 | main: 8 | print "Hello, World!" 9 | -------------------------------------------------------------------------------- /ble/README.md: -------------------------------------------------------------------------------- 1 | # Bluetooth Low-Energy example in Toit 2 | 3 | This example shows how to use Bluetooth Low-Energy (BLE) communication with your Toit devices. 4 | 5 | ## Running the examples 6 | 7 | To run the BLE advertising code example on your ESP32, execute the following Toit CLI command: 8 | 9 | ```bash 10 | toit run advertising.toit 11 | ``` 12 | 13 | To run the BLE scanning code example on your ESP32, execute the following Toit CLI command: 14 | 15 | ```bash 16 | toit run scanning.toit 17 | ``` 18 | -------------------------------------------------------------------------------- /ble/ble_advertising.toit: -------------------------------------------------------------------------------- 1 | /** 2 | Example code for advertising a device to other BLE devices. 3 | 4 | A BLE device can advertise itself as a BLE Peripheral. The advertisement can contain a list of services 5 | and a manufactorer data section. 6 | 7 | This code can be run without any prerequisites on an ESP32 device, since BLE is built into 8 | the ESP32 chip. 9 | 10 | */ 11 | 12 | import ble 13 | 14 | main: 15 | device := ble.Device.default 16 | 17 | // Initialize the advertisement payload. 18 | data := ble.AdvertisementData --name="Toit" 19 | 20 | // Start the advertiser. 21 | advertiser := device.advertise 22 | advertiser.set_data data 23 | advertiser.start 24 | 25 | // Let the advertiser run until done (can be indefinitely). 26 | advertiser.wait_for_done 27 | -------------------------------------------------------------------------------- /ble/ble_scanning.toit: -------------------------------------------------------------------------------- 1 | /** 2 | Example code for scanning and discovering BLE peripherals. 3 | 4 | This program must run on a BLE device, and will scan for nearby BLE devices. 5 | 6 | When a BLE device is found, the program will print its address. 7 | 8 | This program can be run without any prerequisites on an ESP32 device, since BLE is 9 | built into the ESP32 chip. 10 | 11 | If you want to discover and find your mobile phone using this program, you can 12 | download a mobile app, like nRF Connect: 13 | 14 | https://apps.apple.com/us/app/nrf-connect-for-mobile/id1054362403 for iOS 15 | 16 | or 17 | 18 | https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en for Android 19 | 20 | The nRF Connect app allows your iOS or Android device to advertise as a BLE peripheral, 21 | as well as discovering nearby peripherals, like your Toit device. 22 | 23 | */ 24 | 25 | import ble 26 | 27 | main: 28 | device := ble.Device.default 29 | 30 | // Start scanning for devices 31 | device.scan: | remote_device/ble.RemoteDevice | 32 | print "Found $remote_device" 33 | -------------------------------------------------------------------------------- /communication/pubsub/publish_cloud.toit: -------------------------------------------------------------------------------- 1 | /** 2 | PubSub example code for publishing on a cloud topic. 3 | 4 | Publishes the message "Hello from another device" on the cloud topic named 5 | "cloud:hello-world" and then terminates. 6 | 7 | A cloud topic is used for communication between two separate device. 8 | The data is sent via the Toit cloud in this scenario. 9 | The cloud topic can also be used for communication between a device and an 10 | external service or client application. 11 | */ 12 | 13 | import pubsub 14 | topic ::= "cloud:hello-world" 15 | 16 | main: 17 | pubsub.publish topic "Hello from another device" 18 | -------------------------------------------------------------------------------- /communication/pubsub/publish_device.toit: -------------------------------------------------------------------------------- 1 | /** 2 | PubSub example code for publishing on a device topic. 3 | 4 | Publishes the message "Hello, World!" on the device topic named 5 | "device:hello-world" and then terminates. 6 | 7 | A device topic is used for communication between two apps installed on the same device. 8 | The data is not sent to the Toit cloud in this scenario. 9 | */ 10 | 11 | import pubsub 12 | topic ::= "device:hello-world" 13 | 14 | main: 15 | pubsub.publish topic "Hello, World!" 16 | -------------------------------------------------------------------------------- /communication/pubsub/subscribe_cloud.toit: -------------------------------------------------------------------------------- 1 | /** 2 | PubSub example code for subscribing to a cloud topic. 3 | 4 | The subscriber sets up a subscription on a cloud topic named "cloud:hello-world", 5 | and prints the received message and the hardware ID of the device. 6 | 7 | A cloud topic is used for communication between two separate device. 8 | The data is sent via the Toit cloud in this scenario. 9 | The cloud topic can also be used for communication between a device and an external 10 | service or client application. 11 | 12 | */ 13 | 14 | import pubsub 15 | topic ::= "cloud:hello-world" 16 | 17 | main: 18 | print "wakeup - checking messages" 19 | pubsub.subscribe topic --blocking=false: | msg/pubsub.Message | 20 | sender := ? 21 | if msg.sender.is_device: 22 | sender = "Device($msg.sender.hardware_id)" 23 | else: 24 | sender = "ExternalSource($msg.sender.external_name)" 25 | print "Received message '$msg.payload.to_string' from $sender" 26 | print "done processing all messages" 27 | -------------------------------------------------------------------------------- /communication/pubsub/subscribe_cloud.yaml: -------------------------------------------------------------------------------- 1 | # App specification file for the PubSub subscriber, which will run the subscribe_cloud.toit program 2 | # every time a message is received in the cloud topic named "cloud:hello-world". 3 | # 4 | # Using this specification file, the subscriber program will be deployed as a long-lived application 5 | # on your device. 6 | 7 | name: Subscribe App 8 | entrypoint: subscribe_cloud.toit 9 | 10 | triggers: 11 | on_pubsub_topic: 12 | - "cloud:hello-world" 13 | -------------------------------------------------------------------------------- /communication/pubsub/subscribe_device.toit: -------------------------------------------------------------------------------- 1 | /** 2 | PubSub example code for subscribing to a device topic. 3 | 4 | The subscriber sets up a subscription on a device topic named "device:hello-world" 5 | and then terminates. 6 | 7 | A device topic is used for communication between two apps installed on the same device. 8 | The data is not sent to the Toit cloud in this scenario. 9 | */ 10 | 11 | import pubsub 12 | topic ::= "device:hello-world" 13 | 14 | main: 15 | pubsub.subscribe topic: | msg/pubsub.Message | 16 | print "Received message '$msg.payload.to_string'" 17 | // Stop listening for other messages and exit 'main'. 18 | return 19 | -------------------------------------------------------------------------------- /language/README.md: -------------------------------------------------------------------------------- 1 | # Toit language examples 2 | 3 | This folder contains basic Toit language code examples. 4 | 5 | The `class.toit` example shows how to define a class and instantiate it as an object. 6 | 7 | ## Running the Class example 8 | 9 | To run the example on your ESP32 or simulator, execute the following command: 10 | 11 | ```bash 12 | toit run class.toit 13 | ``` 14 | 15 | ## License 16 | 17 | [BSD0](https://choosealicense.com/licenses/0bsd/) 18 | -------------------------------------------------------------------------------- /language/class.toit: -------------------------------------------------------------------------------- 1 | /** 2 | Class example code. 3 | 4 | This example shows how to define a class and instantiate it as an object. 5 | MegaGreeter has two functions: one that says hi and another saying bye. 6 | */ 7 | 8 | // Greeter that says hi to everybody. 9 | class MegaGreeter: 10 | names := [] 11 | 12 | constructor name="World": 13 | names.add name 14 | 15 | say_hi: 16 | // Greet everyone individually! 17 | names.do: print "Hello $it!" 18 | 19 | say_bye: 20 | everyone := names.join ", " 21 | print "Bye $everyone, come back soon." 22 | 23 | main: 24 | greeter := MegaGreeter 25 | greeter.say_hi 26 | greeter.say_bye 27 | 28 | greeter.names.add "Lars" 29 | greeter.names.add "Kasper" 30 | greeter.names.add "Rikke" 31 | 32 | greeter.say_hi 33 | greeter.say_bye 34 | -------------------------------------------------------------------------------- /language/fib.toit: -------------------------------------------------------------------------------- 1 | /** 2 | Fibonacci example in Toit. 3 | 4 | This example shows two versions of a Fibonacci sequence generator: 5 | a recursive (fib) and an iterative (iterative_fib). 6 | 7 | The main function prints the results of fib(17) and iterative_fib(17), 8 | as well as how long it took to execute the two methods. 9 | 10 | */ 11 | 12 | main: 13 | x := 17 14 | start := Time.monotonic_us 15 | print "fib($x) = $(fib x)" 16 | print "iterative_fib($x) = $(iterative_fib x)" 17 | print "Time spent on both runs: $(%.2f (Time.monotonic_us - start)/1000.0) ms" 18 | 19 | fib n: 20 | if n <= 2: return 1 21 | return (fib n - 1) + (fib n - 2) 22 | 23 | iterative_fib n: 24 | if n <= 2: return 1 25 | 26 | n1 := 1 27 | n2 := 1 28 | i := 3 29 | 30 | while i < n: 31 | tmp := n2 32 | n2 = n2 + n1 33 | n1 = tmp 34 | i++ 35 | 36 | return n1 + n2 37 | -------------------------------------------------------------------------------- /language/mandelbrot.toit: -------------------------------------------------------------------------------- 1 | /** 2 | Mandelbrot example in Toit. 3 | 4 | */ 5 | 6 | MAX_PIXEL_VALUE ::= 255 7 | 8 | main: 9 | mandelbrot 200 80 -0.6 0.0 0.015 500 10 | mandelbrot 200 80 -0.743030 0.126433 0.00003 5000 11 | 12 | 13 | mandelbrot width height x_center y_center scale limit: 14 | pixels := List width: ByteArray height 15 | y_scale := scale * 2; 16 | for y := height - 1; y >= 0; y--: 17 | for x := 0; x < width; x++: 18 | pixels[x][y] = do_pixel 19 | (x - (width >> 1)) * scale + x_center 20 | (y - (height >> 1)) * y_scale + y_center 21 | limit 22 | if y & 1 == 0: 23 | print_line pixels width y 24 | 25 | print_line pixels width y: 26 | line := "" 27 | for x := 0; x < width; x += 2: 28 | top_left := (color pixels[x ][y + 1]) ? 0 : 1 29 | top_right := (color pixels[x + 1][y + 1]) ? 0 : 2 30 | bottom_left := (color pixels[x ][y ]) ? 0 : 4 31 | bottom_right := (color pixels[x + 1][y ]) ? 0 : 8 32 | index := top_left + top_right + bottom_left + bottom_right 33 | line += [" ", "▘", "▝", "▀", "▖", "▌", "▞", "▛", "▗", "▚", "▐", "▜", "▄", "▙", "▟", "█"][index] 34 | print line 35 | 36 | magnitude x y: 37 | return x * x + y * y > 4.0 38 | 39 | color iterations: 40 | return iterations != 0 and iterations != 2 and iterations != 4 and iterations != MAX_PIXEL_VALUE 41 | 42 | do_pixel x y limit: 43 | i := 0.0 44 | j := 0.0 45 | limit.repeat: 46 | if magnitude i j: return it < MAX_PIXEL_VALUE ? it : MAX_PIXEL_VALUE - 1 47 | itemp := i * i - j * j + x 48 | j = 2 * i * j + y 49 | i = itemp 50 | return MAX_PIXEL_VALUE 51 | -------------------------------------------------------------------------------- /network/mqtt/README.md: -------------------------------------------------------------------------------- 1 | # MQTT publish/subscribe example 2 | 3 | This example shows how to use the Toit MQTT client to publish messages to a topic, and to subscribe on the same topic in order to 4 | receive data - via a public MQTT broker. 5 | 6 | ## Running the example 7 | 8 | First, install the [mqtt package](https://pkg.toit.io/package/github.com%2Ftoitware%2Fmqtt@v1.1.0) by executing the following commands: 9 | 10 | ```bash 11 | toit pkg init 12 | toit pkg install mqtt 13 | ``` 14 | 15 | To run the example on your ESP32, execute the following command: 16 | 17 | ```bash 18 | toit run publish_subscribe.toit 19 | ``` 20 | -------------------------------------------------------------------------------- /network/mqtt/publish_subscribe.toit: -------------------------------------------------------------------------------- 1 | /** 2 | Example code for publishing message to a topic, and subscribing on the same topic in order to 3 | receive data via a public MQTT broker - by using the Toit MQTT client. 4 | */ 5 | 6 | import net 7 | import mqtt 8 | import encoding.json 9 | import device 10 | 11 | // Unique MQTT client ID to identify each client that connects to the MQTT broker. 12 | CLIENT_ID ::= "$device.hardware_id" 13 | // The publicly available Mosquitto MQTT server/broker is used in this example. 14 | HOST ::= "test.mosquitto.org" 15 | // MQTT port 1883 is for unencrypted communication. 16 | PORT ::= 1883 17 | // MQTT topic name 18 | TOPIC ::= "my/sensor/data" 19 | 20 | main: 21 | socket := net.open.tcp_connect HOST PORT 22 | // Connect the Toit MQTT client to the broker 23 | client := mqtt.Client 24 | CLIENT_ID 25 | mqtt.TcpTransport socket 26 | 27 | // The client is now connected. 28 | print "Connected to MQTT Broker @ $HOST:$PORT" 29 | 30 | // Start subscribing to a topic. 31 | subscribe client 32 | 33 | // Start publishing on a topic. 34 | publish client 42.0 35 | 36 | // Process subscribed messages. 37 | client.handle: | topic/string payload/ByteArray | 38 | decoded := json.decode payload 39 | print "Received message '$(decoded["value"])' on '$topic'" 40 | // Stop after first message. 41 | return 42 | 43 | subscribe client/mqtt.Client: 44 | // Subscripe to a topic 45 | client.subscribe TOPIC --qos=1 46 | print "Subscribed to topic '$TOPIC'" 47 | 48 | publish client/mqtt.Client payload/float: 49 | // Publish message to topic 50 | client.publish 51 | TOPIC 52 | json.encode { 53 | "value": payload 54 | } 55 | print "Published message `$payload` on '$TOPIC'" 56 | -------------------------------------------------------------------------------- /network/web_sockets/encrypted_client.toit: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2021 Toitware ApS. 2 | // Use of this source code is governed by a Zero-Clause BSD license that can 3 | // be found in the LICENSE file. 4 | 5 | /** 6 | A simple example that contacts a public echo server over an encrypted TLS 7 | connection. 8 | */ 9 | 10 | import certificate_roots 11 | import expect show * 12 | import http 13 | import net 14 | import tls 15 | import web_socket 16 | 17 | HOST ::= "ws.ifelse.io" 18 | PORT ::= 443 19 | 20 | YAYA ::= "yayayaya" 21 | LALA ::= "lålalala" 22 | 23 | network ::= net.open 24 | 25 | main: 26 | // Make a TCP connection to the server port on localhost. 27 | tcp := network.tcp_connect HOST PORT 28 | 29 | // Put a TLS socket on the TCP connection 30 | socket := tls.Socket.client tcp 31 | --server_name=HOST 32 | --root_certificates=[certificate_roots.ISRG_ROOT_X1] 33 | 34 | // Create an HTTP connection on the TLS connection. 35 | connection := http.Connection socket HOST 36 | 37 | // Make a GET request which we will use to upgrade to WebSockets. 38 | request := connection.new_request "GET" "/" 39 | 40 | // Upgrade the connection to WebSocket. 41 | web_socket := web_socket.WebSocketClient connection request 42 | 43 | // Write some requests to the server and read the responses. 44 | web_socket.write LALA // Write a Unicode string as TEXT. 45 | print web_socket.read // Welcome message from server. 46 | expect_equals LALA web_socket.read // Verify echo. 47 | 48 | web_socket.write YAYA.to_byte_array // Write a UTF-8 byte array as BINARY. 49 | expect_equals YAYA web_socket.read.to_string // Verify echo. 50 | 51 | web_socket.close_write 52 | expect_null web_socket.read 53 | -------------------------------------------------------------------------------- /network/web_sockets/package.lock: -------------------------------------------------------------------------------- 1 | prefixes: 2 | certificate_roots: toit-cert-roots 3 | packages: 4 | toit-cert-roots: 5 | url: github.com/toitware/toit-cert-roots 6 | version: 1.0.0 7 | hash: bc4e7a2382dc5accb9c6c59673a4292e2f3a5698 8 | -------------------------------------------------------------------------------- /network/web_sockets/package.yaml: -------------------------------------------------------------------------------- 1 | dependencies: 2 | certificate_roots: 3 | url: github.com/toitware/toit-cert-roots 4 | version: ^1.0.0 5 | -------------------------------------------------------------------------------- /network/web_sockets/web_sockets.toit: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2021 Toitware ApS. 2 | // Use of this source code is governed by a Zero-Clause BSD license that can 3 | // be found in the LICENSE file. 4 | 5 | /** 6 | A simple example that creates two tasks. One plays the role of the 7 | WebSockets server, while the other plays the role of the client. 8 | Communication is over HTTP, unencrypted. 9 | */ 10 | 11 | import expect show * 12 | import http 13 | import monitor // For communication between tasks in the same process. 14 | import net 15 | import web_socket 16 | 17 | YAYA ::= "yayayaya" 18 | LALA ::= "lålalala" 19 | 20 | main: 21 | ready := monitor.Channel 1 22 | task:: server ready 23 | port := ready.receive 24 | task:: client port 25 | 26 | network ::= net.open 27 | 28 | server ready/monitor.Channel -> none: 29 | // Listen on a random port. 30 | server := network.tcp_listen 0 31 | // Tell the client the port number. 32 | ready.send server.local_address.port 33 | 34 | // Accept just one TCP connection. 35 | socket := server.accept 36 | // Put an HTTP server on the new TCP connection. 37 | connection := http.Connection socket 38 | // The other side will make a GET request. 39 | request := connection.read_request 40 | // This should be a websocket upgrade request. 41 | expect request.should_web_socket_upgrade 42 | // Create the server-side Websocket. 43 | web_socket := web_socket.WebSocketServer connection request 44 | 45 | // Get some requests from the client, and answer them. 46 | expect_equals LALA web_socket.read 47 | web_socket.write YAYA // Write a string as TEXT. 48 | 49 | expect_equals LALA web_socket.read.to_string 50 | web_socket.write YAYA.to_byte_array // Write a byte array as BINARY. 51 | 52 | web_socket.close_write 53 | expect_null web_socket.read 54 | 55 | client port/int -> none: 56 | // Make a TCP connection to the server port on localhost. 57 | host := "localhost" 58 | socket := network.tcp_connect host port 59 | 60 | // Create an HTTP connection on the TCP connection. 61 | connection := http.Connection socket host 62 | // Make a GET request which we will use to upgrade to WebSockets. 63 | request := connection.new_request "GET" "/" 64 | 65 | // Upgrade the connection to WebSocket. 66 | web_socket := web_socket.WebSocketClient connection request 67 | 68 | // Write some requests to the server and read the responses. 69 | web_socket.write LALA // Write a Unicode string as TEXT. 70 | expect_equals YAYA web_socket.read 71 | 72 | web_socket.write LALA.to_byte_array // Write a UTF-8 byte array as BINARY. 73 | expect_equals YAYA web_socket.read.to_string 74 | 75 | web_socket.close_write 76 | expect_null web_socket.read 77 | -------------------------------------------------------------------------------- /sensors/photoresistor/README.md: -------------------------------------------------------------------------------- 1 | # Photoresistor example 2 | 3 | This example prints whether the lighting level is above a given threshold. 4 | 5 | ## Running the example 6 | 7 | Wire your ESP32 and the photoresistor according to: 8 | 9 | ![Alt text](./photoresistor_breadboard.png "Photoresistor example - breadboard overview") 10 | 11 | ![Alt text](./photoresistor_schematic.png "Photoresistor example - schematic circuit") 12 | 13 | To run the example on your ESP32, execute the following command: 14 | 15 | ```bash 16 | toit run photoresistor.toit 17 | ``` 18 | -------------------------------------------------------------------------------- /sensors/photoresistor/photoresistor.toit: -------------------------------------------------------------------------------- 1 | /** 2 | Photoresistor example code. 3 | 4 | This example prints whether the lighting level is above the threshold. 5 | 6 | How to wire the LED: 7 | - Place a 10K ohm resistor in between the one leg of the photo resistor and ground. 8 | Connect the other leg of the photo resistor to PIN 32. 9 | */ 10 | 11 | import gpio 12 | import gpio.adc 13 | 14 | /// The PIN for the photo resistor. 15 | PHOTO_RESISTOR_PIN ::= 32 16 | 17 | /// Lighting threshold. Change the threshold to fit your lighting conditions. 18 | THRESHOLD ::= 2.2 19 | 20 | main: 21 | pin := gpio.Pin PHOTO_RESISTOR_PIN 22 | sensor := adc.Adc pin 23 | on_off := sensor.get > THRESHOLD ? "on" : "off" 24 | print "Light is $on_off" 25 | -------------------------------------------------------------------------------- /sensors/photoresistor/photoresistor_breadboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toitware/examples/95d57e65aaf137a9d236b7acb56f8f57cbcf7031/sensors/photoresistor/photoresistor_breadboard.png -------------------------------------------------------------------------------- /sensors/photoresistor/photoresistor_schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toitware/examples/95d57e65aaf137a9d236b7acb56f8f57cbcf7031/sensors/photoresistor/photoresistor_schematic.png -------------------------------------------------------------------------------- /sensors/reed/README.md: -------------------------------------------------------------------------------- 1 | # Reed sensor example 2 | 3 | This example prints 'contact' and 'no contact' depending on the state of the reed switch. 4 | 5 | ## Running the example 6 | 7 | Wire your ESP32 and the reed sensor according to: 8 | 9 | ![Alt text](./reed_breadboard.png "Reed example - breadboard overview") 10 | 11 | ![Alt text](./reed_schematic.png "Reed example - schematic circuit") 12 | 13 | To run the example on your ESP32, execute the following command: 14 | 15 | ```bash 16 | toit run reed.toit 17 | ``` 18 | -------------------------------------------------------------------------------- /sensors/reed/reed.toit: -------------------------------------------------------------------------------- 1 | /** 2 | Reed sensor example code. 3 | 4 | This example prints 'contact' and 'no contact' depending on the state of the reed switch. 5 | 6 | How to wire the reed sensor: 7 | - Look at the breadboard or schematic file. 8 | */ 9 | import gpio 10 | 11 | /// The GPIO pin the reed sensor is connected to. 12 | REED ::= 26 13 | 14 | main: 15 | reed := gpio.Pin REED --input 16 | while true: 17 | reed.wait_for 1 18 | print "Contact" 19 | sleep --ms=100 // Debounce. 20 | reed.wait_for 0 21 | print "No contact" 22 | sleep --ms=100 // Debounce. 23 | -------------------------------------------------------------------------------- /sensors/reed/reed_breadboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toitware/examples/95d57e65aaf137a9d236b7acb56f8f57cbcf7031/sensors/reed/reed_breadboard.png -------------------------------------------------------------------------------- /sensors/reed/reed_schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toitware/examples/95d57e65aaf137a9d236b7acb56f8f57cbcf7031/sensors/reed/reed_schematic.png -------------------------------------------------------------------------------- /uart/README.md: -------------------------------------------------------------------------------- 1 | # UART Example 2 | 3 | Demonstrates the use of the UART. 4 | 5 | ## How to run 6 | 7 | This demo can be run on one or two ESP32 boards. 8 | It uses: RX=21, TX=22 for one UART and RX=33, TX=32 for the other. 9 | 10 | Connect pin 21 to pin 32, and pin 22 to pin 33. 11 | 12 | The easiest is to simple use the `toit run` commands. 13 | 14 | For a single board 15 | ``` 16 | toit run pong.toit 17 | ``` 18 | and, in another terminal: 19 | 20 | ``` 21 | toit run ping.toit 22 | ``` 23 | 24 | Alternatively, for two boards: 25 | 26 | ``` 27 | toit run -d DEVICE_NAME_1 pong.toit 28 | ``` 29 | and, in another terminal: 30 | 31 | ``` 32 | toit run -d DEVICE_NAME_2 ping.toit 33 | ``` 34 | -------------------------------------------------------------------------------- /uart/ping.toit: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2021 Toitware ApS. 2 | // Use of this source code is governed by a Zero-Clause BSD license that can 3 | // be found in the LICENSE file. 4 | 5 | import uart 6 | import writer show Writer 7 | import reader show BufferedReader 8 | import gpio 9 | 10 | RX ::= 21 11 | TX ::= 22 12 | 13 | main: 14 | // The UART port can be configured in many ways. 15 | // See https://libs.toit.io/uart/class-Port 16 | port := uart.Port 17 | --rx=gpio.Pin RX 18 | --tx=gpio.Pin TX 19 | --baud_rate=115200 20 | 21 | task:: 22 | // We are using a 'Writer', so we don't need to check whether all data has 23 | // been written. 24 | // The 'port.write' function returns the amount of data that was written, 25 | // and the caller needs to call the 'write' function again if not all data 26 | // was handled. The 'Writer' class does this for us. 27 | writer := Writer port 28 | while true: 29 | print "pinging" 30 | writer.write "ping\n" 31 | sleep --ms=500 32 | 33 | task:: 34 | // We are using a BufferedReader here. 35 | // It would be possible to just write 'data := port.read' instead. 36 | // The advantage of using a BufferedReader is that we don't need 37 | // to worry about Unicode characters (which don't use here anyway). 38 | // See the 'pong.toit' file for the direct use case. 39 | reader := BufferedReader port 40 | while line := reader.read_line: 41 | print line 42 | -------------------------------------------------------------------------------- /uart/pong.toit: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2021 Toitware ApS. 2 | // Use of this source code is governed by a Zero-Clause BSD license that can 3 | // be found in the LICENSE file. 4 | 5 | import uart 6 | import writer show Writer 7 | import gpio 8 | 9 | RX ::= 33 10 | TX ::= 32 11 | 12 | main: 13 | // The UART port can be configured in many ways. 14 | // See https://libs.toit.io/uart/class-Port 15 | port := uart.Port 16 | --rx=gpio.Pin RX 17 | --tx=gpio.Pin TX 18 | --baud_rate=115200 19 | 20 | // We are using a 'Writer', so we don't need to check whether all data has 21 | // been written. 22 | // The 'port.write' function returns the amount of data that was written, 23 | // and the caller needs to call the 'write' function again if not all data 24 | // was handled. The 'Writer' class does this for us. 25 | writer := Writer port 26 | 27 | while data := port.read: 28 | writer.write data 29 | --------------------------------------------------------------------------------