def connect(self): """ Connect AWS IoT """ if self.client is None: # Configure the MQTT client self.client = AWSIoTMQTTClient(config.AWS_IOT_CLIENT_ID) self.client.configureEndpoint(config.AWS_IOT_HOST, config.AWS_IOT_PORT) self.client.configureCredentials(config.AWS_IOT_ROOT_CA, config.AWS_IOT_PRIVATE_KEY, config.AWS_IOT_CLIENT_CERT) self.client.configureOfflinePublishQueueing(config.AWS_IOT_OFFLINE_QUEUE_SIZE) self.client.configureDrainingFrequency(config.AWS_IOT_DRAINING_FREQ) self.client.configureConnectDisconnectTimeout(config.AWS_IOT_CONN_DISCONN_TIMEOUT) self.client.configureMQTTOperationTimeout(config.AWS_IOT_MQTT_OPER_TIMEOUT) # Connect to MQTT Host if self.client.connect(): self.is_connected = True log.info('AWS IoT connection succeeded') else: raise socket.error('AWS IoT connection failed')
class AWS(object): """ AWS IoT communication with Pycom provided libraries """ def __init__(self): """ Initialization of AWS Class """ self.client = None self.is_connected = False def connect(self): """ Connect AWS IoT """ if self.client is None: # Configure the MQTT client self.client = AWSIoTMQTTClient(config.AWS_IOT_CLIENT_ID) self.client.configureEndpoint(config.AWS_IOT_HOST, config.AWS_IOT_PORT) self.client.configureCredentials(config.AWS_IOT_ROOT_CA, config.AWS_IOT_PRIVATE_KEY, config.AWS_IOT_CLIENT_CERT) self.client.configureOfflinePublishQueueing(config.AWS_IOT_OFFLINE_QUEUE_SIZE) self.client.configureDrainingFrequency(config.AWS_IOT_DRAINING_FREQ) self.client.configureConnectDisconnectTimeout(config.AWS_IOT_CONN_DISCONN_TIMEOUT) self.client.configureMQTTOperationTimeout(config.AWS_IOT_MQTT_OPER_TIMEOUT) # Connect to MQTT Host if self.client.connect(): self.is_connected = True log.info('AWS IoT connection succeeded') else: raise socket.error('AWS IoT connection failed') def publish(self, msg=None): """ Publish message """ log.info('Publish [{}]', json.dumps(msg)) self.client.publish(config.AWS_IOT_TOPIC, json.dumps(msg), 1) def disconnect(self): """ Disconnect AWS IoT """ if self.client: if self.client.disconnect(): log.info('AWS IoT disconnected') self.is_connected = False self.client = None
from network import LoRa from network import WLAN from MQTTLib import AWSIoTMQTTClient # Connect to wifi wlan = WLAN(mode=WLAN.STA) wlan.connect(config.WIFI_SSID, auth=(None, config.WIFI_PASS), timeout=5000) while not wlan.isconnected(): time.sleep(0.5) print('WLAN connection succeeded!') time.sleep(2) gc.enable # configure the MQTT client pycomAwsMQTTClient = AWSIoTMQTTClient(config.CLIENT_ID) pycomAwsMQTTClient.configureEndpoint(config.AWS_HOST, config.AWS_PORT) pycomAwsMQTTClient.configureCredentials(config.AWS_ROOT_CA, config.AWS_PRIVATE_KEY, config.AWS_CLIENT_CERT) pycomAwsMQTTClient.configureOfflinePublishQueueing(config.OFFLINE_QUEUE_SIZE) pycomAwsMQTTClient.configureDrainingFrequency(config.DRAINING_FREQ) pycomAwsMQTTClient.configureConnectDisconnectTimeout( config.CONN_DISCONN_TIMEOUT) pycomAwsMQTTClient.configureMQTTOperationTimeout(config.MQTT_OPER_TIMEOUT) pycomAwsMQTTClient.configureLastWill(config.LAST_WILL_TOPIC, config.LAST_WILL_MSG, 1) #Connect to MQTT Host if pycomAwsMQTTClient.connect():
engine = "[[" "\"LIEBHERR RT 936\"" "," + str( si.temperature()) + "," + str(si.humidity()) + "]]" print("Engine Data" + engine) return engine def customCallback(client, userdata, message): print("Received a new message: ") print(message.payload) print("from topic: ") print(message.topic) print("--------------\n\n") # configure the MQTT client pycomAwsMQTTClient = AWSIoTMQTTClient('<SCP IoT Service Device ID>') pycomAwsMQTTClient.configureEndpoint(SCP_IOT_HOST, SCP_IOT_PORT) pycomAwsMQTTClient.configureCredentials(SCP_IOT_ROOT_CA, SCP_IOT_PRIVATE_KEY, SCP_IOT_CLIENT_CERT) pycomAwsMQTTClient.configureConnectDisconnectTimeout( config.CONN_DISCONN_TIMEOUT) pycomAwsMQTTClient.configureMQTTOperationTimeout(config.MQTT_OPER_TIMEOUT) if pycomAwsMQTTClient.connect(): print('SAP CP IoT Service connection succeeded') # Subscribe to topic pycomAwsMQTTClient.subscribe(config.TOPIC_CMD, 1, customCallback) time.sleep(2) while (True):
AWS_CLIENT_CERT = '/flash/cert/YOUR CERTIFICATE.pem' AWS_PRIVATE_KEY = '/flash/cert/YOUR CERTIFICATE.private.key' CLIENT_ID = 'basicPubSub' TOPIC = 'sdk/test/Python' def customCallback(client, userdata, message): print("Received a new message: ") print(message.payload) print("from topic: ") print(message.topic) print("--------------\n\n") # Init AWSIoTMQTTClient myAWSIoTMQTTClient = AWSIoTMQTTClient(CLIENT_ID) myAWSIoTMQTTClient.configureEndpoint(AWS_HOST, AWS_PORT) myAWSIoTMQTTClient.configureCredentials(AWS_ROOT_CA, AWS_PRIVATE_KEY, AWS_CLIENT_CERT) # AWSIoTMQTTClient connection configuration myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20) myAWSIoTMQTTClient.configureOfflinePublishQueueing( -1) # Infinite offline Publish queueing myAWSIoTMQTTClient.configureDrainingFrequency(2) # Draining: 2 Hz myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10) # 10 sec myAWSIoTMQTTClient.configureMQTTOperationTimeout(5) # 5 sec if myAWSIoTMQTTClient.connect(): print('AWS connection successful!')
from MQTTLib import AWSIoTMQTTClient import time import config import ujson from skills import * #Pin14 es el push button global response from machine import Pin from machine import RTC from machine import Timer # configuracion de cliente MQTT #Pin('P11').mode(Pin.OUT) #deepsleepInterrupt = Clock() SmartLuxMQTTClient = AWSIoTMQTTClient(config.CLIENT_ID) SmartLuxMQTTClient.configureEndpoint(config.AWS_HOST, config.AWS_PORT) SmartLuxMQTTClient.configureCredentials(config.AWS_ROOT_CA, config.AWS_PRIVATE_KEY, config.AWS_CLIENT_CERT) SmartLuxMQTTClient.configureOfflinePublishQueueing(config.OFFLINE_QUEUE_SIZE) SmartLuxMQTTClient.configureDrainingFrequency(config.DRAINING_FREQ) SmartLuxMQTTClient.configureConnectDisconnectTimeout( config.CONN_DISCONN_TIMEOUT) SmartLuxMQTTClient.configureMQTTOperationTimeout(config.MQTT_OPER_TIMEOUT) SmartLuxMQTTClient.configureLastWill(config.LAST_WILL_TOPIC, config.LAST_WILL_MSG, 1) #Conectar al host MQTT Host if SmartLuxMQTTClient.connect(): print('Conexion AWS exitosa!')
def PublishMQTT(mailType, timeString="", timeStringCritical=""): ConnectWLAN(True) # Config MQTT mqttClient = AWSIoTMQTTClient(clientID) mqttClient.configureEndpoint(awsHost, awsPort) mqttClient.configureCredentials(awsRootCA, awsPrivateKey, awsClientCert) mqttClient.configureOfflinePublishQueueing(offlineQueueSize) mqttClient.configureConnectDisconnectTimeout(connectDisconnectTimeout) # Connect if mqttClient.connect(): print("MQTT connected") # Publish topic, AWS Lambda Python code uses the json to create mail title and body. mqttClient.publish( topic, json.dumps({ "mailType": str(mailType), "timeString": timeString, "timeStringCritical": timeStringCritical }), 0) # Disconnect if mqttClient.disconnect(): print("MQTT disconnected") ConnectWLAN(False) # Save variables to be loaded when waking up from deep sleep. Save() machine.deepsleep(deepSleepTime)