Hendra Kusumah
Published © LGPL

Connect MyDevice Cayenne Using MicroPython With DS18b20

Connect your ESP8266 to MyDevice Cayenne platform using Micropython.

IntermediateFull instructions provided1 hour3,842
Connect MyDevice Cayenne Using MicroPython With DS18b20

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
ds18b20 (temperature sensor)
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

DFRobot upycraft IDE
MicroPython
MicroPython
Cayenne
myDevices Cayenne

Story

Read more

Schematics

wemos ds18b20

Code

dstemp.py

Python
code to test the temperature sensor
import time
import machine
import ds18x20
import onewire

# the device is on GPIO12
dat = machine.Pin(12)

# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))

# scan for devices on the bus
roms = ds.scan()
print('found devices:', roms)

# loop 10 times and print all temperatures
while True:
    print('temperatures:', end=' ')
    ds.convert_temp()
    time.sleep_ms(750)
    for rom in roms:
        print(ds.read_temp(rom), end=' ')
    print()

cayennetemp.py

Python
code to send your temperature data to the cayenne service
from umqtt.simple import MQTTClient
from machine import Pin
import network
import time
import ds18x20
import onewire

# the device is on GPIO12
dat =Pin(12)
# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))
# scan for devices on the bus
roms = ds.scan()
print('found devices:', roms)

#wifi setting
SSID=" " #insert your wifi ssid
PASSWORD=" " #insert your wifi password

SERVER = "mqtt.mydevices.com"
CLIENT_ID = " " #insert your client ID
username=' ' #insert your MQTT username
password=' ' #insert your MQTT password
TOPIC = ("v1/%s/things/%s/data/1" % (username, CLIENT_ID))

def connectWifi(ssid,passwd):
  global wlan
  wlan=network.WLAN(network.STA_IF)
  wlan.active(True)
  wlan.disconnect()
  wlan.connect(ssid,passwd)
  while(wlan.ifconfig()[0]=='0.0.0.0'):
    time.sleep(1)
    
connectWifi(SSID,PASSWORD)
server=SERVER
c = MQTTClient(CLIENT_ID, server,0,username,password)
c.connect()

def senddata():
  ds.convert_temp()
  time.sleep_ms(100)
  temp = ds.read_temp(roms[0])
  c.publish(TOPIC, str(temp))

  time.sleep(10)
  print("temperature is: ", temp)
  print("data sent")
  
while True:
	try:
		senddata()
	except OSError:
		pass

Credits

Hendra Kusumah

Hendra Kusumah

27 projects • 125 followers
Love hacking and making new things from IoT to robotics

Comments