Close

Messaging formats: Dumping JSON alert message to file

A project log for Elephant AI

a system to prevent human-elephant conflict by detecting elephants using machine vision, and warning humans and/or repelling elephants

neil-k-sheridanNeil K. Sheridan 09/21/2017 at 19:030 Comments

Here I've got the variables obtained from the detection device (e.g. was an elephant detected, what kind, time, location of detection device, etc) and I dump them to the JSON format file.  We can go ahead and send this file to the deter device, or upload it to a server. If you've been reading the updates you'll realise I gave up on using XML!

# Here we write the values of variables  we've obtained to our JSON 
# elephant alert
# message file 
import json
#the variables with dummy values
spotted = "Yes"
time = "0130"
date = "01/10/2017"
lat = "-11111111"
lon = "-00000000"
lonemale = "yes"
herd = "no"
calves = "no"
deter_sent = "yes"
audio_deter = "yes"
physical_deter = "no"
batteryvoltage = "12"
#getting them into JSON format
data = { 
    "spotted" : spotted,
    "time:" : time,
    "date" : date,
    "latitude" : lat,
    "longitude" : lon,
    "details" : {
        "lonemale": lonemale,
        "herd": herd,
        "calves": calves
        },
    "deter" : {
        "deterdone" : deter_sent,
        "deteraudio" : audio_deter,
        "deterphysical" : physical_deter
        },
    "battery" : batteryvoltage
}
#writing them to JSONelephants.json JSON file
with open('JSONelephants.json', 'w') as f:
    json.dump(data, f)

I did rather question the whole concept of actually storing our variables in JSON-format alert message files after I finished up doing it! Why not just send the variables themselves to the web server, SMS, twitter? That's fine for twitter and SMS, but I figure uploading the JSON to the server is going to save a bit of code. Hopefully!

Discussions