-1

I am creating a lambda function which simulates air quality data (as seen below) and then I want the 10 generated messages to contain 10 different results

E.g. when the lambda is triggered it will fire off 10 different messages to the IoT Topic

I am using the below script, which is currently just printing 10 results of the same

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

1 Answers1

0

I tried to repeat your steps without a function and it is working, the .json file is created and open right. You can easily create a function from this code if you will have some questions about the code or future errors I will be glad to help you.

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)
Ulewsky
  • 310
  • 1
  • 11