【问题标题】:AWS IoT - Create lambda loop to give alternating simulated resultsAWS IoT - 创建 lambda 循环以提供交替的模拟结果
【发布时间】:2021-09-23 11:34:14
【问题描述】:

我正在创建一个模拟空气质量数据的 lambda 函数(如下所示),然后我希望生成的 10 条消息包含 10 个不同的结果

例如当 lambda 被触发时,它将向 IoT 主题发送 10 条不同的消息

我正在使用下面的脚本,目前只打印 10 个相同的结果

from awscrt import io, mqtt, auth, http
from awsiot import mqtt_connection_builder
import time as t
import json
import os, tempfile
import boto3
from datetime import datetime
import pandas as pd
import random

pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
format = '%m/%d/%Y %I:%M:%S %p'

d = datetime.now().strftime(format)
df = pd.DataFrame({'LAST UPDATE': str(d),
                              'NO2 concentration': round(random.uniform(0, 0.017), 3),
                              'SO2 concentration': round(random.uniform(0, 0.022), 3),
                              'temperature': str(random.randint(3, 18)),
                              'relative humidity': str(random.randint(75, 90)),
                              'air pressure': str(random.randint(1011, 1016)),
                              'PM1.0 concentration': str(random.randint(5, 50)),
                              'PM2.5 concentration': str(random.randint(11, 26)),
                              'PM4 concentration': str(random.randint(7, 22)),
                              'PM10 concentration': str(random.randint(1, 20))[:4]}, index=[0],
                        columns = ['LAST UPDATE', 'NO2 concentration', 'SO2 concentration', 'temperature', 'relative humidity', 'air pressure', 'PM1.0 concentration', 'PM2.5 concentration', 'PM4 concentration', 'PM10 concentration'])

result = df.to_json(orient="records")
parsed = json.loads(result)
json.dumps(parsed, indent=4)


MESSAGE = parsed
RANGE = 10

【问题讨论】:

    标签: python pandas dataframe aws-iot


    【解决方案1】:

    我尝试在没有函数的情况下重复您的步骤,并且它正在工作,.json 文件已创建并正确打开。如果您对代码有一些疑问或未来的错误,您可以轻松地从此代码创建函数,我很乐意为您提供帮助。

    import json
    import pandas as pd
    from datetime import datetime
    import random
    format = '%m/%d/%Y %I:%M:%S %p'
    d = datetime.now().strftime(format)
    df = pd.DataFrame(data={'LAST UPDATE': str(d),
                            'NO2 concentration': round(random.uniform(0, 0.017), 3),
                            'SO2 concentration': round(random.uniform(0, 0.022), 3),
                            'temperature': str(random.randint(3, 18)),
                            'relative humidity': str(random.randint(75, 90)),
                            'air pressure': str(random.randint(1011, 1016)),
                            'PM1.0 concentration': str(random.randint(5, 50)),
                            'PM2.5 concentration': str(random.randint(11, 26)),
                            'PM4 concentration': str(random.randint(7, 22)),
                            'PM10 concentration': str(random.randint(1, 20))[:4]}, index=[0])
    with open('temp.json', 'w') as f:
        f.write(df.to_json(orient='records', lines=True))
    
    f = open('temp.json',)
    data = json.load(f)
    f.close()
    print(data)
    

    【讨论】:

    • 感谢您的帮助!我已经更新了我的问题 - 道歉。
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 2020-05-05
    • 1970-01-01
    相关资源
    最近更新 更多