I'm already trying to figure out for a few days the hell is wrong with my environment.
I bought a board ESP-Wroom-32 and a temperature sensor, and I'm trying to make everything work.
Whenever I try to import the "machine" module, it returns "Importerror: no module named 'fake_machine' "
This is my whole main.py, if I would un-comment import machine, everything stops working. Otherwise, the code works perfectly. Feel free to roast it although that's out of the topic
Code: Select all
#import machine
import time
from time import sleep
import ntptime
import urequests
import ujson
from machine import I2C, Pin
import BMP280
import gc
import sys
# Functions
def setNtpTime():
# This sets the esp32 time to be accurate using Network Time Protocol.
ntptime.host = "1.europe.pool.ntp.org"
ntptime.settime()
del(ntptime.host)
gc.collect()
def turnOnBmp280():
try:
i2c = I2C(1,scl=Pin(22), sda=Pin(21), freq=10000)
bmp = BMP280.BMP280(i2c)
print("sensor connected")
return(bmp)
except Exception as e:
print("Error: ", e)
pass
def sendTemperatureToInfluxDB(configuration, temperature):
endpoint = configuration.get("influx_db_endpoint")
apiKey = configuration.get("influx_db_api_key")
headers = {}
headers['Authorization'] = 'Token %s' % apiKey
timestamp = time.time_ns() + 946684800000000000 #adds 30 years in nanoseconds
influxMeasurement = "temperature"
deviceName = configuration.get("device_name")
sensorName = configuration.get("sensor_name")
sensorLocation = configuration.get("sensor_location")
body = "{},device={},sensor={},location={} temperature={} {}".format(
influxMeasurement, deviceName, sensorName, sensorLocation, temperature, timestamp
)
del(influxMeasurement, deviceName, sensorName, sensorLocation, temperature, timestamp)
sendPostRequest(endpoint, headers, body)
pass
def sendPostRequest(url, headers, data):
#print(url, headers, data)
response = urequests.post(url, headers=headers, data=data)
if not response:
response ="Error sending post request. No response received"
pass
print(response.status_code)
del(response)
pass
# Main
def main():
configuration = ujson.loads(open('configuration.json').read())
submitInterval = configuration.get("submit_interval")
setNtpTime()
sensor = turnOnBmp280()
while sensor:
try:
sendTemperatureToInfluxDB(configuration, sensor.temperature)
print(sensor.temperature)
sleep(float(submitInterval))
gc.collect()
except Exception as e:
print("Error: ", e)
gc.collect()
#machine.reset()
main()