commit e319bbd89b5ffddb38eaede1a05db3a663890c17 Author: Alauddin Maulana Hirzan Date: Fri May 10 18:26:38 2024 +0700 First Commit diff --git a/Example - 7 Segment/README.md b/Example - 7 Segment/README.md new file mode 100644 index 0000000..335e8c8 --- /dev/null +++ b/Example - 7 Segment/README.md @@ -0,0 +1,3 @@ +# 7 Segment Example Code # + +I adapted original code from somewhere into this code. There is an example how to use the code in **main section** of the code. diff --git a/Example - 7 Segment/main_7Segment.py b/Example - 7 Segment/main_7Segment.py new file mode 100644 index 0000000..ebd0b5c --- /dev/null +++ b/Example - 7 Segment/main_7Segment.py @@ -0,0 +1,151 @@ +from machine import Pin +from time import time,sleep +from utime import sleep_ms + +# Map 1st Segment Pins +d1_p1 = Pin(2,Pin.OUT) # E +d1_p2 = Pin(3,Pin.OUT) # D +d1_p3 = Pin(4,Pin.OUT) # DP +d1_p4 = Pin(5,Pin.OUT) # C +d1_p5 = Pin(6,Pin.OUT) # G +d1_p7 = Pin(22,Pin.OUT) # B +d1_p10 = Pin(26,Pin.OUT) # F +d1_p11 = Pin(27,Pin.OUT) # A + +# Map 1st Digit Pins +c1_1 = Pin(10,Pin.OUT) +c1_2 = Pin(9,Pin.OUT) +c1_3 = Pin(8,Pin.OUT) + +# Map 2nd Segment Pins +d2_p1 = Pin(11,Pin.OUT) # E +d2_p2 = Pin(12,Pin.OUT) # D +d2_p3 = Pin(13,Pin.OUT) # DP +d2_p4 = Pin(14,Pin.OUT) # C +d2_p5 = Pin(15,Pin.OUT) # G +d2_p7 = Pin(16,Pin.OUT) # B +d2_p10 = Pin(17,Pin.OUT) # F +d2_p11 = Pin(18,Pin.OUT) # A + +# Map 2nd Digit Pins +c2_1 = Pin(19,Pin.OUT) +c2_2 = Pin(20,Pin.OUT) +c2_3 = Pin(21,Pin.OUT) + +# Store into Lists +c1_pins = [c1_1,c1_2,c1_3] +d1_pins = [d1_p11,d1_p7,d1_p4,d1_p2,d1_p1,d1_p10,d1_p5] +c2_pins = [c2_1,c2_2,c2_3] +d2_pins = [d2_p11,d2_p7,d2_p4,d2_p2,d2_p1,d2_p10,d2_p5] + +# Define Numeric Patterns +num_patterns = [ + 0b1111110, # 0 + 0b0110000, # 1 + 0b1101101, # 2 + 0b1111001, # 3 + 0b0110011, # 4 + 0b1011011, # 5 + 0b1011111, # 6 + 0b1110000, # 7 + 0b1111111, # 8 + 0b1111011 # 9 + ] + +def turnDigits(board,power): + if(board == 1): + if(power): + for control in c1_pins: + control.value(0) + else: + for control in c1_pins: + control.value(1) + else: + if(power): + for control in c2_pins: + control.value(0) + else: + for control in c2_pins: + control.value(1) + +def switchDigits(board,digit): + if(board == 1): + if(digit == 0): + c1_pins[0].value(0) + c1_pins[1].value(1) + c1_pins[2].value(1) + elif(digit == 1): + c1_pins[0].value(1) + c1_pins[1].value(0) + c1_pins[2].value(1) + else: + c1_pins[0].value(1) + c1_pins[1].value(1) + c1_pins[2].value(0) + else: + if(digit == 0): + c2_pins[0].value(0) + c2_pins[1].value(1) + c2_pins[2].value(1) + elif(digit == 1): + c2_pins[0].value(1) + c2_pins[1].value(0) + c2_pins[2].value(1) + else: + c2_pins[0].value(1) + c2_pins[1].value(1) + c2_pins[2].value(0) + +def displayNumber(board,number): + # Extract the individual digits from the number + num1 = number // 100 + num2 = (number % 100) // 10 + num3 = number % 10 + + # Turn Off All + turnDigits(board,False) + + start_time = time() + + # Display the digits on the 7-segment display + while time() - start_time < .25: + switchDigits(board,0) + setDisplay(board, 0, num3) + sleep_ms(5) + switchDigits(board,1) + setDisplay(board, 1, num2) + sleep_ms(5) + switchDigits(board,2) + setDisplay(board, 2, num1) + sleep_ms(5) + else: + turnDigits(board,False) + +def setDisplay(board,digit,num): + if(board == 1): + # Set Pattern to Display + for i, pin in enumerate(d1_pins): + pattern = num_patterns[num] + target = (pattern >> (6 - i)) & 1 + pin.value(target) + else: + # Set Pattern to Display + for i, pin in enumerate(d2_pins): + pattern = num_patterns[num] + target = (pattern >> (6 - i)) & 1 + pin.value(target) + + +# Main Script +def main(): + board = 1 + powerOff = False + + if(powerOff): + turnDigits(board,False) + else: + for data in range(100): + displayNumber(board,data) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/Example - ADC Reading/README.md b/Example - ADC Reading/README.md new file mode 100644 index 0000000..94ca3cd --- /dev/null +++ b/Example - ADC Reading/README.md @@ -0,0 +1,3 @@ +# ADC Reading Example Code # + +This is a simple code to read ADC using MicroPython. diff --git a/Example - ADC Reading/main_ReadADC.py b/Example - ADC Reading/main_ReadADC.py new file mode 100644 index 0000000..e1f6783 --- /dev/null +++ b/Example - ADC Reading/main_ReadADC.py @@ -0,0 +1,11 @@ +from machine import ADC +import time + +# Assign ADC Pin 0 to Sensor +sensor = ADC(0) + +# Repeatedly Read u16 value and print +while True: + result = sensor.read_u16() + print(result) + time.sleep(1) \ No newline at end of file diff --git a/Example - Blynk API/BlynkAPI.py b/Example - Blynk API/BlynkAPI.py new file mode 100644 index 0000000..b1289de --- /dev/null +++ b/Example - Blynk API/BlynkAPI.py @@ -0,0 +1,36 @@ +from urequests import get + +## Blynk API Class +class BlynkAPI: + # Initialize Blynk API Class + def __init__(self): + self.server_address: str = "blynk.cloud" + self.token: str = "" + self.fetch_link = f"https://{self.server_address}/external/api/get?token={self.token}&" + self.update_link = f"https://{self.server_address}/external/api/update?token={self.token}&" + + # Method to get vpin value (pin: v0, v1, v2, ...) + def fetchPin(self, pin: str): + response = get(self.fetch_link+pin) + result = response.json() # For some reason this line will cause some devices to stuck. Only to limited to Blynk API Response + status = response.status_code + response.close() + return status, result # Return Status Code and Result + + # Method to update vpin value (pin: v0, v1, v2, ...; value: int or str) + def updatePin(self, pin: str, value): + if type(value) is int: + value = str(value) + response = get(self.update_link+pin+"="+value) + status = response.status_code + response.close + return status # Return Status Code + + +## Usage Sample +def main(): + # Connect to Network + # Initialize Class + api = BlynkAPI() + status, result = fetchPin("v0") # Fetch vPin v0 + status = updatePin("v1", 10) # Update vPin v1 to 10 \ No newline at end of file diff --git a/Example - Blynk API/README.md b/Example - Blynk API/README.md new file mode 100644 index 0000000..4995677 --- /dev/null +++ b/Example - Blynk API/README.md @@ -0,0 +1,3 @@ +# Blynk HTTP API Access Example Code # + +This is a simple Blynk HTTP API access using MicroPython. However, for some reason there is a problem extracting data from Blynk HTTP API. This problem only limited to Blynk HTTP API response data. diff --git a/Example - DHT11/README.md b/Example - DHT11/README.md new file mode 100644 index 0000000..20cdebb --- /dev/null +++ b/Example - DHT11/README.md @@ -0,0 +1,3 @@ +# DHT11/22 Reading Example Code # + +This is a simple code for reading DHT11/22 sensor using MicroPython. With extra LED blinks diff --git a/Example - DHT11/main_DHT11.py b/Example - DHT11/main_DHT11.py new file mode 100644 index 0000000..9c1d77a --- /dev/null +++ b/Example - DHT11/main_DHT11.py @@ -0,0 +1,32 @@ +from machine import Pin +from time import sleep +import dht +from utime import sleep_ms + +# Define DHT Version (11 or 22) and Input Pin +sensor = dht.DHT22(Pin(16)) +# Define Onboard LED Pin +led = Pin(2,Pin.OUT) +# Turn it off first +led.value(0) + +# Define blink function +def blink(): + led.value(0) + sleep_ms(50) + led.value(1) + +# Main Script Here +while True: + try: + sleep(2) + sensor.measure() + temp = sensor.temperature() + hum = sensor.humidity() + if(temp > 24.0 or hum > 49.0): + blink() + print('Temperature: %3.1f C' %temp) + print('Humidity: %3.1f %%' %hum) + print("") + except OSError as e: + print('Failed to read sensor.') \ No newline at end of file diff --git a/Example - HCSR04 Ultrasonic/README.md b/Example - HCSR04 Ultrasonic/README.md new file mode 100644 index 0000000..ce57466 --- /dev/null +++ b/Example - HCSR04 Ultrasonic/README.md @@ -0,0 +1,7 @@ +# HC-SR04 Example Code # + +This code show how to use the HC-SR04 Ultrasonic Sensor using Micropython. + + * Original Driver code : https://github.com/rsc1975/micropython-hcsr04/tree/master. + + * The main code I made it myself. diff --git a/Example - HCSR04 Ultrasonic/hcsr04.py b/Example - HCSR04 Ultrasonic/hcsr04.py new file mode 100644 index 0000000..d38aebd --- /dev/null +++ b/Example - HCSR04 Ultrasonic/hcsr04.py @@ -0,0 +1,82 @@ +from machine import Pin, time_pulse_us +from utime import sleep_us + +__version__ = '0.2.1' +__author__ = 'Roberto Sánchez' +__license__ = "Apache License 2.0. https://www.apache.org/licenses/LICENSE-2.0" + +class HCSR04: + """ + Driver to use the untrasonic sensor HC-SR04. + The sensor range is between 2cm and 4m. + + The timeouts received listening to echo pin are converted to OSError('Out of range') + + """ + # echo_timeout_us is based in chip range limit (400cm) + def __init__(self, trigger_pin, echo_pin, echo_timeout_us=500*2*30): + """ + trigger_pin: Output pin to send pulses + echo_pin: Readonly pin to measure the distance. The pin should be protected with 1k resistor + echo_timeout_us: Timeout in microseconds to listen to echo pin. + By default is based in sensor limit range (4m) + """ + self.echo_timeout_us = echo_timeout_us + # Init trigger pin (out) + self.trigger = Pin(trigger_pin, mode=Pin.OUT, pull=None) + self.trigger.value(0) + + # Init echo pin (in) + self.echo = Pin(echo_pin, mode=Pin.IN, pull=None) + + def _send_pulse_and_wait(self): + """ + Send the pulse to trigger and listen on echo pin. + We use the method `machine.time_pulse_us()` to get the microseconds until the echo is received. + """ + self.trigger.value(0) # Stabilize the sensor + sleep_us(5) + self.trigger.value(1) + # Send a 10us pulse. + sleep_us(10) + self.trigger.value(0) + try: + pulse_time = time_pulse_us(self.echo, 1, self.echo_timeout_us) + # time_pulse_us returns -2 if there was timeout waiting for condition; and -1 if there was timeout during the main measurement. It DOES NOT raise an exception + # ...as of MicroPython 1.17: http://docs.micropython.org/en/v1.17/library/machine.html#machine.time_pulse_us + if pulse_time < 0: + MAX_RANGE_IN_CM = const(500) # it's really ~400 but I've read people say they see it working up to ~460 + pulse_time = int(MAX_RANGE_IN_CM * 29.1) # 1cm each 29.1us + return pulse_time + except OSError as ex: + if ex.args[0] == 110: # 110 = ETIMEDOUT + raise OSError('Out of range') + raise ex + + def distance_mm(self): + """ + Get the distance in milimeters without floating point operations. + """ + pulse_time = self._send_pulse_and_wait() + + # To calculate the distance we get the pulse_time and divide it by 2 + # (the pulse walk the distance twice) and by 29.1 becasue + # the sound speed on air (343.2 m/s), that It's equivalent to + # 0.34320 mm/us that is 1mm each 2.91us + # pulse_time // 2 // 2.91 -> pulse_time // 5.82 -> pulse_time * 100 // 582 + mm = pulse_time * 100 // 582 + return mm + + def distance_cm(self): + """ + Get the distance in centimeters with floating point operations. + It returns a float + """ + pulse_time = self._send_pulse_and_wait() + + # To calculate the distance we get the pulse_time and divide it by 2 + # (the pulse walk the distance twice) and by 29.1 becasue + # the sound speed on air (343.2 m/s), that It's equivalent to + # 0.034320 cm/us that is 1cm each 29.1us + cms = (pulse_time / 2) / 29.1 + return cms \ No newline at end of file diff --git a/Example - HCSR04 Ultrasonic/main_Ultrasonic.py b/Example - HCSR04 Ultrasonic/main_Ultrasonic.py new file mode 100644 index 0000000..c0938d6 --- /dev/null +++ b/Example - HCSR04 Ultrasonic/main_Ultrasonic.py @@ -0,0 +1,24 @@ +from hcsr04 import HCSR04 +from time import sleep +from machine import Pin +from utime import sleep_ms + +# Load Driver and Assign Each Pin +sensor = HCSR04(trigger_pin=4, echo_pin=5, echo_timeout_us=10000) +# LED Pin for Fun +led = Pin(2,Pin.OUT) +led.value(0) + +# Create Blink Function +def blink(): + led.value(0) + sleep_ms(50) + led.value(1) + +# Main Script Here +while True: + distance = sensor.distance_cm() + print('Distance:', distance, 'cm') + if(distance<=20): + blink() + sleep(1) \ No newline at end of file diff --git a/Example - I2C LED Display/README.md b/Example - I2C LED Display/README.md new file mode 100644 index 0000000..f9000a0 --- /dev/null +++ b/Example - I2C LED Display/README.md @@ -0,0 +1,3 @@ +# I2C Display Controller Example Code # + +This is a simple code to control I2C Display. MicroPython needs **ssd1306 driver** to work. diff --git a/Example - I2C LED Display/main_I2CDisplay.py b/Example - I2C LED Display/main_I2CDisplay.py new file mode 100644 index 0000000..9d110ae --- /dev/null +++ b/Example - I2C LED Display/main_I2CDisplay.py @@ -0,0 +1,11 @@ +from machine import Pin, SoftI2C +# This Driver Lib may not installed yet +import ssd1306 + +# Define i2c peripheral +i2c = SoftI2C(sda=Pin(5), scl=Pin(4)) +# Configure I2C Display Resolution +display = ssd1306.SSD1306_I2C(128, 64, i2c) +# Test Output +display.text('Hello, World!', 0, 0, 1) +display.show() \ No newline at end of file diff --git a/Example - MQ9/README.md b/Example - MQ9/README.md new file mode 100644 index 0000000..9f7d81d --- /dev/null +++ b/Example - MQ9/README.md @@ -0,0 +1,8 @@ + +# MQ-9 Gas Sensor Example Code + +The main script I wrote it myself adjusting to my needs + + * Original Driver: https://github.com/tutRPi/Raspberry-Pi-Gas-Sensor-MQ + * Adapted by : https://github.com/leech001/MQ9/tree/master + * The main code I made it myself. diff --git a/Example - MQ9/main_MQ9.py b/Example - MQ9/main_MQ9.py new file mode 100644 index 0000000..418aa03 --- /dev/null +++ b/Example - MQ9/main_MQ9.py @@ -0,0 +1,69 @@ +from machine import ADC, Pin +from mq9 import MQ +import time +import dht +import network +import ntptime +import urequests + +# Global Params +gas = ADC(Pin(35)) +led = Pin(2,Pin.OUT) +led.value(0) + +# Function to read MQ9 Sensor +def read_mq9(mq): + while True: + try: + adc = gas.read_u16() + sensor_volt = gas.read_uv()/1000000 + perc = mq.MQPercentage() + break + except: + pass + co = perc["CO"]*1000 + + if co >= 10000: + cp = 10000 + + result = [adc,sensor_volt,co] + return result + +# Function to Blink +def blink(): + led.value(1) + time.sleep(0.5) + led.value(0) + time.sleep(0.5) + +# Function to Blink Faster +def blink_fast(): + led.value(1) + time.sleep(0.25) + led.value(0) + time.sleep(0.25) + +# Main Script +def main(): + # Calibrate Sensor + print("Calibrating MQ-9 Sensor") + led.value(1) + while True: + try: + mq = MQ() + break + except: + pass + led.value(0) + print("Calibrated") + print("") + + print("Start Monitoring:") + print("") + + while True: + result = read_mq9(mq) + print(f"ADC Value : {result[0]}, Sensor Voltage : {result[1]} Volt, and Carbon Monoxide : {result[2]} ppm") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/Example - MQ9/mq9.py b/Example - MQ9/mq9.py new file mode 100644 index 0000000..ff91138 --- /dev/null +++ b/Example - MQ9/mq9.py @@ -0,0 +1,125 @@ +# adapted from https://github.com/tutRPi/Raspberry-Pi-Gas-Sensor-MQ + +import time +import math +from machine import ADC,Pin + + +class MQ: + # Hardware Related Macros + RL_VALUE = 10 # define the load resistance on the board, in kilo ohms + RO_CLEAN_AIR_FACTOR = 9.83 # RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO, + # which is derived from the chart in datasheet + + # Software Related Macros + CALIBARAION_SAMPLE_TIMES = 50 # define how many samples you are going to take in the calibration phase + CALIBRATION_SAMPLE_INTERVAL = 500 # define the time interal(in milisecond) between each samples in the + # cablibration phase + READ_SAMPLE_INTERVAL = 50 # define how many samples you are going to take in normal operation + READ_SAMPLE_TIMES = 5 # define the time interal(in milisecond) between each samples in + # normal operation + + # Application Related Macros + GAS_LPG = 0 + GAS_CO = 1 + GAS_SMOKE = 2 + + def __init__(self, ro=10): + self.ro = ro + self.adc = ADC(Pin(35)) + + self.LPGCurve = [2.3, 0.21, -0.47] # two points are taken from the curve. + # with these two points, a line is formed which is "approximately equivalent" + # to the original curve. + # data format:{ x, y, slope}; point1: (lg200, 0.21), point2: (lg10000, -0.59) + self.COCurve = [2.3, 0.72, -0.34] # two points are taken from the curve. + # with these two points, a line is formed which is "approximately equivalent" + # to the original curve. + # data format:[ x, y, slope]; point1: (lg200, 0.72), point2: (lg10000, 0.15) + self.SmokeCurve = [2.3, 0.53, -0.44] # two points are taken from the curve. + # with these two points, a line is formed which is "approximately equivalent" + # to the original curve. + # data format:[ x, y, slope]; point1: (lg200, 0.53), point2: (lg10000, -0.22) + + # print("Calibrating...") + self.ro = self.MQCalibration() + # print("Calibration is done...\n") + # print("Ro=%f kohm" % self.ro) + + def MQPercentage(self): + val = {} + read = self.MQRead() + val["GAS_LPG"] = self.MQGetGasPercentage(read / self.ro, self.GAS_LPG) + val["CO"] = self.MQGetGasPercentage(read / self.ro, self.GAS_CO) + val["SMOKE"] = self.MQGetGasPercentage(read / self.ro, self.GAS_SMOKE) + return val + + # MQResistanceCalculation + # Input: raw_adc - raw value read from adc, which represents the voltage + # Output: the calculated sensor resistance + # Remarks: The sensor and the load resistor forms a voltage divider. Given the voltage + # across the load resistor and its resistance, the resistance of the sensor + # could be derived. + def MQResistanceCalculation(self, raw_adc): + return float(self.RL_VALUE * (4095.0 - raw_adc) / float(raw_adc)) + + # MQCalibration + # Output: Ro of the sensor + # Remarks: This function assumes that the sensor is in clean air. It use + # MQResistanceCalculation to calculates the sensor resistance in clean air + # and then divides it with RO_CLEAN_AIR_FACTOR. RO_CLEAN_AIR_FACTOR is about + # 10, which differs slightly between different sensors. + def MQCalibration(self): + val = 0.0 + for i in range(self.CALIBARAION_SAMPLE_TIMES): # take multiple samples + val += self.MQResistanceCalculation(self.adc.read()) + time.sleep(self.CALIBRATION_SAMPLE_INTERVAL / 1000.0) + + val = val / self.CALIBARAION_SAMPLE_TIMES # calculate the average value + + val = val / self.RO_CLEAN_AIR_FACTOR # divided by RO_CLEAN_AIR_FACTOR yields the Ro + # according to the chart in the datasheet + return val + + # MQRead + # Output: Rs of the sensor + # Remarks: This function use MQResistanceCalculation to caculate the sensor resistenc (Rs). + # The Rs changes as the sensor is in the different consentration of the target + # gas. The sample times and the time interval between samples could be configured + # by changing the definition of the macros. + def MQRead(self): + rs = 0.0 + + for i in range(self.READ_SAMPLE_TIMES): + rs += self.MQResistanceCalculation(self.adc.read()) + time.sleep(self.READ_SAMPLE_INTERVAL / 1000.0) + + rs = rs / self.READ_SAMPLE_TIMES + + return rs + + # MQGetGasPercentage + # Input: rs_ro_ratio - Rs divided by Ro + # gas_id - target gas type + # Output: ppm of the target gas + # Remarks: This function passes different curves to the MQGetPercentage function which + # calculates the ppm (parts per million) of the target gas. + def MQGetGasPercentage(self, rs_ro_ratio, gas_id): + if gas_id == self.GAS_LPG: + return self.MQGetPercentage(rs_ro_ratio, self.LPGCurve) + elif gas_id == self.GAS_CO: + return self.MQGetPercentage(rs_ro_ratio, self.COCurve) + elif gas_id == self.GAS_SMOKE: + return self.MQGetPercentage(rs_ro_ratio, self.SmokeCurve) + return 0 + + # MQGetPercentage + # Input: rs_ro_ratio - Rs divided by Ro + # pcurve - pointer to the curve of the target gas + # Output: ppm of the target gas + # Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm) + # of the line could be derived if y(rs_ro_ratio) is provided. As it is a + # logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic + # value. + def MQGetPercentage(self, rs_ro_ratio, pcurve): + return math.pow(10, (((math.log(rs_ro_ratio) - pcurve[1]) / pcurve[2]) + pcurve[0])) diff --git a/Example - Networking and Clients/LibInstaller.py b/Example - Networking and Clients/LibInstaller.py new file mode 100644 index 0000000..e00df33 --- /dev/null +++ b/Example - Networking and Clients/LibInstaller.py @@ -0,0 +1,63 @@ +import network +import mip + +# Put Your SSID and Password +SSID: str = "" +Password: str = "" +reset = True # To Reset Network State and Force Connect + +print(">> Start MicroPython Libs Installer ... ") +print(">> Connecting ... ") +wlan0 = network.WLAN(network.STA_IF) +if reset: + wlan0.active(True) + + # Masukkan SSID dan Password + wlan0.connect(SSID, Password) + while not wlan0.isconnected(): + wlan0.active(True) + pass +status = wlan0.isconnected() + +if(status == True): + print("==>> Connected") + print(">> Verifying Libs urequests ...") + try: + import urequests + print("==>> OK") + except: + print("==>> Failed. Installing Libs ... ") + mip.install('urequests') + + print(">> Verifying Libs umqtt.robust ...") + try: + import umqtt.robust + print("==>> OK") + except: + print("==>> Failed. Installing Libs ... ") + mip.install("umqtt.robust") + + print(">> Verifying Libs umqtt.simple ...") + try: + import umqtt.simple + print("==>> OK") + except: + print("==>> Failed. Installing Libs ... ") + mip.install("umqtt.simple") + print(">> Verifying Libs ssd1306 ...") + try: + import ssd1306 + print("==>> OK") + except: + print("==>> Failed. Installing Libs ... ") + mip.install("ssd1306") + print(">> Verifying Libs aioble ...") + try: + import aioble + print("==>> OK") + except: + print("==>> Failed. Installing Libs ... ") + mip.install("aioble") + +else: + print("==>> Connection Failed") diff --git a/Example - Networking and Clients/NTPSetup.py b/Example - Networking and Clients/NTPSetup.py new file mode 100644 index 0000000..b6eb46e --- /dev/null +++ b/Example - Networking and Clients/NTPSetup.py @@ -0,0 +1,24 @@ +import ntptime +import time + +class NTPSetup(): + def __init__(self): + # Set Date Time + ntptime.settime() + + # Get Date Time + def getDateTime(self): + self.devdatetime = time.localtime() + self.devtime = str(self.devdatetime[3]+7)+":"+str(self.devdatetime[4])+":"+str(self.devdatetime[5]) + self.devdate = str(self.devdatetime[2])+"/"+str(self.devdatetime[1])+"/"+str(self.devdatetime[0]) + return self.devdate,self.devtime + + +def main(): + # Connect to Internet + ntp = NTPSetup() + devdate,devtime = ntp.getDateTime() + print(devdate,devtime) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/Example - Networking and Clients/README.md b/Example - Networking and Clients/README.md new file mode 100644 index 0000000..0cfabda --- /dev/null +++ b/Example - Networking and Clients/README.md @@ -0,0 +1,6 @@ +# A Collection of Helper MicroPython Scripts # + + * **NTPSetup.py** : A helper class to configure NTP (Need ISO Date Formatting) + * **WiFiManager.py** : A helper class to connect WiFi and verify Internet using simple requests + * **LibInstaller.py** : A helper class to install extra libraries from micropython. + diff --git a/Example - Networking and Clients/WiFiManager.py b/Example - Networking and Clients/WiFiManager.py new file mode 100644 index 0000000..58df1ce --- /dev/null +++ b/Example - Networking and Clients/WiFiManager.py @@ -0,0 +1,42 @@ +import network +from urequests import get + +class WiFiManager(): + def __init__(self,): + self.reset = True + + # Koneksi Wi-Fi + def connectWifi(self,SSID,Password): + self.wlan0 = network.WLAN(network.STA_IF) + if self.reset: + self.wlan0.active(True) + self.wlan0.connect(SSID, Password) + + while not self.wlan0.isconnected(): + self.wlan0.active(True) + pass + + self.status = self.wlan0.isconnected() + self.ip_addr = self.wlan0.ifconfig() + return self.status,self.ip_addr + + # Tes Koneksi + def testInternet(self): + response = get("https://example.com/") + status = response.status_code + response.close() + if status == 200: + return True + else: + return False + + +def main(): + handler = WiFiManager() + status,ip = handler.connectWLAN("","") + print(ip,status) + status = handler.testInternet() + print(status) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/Example - SPI OLED Display/README.md b/Example - SPI OLED Display/README.md new file mode 100644 index 0000000..3946191 --- /dev/null +++ b/Example - SPI OLED Display/README.md @@ -0,0 +1,3 @@ +# OLED Display Example Code # + +This is a simple code to control OLED Display using SPI Protocol diff --git a/Example - SPI OLED Display/main_OLED.py b/Example - SPI OLED Display/main_OLED.py new file mode 100644 index 0000000..4b9a3b2 --- /dev/null +++ b/Example - SPI OLED Display/main_OLED.py @@ -0,0 +1,28 @@ +from machine import Pin, SoftSPI +import ssd1306 + +sck = Pin(14) # D0 +scl = Pin(13) # D1 + +spi = SoftSPI(baudrate=500000, polarity=1, phase=0, sck=sck, mosi=scl, miso=Pin(12)) + +rst = Pin(4) # reset +dc = Pin(5) # data/command +cs = Pin(15) # chip select, some modules do not have a pin for this + +display = ssd1306.SSD1306_SPI(128, 64, spi, dc, rst, cs) + +# display.text('Hello, World!', 0, 0, 1) +# display.show() + +display.fill(0) +display.fill_rect(0, 0, 32, 32, 1) +display.fill_rect(2, 2, 28, 28, 0) +display.vline(9, 8, 22, 1) +display.vline(16, 2, 22, 1) +display.vline(23, 8, 22, 1) +display.fill_rect(26, 24, 2, 4, 1) +display.text('MicroPython', 40, 0, 1) +display.text('SSD1306', 40, 12, 1) +display.text('OLED 128x64', 40, 24, 1) +display.show() \ No newline at end of file diff --git a/Manuals/Manual-7Segment-5631AS.pdf b/Manuals/Manual-7Segment-5631AS.pdf new file mode 100644 index 0000000..3484c93 Binary files /dev/null and b/Manuals/Manual-7Segment-5631AS.pdf differ diff --git a/Manuals/Manual-MQ-9_Hanwei.pdf b/Manuals/Manual-MQ-9_Hanwei.pdf new file mode 100644 index 0000000..3ca7acb Binary files /dev/null and b/Manuals/Manual-MQ-9_Hanwei.pdf differ diff --git a/Manuals/Manual-OLED-Guide.pdf b/Manuals/Manual-OLED-Guide.pdf new file mode 100644 index 0000000..d34d9d8 Binary files /dev/null and b/Manuals/Manual-OLED-Guide.pdf differ diff --git a/Pinouts/Pinout-ESP32-Wroom.jpg b/Pinouts/Pinout-ESP32-Wroom.jpg new file mode 100644 index 0000000..fbc12b7 Binary files /dev/null and b/Pinouts/Pinout-ESP32-Wroom.jpg differ diff --git a/Pinouts/Pinout-ESP32.webp b/Pinouts/Pinout-ESP32.webp new file mode 100644 index 0000000..a32ce14 Binary files /dev/null and b/Pinouts/Pinout-ESP32.webp differ diff --git a/Pinouts/Pinout-ESP8266.webp b/Pinouts/Pinout-ESP8266.webp new file mode 100644 index 0000000..6c394c6 Binary files /dev/null and b/Pinouts/Pinout-ESP8266.webp differ diff --git a/Pinouts/Pinout-MQ7.png b/Pinouts/Pinout-MQ7.png new file mode 100644 index 0000000..e5f5148 Binary files /dev/null and b/Pinouts/Pinout-MQ7.png differ diff --git a/Pinouts/Pinout-Pico-W.webp b/Pinouts/Pinout-Pico-W.webp new file mode 100644 index 0000000..bbb764b Binary files /dev/null and b/Pinouts/Pinout-Pico-W.webp differ diff --git a/Pinouts/Pinout-Pico.jpg b/Pinouts/Pinout-Pico.jpg new file mode 100644 index 0000000..b285d39 Binary files /dev/null and b/Pinouts/Pinout-Pico.jpg differ diff --git a/Pinouts/Pinout-SIM800L.jpg b/Pinouts/Pinout-SIM800L.jpg new file mode 100644 index 0000000..c797ae8 Binary files /dev/null and b/Pinouts/Pinout-SIM800L.jpg differ diff --git a/README.md b/README.md new file mode 100644 index 0000000..965bd2d --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# A Collection of Example Codes # + +This folder contains many simple codes to access several sensors or peripherals. Need to be noted that this **MAY NOT** work in your board. Always **VERIFY** before using.