kaluma-project / kaluma

A tiny JavaScript runtime for RP2040 (Raspberry Pi Pico)
https://kalumajs.org
Apache License 2.0
648 stars 37 forks source link

http or tcp server not opening #597

Closed pclokcer closed 1 year ago

pclokcer commented 1 year ago
const ssid = 'test'
const pwd = 'testtest'

const { WiFi } = require('wifi')
const wifi = new WiFi()

wifi._dev.ap_mode({
  ssid: ssid,
  password: pwd,
}, (a) => {
  if (a === 0) {
    console.log('access point started')
    require('./http')()
  } else {
    console.log('access point stoped')
  }
})

http.js

const http = require('http')

module.exports = () => {
  var message = '<h1>Hello</h1>'
  var port = 80

  var server = http.createServer((req, res) => {
    console.log('Request path: ' + req.url)
    res.writeHead(200, 'OK', {
      'Content-Type': 'text/html',
      'Content-Length': message.length,
    })
    res.write(message)
    res.end()
  })

  try {
    server.listen(port, function () {
      console.log('HTTP server listening on port: ' + port)
      require('./led')()
    })
  } catch (error) {
    console.log('http ->', error)
  }
}

giving error on http.js. error is tcp -> Error: WiFi is not connected. How can I fix this problem ?

pclokcer commented 1 year ago

I fixed that like

const ssid = 'test'
const pwd = 'testtest'

const { WiFi } = require('wifi')
const wifi = new WiFi()

wifi._dev.ap_mode({
  ssid: ssid,
  password: pwd,
}, (a) => {
  if (a === 0) {
    console.log('access point started')

    wifi.connect((err, info) => {
      if (err) {
        console.error(err)
      } else {
        require('./http')()
      }
    })
  } else {
    console.log('access point stoped')
  }
})