TypeError: can't convert function to int

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
jayjoshi
Posts: 1
Joined: Thu May 27, 2021 6:24 am

TypeError: can't convert function to int

Post by jayjoshi » Thu May 27, 2021 6:29 am

Hello there, I want to make a program in ESP8266 in which I will controll an ON BOARD LED with a webpage as well as My ESP8266 will send a Message to the WEBPAGE whenever I press button connected with GPIO 5 using Interrupt. But i am having Error.
The Error :

Code: Select all

Connection successful
('192.168.43.150', '255.255.255.0', '192.168.43.72', '192.168.43.72')
Traceback (most recent call last):
  File "<stdin>", line 80, in <module>
TypeError: can't convert function to int
The Code :

Code: Select all

try:
  import usocket as socket
except:
  import socket
  
from time import sleep
import urequests
from machine import Pin,Timer
import network

import esp
esp.osdebug(None)

import gc
gc.collect()

ssid = 'Memesterr'
password = 'asdfghjkl'

station = network.WLAN(network.STA_IF)

station.active(True)
station.connect(ssid, password)

while station.isconnected() == False:
  pass
# Complete project details at https://RandomNerdTutorials.com
print('Connection successful')
print(station.ifconfig())

led = Pin(2, Pin.OUT)
button=Pin(5,Pin.IN)

def web_page():

  if led.value() == 1:
    state="ON"
  elif led.value()==0:
    state="OFF"

  html = """<html>
    <head>
    <title>ESP Web Server</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body> <h1>ESP Web Server</h1> 
    <p>LED is <strong>""" + state + """</strong></p><p>
    <form>
        <br><button name="LED" value="0" type="submit">ON</button>
        <br><button name="LED" value="1" type="submit">OFF</button>
    </form>
    </body>
    </html>"""
  return html

def req_page():
  html="""<html>
  <head>
  <title>ESP Web Server</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body> <h1>REQUEST</h1> 
  <p>Request From ESP8266</p>

  </body>
  </html>
  """
  return html


def SendPage(self):
    SendState=True
SendState=False

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 80))
s.listen(5)

button.irq(machine.Pin.IRQ_FALLING,SendPage)

while True:
  conn, addr = s.accept()
  print('Got a connection from %s' % str(addr))
  request = conn.recv(1024)
  request = str(request)
  print('Content = %s' % request)
  led_on = request.find("/?LED=0")
  led_off = request.find("/?LED=1")
  
  #led_on.irq(request)

  if SendState==False:
     
    if led_on == 6:
      print('LED ON')
      led.value(1)
          
    elif led_off == 6:
      print('LED OFF')
      led.value(0)
          
    response = web_page()
    conn.send('HTTP/1.1 200 OK\n')
    conn.send('Content-Type: text/html\n')
    conn.send('Connection: close\n\n')
    conn.sendall(response)
    #print("response",response)
    conn.close()

  elif SendState==True:
    req=req_page()
    conn.sendall(req)
    conn.close()
Also pls tell me that wheater this Code is Correct or Not

rizarefaldi
Posts: 2
Joined: Wed Jun 02, 2021 6:27 pm

Re: TypeError: can't convert function to int

Post by rizarefaldi » Wed Jun 02, 2021 6:32 pm

jayjoshi wrote:
Thu May 27, 2021 6:29 am
Hello there, I want to make a program in ESP8266 in which I will controll an ON BOARD LED with a webpage as well as My ESP8266 will send a Message to the WEBPAGE whenever I press button connected with GPIO 5 using Interrupt a4. But i am having Error.
The Error :

Code: Select all

Connection successful
('192.168.43.150', '255.255.255.0', '192.168.43.72', '192.168.43.72')
Traceback (most recent call last):
  File "<stdin>", line 80, in <module>
TypeError: can't convert function to int
The Code :

Code: Select all

try:
  import usocket as socket
except:
  import socket
  
from time import sleep
import urequests
from machine import Pin,Timer
import network

import esp
esp.osdebug(None)

import gc
gc.collect()

ssid = 'Memesterr'
password = 'asdfghjkl'

station = network.WLAN(network.STA_IF)

