'''
Setting Up ESP32 Data Monitoring and Control with Beebotte
1. Create a Beebotte Account
- Visit https://beebotte.com
- Sign up for a free account
2. Configure a New Channel
- Create a channel named "esp32"
- Add two resources:
a. "sensor" (type: number) for receiving data
b. "led" (type: boolean) for controlling an LED
3. Set Up a Dashboard
- Create a new dashboard called "Esp32Dashboard"
- Add two widgets:
a. "Basic Value" widget: Connect to "esp32" channel and "sensor" resource
b. "On/Off" widget: Connect to "esp32" channel and "led" resource
4. Obtain Channel Token
- Navigate to the "esp32" channel
- Locate and copy the "Channel Token"
5. Configure the Controller
- Paste the Channel Token into the appropriate place in your code
- Upload and run the code on your ESP32 controller
6. Monitor and Control
- Open the "Esp32Dashboard"
- Observe real-time sensor data updates every 10 seconds
- Use the "On/Off" widget to control the LED connected to the hardware
Author: Gadi Herman
GitHub: https://github.com/GadiHerman
License: MIT
'''
from machine import Pin, Timer
from umqtt.simple import MQTTClient
import ujson
import sys
import os
from time import sleep
LED = Pin(2, Pin.OUT)
PING_PERIOD = 120
PUBLISH_PERIOD = 10
msgNumber = 12.24
CHANNEL_TOKEN = 'token_Cl6WxOAn8cae5lSU'
CHANNEL_NAME = 'ESP32_E1'
MQTT_SERVER = 'mqtt.beebotte.com'
MQTT_USER = 'token:' + CHANNEL_TOKEN
RESOURCE_NAME_PUBLISH = 'sensor'
RESOURCE_NAME_SUBSCRIBE = 'led'
MQTT_TOPIC_PUBLISH = CHANNEL_NAME + '/' + RESOURCE_NAME_PUBLISH
MQTT_TOPIC_SUBSCRIBE = CHANNEL_NAME + '/' + RESOURCE_NAME_SUBSCRIBE
def handleTimer0Int(timer):
client.ping()
print('ping')
def handleTimer1Int(timer):
global msgNumber
global timeSave
msg = b'{"data": ' + str(msgNumber) + b', "write": true}'
client.publish(MQTT_TOPIC_PUBLISH,msg, qos=0)
print("Publish:",msg)
msgNumber += 1.12
timeSave = 0
def callback_func(topic, msg):
print("topic:",topic," msg:", msg)
json_data= ujson.loads(msg)
dt= json_data["data"]
print("*** " + str(dt) + " ***")
if dt:
LED.value(1)
else:
LED.value(0)
# create a random MQTT clientID
random_num = int.from_bytes(os.urandom(3), 'little')
mqtt_client_id = bytes('client_'+str(random_num), 'utf-8')
client = MQTTClient(mqtt_client_id, MQTT_SERVER, user=MQTT_USER, password='', keepalive=PING_PERIOD*2 )
timer0 = Timer(0) #subscribe
timer1 = Timer(1) #publish
try:
client.connect()
timer0.init(period=PING_PERIOD*1000, mode=Timer.PERIODIC, callback=handleTimer0Int)
timer1.init(period=PUBLISH_PERIOD*1000, mode=Timer.PERIODIC, callback=handleTimer1Int)
except Exception as e:
print('could not connect to MQTT server {}{}'.format(type(e).__name__, e))
sys.exit()
client.set_callback(callback_func)
client.subscribe(MQTT_TOPIC_SUBSCRIBE)
while True:
try:
client.wait_msg()
except KeyboardInterrupt:
print('Ctrl-C pressed...exiting')
client.disconnect()
sys.exit()