├── T9_DualCore_Program_PIco ├── README.md └── Codes │ └── main.py ├── IMAGES ├── coverimage.jpg └── PinDescription.png ├── T3_ADC_on_Pico ├── images │ └── Image_T3_ADC_on_Pico.png ├── codes │ ├── main.py │ └── ssd1306.py └── README.md ├── T5_Interfacing_LCD ├── images │ └── T6_Interfacing_LCD.jpg ├── README.md └── codes │ ├── main.py │ └── lcd_pico.py ├── T2_Interfacing_An_OLED ├── images │ └── T2_Interfacing_An_OLED.jpg ├── codes │ ├── img_to_bmp.py │ ├── main.py │ └── ssd1306.py └── README.md ├── T6_How to Control a Servo Motor using Pico ├── README.md ├── images │ └── T6_Control-Servo-Motor-using-Raspberry-Pi-Pico.jpg └── code │ └── main.py ├── T1_Getting_Started_with_Raspberry_Pi_Pico ├── codes │ └── main.py ├── images │ └── Getting-Started-with-Raspberry-pi-and-Micropython.jpg └── README.md ├── T8_bluetooth_pico_hc05_led_blink ├── images │ └── T7_How-to-Use-Bluetooth-on-Raspberry-Pi.jpg ├── code │ └── main.py └── README.md ├── T7_Interfacing_ESP8266-01_WiFi ├── images │ └── T7_Interfacing-ESP8266-01-with-Raspberry-Pi-Pico.jpg ├── README.md └── Codes │ ├── main.py │ ├── httpParser.py │ └── esp8266.py ├── T4_Interfacing_HC-SR04_Ultrasonic_sensor ├── images │ └── T4_Interface_Ultrasonic_HC-SR04_with_pico.jpg ├── README.md └── codes │ ├── main.py │ └── ssd1306.py ├── T10_Interfacing_Rotaryencoder_with_raspberrypi_pico ├── images │ └── Raspberry pi Pico Rotary encoder.jpg ├── README.md └── codes │ └── Rotaryencoder_Pico.py └── README.md /T9_DualCore_Program_PIco/README.md: -------------------------------------------------------------------------------- 1 | # Dual Core programming on PICO 2 | 3 | Documentation comming soon... 4 | 5 | -------------------------------------------------------------------------------- /IMAGES/coverimage.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/HEAD/IMAGES/coverimage.jpg -------------------------------------------------------------------------------- /IMAGES/PinDescription.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/HEAD/IMAGES/PinDescription.png -------------------------------------------------------------------------------- /T3_ADC_on_Pico/images/Image_T3_ADC_on_Pico.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/HEAD/T3_ADC_on_Pico/images/Image_T3_ADC_on_Pico.png -------------------------------------------------------------------------------- /T5_Interfacing_LCD/images/T6_Interfacing_LCD.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/HEAD/T5_Interfacing_LCD/images/T6_Interfacing_LCD.jpg -------------------------------------------------------------------------------- /T2_Interfacing_An_OLED/images/T2_Interfacing_An_OLED.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/HEAD/T2_Interfacing_An_OLED/images/T2_Interfacing_An_OLED.jpg -------------------------------------------------------------------------------- /T6_How to Control a Servo Motor using Pico/README.md: -------------------------------------------------------------------------------- 1 | # How to Control a Servo Motor with Raspberry Pi Pico using MicroPython 2 | 3 | 4 | 5 | 6 |
7 |
8 | Coming Soon,.... 9 |
10 | -------------------------------------------------------------------------------- /T1_Getting_Started_with_Raspberry_Pi_Pico/codes/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | import time 3 | led = Pin(25, Pin.OUT) 4 | while True: 5 | led.high() 6 | time.sleep(.25) 7 | led.low() 8 | time.sleep(.25) -------------------------------------------------------------------------------- /T8_bluetooth_pico_hc05_led_blink/images/T7_How-to-Use-Bluetooth-on-Raspberry-Pi.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/HEAD/T8_bluetooth_pico_hc05_led_blink/images/T7_How-to-Use-Bluetooth-on-Raspberry-Pi.jpg -------------------------------------------------------------------------------- /T7_Interfacing_ESP8266-01_WiFi/images/T7_Interfacing-ESP8266-01-with-Raspberry-Pi-Pico.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/HEAD/T7_Interfacing_ESP8266-01_WiFi/images/T7_Interfacing-ESP8266-01-with-Raspberry-Pi-Pico.jpg -------------------------------------------------------------------------------- /T4_Interfacing_HC-SR04_Ultrasonic_sensor/images/T4_Interface_Ultrasonic_HC-SR04_with_pico.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/HEAD/T4_Interfacing_HC-SR04_Ultrasonic_sensor/images/T4_Interface_Ultrasonic_HC-SR04_with_pico.jpg -------------------------------------------------------------------------------- /T10_Interfacing_Rotaryencoder_with_raspberrypi_pico/images/Raspberry pi Pico Rotary encoder.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/HEAD/T10_Interfacing_Rotaryencoder_with_raspberrypi_pico/images/Raspberry pi Pico Rotary encoder.jpg -------------------------------------------------------------------------------- /T6_How to Control a Servo Motor using Pico/images/T6_Control-Servo-Motor-using-Raspberry-Pi-Pico.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/HEAD/T6_How to Control a Servo Motor using Pico/images/T6_Control-Servo-Motor-using-Raspberry-Pi-Pico.jpg -------------------------------------------------------------------------------- /T1_Getting_Started_with_Raspberry_Pi_Pico/images/Getting-Started-with-Raspberry-pi-and-Micropython.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/HEAD/T1_Getting_Started_with_Raspberry_Pi_Pico/images/Getting-Started-with-Raspberry-pi-and-Micropython.jpg -------------------------------------------------------------------------------- /T8_bluetooth_pico_hc05_led_blink/code/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin,UART 2 | 3 | uart = UART(0,9600) 4 | 5 | LedGPIO = 16 6 | 7 | led = Pin(LedGPIO, Pin.OUT) 8 | 9 | while True: 10 | if uart.any(): 11 | command = uart.readline() 12 | # print(command) # uncomment this line to see the recieved data 13 | if command==b'\xd0': 14 | led.high() 15 | print("ON") 16 | elif command==b'\xd5': 17 | led.low() 18 | print("OFF") 19 | -------------------------------------------------------------------------------- /T6_How to Control a Servo Motor using Pico/code/main.py: -------------------------------------------------------------------------------- 1 | from time import sleep 2 | from machine import Pin 3 | from machine import PWM 4 | 5 | pwm = PWM(Pin(0)) 6 | pwm.freq(50) 7 | 8 | 9 | 10 | #Function to set an angle 11 | #The position is expected as a parameter 12 | def setServoCycle (position): 13 | pwm.duty_u16(position) 14 | sleep(0.01) 15 | 16 | while True: 17 | for pos in range(1000,9000,50): 18 | setServoCycle(pos) 19 | for pos in range(9000,1000,-50): 20 | setServoCycle(pos) -------------------------------------------------------------------------------- /T10_Interfacing_Rotaryencoder_with_raspberrypi_pico/README.md: -------------------------------------------------------------------------------- 1 | Rotary encoders are popular input devices that can be used to measure and control the position and rotation of a shaft. The Raspberry Pi Pico, with its GPIO pins and capabilities, provides an excellent platform for integrating a rotary encoder into your projects. In this step-by-step guide, we will walk through the process of connecting a rotary encoder to the Raspberry Pi Pico and writing the code to read and interpret its input 2 | ![image](https://github.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/assets/53686087/6fec1c3a-d37e-4757-8d0d-751f1db51e69) 3 | 4 | -------------------------------------------------------------------------------- /T5_Interfacing_LCD/README.md: -------------------------------------------------------------------------------- 1 | # Interface an LCD with the Raspberry Pi Pico using Micropython 2 | 3 | alt_text 4 | 5 |
6 |
7 | 8 | Hey everyone, in this tutorial we are going to interface a Liquid Crystal Display (LCD) module with the Raspberry Pi Pico using Micropython. By the end of this tutorial, you will be able to display strings, characters on the LCD using Micropython. Previously we have worked with an OLED display and we have seen the I2C and ADC on the Raspberry Pi Pico. Now, let’s see how to interface an LCD display module with the pico board. 9 |
10 | 11 | 12 | 13 |
14 |
15 | 16 |
17 | -------------------------------------------------------------------------------- /T2_Interfacing_An_OLED/codes/img_to_bmp.py: -------------------------------------------------------------------------------- 1 | import base64 2 | 3 | 4 | import io 5 | from PIL import Image 6 | 7 | input_filename_with_path = "rpilogo1" 8 | 9 | file_in = input_filename_with_path + ".png" 10 | file_out = input_filename_with_path+".bmp" 11 | img = Image.open(file_in) 12 | 13 | img = img.transpose(Image.FLIP_LEFT_RIGHT) 14 | 15 | threshold = 65 16 | func = lambda x : 255 if x > threshold else 0 17 | img = img.convert('L').point(func,mode='1') 18 | img.save(file_out) 19 | 20 | img = Image.open(file_out,mode='r') 21 | img_bytes = io.BytesIO() 22 | img.save(img_bytes,format='BMP') 23 | img_bytes = img_bytes.getvalue() 24 | 25 | print("Copy this bitmap array:") 26 | print('\n') 27 | print(img_bytes) 28 | print('\n') 29 | print('Image Resolution: ({0},{1})'.format(img.width,img.height)) -------------------------------------------------------------------------------- /T10_Interfacing_Rotaryencoder_with_raspberrypi_pico/codes/Rotaryencoder_Pico.py: -------------------------------------------------------------------------------- 1 | import time   2 | from rotary_irq_rp2 import RotaryIRQ   3 | from machine import Pin   4 | SW=Pin(20,Pin.IN,Pin.PULL_UP)   5 | r = RotaryIRQ(pin_num_clk=18,   6 |         pin_num_dt=19,   7 |         min_val=0,   8 |         reverse=False,   9 |         range_mode=RotaryIRQ.RANGE_UNBOUNDED)   10 | val_old = r.value()   11 | while True:   12 |    try:   13 |      val_new = r.value() 14 |      if SW.value()==0:   15 |        print("Button Pressed")   16 |        print("Selected Number is : ",val_new)     17 |        while SW.value()==0:   18 |          continue     19 |      if val_old != val_new:   20 |        val_old = val_new   21 |        print('result =', val_new)   22 |      time.sleep_ms(50)   23 |    except     KeyboardInterrupt:   24 |      break 25 | 26 | -------------------------------------------------------------------------------- /T4_Interfacing_HC-SR04_Ultrasonic_sensor/README.md: -------------------------------------------------------------------------------- 1 | # Interface an Ultrasonic Sensor with the Raspberry Pi Pico – Displaying the distance on OLED display Using MicroPython 2 | 3 | alt_text 4 | 5 |
6 |
7 | 8 | Hey everyone welcome back to the another tutorial of the Raspberry Pi Pico. If you are following our Raspberry Pi Pico tutorial series, then you should know that we have covered how to interface an OLED display module with pico board and We have measured the ADC value from the ADC channel of the pico board. In this tutorial we will interface an [Ultrasonic Sensor](https://circuitdigest.com/tags/ultrasonic-sensor) with the pico board and we are going to measure the distance of an object and then we will display the distance on the OLED display module using MicroPython. 9 |
10 | -------------------------------------------------------------------------------- /T9_DualCore_Program_PIco/Codes/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | import utime 3 | import _thread 4 | 5 | led1 = Pin(16, machine.Pin.OUT) 6 | led2 = Pin(15, machine.Pin.OUT) 7 | 8 | 9 | sLock = _thread.allocate_lock() 10 | 11 | def CoreTask(): 12 | while True: 13 | sLock.acquire() 14 | print("Entered into the second Thred") 15 | utime.sleep(1) 16 | led2.high() 17 | print("Led 2 turned on") 18 | utime.sleep(2) 19 | led2.low() 20 | print("Led 2 turned off") 21 | utime.sleep(1) 22 | print("Exiting from the 2nd Thread") 23 | utime.sleep(1) 24 | sLock.release() 25 | 26 | _thread.start_new_thread(CoreTask, ()) 27 | 28 | while True: 29 | # We acquire the semaphore lock 30 | sLock.acquire() 31 | print("Entered into the main Thred") 32 | led1.toggle() 33 | utime.sleep(0.15) 34 | print("Led 1 started to toggle.") 35 | utime.sleep(1) 36 | print("Exiting from the main Thread") 37 | utime.sleep(1) 38 | # We release the semaphore lock 39 | sLock.release() -------------------------------------------------------------------------------- /T3_ADC_on_Pico/codes/main.py: -------------------------------------------------------------------------------- 1 | # Display Image & text on I2C driven ssd1306 OLED display 2 | from machine import Pin, I2C, ADC 3 | from ssd1306 import SSD1306_I2C 4 | 5 | import utime 6 | 7 | 8 | analog_value = machine.ADC(28) 9 | conversion_factor = 3.3 / (65535) 10 | 11 | 12 | 13 | WIDTH = 128 # oled display width 14 | HEIGHT = 64 # oled display height 15 | 16 | i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=200000) # Init I2C using pins GP8 & GP9 (default I2C0 pins) 17 | 18 | 19 | oled = SSD1306_I2C(WIDTH, HEIGHT, i2c) # Init oled display 20 | 21 | 22 | # Clear the oled display in case it has junk on it. 23 | oled.fill(0) 24 | 25 | # Add some text 26 | oled.text("CIRCUIT",5,8) 27 | oled.text("DIGEST",45,18) 28 | oled.text("ADC",28,40) 29 | 30 | # Finally update the oled display so the image & text is displayed 31 | oled.show() 32 | utime.sleep(1) 33 | 34 | while True: 35 | 36 | oled.fill(0) 37 | reading = analog_value.read_u16() 38 | print("ADC: ",reading) 39 | utime.sleep(0.2) 40 | voltageValue = reading* conversion_factor 41 | 42 | 43 | oled.text("Voltage:",5,8) 44 | oled.text(str(voltageValue)+"V",15,25) 45 | oled.show() 46 | utime.sleep(1) 47 | -------------------------------------------------------------------------------- /T3_ADC_on_Pico/README.md: -------------------------------------------------------------------------------- 1 | # ADC on Raspberry Pi Pico Using MicroPython - Reading ADC value and Displaying on OLED Display Module 2 | 3 | alt_text 4 | 5 | 6 |
7 | circuitdigest 8 |
9 | 10 | [

Click here](https://circuitdigest.com/microcontroller-projects/adc-on-raspberry-pi-pico-using-micropython) for complete tutorial.

11 | 12 |
13 | 14 |
15 |
16 | 17 | Hey everyone, In this tutorial we are going to perform [ADC](https://circuitdigest.com/tags/adc) on the Pico board. In this tutorial we will perform an ADC conversion with the Pico board. The Raspberry Pi Pico has 4 ADC channels with 12-bit resolution but one ADC channel is connected to the internal temperature sensor. And the rest of all ADCs can be found at the GPIO26, GPIO27 and GPIO28 as ADC0, ADC1 and ADC2 respectively. You can refer the below image where I marked the ADC pins in red colored. 18 |
19 | 20 | -------------------------------------------------------------------------------- /T8_bluetooth_pico_hc05_led_blink/README.md: -------------------------------------------------------------------------------- 1 | # How to use Bluetooth on Raspberry Pi Pico and Control an LED using Mobile 2 | 3 | alt_text 4 | 5 | 6 |
7 | circuitdigest 8 |
9 | 10 | [

Click here](https://circuitdigest.com/microcontroller-projects/how-to-use-bluetooth-on-raspberry-pi-pico-and-control-led-using-mobile) for complete tutorial.

11 | 12 |
13 | 14 |
15 |
16 | Hey everyone welcome back to the another tutorial of the Raspberry Pi Pico tutorial series. In this tutorial we are going to control an LED with an android app using Bluetooth and Micro Python. The HC-06 Bluetooth module is very cheap and easy to interface with the microcontrollers and it is generally implemented by using the UART communication. So, our task is to established an UART communication in between the pico board and the Bluetooth module. 17 |
18 | 19 | -------------------------------------------------------------------------------- /T1_Getting_Started_with_Raspberry_Pi_Pico/README.md: -------------------------------------------------------------------------------- 1 | # Getting started with Raspberry Pi Pico using MicroPython 2 |
3 | alt_text 4 |
5 | circuitdigest 6 |
7 | 8 | [

Click here](https://circuitdigest.com/microcontroller-projects/getting-started-with-raspberry-pi-pico-with-micropython) for complete tutorial.

9 | 10 |
11 |
12 | In this tutorial, we will learn, How to Program a Raspberry Pi Pico using MicroPython and understand the basics of the board. In the end of this tutorial you will be able to Setup the MicroPython for Raspberry Pi Pico on Linux Based System. And we are going to blink the in-built LED on the Raspberry Pi Pico board. 13 | 14 |
15 |
16 | alt_text 17 | 18 | -------------------------------------------------------------------------------- /T2_Interfacing_An_OLED/README.md: -------------------------------------------------------------------------------- 1 | # Interfacing an OLED display with the Raspberry Pi Pico Using Micropython 2 | 3 | alt_text 4 | 5 |
6 | circuitdigest 7 |
8 | 9 | [

Click here](https://circuitdigest.com/microcontroller-projects/interfacing-ssd1306-oled-display-module-with-raspberry-pi-pico) for complete tutorial.

10 | 11 |
12 | 13 | Hey everyone, In this tutorial we are going to interface an [OLED display module](https://circuitdigest.com/tags/oled) with the Pico board. You will be able to perform an I2C communication on Raspberry Pi Pico at the end of this tutorial. In this tutorial we are going to display some strings and images on the OLED display by using Micropython. We already have some articles and projects on Interfacing of the OLED display. You can refer those articles and projects before to get a proper idea on the working of an OLED display. 14 |
15 | -------------------------------------------------------------------------------- /T7_Interfacing_ESP8266-01_WiFi/README.md: -------------------------------------------------------------------------------- 1 | # Interface ESP8266-01 Wi-Fi Module with Raspberry Pi Pico using MicroPython 2 | 3 | alt_text 4 | 5 | 6 |
7 | circuitdigest 8 |
9 | 10 | [

Click here](https://circuitdigest.com/microcontroller-projects/interfacing-esp8266-01-wifi-module-with-raspberry-pi-pico) for complete tutorial.

11 | 12 |
13 | 14 |
15 |
16 | We know that ESP-01 is the best and cheaper module available in the market. As the title suggest, in this tutorial, we are going to interface a separate Wi-Fi module with the Raspberry Pi Pico. We will learn how to connect and program the ESP01 module with Raspberry Pi Pico using Micropython and ESP8266 library. 17 | 18 | The ESP8266-01 module comes with default firmware loaded, which we can access through UART by sending the AT command sets. Using the Micropython ESP8266 we can easily interface ESP8266 with Raspberry Pi Pico and complete the http get/post operation. 19 |
20 | 21 | -------------------------------------------------------------------------------- /T5_Interfacing_LCD/codes/main.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | 4 | rs = machine.Pin(16,machine.Pin.OUT) 5 | e = machine.Pin(17,machine.Pin.OUT) 6 | d4 = machine.Pin(18,machine.Pin.OUT) 7 | d5 = machine.Pin(19,machine.Pin.OUT) 8 | d6 = machine.Pin(20,machine.Pin.OUT) 9 | d7 = machine.Pin(21,machine.Pin.OUT) 10 | 11 | def pulseE(): 12 | e.value(1) 13 | utime.sleep_us(40) 14 | e.value(0) 15 | utime.sleep_us(40) 16 | def send2LCD4(BinNum): 17 | d4.value((BinNum & 0b00000001) >>0) 18 | d5.value((BinNum & 0b00000010) >>1) 19 | d6.value((BinNum & 0b00000100) >>2) 20 | d7.value((BinNum & 0b00001000) >>3) 21 | pulseE() 22 | def send2LCD8(BinNum): 23 | d4.value((BinNum & 0b00010000) >>4) 24 | d5.value((BinNum & 0b00100000) >>5) 25 | d6.value((BinNum & 0b01000000) >>6) 26 | d7.value((BinNum & 0b10000000) >>7) 27 | pulseE() 28 | d4.value((BinNum & 0b00000001) >> 0) 29 | d5.value((BinNum & 0b00000010) >> 1) 30 | d6.value((BinNum & 0b00000100) >> 2) 31 | d7.value((BinNum & 0b00001000) >> 3) 32 | pulseE() 33 | def setUpLCD(): 34 | rs.value(0) 35 | send2LCD4(0b0011)#8 bit 36 | send2LCD4(0b0011)#8 bit 37 | send2LCD4(0b0011)#8 bit 38 | send2LCD4(0b0010)#4 bit 39 | send2LCD8(0b00101000)#4 bit,2 lines?,5*8 bots 40 | send2LCD8(0b00001100)#lcd on, blink off, cursor off. 41 | send2LCD8(0b00000110)#increment cursor, no display shift 42 | send2LCD8(0b00000001)#clear screen 43 | utime.sleep_ms(2)#clear screen needs a long delay 44 | 45 | setUpLCD() 46 | rs.value(1) 47 | for x in 'Hello World!': 48 | send2LCD8(ord(x)) 49 | utime.sleep_ms(100) -------------------------------------------------------------------------------- /T4_Interfacing_HC-SR04_Ultrasonic_sensor/codes/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, I2C 2 | from ssd1306 import SSD1306_I2C 3 | 4 | import utime 5 | 6 | trigger = Pin(3, Pin.OUT) 7 | echo = Pin(2, Pin.IN) 8 | 9 | 10 | def ultrasonnic(): 11 | timepassed=0 12 | trigger.low() 13 | utime.sleep_us(2) 14 | trigger.high() 15 | utime.sleep_us(10) 16 | trigger.low() 17 | while echo.value() == 0: 18 | signaloff = utime.ticks_us() 19 | while echo.value() == 1: 20 | signalon = utime.ticks_us() 21 | timepassed = signalon - signaloff 22 | 23 | return timepassed 24 | 25 | 26 | WIDTH = 128 # oled display width 27 | HEIGHT = 64 # oled display height 28 | 29 | i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=200000) # Init I2C using pins GP8 & GP9 (default I2C0 pins) 30 | print("I2C Address : "+hex(i2c.scan()[0]).upper()) # Display device address 31 | print("I2C Configuration: "+str(i2c)) # Display I2C config 32 | 33 | 34 | oled = SSD1306_I2C(WIDTH, HEIGHT, i2c) # Init oled display 35 | 36 | 37 | # Clear the oled display in case it has junk on it. 38 | oled.fill(0) 39 | 40 | # Add some text 41 | oled.text("CIRCUIT DIGEST",5,8) 42 | 43 | oled.text("INTERFACING THE",5,30) 44 | oled.text("ULTRASONNIC",28,40) 45 | oled.text("SENSOR",50,50) 46 | 47 | # Finally update the oled display so the image & text is displayed 48 | oled.show() 49 | utime.sleep(4) 50 | 51 | while True: 52 | 53 | oled.fill(0) 54 | measured_time = ultrasonnic() 55 | distance_cm = (measured_time * 0.0343) / 2 56 | distance_cm = round(distance_cm,2) 57 | 58 | 59 | oled.text("",0,8) 60 | oled.text(">> "+str(distance_cm)+" cm",2,25) 61 | oled.show() 62 | utime.sleep(1) 63 | 64 | -------------------------------------------------------------------------------- /T5_Interfacing_LCD/codes/lcd_pico.py: -------------------------------------------------------------------------------- 1 | def pulseE(): 2 | e.value(1) 3 | utime.sleep_us(40) 4 | e.value(0) 5 | utime.sleep_us(40) 6 | 7 | def longDelay(t): 8 | utime.sleep_ms(t) 9 | 10 | def shortDelay(t): 11 | utime.sleep_us(t) 12 | 13 | 14 | def send2LCD4(BinNum): 15 | d4.value((BinNum & 0b00000001) >>0) 16 | d5.value((BinNum & 0b00000010) >>1) 17 | d6.value((BinNum & 0b00000100) >>2) 18 | d7.value((BinNum & 0b00001000) >>3) 19 | pulseE() 20 | 21 | def send2LCD8(BinNum): 22 | d4.value((BinNum & 0b00010000) >>4) 23 | d5.value((BinNum & 0b00100000) >>5) 24 | d6.value((BinNum & 0b01000000) >>6) 25 | d7.value((BinNum & 0b10000000) >>7) 26 | pulseE() 27 | d4.value((BinNum & 0b00000001) >> 0) 28 | d5.value((BinNum & 0b00000010) >> 1) 29 | d6.value((BinNum & 0b00000100) >> 2) 30 | d7.value((BinNum & 0b00001000) >> 3) 31 | pulseE() 32 | 33 | def setCursor(line, pos): 34 | b = 0 35 | if line==1: 36 | b=0 37 | elif line==2: 38 | b=40 39 | returnHome() 40 | for i in range(0, b+pos): 41 | moveCursorRight() 42 | 43 | def returnHome(): 44 | rs.value(0) 45 | send2LCD8(0b00000010) 46 | rs.value(1) 47 | longDelay(2) 48 | 49 | def moveCursorRight(): 50 | rs.value(0) 51 | send2LCD8(0b00010100) 52 | rs.value(1) 53 | 54 | def setupLCD(): 55 | 56 | rs= machine.Pin(16,machine.Pin.OUT) 57 | e = machine.Pin(17,machine.Pin.OUT) 58 | d4 = machine.Pin(18,machine.Pin.OUT) 59 | d5 = machine.Pin(19,machine.Pin.OUT) 60 | d6 = machine.Pin(20,machine.Pin.OUT) 61 | d7 = machine.Pin(21,machine.Pin.OUT) 62 | rs.value(0) 63 | send2LCD4(0b0011) 64 | send2LCD4(0b0011) 65 | send2LCD4(0b0011) 66 | send2LCD4(0b0010) 67 | send2LCD8(0b00101000) 68 | send2LCD8(0b00001100)#lcd on, blink off, cursor off. 69 | send2LCD8(0b00000110)#increment cursor, no display shift 70 | send2LCD8(0b00000001)#clear screen 71 | longDelay(2)#clear screen needs a long delay 72 | rs.value(1) 73 | 74 | 75 | 76 | def displayString(row, col, input_string): 77 | setCursor(row,col) 78 | for x in input_string: 79 | send2LCD8(ord(x)) 80 | utime.sleep_ms(100) -------------------------------------------------------------------------------- /T7_Interfacing_ESP8266-01_WiFi/Codes/main.py: -------------------------------------------------------------------------------- 1 | ''' 2 | File name: main.py 3 | Author: Noyel Seth 4 | Email: noyelseth@gmail.com 5 | MicroPython Version: 3.4 6 | ''' 7 | 8 | from machine import UART, Pin 9 | import time 10 | from esp8266 import ESP8266 11 | 12 | esp01 = ESP8266() 13 | esp8266_at_ver = None 14 | 15 | led=Pin(25,Pin.OUT) 16 | 17 | print("StartUP",esp01.startUP()) 18 | #print("ReStart",esp01.reStart()) 19 | print("StartUP",esp01.startUP()) 20 | print("Echo-Off",esp01.echoING()) 21 | print("\r\n\r\n") 22 | 23 | ''' 24 | Print ESP8266 AT comand version and SDK details 25 | ''' 26 | esp8266_at_var = esp01.getVersion() 27 | if(esp8266_at_var != None): 28 | print(esp8266_at_var) 29 | 30 | ''' 31 | set the current WiFi in SoftAP+STA 32 | ''' 33 | esp01.setCurrentWiFiMode() 34 | 35 | #apList = esp01.getAvailableAPs() 36 | #for items in apList: 37 | # print(items) 38 | #for item in tuple(items): 39 | # print(item) 40 | 41 | print("\r\n\r\n") 42 | 43 | ''' 44 | Connect with the WiFi 45 | ''' 46 | print("Try to connect with the WiFi..") 47 | while (1): 48 | if "WIFI CONNECTED" in esp01.connectWiFi("ssid","pwd"): 49 | print("ESP8266 connect with the WiFi..") 50 | break; 51 | else: 52 | print(".") 53 | time.sleep(2) 54 | 55 | 56 | print("\r\n\r\n") 57 | print("Now it's time to start HTTP Get/Post Operation.......\r\n") 58 | 59 | while(1): 60 | led.toggle() 61 | time.sleep(1) 62 | 63 | ''' 64 | Going to do HTTP Get Operation with www.httpbin.org/ip, It return the IP address of the connected device 65 | ''' 66 | httpCode, httpRes = esp01.doHttpGet("www.httpbin.org","/ip","RaspberryPi-Pico", port=80) 67 | print("------------- www.httpbin.org/ip Get Operation Result -----------------------") 68 | print("HTTP Code:",httpCode) 69 | print("HTTP Response:",httpRes) 70 | print("-----------------------------------------------------------------------------\r\n\r\n") 71 | 72 | 73 | ''' 74 | Going to do HTTP Post Operation with www.httpbin.org/post 75 | ''' 76 | post_json="abcdefghijklmnopqrstuvwxyz" #"{\"name\":\"Noyel\"}" 77 | httpCode, httpRes = esp01.doHttpPost("www.httpbin.org","/post","RPi-Pico", "application/json",post_json,port=80) 78 | print("------------- www.httpbin.org/post Post Operation Result -----------------------") 79 | print("HTTP Code:",httpCode) 80 | print("HTTP Response:",httpRes) 81 | print("--------------------------------------------------------------------------------\r\n\r\n") 82 | #break 83 | 84 | 85 | -------------------------------------------------------------------------------- /T7_Interfacing_ESP8266-01_WiFi/Codes/httpParser.py: -------------------------------------------------------------------------------- 1 | ''' 2 | File name: httpParser.py 3 | Author: Noyel Seth 4 | Email: noyelseth@gmail.com 5 | MicroPython Version: 3.4 6 | ''' 7 | 8 | class HttpParser: 9 | """ 10 | This is a class for parse HTTP response, coming from ESP8266 after complete the Post/Get operation 11 | Using this class, you parse the HTTP Post/Get operation's response and get HTTP status code, Response. 12 | """ 13 | 14 | __httpErrorCode=None 15 | __httpHeader=None 16 | __httpResponseLength=None 17 | __httpResponse=None 18 | 19 | def __init__(self): 20 | """ 21 | The constaructor for HttpParser class 22 | """ 23 | self.__httpErrCode=None 24 | self.__httpHeader=None 25 | self.__httpResponseLength=None 26 | self.__httpResponse=None 27 | 28 | def parseHTTP(self, httpRes): 29 | """ 30 | This funtion use to parse the HTTP response and return back the HTTP status code 31 | 32 | Return: 33 | HTTP status code. 34 | """ 35 | #print(">>>>",httpRes) 36 | if(httpRes != None): 37 | retParseResponse=str(httpRes).partition("+IPD,")[2] 38 | #print(">>>>>>>>>>>>>>>>>",retParseResponse) 39 | retParseResponse=retParseResponse.split(r"\r\n\r\n"); 40 | #print(">>>>>>>>>>>>>>>>>",retParseResponse[0]) 41 | self.__httpResponse = retParseResponse[1] 42 | #print(">>>>>>>>>>>>>>>>>???",retParseResponse[1]) 43 | self.__httpHeader=str(retParseResponse[0]).partition(":")[2] 44 | #print("--",self.__httpHeader) 45 | for code in str(self.__httpHeader.partition(r"\r\n")[0]).split(): 46 | if code.isdigit(): 47 | self.__httpErrCode=int(code) 48 | 49 | if(self.__httpErrCode != 200): 50 | self.__httpResponse=None 51 | 52 | return self.__httpErrCode 53 | else: 54 | return 0 55 | 56 | def getHTTPErrCode(self): 57 | """ 58 | This funtion use to get latest parsed HTTP response's status code 59 | 60 | Return: 61 | HTTP status code. 62 | """ 63 | return self.__httpErrCode 64 | 65 | def getHTTPResponse(self): 66 | """ 67 | This funtion use to get latest parsed HTTP response's response massage. 68 | 69 | Return: 70 | HTTP response message. 71 | """ 72 | return self.__httpResponse 73 | 74 | def __del__(self): 75 | """ 76 | The distaructor for HttpParser class 77 | """ 78 | pass 79 | 80 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Raspberry Pi Pico Tutorial 2 | 3 | alt_text 4 |
5 | circuitdigest 6 |
7 | 8 | [Click here](https://circuitdigest.com/tags/raspberry-pi-pico) for complete tutorial series on Raspberry Pi Pico. 9 | 10 |
11 | Hello everyone, this repository contains the complete tutorial series on the Raspberry Pi Pico and it can be very usefull resource for you. The Raspberry Pi Pico is based on the RP4060 microcontroller chip which is the first ever microcontroller family class by Raspberry Pi foundation. If you are searching for the low cost development board with high-performance features, then the Pico board is the best choice. We can program this Pico board in various methods as it supports basic debugging tools and libraries for C/C++ Development and Micropython Environment. 12 |
13 | [ Note: Let's see the folder structure of this repo. Every tutorials has three folders. (i.e. images,schematics,codes) ] 14 |
15 |
16 | 17 | **Pin Description of the Raspberry Pi Pico** 18 | 19 | alt_text 20 |
21 |
22 | 23 | **The Raspberry Pi Pico Tutorial Series by Circuit Digest** 24 |
25 | ✅Tutorial 1:[Getting started with Raspberry Pi Pico using MicroPython](https://github.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/tree/main/T1_Getting_Started_with_Raspberry_Pi_Pico) 26 |
27 | ✅Tutorial 2:[Interfacing an OLED display with the Raspberry Pi Pico Using Micropython](https://github.com/Circuit-Digest/Raspberry_Pi_Pico_Tutorial/tree/main/T2_Interfacing_An_OLED) 28 |
29 | ✅Tutorial 3:[Interfacing Ultrasonic Sensor with Raspberry Pi Pico and Displaying the distance on OLED display Using MicroPython](https://circuitdigest.com/microcontroller-projects/interfacing-ultrasonic-sensor-with-raspberry-pi-pico) 30 |
31 | ✅Tutorial 4:[ADC on Raspberry Pi Pico using MicroPython - Reading ADC Value and Displaying on OLED Display Module](https://circuitdigest.com/microcontroller-projects/adc-on-raspberry-pi-pico-using-micropython) 32 |
33 | ✅Tutorial 5:[Interfacing LCD Display with Raspberry Pi Pico using Micropython](https://circuitdigest.com/microcontroller-projects/interfacing-raspberry-pi-pico-with-16x2-lcd-using-micropython) 34 |
35 | ✅Tutorial 6:[Control a Servo Motor with Raspberry Pi Pico Using PWM in MicroPython](https://circuitdigest.com/microcontroller-projects/control-a-servo-motor-with-raspberry-pi-pico-using-pwm-in-micropython) 36 |
37 | 38 | -------------------------------------------------------------------------------- /T2_Interfacing_An_OLED/codes/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, I2C 2 | from ssd1306 import SSD1306_I2C 3 | import utime 4 | import framebuf,sys 5 | 6 | WIDTH = 128 # oled display width 7 | HEIGHT = 64 # oled display height 8 | 9 | image_byte_arr = b'BM\xbe\x00\x00\x00\x00\x00\x00\x00>\x00\x00\x00(\x00\x00\x00 \x00\x00\x00 \x00\x00\x00\x01\x00\x01\x00\x00\x00\x00\x00\x80\x00\x00\x00\xc4\x0e\x00\x00\xc4\x0e\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xfc?\xff\xff\xfb\xcf\xff\xff\xe7\xf7\xff\xff\xc7\xf1\xff\xffs\xc6\x7f\xfe\xf1\xcf\xbf\xfd\xf7\xef\xbf\xfd\xf7\xf7\xdf\xff\xff\xf7\xdf\xfb\xef\xf3\xdf\xf9\xc7\xf1\x8f\xf6\x13\xeco\xf7|>w\xf7~\x7fw\xf7\xfe\x7fw\xf7\xff\x7fo\xfb\x7f\x7fo\xfc~~\x1f\xfd\xbc=\xff\xff\xe3\xe3\xbf\xfe\xf7\xf7\xbf\xff\x7f\xff\x7f\xff3\xe4\xff\xffx\x1f\x7f\xfe\xfc?\xbf\xfd\xfe?\xff\xfb\xff\x7f\xdf\xfb\xff\x7f\xdf\xfb\xfe\x7f\xef\xfb\xff\xbf\xef\xf9\xfb\xdf\x9f\xff\x0f\xf0\xff' 10 | image_width = 32 11 | image_height = 32 12 | 13 | #OLED I2C Configuration 14 | i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=200000) #Init I2C using pinsGP17&GP16 (defaultI2C0 pins) 15 | print("I2C Address : "+hex(i2c.scan()[0]).upper()) # Display device address 16 | print("I2C Configuration: "+str(i2c)) # Display I2C config 17 | oled = SSD1306_I2C(WIDTH, HEIGHT, i2c) # Init oled display 18 | 19 | 20 | 21 | #OLED Text display Function 22 | def displayText(text, position=(0,0),clear_oled=True,show_text=True): 23 | if clear_oled: 24 | oled.fill(0) # Clear the oled display in case it has junk on it. 25 | oled.text(text,position[0],position[1]) # dispaying text 26 | if show_text: 27 | oled.show() # Updating the display 28 | 29 | 30 | #OLED Image display function 31 | 32 | def displayImage(image_byte_array, image_resolution,position=(0,0),clear_oled=False,show_img=True): 33 | img = bytearray(image_byte_array) 34 | img = bytearray([img[i] for i in range(len(img)-1,-1,-1)]) 35 | 36 | frame = framebuf.FrameBuffer(img, image_resolution[0], image_resolution[1], framebuf.MONO_HMSB) # MONO_HLSB, MONO_VLSB, MONO_HMSB 37 | 38 | if clear_oled: 39 | oled.fill(0) # clear the OLED 40 | print("clear") 41 | if show_img: 42 | oled.blit(frame, position[0],position[1]) # show the image at location (x=0,y=0) 43 | oled.show() 44 | 45 | print("display") 46 | 47 | 48 | #Scrolling Text on OLED 49 | text1 = "Welcome to" 50 | text2 = "CircuitDigest" 51 | 52 | for x in range(0, WIDTH): 53 | displayText(text1,(x,0),clear_oled=False,show_text=True) 54 | displayText(text2,(WIDTH-x,20),clear_oled=True,show_text=True) 55 | if x == WIDTH: 56 | break 57 | else: 58 | x+=5 59 | 60 | 61 | 62 | 63 | 64 | while True: 65 | y=0 66 | text = "Interfacing OLED" 67 | oled.fill(0) 68 | 69 | #Scrolling Text And Image Horizontaly 70 | for x in range(0,WIDTH-image_width): 71 | displayImage(image_byte_arr,(image_width,image_height),(x,y),clear_oled=False) 72 | displayText(text,(x,y+40),clear_oled=False,show_text=True) 73 | 74 | if x == (WIDTH-image_width)-1: 75 | break 76 | else: 77 | x+=2 78 | oled.fill(0) 79 | 80 | #Reverse Scrolling Text And Image Horizontaly 81 | for x in range(WIDTH-image_width,-1,-1): 82 | displayImage(image_byte_arr,(image_width,image_height),(x,y),clear_oled=True) 83 | displayText(text,(x,y+40),clear_oled=False,show_text=True) 84 | if x == 0: 85 | break 86 | else: 87 | x-=2 88 | 89 | -------------------------------------------------------------------------------- /T3_ADC_on_Pico/codes/ssd1306.py: -------------------------------------------------------------------------------- 1 | # MicroPython SSD1306 OLED driver, I2C and SPI interfaces 2 | 3 | from micropython import const 4 | import framebuf 5 | 6 | 7 | # register definitions 8 | SET_CONTRAST = const(0x81) 9 | SET_ENTIRE_ON = const(0xA4) 10 | SET_NORM_INV = const(0xA6) 11 | SET_DISP = const(0xAE) 12 | SET_MEM_ADDR = const(0x20) 13 | SET_COL_ADDR = const(0x21) 14 | SET_PAGE_ADDR = const(0x22) 15 | SET_DISP_START_LINE = const(0x40) 16 | SET_SEG_REMAP = const(0xA0) 17 | SET_MUX_RATIO = const(0xA8) 18 | SET_COM_OUT_DIR = const(0xC0) 19 | SET_DISP_OFFSET = const(0xD3) 20 | SET_COM_PIN_CFG = const(0xDA) 21 | SET_DISP_CLK_DIV = const(0xD5) 22 | SET_PRECHARGE = const(0xD9) 23 | SET_VCOM_DESEL = const(0xDB) 24 | SET_CHARGE_PUMP = const(0x8D) 25 | 26 | # Subclassing FrameBuffer provides support for graphics primitives 27 | # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html 28 | class SSD1306(framebuf.FrameBuffer): 29 | def __init__(self, width, height, external_vcc): 30 | self.width = width 31 | self.height = height 32 | self.external_vcc = external_vcc 33 | self.pages = self.height // 8 34 | self.buffer = bytearray(self.pages * self.width) 35 | super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB) 36 | self.init_display() 37 | 38 | def init_display(self): 39 | for cmd in ( 40 | SET_DISP | 0x00, # off 41 | # address setting 42 | SET_MEM_ADDR, 43 | 0x00, # horizontal 44 | # resolution and layout 45 | SET_DISP_START_LINE | 0x00, 46 | SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 47 | SET_MUX_RATIO, 48 | self.height - 1, 49 | SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 50 | SET_DISP_OFFSET, 51 | 0x00, 52 | SET_COM_PIN_CFG, 53 | 0x02 if self.width > 2 * self.height else 0x12, 54 | # timing and driving scheme 55 | SET_DISP_CLK_DIV, 56 | 0x80, 57 | SET_PRECHARGE, 58 | 0x22 if self.external_vcc else 0xF1, 59 | SET_VCOM_DESEL, 60 | 0x30, # 0.83*Vcc 61 | # display 62 | SET_CONTRAST, 63 | 0xFF, # maximum 64 | SET_ENTIRE_ON, # output follows RAM contents 65 | SET_NORM_INV, # not inverted 66 | # charge pump 67 | SET_CHARGE_PUMP, 68 | 0x10 if self.external_vcc else 0x14, 69 | SET_DISP | 0x01, 70 | ): # on 71 | self.write_cmd(cmd) 72 | self.fill(0) 73 | self.show() 74 | 75 | def poweroff(self): 76 | self.write_cmd(SET_DISP | 0x00) 77 | 78 | def poweron(self): 79 | self.write_cmd(SET_DISP | 0x01) 80 | 81 | def contrast(self, contrast): 82 | self.write_cmd(SET_CONTRAST) 83 | self.write_cmd(contrast) 84 | 85 | def invert(self, invert): 86 | self.write_cmd(SET_NORM_INV | (invert & 1)) 87 | 88 | def show(self): 89 | x0 = 0 90 | x1 = self.width - 1 91 | if self.width == 64: 92 | # displays with width of 64 pixels are shifted by 32 93 | x0 += 32 94 | x1 += 32 95 | self.write_cmd(SET_COL_ADDR) 96 | self.write_cmd(x0) 97 | self.write_cmd(x1) 98 | self.write_cmd(SET_PAGE_ADDR) 99 | self.write_cmd(0) 100 | self.write_cmd(self.pages - 1) 101 | self.write_data(self.buffer) 102 | 103 | 104 | class SSD1306_I2C(SSD1306): 105 | def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False): 106 | self.i2c = i2c 107 | self.addr = addr 108 | self.temp = bytearray(2) 109 | self.write_list = [b"\x40", None] # Co=0, D/C#=1 110 | super().__init__(width, height, external_vcc) 111 | 112 | def write_cmd(self, cmd): 113 | self.temp[0] = 0x80 # Co=1, D/C#=0 114 | self.temp[1] = cmd 115 | self.i2c.writeto(self.addr, self.temp) 116 | 117 | def write_data(self, buf): 118 | self.write_list[1] = buf 119 | self.i2c.writevto(self.addr, self.write_list) 120 | 121 | 122 | class SSD1306_SPI(SSD1306): 123 | def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): 124 | self.rate = 10 * 1024 * 1024 125 | dc.init(dc.OUT, value=0) 126 | res.init(res.OUT, value=0) 127 | cs.init(cs.OUT, value=1) 128 | self.spi = spi 129 | self.dc = dc 130 | self.res = res 131 | self.cs = cs 132 | import time 133 | 134 | self.res(1) 135 | time.sleep_ms(1) 136 | self.res(0) 137 | time.sleep_ms(10) 138 | self.res(1) 139 | super().__init__(width, height, external_vcc) 140 | 141 | def write_cmd(self, cmd): 142 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 143 | self.cs(1) 144 | self.dc(0) 145 | self.cs(0) 146 | self.spi.write(bytearray([cmd])) 147 | self.cs(1) 148 | 149 | def write_data(self, buf): 150 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 151 | self.cs(1) 152 | self.dc(1) 153 | self.cs(0) 154 | self.spi.write(buf) 155 | self.cs(1) -------------------------------------------------------------------------------- /T2_Interfacing_An_OLED/codes/ssd1306.py: -------------------------------------------------------------------------------- 1 | # MicroPython SSD1306 OLED driver, I2C and SPI interfaces 2 | 3 | from micropython import const 4 | import framebuf 5 | 6 | 7 | # register definitions 8 | SET_CONTRAST = const(0x81) 9 | SET_ENTIRE_ON = const(0xA4) 10 | SET_NORM_INV = const(0xA6) 11 | SET_DISP = const(0xAE) 12 | SET_MEM_ADDR = const(0x20) 13 | SET_COL_ADDR = const(0x21) 14 | SET_PAGE_ADDR = const(0x22) 15 | SET_DISP_START_LINE = const(0x40) 16 | SET_SEG_REMAP = const(0xA0) 17 | SET_MUX_RATIO = const(0xA8) 18 | SET_COM_OUT_DIR = const(0xC0) 19 | SET_DISP_OFFSET = const(0xD3) 20 | SET_COM_PIN_CFG = const(0xDA) 21 | SET_DISP_CLK_DIV = const(0xD5) 22 | SET_PRECHARGE = const(0xD9) 23 | SET_VCOM_DESEL = const(0xDB) 24 | SET_CHARGE_PUMP = const(0x8D) 25 | 26 | # Subclassing FrameBuffer provides support for graphics primitives 27 | # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html 28 | class SSD1306(framebuf.FrameBuffer): 29 | def __init__(self, width, height, external_vcc): 30 | self.width = width 31 | self.height = height 32 | self.external_vcc = external_vcc 33 | self.pages = self.height // 8 34 | self.buffer = bytearray(self.pages * self.width) 35 | super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB) 36 | self.init_display() 37 | 38 | def init_display(self): 39 | for cmd in ( 40 | SET_DISP | 0x00, # off 41 | # address setting 42 | SET_MEM_ADDR, 43 | 0x00, # horizontal 44 | # resolution and layout 45 | SET_DISP_START_LINE | 0x00, 46 | SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 47 | SET_MUX_RATIO, 48 | self.height - 1, 49 | SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 50 | SET_DISP_OFFSET, 51 | 0x00, 52 | SET_COM_PIN_CFG, 53 | 0x02 if self.width > 2 * self.height else 0x12, 54 | # timing and driving scheme 55 | SET_DISP_CLK_DIV, 56 | 0x80, 57 | SET_PRECHARGE, 58 | 0x22 if self.external_vcc else 0xF1, 59 | SET_VCOM_DESEL, 60 | 0x30, # 0.83*Vcc 61 | # display 62 | SET_CONTRAST, 63 | 0xFF, # maximum 64 | SET_ENTIRE_ON, # output follows RAM contents 65 | SET_NORM_INV, # not inverted 66 | # charge pump 67 | SET_CHARGE_PUMP, 68 | 0x10 if self.external_vcc else 0x14, 69 | SET_DISP | 0x01, 70 | ): # on 71 | self.write_cmd(cmd) 72 | self.fill(0) 73 | self.show() 74 | 75 | def poweroff(self): 76 | self.write_cmd(SET_DISP | 0x00) 77 | 78 | def poweron(self): 79 | self.write_cmd(SET_DISP | 0x01) 80 | 81 | def contrast(self, contrast): 82 | self.write_cmd(SET_CONTRAST) 83 | self.write_cmd(contrast) 84 | 85 | def invert(self, invert): 86 | self.write_cmd(SET_NORM_INV | (invert & 1)) 87 | 88 | def show(self): 89 | x0 = 0 90 | x1 = self.width - 1 91 | if self.width == 64: 92 | # displays with width of 64 pixels are shifted by 32 93 | x0 += 32 94 | x1 += 32 95 | self.write_cmd(SET_COL_ADDR) 96 | self.write_cmd(x0) 97 | self.write_cmd(x1) 98 | self.write_cmd(SET_PAGE_ADDR) 99 | self.write_cmd(0) 100 | self.write_cmd(self.pages - 1) 101 | self.write_data(self.buffer) 102 | 103 | 104 | class SSD1306_I2C(SSD1306): 105 | def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False): 106 | self.i2c = i2c 107 | self.addr = addr 108 | self.temp = bytearray(2) 109 | self.write_list = [b"\x40", None] # Co=0, D/C#=1 110 | super().__init__(width, height, external_vcc) 111 | 112 | def write_cmd(self, cmd): 113 | self.temp[0] = 0x80 # Co=1, D/C#=0 114 | self.temp[1] = cmd 115 | self.i2c.writeto(self.addr, self.temp) 116 | 117 | def write_data(self, buf): 118 | self.write_list[1] = buf 119 | self.i2c.writevto(self.addr, self.write_list) 120 | 121 | 122 | class SSD1306_SPI(SSD1306): 123 | def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): 124 | self.rate = 10 * 1024 * 1024 125 | dc.init(dc.OUT, value=0) 126 | res.init(res.OUT, value=0) 127 | cs.init(cs.OUT, value=1) 128 | self.spi = spi 129 | self.dc = dc 130 | self.res = res 131 | self.cs = cs 132 | import time 133 | 134 | self.res(1) 135 | time.sleep_ms(1) 136 | self.res(0) 137 | time.sleep_ms(10) 138 | self.res(1) 139 | super().__init__(width, height, external_vcc) 140 | 141 | def write_cmd(self, cmd): 142 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 143 | self.cs(1) 144 | self.dc(0) 145 | self.cs(0) 146 | self.spi.write(bytearray([cmd])) 147 | self.cs(1) 148 | 149 | def write_data(self, buf): 150 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 151 | self.cs(1) 152 | self.dc(1) 153 | self.cs(0) 154 | self.spi.write(buf) 155 | self.cs(1) -------------------------------------------------------------------------------- /T4_Interfacing_HC-SR04_Ultrasonic_sensor/codes/ssd1306.py: -------------------------------------------------------------------------------- 1 | # MicroPython SSD1306 OLED driver, I2C and SPI interfaces 2 | 3 | from micropython import const 4 | import framebuf 5 | 6 | 7 | # register definitions 8 | SET_CONTRAST = const(0x81) 9 | SET_ENTIRE_ON = const(0xA4) 10 | SET_NORM_INV = const(0xA6) 11 | SET_DISP = const(0xAE) 12 | SET_MEM_ADDR = const(0x20) 13 | SET_COL_ADDR = const(0x21) 14 | SET_PAGE_ADDR = const(0x22) 15 | SET_DISP_START_LINE = const(0x40) 16 | SET_SEG_REMAP = const(0xA0) 17 | SET_MUX_RATIO = const(0xA8) 18 | SET_COM_OUT_DIR = const(0xC0) 19 | SET_DISP_OFFSET = const(0xD3) 20 | SET_COM_PIN_CFG = const(0xDA) 21 | SET_DISP_CLK_DIV = const(0xD5) 22 | SET_PRECHARGE = const(0xD9) 23 | SET_VCOM_DESEL = const(0xDB) 24 | SET_CHARGE_PUMP = const(0x8D) 25 | 26 | # Subclassing FrameBuffer provides support for graphics primitives 27 | # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html 28 | class SSD1306(framebuf.FrameBuffer): 29 | def __init__(self, width, height, external_vcc): 30 | self.width = width 31 | self.height = height 32 | self.external_vcc = external_vcc 33 | self.pages = self.height // 8 34 | self.buffer = bytearray(self.pages * self.width) 35 | super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB) 36 | self.init_display() 37 | 38 | def init_display(self): 39 | for cmd in ( 40 | SET_DISP | 0x00, # off 41 | # address setting 42 | SET_MEM_ADDR, 43 | 0x00, # horizontal 44 | # resolution and layout 45 | SET_DISP_START_LINE | 0x00, 46 | SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 47 | SET_MUX_RATIO, 48 | self.height - 1, 49 | SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 50 | SET_DISP_OFFSET, 51 | 0x00, 52 | SET_COM_PIN_CFG, 53 | 0x02 if self.width > 2 * self.height else 0x12, 54 | # timing and driving scheme 55 | SET_DISP_CLK_DIV, 56 | 0x80, 57 | SET_PRECHARGE, 58 | 0x22 if self.external_vcc else 0xF1, 59 | SET_VCOM_DESEL, 60 | 0x30, # 0.83*Vcc 61 | # display 62 | SET_CONTRAST, 63 | 0xFF, # maximum 64 | SET_ENTIRE_ON, # output follows RAM contents 65 | SET_NORM_INV, # not inverted 66 | # charge pump 67 | SET_CHARGE_PUMP, 68 | 0x10 if self.external_vcc else 0x14, 69 | SET_DISP | 0x01, 70 | ): # on 71 | self.write_cmd(cmd) 72 | self.fill(0) 73 | self.show() 74 | 75 | def poweroff(self): 76 | self.write_cmd(SET_DISP | 0x00) 77 | 78 | def poweron(self): 79 | self.write_cmd(SET_DISP | 0x01) 80 | 81 | def contrast(self, contrast): 82 | self.write_cmd(SET_CONTRAST) 83 | self.write_cmd(contrast) 84 | 85 | def invert(self, invert): 86 | self.write_cmd(SET_NORM_INV | (invert & 1)) 87 | 88 | def show(self): 89 | x0 = 0 90 | x1 = self.width - 1 91 | if self.width == 64: 92 | # displays with width of 64 pixels are shifted by 32 93 | x0 += 32 94 | x1 += 32 95 | self.write_cmd(SET_COL_ADDR) 96 | self.write_cmd(x0) 97 | self.write_cmd(x1) 98 | self.write_cmd(SET_PAGE_ADDR) 99 | self.write_cmd(0) 100 | self.write_cmd(self.pages - 1) 101 | self.write_data(self.buffer) 102 | 103 | 104 | class SSD1306_I2C(SSD1306): 105 | def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False): 106 | self.i2c = i2c 107 | self.addr = addr 108 | self.temp = bytearray(2) 109 | self.write_list = [b"\x40", None] # Co=0, D/C#=1 110 | super().__init__(width, height, external_vcc) 111 | 112 | def write_cmd(self, cmd): 113 | self.temp[0] = 0x80 # Co=1, D/C#=0 114 | self.temp[1] = cmd 115 | self.i2c.writeto(self.addr, self.temp) 116 | 117 | def write_data(self, buf): 118 | self.write_list[1] = buf 119 | self.i2c.writevto(self.addr, self.write_list) 120 | 121 | 122 | class SSD1306_SPI(SSD1306): 123 | def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): 124 | self.rate = 10 * 1024 * 1024 125 | dc.init(dc.OUT, value=0) 126 | res.init(res.OUT, value=0) 127 | cs.init(cs.OUT, value=1) 128 | self.spi = spi 129 | self.dc = dc 130 | self.res = res 131 | self.cs = cs 132 | import time 133 | 134 | self.res(1) 135 | time.sleep_ms(1) 136 | self.res(0) 137 | time.sleep_ms(10) 138 | self.res(1) 139 | super().__init__(width, height, external_vcc) 140 | 141 | def write_cmd(self, cmd): 142 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 143 | self.cs(1) 144 | self.dc(0) 145 | self.cs(0) 146 | self.spi.write(bytearray([cmd])) 147 | self.cs(1) 148 | 149 | def write_data(self, buf): 150 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 151 | self.cs(1) 152 | self.dc(1) 153 | self.cs(0) 154 | self.spi.write(buf) 155 | self.cs(1) -------------------------------------------------------------------------------- /T7_Interfacing_ESP8266-01_WiFi/Codes/esp8266.py: -------------------------------------------------------------------------------- 1 | ''' 2 | File name: esp8266.py 3 | Author: Noyel Seth 4 | Email: noyelseth@gmail.com 5 | MicroPython Version: 3.4 6 | ''' 7 | 8 | 9 | from machine import UART, Pin 10 | import time 11 | from httpParser import HttpParser 12 | 13 | ESP8266_OK_STATUS = "OK\r\n" 14 | ESP8266_ERROR_STATUS = "ERROR\r\n" 15 | ESP8266_FAIL_STATUS = "FAIL\r\n" 16 | ESP8266_WIFI_CONNECTED="WIFI CONNECTED\r\n" 17 | ESP8266_WIFI_GOT_IP_CONNECTED="WIFI GOT IP\r\n" 18 | ESP8266_WIFI_DISCONNECTED="WIFI DISCONNECT\r\n" 19 | ESP8266_WIFI_AP_NOT_PRESENT="WIFI AP NOT FOUND\r\n" 20 | ESP8266_WIFI_AP_WRONG_PWD="WIFI AP WRONG PASSWORD\r\n" 21 | ESP8266_BUSY_STATUS="busy p...\r\n" 22 | UART_Tx_BUFFER_LENGTH = 1024 23 | UART_Rx_BUFFER_LENGTH = 1024*2 24 | 25 | 26 | class ESP8266: 27 | """ 28 | This is a class for access ESP8266 using AT commands 29 | Using this class, you access WiFi and do HTTP Post/Get operations. 30 | 31 | Attributes: 32 | uartPort (int): The Uart port numbet of the RPI Pico's UART BUS [Default UART0] 33 | baudRate (int): UART Baud-Rate for communncating between RPI Pico's & ESP8266 [Default 115200] 34 | txPin (init): RPI Pico's Tx pin [Default Pin 0] 35 | rxPin (init): RPI Pico's Rx pin [Default Pin 1] 36 | """ 37 | 38 | __rxData=None 39 | __txData=None 40 | __httpResponse=None 41 | 42 | def __init__(self, uartPort=0 ,baudRate=115200, txPin=(0), rxPin=(1)): 43 | """ 44 | The constaructor for ESP8266 class 45 | 46 | Parameters: 47 | uartPort (int): The Uart port numbet of the RPI Pico's UART BUS [Default UART0] 48 | baudRate (int): UART Baud-Rate for communncating between RPI Pico's & ESP8266 [Default 115200] 49 | txPin (init): RPI Pico's Tx pin [Default Pin 0] 50 | rxPin (init): RPI Pico's Rx pin [Default Pin 1] 51 | """ 52 | self.__uartPort=uartPort 53 | self.__baudRate=baudRate 54 | self.__txPin=txPin 55 | self.__rxPin=rxPin 56 | #print(self.__uartPort, self.__baudRate, self.__txPin, self.__rxPin) 57 | self.__uartObj = UART(self.__uartPort, baudrate=self.__baudRate, tx=Pin(self.__txPin), rx=Pin(self.__rxPin), txbuf=UART_Tx_BUFFER_LENGTH, rxbuf=UART_Rx_BUFFER_LENGTH) 58 | #print(self.__uartObj) 59 | 60 | def _createHTTPParseObj(self): 61 | """ 62 | This is private function for create HTTP response every time 63 | before doing the HTTP Post/Get operation 64 | 65 | """ 66 | if(self.__httpResponse != None): 67 | del self.__httpResponse 68 | self.__httpResponse=HttpParser() 69 | else: 70 | #del self.__httpResponse 71 | self.__httpResponse=HttpParser() 72 | 73 | def _sendToESP8266(self, atCMD, delay=1): 74 | """ 75 | This is private function for complete ESP8266 AT command Send/Receive operation. 76 | """ 77 | self.__rxData=str() 78 | self.__txData=atCMD 79 | #print("---------------------------"+self.__txData) 80 | self.__uartObj.write(self.__txData) 81 | self.__rxData=bytes() 82 | 83 | time.sleep(delay) 84 | 85 | #while self.__uartObj.any()>0: 86 | # self.__rxData += self.__uartObj.read(1) 87 | 88 | while True: 89 | #print(".") 90 | if self.__uartObj.any()>0: 91 | #print(self.__uartObj.any()) 92 | break 93 | 94 | while self.__uartObj.any()>0: 95 | self.__rxData += self.__uartObj.read(UART_Rx_BUFFER_LENGTH) 96 | 97 | #print(self.__rxData) 98 | if ESP8266_OK_STATUS in self.__rxData: 99 | return self.__rxData 100 | elif ESP8266_ERROR_STATUS in self.__rxData: 101 | return self.__rxData 102 | elif ESP8266_FAIL_STATUS in self.__rxData: 103 | return self.__rxData 104 | elif ESP8266_BUSY_STATUS in self.__rxData: 105 | return "ESP BUSY\r\n" 106 | else: 107 | return None 108 | 109 | def startUP(self): 110 | """ 111 | This funtion use to check the communication between ESP8266 & RPI Pico 112 | 113 | Return: 114 | True if communication success with the ESP8266 115 | False if unable to communication with the ESP8266 116 | """ 117 | retData = self._sendToESP8266("AT\r\n") 118 | if(retData != None): 119 | if ESP8266_OK_STATUS in retData: 120 | return True 121 | else: 122 | return False 123 | else: 124 | False 125 | 126 | def reStart(self): 127 | """ 128 | This funtion use to Reset the ESP8266 129 | 130 | Return: 131 | True if Reset successfully done with the ESP8266 132 | False if unable to reset the ESP8266 133 | """ 134 | retData = self._sendToESP8266("AT+RST\r\n") 135 | if(retData != None): 136 | if ESP8266_OK_STATUS in retData: 137 | time.sleep(5) 138 | #self.startUP() 139 | return self.startUP() 140 | else: 141 | return False 142 | else: 143 | False 144 | 145 | 146 | def echoING(self, enable=False): 147 | """ 148 | This function use to enable/diable AT command echo [Default set as false for diable Echo] 149 | 150 | Return: 151 | True if echo off/on command succefully initiate with the ESP8266 152 | False if echo off/on command failed to initiate with the ESP8266 153 | 154 | """ 155 | if enable==False: 156 | retData = self._sendToESP8266("ATE0\r\n") 157 | if(retData != None): 158 | if ESP8266_OK_STATUS in retData: 159 | return True 160 | else: 161 | return False 162 | else: 163 | return False 164 | else: 165 | retData = self._sendToESP8266("ATE1\r\n") 166 | if(retData != None): 167 | if ESP8266_OK_STATUS in retData: 168 | return True 169 | else: 170 | return False 171 | else: 172 | return False 173 | 174 | 175 | def getVersion(self): 176 | """ 177 | This function use to get AT command Version details 178 | 179 | Return: 180 | Version details on success else None 181 | """ 182 | retData = self._sendToESP8266("AT+GMR\r\n") 183 | if(retData != None): 184 | if ESP8266_OK_STATUS in retData: 185 | #print(str(retData,"utf-8")) 186 | retData = str(retData).partition(r"OK")[0] 187 | #print(str(retData,"utf-8")) 188 | retData = retData.split(r"\r\n") 189 | retData[0] = retData[0].replace("b'","") 190 | retData=str(retData[0]+"\r\n"+retData[1]+"\r\n"+retData[2]) 191 | return retData 192 | else: 193 | return None 194 | else: 195 | return None 196 | 197 | def reStore(self): 198 | """ 199 | This function use to reset the ESP8266 into the Factory reset mode & delete previous configurations 200 | Return: 201 | True on ESP8266 restore succesfully 202 | False on failed to restore ESP8266 203 | """ 204 | retData = self._sendToESP8266("AT+RESTORE\r\n") 205 | if(retData != None): 206 | if ESP8266_OK_STATUS in retData: 207 | return True 208 | else: 209 | return False 210 | else: 211 | return None 212 | """ 213 | def chcekSYSRAM(self): 214 | #retData = self._sendToESP8266("AT+SYSRAM?\r\n") 215 | self.__rxData=b'' 216 | self.__txData="AT+SYSRAM?\r\n" 217 | self.__uartObj.write(self.__txData) 218 | self.__rxData=bytes() 219 | 220 | time.sleep(2) 221 | 222 | while self.__uartObj.any()>0: 223 | self.__rxData += self.__uartObj.read(1) 224 | 225 | print(self.__rxData.decode()) 226 | if ESP8266_OK_STATUS in self.__rxData: 227 | return self.__rxData 228 | else: 229 | return 1 230 | """ 231 | 232 | def getCurrentWiFiMode(self): 233 | """ 234 | This fucntion use to query ESP8266 WiFi's current mode [STA: Station, SoftAP: Software AccessPoint, or Both] 235 | 236 | Return: 237 | STA if ESP8266's wifi's current mode pre-config as Station 238 | SoftAP if ESP8266's wifi's current mode pre-config as SoftAP 239 | SoftAP+STA if ESP8266's wifi's current mode set pre-config Station & SoftAP 240 | None failed to detect the wifi's current pre-config mode 241 | """ 242 | retData = self._sendToESP8266("AT+CWMODE_CUR?\r\n") 243 | if(retData != None): 244 | if "1" in retData: 245 | return "STA" 246 | elif "2" in retData: 247 | return "SoftAP" 248 | elif "3" in retData: 249 | return "SoftAP+STA" 250 | else: 251 | return None 252 | else: 253 | return None 254 | 255 | 256 | def setCurrentWiFiMode(self, mode=3): 257 | """ 258 | This fucntion use to set ESP8266 WiFi's current mode [STA: Station, SoftAP: Software AccessPoint, or Both] 259 | 260 | Parameter: 261 | mode (int): ESP8266 WiFi's [ 1: STA, 2: SoftAP, 3: SoftAP+STA(default)] 262 | 263 | Return: 264 | True on successfully set the current wifi mode 265 | False on failed set the current wifi mode 266 | 267 | """ 268 | txData="AT+CWMODE_CUR="+str(mode)+"\r\n" 269 | retData = self._sendToESP8266(txData) 270 | if(retData!=None): 271 | if ESP8266_OK_STATUS in retData: 272 | return True 273 | else: 274 | return False 275 | else: 276 | return False 277 | 278 | def getDefaultWiFiMode(self): 279 | """ 280 | This fucntion use to query ESP8266 WiFi's default mode [STA: Station, SoftAP: Software AccessPoint, or Both] 281 | 282 | Return: 283 | STA if ESP8266's wifi's default mode pre-config as Station 284 | SoftAP if ESP8266's wifi's default mode pre-config as SoftAP 285 | SoftAP+STA if ESP8266's wifi's default mode set pre-config Station & SoftAP 286 | None failed to detect the wifi's default pre-config mode 287 | 288 | """ 289 | retData = self._sendToESP8266("AT+CWMODE_DEF?\r\n") 290 | if(retData!=None): 291 | if "1" in retData: 292 | return "STA" 293 | elif "2" in retData: 294 | return "SoftAP" 295 | elif "3" in retData: 296 | return "SoftAP+STA" 297 | else: 298 | return None 299 | else: 300 | return None 301 | 302 | def setDefaultWiFiMode(self, mode=3): 303 | """ 304 | This fucntion use to set ESP8266 WiFi's default mode [STA: Station, SoftAP: Software AccessPoint, or Both] 305 | 306 | Parameter: 307 | mode (int): ESP8266 WiFi's [ 1: STA, 2: SoftAP, 3: SoftAP+STA(default)] 308 | 309 | Return: 310 | True on successfully set the default wifi mode 311 | False on failed set the default wifi mode 312 | 313 | """ 314 | txData="AT+CWMODE_DEF="+str(mode)+"\r\n" 315 | retData = self._sendToESP8266(txData) 316 | if(retData!=None): 317 | if ESP8266_OK_STATUS in retData: 318 | return True 319 | else: 320 | return False 321 | else: 322 | return False 323 | 324 | def getAvailableAPs(self): 325 | """ 326 | This fucntion use to query ESP8266 for available WiFi AccessPoins 327 | 328 | Retuns: 329 | List of Available APs or None 330 | """ 331 | retData = str(self._sendToESP8266("AT+CWLAP\r\n", delay=10)) 332 | if(retData != None): 333 | retData = retData.replace("+CWLAP:", "") 334 | retData = retData.replace(r"\r\n\r\nOK\r\n", "") 335 | retData = retData.replace(r"\r\n","@") 336 | retData = retData.replace("b'(","(").replace("'","") 337 | retData = retData.split("@") 338 | retData =list(retData) 339 | apLists=list() 340 | 341 | for items in retData: 342 | data=str(items).replace("(","").replace(")","").split(",") 343 | data=tuple(data) 344 | apLists.append(data) 345 | 346 | return apLists 347 | else: 348 | return None 349 | 350 | def connectWiFi(self,ssid,pwd): 351 | """ 352 | This fucntion use to connect ESP8266 with a WiFi AccessPoins 353 | 354 | Parameters: 355 | ssid : WiFi AP's SSID 356 | pwd : WiFi AP's Password 357 | 358 | Retuns: 359 | WIFI DISCONNECT when ESP8266 failed connect with target AP's credential 360 | WIFI AP WRONG PASSWORD when ESP8266 tried connect with taget AP with wrong password 361 | WIFI AP NOT FOUND when ESP8266 cann't find the target AP 362 | WIFI CONNECTED when ESP8266 successfully connect with the target AP 363 | """ 364 | txData="AT+CWJAP_CUR="+'"'+ssid+'"'+','+'"'+pwd+'"'+"\r\n" 365 | #print(txData) 366 | retData = self._sendToESP8266(txData, delay=15) 367 | #print(".....") 368 | #print(retData) 369 | if(retData!=None): 370 | if "+CWJAP" in retData: 371 | if "1" in retData: 372 | return ESP8266_WIFI_DISCONNECTED 373 | elif "2" in retData: 374 | return ESP8266_WIFI_AP_WRONG_PWD 375 | elif "3" in retData: 376 | return ESP8266_WIFI_AP_NOT_PRESENT 377 | elif "4" in retData: 378 | return ESP8266_WIFI_DISCONNECTED 379 | else: 380 | return None 381 | elif ESP8266_WIFI_CONNECTED in retData: 382 | if ESP8266_WIFI_GOT_IP_CONNECTED in retData: 383 | return ESP8266_WIFI_CONNECTED 384 | else: 385 | return ESP8266_WIFI_DISCONNECTED 386 | else: 387 | return ESP8266_WIFI_DISCONNECTED 388 | else: 389 | return ESP8266_WIFI_DISCONNECTED 390 | 391 | 392 | 393 | def disconnectWiFi(self): 394 | """ 395 | This fucntion use to disconnect ESP8266 with a connected WiFi AccessPoins 396 | 397 | Return: 398 | False on failed to disconnect the WiFi 399 | True on successfully disconnected 400 | """ 401 | retData = self._sendToESP8266("AT+CWQAP\r\n") 402 | if(retData!=None): 403 | if ESP8266_OK_STATUS in retData: 404 | return True 405 | else: 406 | return False 407 | else: 408 | return False 409 | 410 | def _createTCPConnection(self, link, port=80): 411 | """ 412 | This fucntion use to create connect between ESP8266 and Host. 413 | Just like create a socket before complete the HTTP Get/Post operation. 414 | 415 | Return: 416 | False on failed to create a socket connection 417 | True on successfully create and establish a socket connection. 418 | """ 419 | #self._sendToESP8266("AT+CIPMUX=0") 420 | txData="AT+CIPSTART="+'"'+"TCP"+'"'+','+'"'+link+'"'+','+str(port)+"\r\n" 421 | #print(txData) 422 | retData = self._sendToESP8266(txData) 423 | #print(".....") 424 | #print(retData) 425 | if(retData != None): 426 | if ESP8266_OK_STATUS in retData: 427 | return True 428 | else: 429 | return False 430 | else: 431 | False 432 | 433 | def doHttpGet(self,host,path,user_agent="RPi-Pico", port=80): 434 | """ 435 | This fucntion use to complete a HTTP Get operation 436 | 437 | Parameter: 438 | host (str): Host URL [ex: get operation URL: www.httpbin.org/ip. so, Host URL only "www.httpbin.org"] 439 | path (str): Get operation's URL path [ex: get operation URL: www.httpbin.org/ip. so, the path "/ip"] 440 | user-agent (str): User Agent Name [Default "RPi-Pico"] 441 | post (int): HTTP post number [Default port number 80] 442 | 443 | Return: 444 | HTTP error code & HTTP response[If error not equal to 200 then the response is None] 445 | On failed return 0 and None 446 | 447 | """ 448 | if(self._createTCPConnection(host, port) == True): 449 | self._createHTTPParseObj() 450 | #getHeader="GET "+path+" HTTP/1.1\r\n"+"Host: "+host+":"+str(port)+"\r\n"+"User-Agent: "+user_agent+"\r\n"+"\r\n"; 451 | getHeader="GET "+path+" HTTP/1.1\r\n"+"Host: "+host+"\r\n"+"User-Agent: "+user_agent+"\r\n"+"\r\n"; 452 | #print(getHeader,len(getHeader)) 453 | txData="AT+CIPSEND="+str(len(getHeader))+"\r\n" 454 | retData = self._sendToESP8266(txData) 455 | if(retData != None): 456 | if ">" in retData: 457 | retData = self._sendToESP8266(getHeader, delay=2) 458 | self._sendToESP8266("AT+CIPCLOSE\r\n") 459 | retData=self.__httpResponse.parseHTTP(retData) 460 | return retData, self.__httpResponse.getHTTPResponse() 461 | else: 462 | return 0, None 463 | else: 464 | return 0, None 465 | else: 466 | self._sendToESP8266("AT+CIPCLOSE\r\n") 467 | return 0, None 468 | 469 | 470 | def doHttpPost(self,host,path,user_agent="RPi-Pico",content_type,content,port=80): 471 | """ 472 | This fucntion use to complete a HTTP Post operation 473 | 474 | Parameter: 475 | host (str): Host URL [ex: get operation URL: www.httpbin.org/ip. so, Host URL only "www.httpbin.org"] 476 | path (str): Get operation's URL path [ex: get operation URL: www.httpbin.org/ip. so, the path "/ip"] 477 | user-agent (str): User Agent Name [Default "RPi-Pico"] 478 | content_type (str): Post operation's upload content type [ex. "application/json", "application/x-www-form-urlencoded", "text/plain" 479 | content (str): Post operation's upload content 480 | post (int): HTTP post number [Default port number 80] 481 | 482 | Return: 483 | HTTP error code & HTTP response[If error not equal to 200 then the response is None] 484 | On failed return 0 and None 485 | 486 | """ 487 | if(self._createTCPConnection(host, port) == True): 488 | self._createHTTPParseObj() 489 | postHeader="POST "+path+" HTTP/1.1\r\n"+"Host: "+host+"\r\n"+"User-Agent: "+user_agent+"\r\n"+"Content-Type: "+content_type+"\r\n"+"Content-Length: "+str(len(content))+"\r\n"+"\r\n"+content+"\r\n"; 490 | #print(postHeader,len(postHeader)) 491 | txData="AT+CIPSEND="+str(len(postHeader))+"\r\n" 492 | retData = self._sendToESP8266(txData) 493 | if(retData != None): 494 | if ">" in retData: 495 | retData = self._sendToESP8266(postHeader, delay=2) 496 | #print(".......@@",retData) 497 | self._sendToESP8266("AT+CIPCLOSE\r\n") 498 | #print(self.__httpResponse) 499 | retData=self.__httpResponse.parseHTTP(retData) 500 | return retData, self.__httpResponse.getHTTPResponse() 501 | else: 502 | return 0, None 503 | else: 504 | return 0, None 505 | else: 506 | self._sendToESP8266("AT+CIPCLOSE\r\n") 507 | return 0, None 508 | 509 | def __del__(self): 510 | """ 511 | The distaructor for ESP8266 class 512 | """ 513 | print('Destructor called, ESP8266 deleted.') 514 | pass 515 | --------------------------------------------------------------------------------