Hi
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
mqqt_port=1883
def on_connect(mqttc, obj, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("/iotdemo/temp")
# 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
sample=json.loads(v)
t=str(sample['time'])
t=t[1:]
t=t[:-1]
t=t.split(",")
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 = hmac.new(secret, 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 = requests.post(url,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)
client.loop_forever()