Created
July 18, 2019 21:55
-
-
Save unforgiven512/07aea00f4086ff1dcdaad56af32d709f to your computer and use it in GitHub Desktop.
old main.py file on MicroPython ESP8266 board #7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# main.py | |
# | |
# Example for MicroPython | |
# | |
from machine import Pin, RTC, PWM, Signal, I2C | |
# from machine import I2C | |
# from machine import SPI | |
import utime | |
import onewire | |
import ds18x20 | |
# import sys | |
# import os | |
# import esp | |
# import network | |
# import socket | |
import ntptime | |
# import bme280 | |
# NOTE: | |
# There is a negative-logic LED on pin D1 / GPIO 5 | |
# Create RTC object | |
rtc = RTC() | |
# Create I2C interface object | |
# SCL = D5 / GPIO 14 | |
# SDA = D6 / GPIO 12 | |
i2c = I2C(scl=Pin(14), sda=Pin(12), freq=100000) | |
# Create OneWire bus object | |
# on pin D2 / GPIO 4 | |
owBus = onewire.OneWire(Pin(4)) | |
# Create our owTemp (ds18x20) object | |
owTemp = ds18x20.DS18X20(owBus) | |
# yellowLed = Pin(5, Pin.OUT) | |
# Setup our yellow LED as a PWM object | |
pwmLed0 = PWM(Pin(5), freq=800, duty=511) | |
# setup our blue LED as a Signal object | |
pBlueLED = Pin(13, Pin.OUT) | |
blueLED = Signal(pBlueLED, invert=True) | |
# Global variables for the button callback | |
btnCbValue = 1 | |
btnCbCounter = 0 | |
def btn_cb(v): | |
global btnCbValue, btnCbCounter | |
btnCbCounter += 1 | |
if btnCbValue == 0: | |
btnCbValue = 1 | |
else: | |
btnCbValue = 0 | |
print("IRQ: ", btnCbCounter) | |
print("State: ", btnCbValue) | |
# setup our button on GPIO 15 | |
myButton = Pin(15, Pin.IN) | |
myButton.irq(trigger=Pin.IRQ_FALLING, handler=btn_cb) | |
def pwm_up(step_ms=1): | |
""" | |
Taper the PWM duty cycle of the yellow LED from OFF to ON over a period of 1024 milliseconds (default) or a multiple | |
of 1024 milliseconds (set by the step_ms parameter). | |
:param step_ms: The interval, in milliseconds, between each "step" of the PWM fade. | |
:return: True | |
""" | |
pwm_dc = 1023 | |
while pwm_dc >= 0: | |
pwmLed0.duty(pwm_dc) | |
pwm_dc = pwm_dc - 1 | |
utime.sleep_ms(step_ms) | |
return True | |
def pwm_down(step_ms=1): | |
""" | |
Taper the PWM duty cycle of the yellow LED from ON to OFF over a period of 1024 milliseconds (default) or a multiple | |
of 1024 milliseconds (set by the step_ms parameter). | |
:param step_ms: The interval, in milliseconds, between each "step" of the PWM fade. | |
:return: True | |
""" | |
pwm_dc = 0 | |
while pwm_dc < 1024: | |
pwmLed0.duty(pwm_dc) | |
pwm_dc = pwm_dc + 1 | |
utime.sleep_ms(step_ms) | |
return True | |
def blink_blue_led(times, interval_on_ms=250, interval_off_ms=250): | |
""" | |
Blink the blue LED a specified amount of times. | |
:param times: An integer value expressing the amount of times to blink the LED. | |
:param interval_on_ms: The amount of time, in milliseconds, to wait between ON and OFF transitions. | |
:param interval_off_ms: The amount of time, in milliseconds, to wait between OFF and ON transitions. | |
:return: True | |
""" | |
i = 0 | |
while i < times: | |
blueLED.on() | |
utime.sleep_ms(interval_on_ms) | |
blueLED.off() | |
utime.sleep_ms(interval_off_ms) | |
i = i + 1 | |
return True | |
def read_onewire_temperature(): | |
""" | |
Read the temperature of any temperature sensors on the 1-Wire bus. | |
""" | |
print('\t* Scanning OneWire bus...') | |
ow_temp_sensors = owTemp.scan() | |
print('\t* Performing temperature conversions...') | |
owTemp.convert_temp() | |
utime.sleep_ms(750) | |
for owTempSensor in ow_temp_sensors: | |
temp_string = 'Temperature: %d' % (owTemp.read_temp(owTempSensor)) | |
print(temp_string) | |
def set_rtc_from_ntp(): | |
print('* Setting RTC time from NTP') | |
# Set the NTP host to one of our local time servers | |
ntptime.host = 'time4.ud-home.net' | |
# Use NTP to set the correct time on the RTC | |
ntptime.settime() | |
def print_datetime(): | |
print('* Printing current timestamp:') | |
print(rtc.datetime()) | |
def main(): | |
print('\n\n--- Hello, World (from MicroPython on ESP8266)! ---\n\n') | |
utime.sleep_ms(200) | |
blueLED.on() | |
utime.sleep_ms(1500) | |
blueLED.off() | |
cycle = 0 | |
while cycle < 5: | |
blueLED.on() | |
utime.sleep_ms(200) | |
blueLED.off() | |
utime.sleep_ms(200) | |
cycle = cycle + 1 | |
print('\n--- READY ---') | |
utime.sleep_ms(100) | |
set_rtc_from_ntp() | |
while True: | |
pass | |
# bme = bme280.BME280(i2c=i2c) | |
# print('Temperature:\t' + bme.temperature) | |
# print('Pressure:\t' + bme.pressure) | |
# print('Humidity:\t' + bme.humidity) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment