Skip to content

AWS Greengrass + Local Historian

0

Hi again,

I have a deployed AWS Greegrass, is working fine. For reasons, i need to create local historian (mongodb, hosted locally).

I have an aws lambda running in Greengrass core that connects to OPC UA server through the OPC nodejs client created by AWS Team. I can see the values.

[2019-05-06T23:47:32.95Z][INFO]-Posting work for function [arn:aws:lambda:::function:GGRouter] to /2016-11-01/functions/arn:aws:lambda:::function:GGRouter
[2019-05-06T23:47:32.951Z][INFO]-Invoking local lambda arn:aws:lambda:::function:GGRouter with payload {"id":"ns=1;s=Temperature","value":{"dataType":"Double","arrayType":"Scalar","value":17.27744731362982}} and client context eyJjdXN0b20iOnsic291cmNlIjoiYXJuOmF3czpsYW1iZGE6ZXUtd2VzdC0xOjkyMDc5NDczOTM4NDpmdW5jdGlvbjpvcGN1YV9zaW11bGF0b3I6MSIsInN1YmplY3QiOiIvb3BjdWEvb3BjLXNpbXVsYXRvci9ub2RlL1RlbXBlcmF0dXJlIn19
[2019-05-06T23:47:32.951Z][INFO]-Work posted with invocation ID [34d4180e-f631-4d21-4232-4ea3f293918f]

Now i want to fetch the information from the MQTT and send it to mongodb (local historian)

but nothing happens, and i dont have nothing error in logs.

Here is my python code:

import paho.mqtt.client as mqtt
import greengrasssdk
import time
import json
import datetime
import os
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure, InvalidName
import future

def on_connect(mqtt, userdata, flags, rc):
print("Connected with result code "+str(rc))
mqtt.subscribe("/opcua/#")

def on_message(mqtt, userdata, msg, collection):
receiveTime = datetime.datetime.now()
message = msg.payload.decode("utf-8")
isfloatValue = False
try:
# Convert the string to a float so that it is stored as a number and not a string in the database
val = float(message)
isfloatValue = True
except:
isfloatValue = False

if isfloatValue:  
    print(str(receiveTime) _ ": " _ msg.topic _ " " _ str(val))  
    post = {"time": receiveTime, "topic": msg.topic, "value": val}  
else:  
    print(str(receiveTime) _ ": " _ msg.topic _ " " _ message)  
    post = {"time": receiveTime, "topic": msg.topic, "value": message}  

collection.insert_one(post)  

def mongodb_connection():
mongodb_conn = os.environ['mongodb_connnection']
mongoClient = MongoClient(mongodb_conn)
db = mongoClient.plc_poc_db
collection = db.plc_poc
return collection

def topic_to_mongo():
mqtt_server_conn = os.environ['mqtt_conn']
mqttc = mqtt.Client(mqtt_server_conn)
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.loop_forever()

def start_listening():
topic_to_mongo()

def function_handler(event, context):
return

any suggestion?

Edit: for some reason the os.environ variables are not showing up

asked 7 years ago329 views
1 Answer
0

I have solved this issue.

In case you are curious, here is how: https://github.com/mariopoeta/aws-lambda-awsgreengrass-opcua-mongodb

Edited by: poeta on May 7, 2019 7:44 AM

answered 7 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.