station.active(True)
station.connect(ssid, password)

while station.isconnected() == False:
  pass
# Complete project details at https://RandomNerdTutorials.com
print('Connection successful')
print(station.ifconfig())

led = Pin(2, Pin.OUT)
button=Pin(5,Pin.IN)

def web_page():

  if led.value() == 1:
    state="ON"
  elif led.value()==0:
    state="OFF"

  html = """<html>
    <head>
    <title>ESP Web Server</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body> <h1>ESP Web Server</h1> 
    <p>LED is <strong>""" + state + """</strong></p><p>
    <form>
        <br><button name="LED" value="0" type="submit">ON</button>
        <br><button name="LED" value="1" type="submit">OFF</button>
    </form>
    </body>
    </html>"""
  return html

def req_page():
  html="""<html>
  <head>
  <title>ESP Web Server</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body> <h1>REQUEST</h1> 
  <p>Request From ESP8266</p>

  </body>
  </html>
  """
  return html


def SendPage(self):
    SendState=True
SendState=False

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 80))
s.listen(5)

button.irq(machine.Pin.IRQ_FALLING,SendPage)

while True:
  conn, addr = s.accept()
  print('Got a connection from %s' % str(addr))
  request = conn.recv(1024)
  request = str(request)
  print('Content = %s' % request)
  led_on = request.find("/?LED=0")
  led_off = request.find("/?LED=1")
  
  #led_on.irq(request)

  if SendState==False:
     
    if led_on == 6:
      print('LED ON')
      led.value(1)
          
    elif led_off == 6:
      print('LED OFF')
      led.value(0)
          
    response = web_page()
    conn.send('HTTP/1.1 200 OK\n')
    conn.send('Content-Type: text/html\n')
    conn.send('Connection: close\n\n')
    conn.sendall(response)
    #print("response",response)
    conn.close()

  elif SendState==True:
    req=req_page()
    conn.sendall(req)
    conn.close()
Also pls tell me that wheater this Code is Correct or Not
getting the same issue. Did you fix this problem? please update the thread if you have the solution.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: TypeError: can't convert function to int

Post by dhylands » Wed Jun 02, 2021 6:47 pm

If I'm reading this correctly, line 80 is this line:

Code: Select all

button.irq(machine.Pin.IRQ_FALLING,SendPage)
This tells me that button.irq is expecting the second argument to be an int and you're passing in a function (SendPage).

Looking at the documentation: http://docs.micropython.org/en/latest/l ... ne.Pin.irq it shows that the handler should be the first argument and the trigger should the second, so you either need to name your arguments:

Code: Select all

button.irq(trigger=machine.Pin.IRQ_FALLING, handler=SendPage)
or reorder them:

Code: Select all

button.irq(SendPage, machine.Pin.IRQ_FALLING)
Disclaimer: I don't sue the esp8266 so I'm basing my answer purely off of the error and the docs.

lesmanaharmonic
Posts: 2
Joined: Mon Jul 05, 2021 10:09 am

Re: TypeError: can't convert function to int

Post by lesmanaharmonic » Mon Jul 05, 2021 10:14 am

Hi all,
i just wanna to ask what is the function of this number '6' of these script

if led_on == 6:
print('LED ON')

Thanx for answer

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

Re: TypeError: can't convert function to int

Post by Roberthh » Mon Jul 05, 2021 10:22 am

If you look into the script where the value of led_on is set, you'll find:

led_on = request.find("/?LED=0")

So led_on tells the position of the string "/?LED=0" in the string request. It is expected to be at the position 6, which is the 7th character of the string request.

lesmanaharmonic
Posts: 2
Joined: Mon Jul 05, 2021 10:09 am

Re: TypeError: can't convert function to int

Post by lesmanaharmonic » Wed Jul 07, 2021 8:41 am

Roberthh wrote:
Mon Jul 05, 2021 10:22 am
If you look into the script where the value of led_on is set, you'll find:

led_on = request.find("/?LED=0")

So led_on tells the position of the string "/?LED=0" in the string request. It is expected to be at the position 6, which is the 7th character of the string request.
Thanks Robert for explanations..

Post Reply