예제 #1
0
def sense():
    import machine, onewire, ds18x20
    import network
    import boot
    import ubinascii
    from umqtt.simple import MQTTClient
    import time

    # connect to the network and set network varialbe (just in case)
    # boot.connect()
    # sta_if = network.WLAN(network.STA_IF)

    #pull from the temperature.py script
    from temperature import TemperatureSensor
    t = TemperatureSensor(21)
    t.read_temp()  # use t.read_temp(False) to return Celsius

    # connect to devans laptop IP
    c = MQTTClient("ESP32_dev", '192.168.121.156')
    c.connect()

    try:
        while True:
            c.publish('sensor-data', str(t.read_temp()))
            time.sleep(3)
    except KeyboardInterrupt:
        print('interrupted!')

    c.disconnect()
예제 #2
0
def sense():
    import machine, onewire, ds18x20
    import network
    import boot
    import ubinascii
    from umqtt.simple import MQTTClient
    import time
    pin = 21
    # connect to the network and set network varialbe (just in case)
    # boot.connect()
    # sta_if = network.WLAN(network.STA_IF)

    #pull from the temperature.py script

    print("setting up sensor")
    from temperature import TemperatureSensor
    t = TemperatureSensor(pin, 'DS18B20-1')
    num_sens = len(t.ds.scan())

    print('Found ' + str(num_sens) + ' Sensors. reading....')
    for k in range(0, num_sens):
        print('Temp of sensor ' + str(k) + ': ')
        temp = t.read_temp(
            addr_num=k)  # use t.read_temp(False) to return Celsius
        print(temp)

    # connect to devans laptop IP
    print("connecting.... ")

    c = MQTTClient("ESP32_dev", "192.168.121.117", port=1883, keepalive=60)
    c.connect()

    print("Done it should be connected")
    c.publish('sensor-setup', str(num_sens))

    try:
        while True:
            for k in range(0, num_sens):
                print("Data To Published, Temperature is " +
                      str(t.read_temp(addr_num=k)) + ' F' + ' From Sensor ' +
                      str(k))
                c.publish(
                    'sensor-data',
                    str(t.name) + '-' + str(k) + ' Temp: ' +
                    str(t.read_temp(addr_num=k)) + ' F')
                print("Done!")
                time.sleep(1)
    except KeyboardInterrupt:
        print('interrupted!')

    c.disconnect()
예제 #3
0
class TemperatureClient:
    """
    Represents an MQTT client which publishes temperature data on an interval
    """
    def __init__(self,
                 client_id,
                 server,
                 pin,
                 fahrenheit=True,
                 topic=None,
                 **kwargs):
        """
        Instantiates a TemperatureSensor and MQTTClient; connects to the
        MQTT broker.
        Arguments `server` and `client_id` are required.
        :param client_id: Unique MQTT client ID
        :type client_id: str
        :param server: MQTT broker domain name / IP
        :type server: str
        :param pin: 1-Wire bus pin
        :type pin: int
        :param fahrenheit: Whether or not to publish temperature in Fahrenheit
        :type fahrenheit: bool
        :param topic: Topic to publish temperature on
        :type topic: str
        :param kwargs: Arguments for MQTTClient constructor
        """
        self.sensor = TemperatureSensor(pin)
        self.client = MQTTClient(client_id, server, **kwargs)
        if not topic:
            self.topic = 'devices/%s/temperature/degrees' % \
                         self.client.client_id
        else:
            self.topic = topic
        self.fahrenheit = bool(fahrenheit)

        self.client.connect()

    def publishTemperature(self):
        """
        Reads the current temperature and publishes a JSON payload on the
        configured topic, e.g., `{"unit": "F", "degrees": 72.5}`
        """
        t = self.sensor.read_temp(self.fahrenheit)
        payload = dict(degrees=t)
        if self.fahrenheit:
            payload['unit'] = 'F'
        else:
            payload['unit'] = 'C'
        self.client.publish(self.topic, json.dumps(payload))

    def start(self, interval=60):
        """
        Begins to publish temperature data on an interval (in seconds).
        This function will not exit! Consider using deep sleep instead.
        :param interval: How often to publish temperature data (60s default)
        :type interval: int
        """
        while True:
            self.publishTemperature()
예제 #4
0
import ubinascii
import time
import machine

from blink import Blink
from temperature import TemperatureSensor
from temperature_client import TemperatureClient

client_id = ubinascii.hexlify(machine.unique_id())
sensor = TemperatureSensor(5)
led = Blink(16)
tc = TemperatureClient(client_id, '93.80.147.216', 5, topic='esp/temp')
while True:
    tc.publishTemperature()
    print(str(time.localtime()) + ' | temp: ' + str(sensor.read_temp()))
    led.blink(2, 0.2)
    time.sleep(60)
예제 #5
0
from temperature import TemperatureSensor

ONE_WIRE_PIN = 32
I2C_SCL_PIN = 22
I2C_SDA_PIN = 21

ts = TemperatureSensor(ONE_WIRE_PIN)

i2c = machine.I2C(-1, machine.Pin(I2C_SCL_PIN), machine.Pin(I2C_SDA_PIN))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

# test_whole display
oled.fill(1)
oled.show()

time.sleep_ms(300)

# reset display
oled.fill(0)
oled.show()

while True:
    temp = ts.read_temp()
    # print to console
    print(temp)
    # print on display
    oled.fill(0) # reset display
    oled.text('T = %.1f C' % temp, 25, 20)
    oled.show()
    time.sleep_ms(500)
예제 #6
0
from umqtt.robust import MQTTClient