MongoDB IoT Example Issue

I want to recreate an example on IoT devices with MongoDB but I have an issue. The example is from 2018 and there was not yet introduced Realm it was stitch apps , so my issue is that I have everything done once that was last year and it worked perfectly transfering data from ESP8266 to Gateway (raspberry pi ) and than to the MongoDB. But this year I got stuck the ESP8266 does connect and send data to the gateway(raspberry pi) but the gateway does not connect to the MondoDB.

So here is the example: Example

And the configuration to the raspberry pi is done as it says I changed in the fields where it askes for information of the HTTP service and still noting happened. I don`t know if the issue is in the raspberry pi or the configuration of the function on the http service.

    import hashlib;
    import hmac;
    import datetime;
    import paho.mqtt.client as mqtt
    import jsonimport calendar
    import requests

    mqqt_server=#enter your mqqt server here

    def on_connect(mqttc, obj, flags, rc):
       print("Connected with result code "+str(rc))
    # The callback for when a PUBLISH message is received from the server.

    def on_message(client, userdata, msg):
       v=str(msg.payload.decode('utf8'))   #string comes in as bytes so need to convert it
       time_t=datetime.datetime(int(t[0]), int(t[1]), int(t[2]), int(t[4]), int(t[5]), int(t[6]), int(t[7]))
       timestamp_utc = calendar.timegm(time_t.timetuple())
       print('Processing sample : ' + str(sample['value']))
       body='{ "device":"' + str(sample['device']) + '", "sample_date" : "' + time_t.strftime("%Y-%m-%d") + '", "value":"' + str(sample['value']) + '", "time":"' + repr(timestamp_utc) + '" }'
       secret = #b'Enter your MongoDB Stitch Webhook password here'
       hash =, body.encode("utf-8"), hashlib.sha256)
       url = #enter your Stitch Web API URL here
       header={"Content-Type":"application/json","X-Hook-Signature":"sha256=" + hash.hexdigest() }
       myResponse =,headers=header, data=body )
       print (myResponse.status_code)
    # uncomment to debug
    #def on_log(mqttc, obj, level, string)
    :#   print(string)print('Connecting to MQQT broker')
    client = mqtt.Client()
    client.on_message = on_message
    client.on_connect = on_connect
    #client.on_log = on_log
    client.connect(mqqt_server, mqqt_port, 60)

I’ll look to update this article soon, for now though, check the name of the Realm service under “Linked Data Sources”. It can be something other than "mongodb-atlas’ in that case you’ll have to update your function with the service name.

e.g. check the name of the service in the code

exports = function(payload,response) {
const mongodb =“mongodb-atlas”);

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.