Can't import 'machine' module, tries to import fake_machine

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
jairov96
Posts: 4
Joined: Sat Aug 06, 2022 2:32 pm

Can't import 'machine' module, tries to import fake_machine

Post by jairov96 » Sat Aug 06, 2022 2:39 pm

Hello!

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 :P


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()
Any hints will be welcome. I've installed the firmware from https://micropython.org/download/esp32/, to be specific v1.19.1 (2022-06-18) .bin following the instructions on that same page.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Can't import 'machine' module, tries to import fake_machine

Post by Roberthh » Sat Aug 06, 2022 2:46 pm

This is a strange error. What dou you get when you type:

import machine

at the REPL prompt?
And how do you run the code?

jairov96
Posts: 4
Joined: Sat Aug 06, 2022 2:32 pm

Re: Can't import 'machine' module, tries to import fake_machine

Post by jairov96 » Sun Aug 07, 2022 11:48 am

Turns out it was an error of the IDE I was using, which was pymakr.

Really weird! When loading the exact same file using any other IDE, everything works as intended.

Post Reply