Left here for archival purposes.

User avatar
By Pram
#10542 I am very new to this kind of stuff, but today I tried to make a wifi temperature web server. It works fine, but only when I am trying to connect to it via firefox or via my android phone by tiping in the address like 192.168.*.***. When I try to open the page with an ipad/iphone or safari it does not work and the message:"Safari can not open the page because the Internet connection has been interrupted" pops up. So what is the problem here?

My code:
init.lua:
Code: Select all-- andere Programme
t=require("ds18b20")

-- Variablen
ssid = "*****"
password = "*******"

temp = 0
 

ds18b20.setup(3)
 
-- Starte Verbindung zu Netzwerk
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid, password)
 
-- Wiederhole Schleife bis zur Verbindung
tmr.alarm(0, 1000, 1, function()
     if wifi.sta.getip() == nil then
          print("verbinde...")
     else
          print("IP: ", wifi.sta.getip())
          tmr.stop(0) -- Stoppe Schleife
          main() -- Starte Hauptprogramm
     end
end)
 
-- Hauptprogramm
function main()
     server = net.createServer(net.TCP) -- Erstelle TCP Server
     server:listen(80, function(connection) -- Server reagiert auf Port 80
          connection:on("receive", function(connection, payload) -- Verbindung reagiert auf empfangene Daten
 
               print(payload) -- Empfangene Daten ausgeben

                    temp=t.read(nil,t.C)
                    temp=t.read(nil,t.C)
                    print(temp)
                    connection:send("<p>Temperaturanzeige</br>Temperatur: "..temp.."</p>")
 
               connection:close() -- Verbindung trennen
          end)
     end)
end


ds18b20.lua:
Code: Select all--------------------------------------------------------------------------------
-- DS18B20 one wire module for NODEMCU
-- NODEMCU TEAM
-- LICENCE: http://opensource.org/licenses/MIT
-- Vowstar <vowstar@nodemcu.com>
--------------------------------------------------------------------------------

-- Set module name as parameter of require
local modname = ...
local M = {}
_G[modname] = M
--------------------------------------------------------------------------------
-- Local used variables
--------------------------------------------------------------------------------
-- DS18B20 dq pin
local pin = nil
-- DS18B20 default pin
local defaultPin = 9
--------------------------------------------------------------------------------
-- Local used modules
--------------------------------------------------------------------------------
-- Table module
local table = table
-- String module
local string = string
-- One wire module
local ow = ow
-- Timer module
local tmr = tmr
-- Limited to local environment
setfenv(1,M)
--------------------------------------------------------------------------------
-- Implementation
--------------------------------------------------------------------------------
C = 0
F = 1
K = 2
function setup(dq)
  pin = dq
  if(pin == nil) then
    pin = defaultPin
  end
  ow.setup(pin)
end

function addrs()
  setup(pin)
  tbl = {}
  ow.reset_search(pin)
  repeat
    addr = ow.search(pin)
    if(addr ~= nil) then
      table.insert(tbl, addr)
    end
    tmr.wdclr()
  until (addr == nil)
  ow.reset_search(pin)
  return tbl
end

function readNumber(addr, unit)
  result = nil
  setup(pin)
  flag = false
  if(addr == nil) then
    ow.reset_search(pin)
    count = 0
    repeat
      count = count + 1
      addr = ow.search(pin)
      tmr.wdclr()
    until((addr ~= nil) or (count > 100))
    ow.reset_search(pin)
  end
  if(addr == nil) then
    return result
  end
  crc = ow.crc8(string.sub(addr,1,7))
  if (crc == addr:byte(8)) then
    if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
      -- print("Device is a DS18S20 family device.")
      ow.reset(pin)
      ow.select(pin, addr)
      ow.write(pin, 0x44, 1)
      -- tmr.delay(1000000)
      present = ow.reset(pin)
      ow.select(pin, addr)
      ow.write(pin,0xBE,1)
      -- print("P="..present)
      data = nil
      data = string.char(ow.read(pin))
      for i = 1, 8 do
        data = data .. string.char(ow.read(pin))
      end
      -- print(data:byte(1,9))
      crc = ow.crc8(string.sub(data,1,8))
      -- print("CRC="..crc)
      if (crc == data:byte(9)) then
        if(unit == nil or unit == C) then
          t = (data:byte(1) + data:byte(2) * 256) * 625
        elseif(unit == F) then
          t = (data:byte(1) + data:byte(2) * 256) * 1125 + 320000
        elseif(unit == K) then
          t = (data:byte(1) + data:byte(2) * 256) * 625 + 2731500
        else
          return nil
        end
        t = t / 10000
        -- print("Temperature="..t1.."."..t2.." Centigrade")
        -- result = t1.."."..t2
        return t
      end
      tmr.wdclr()
    else
    -- print("Device family is not recognized.")
    end
  else
  -- print("CRC is not valid!")
  end
  return result
end

function read(addr, unit)
  t = readNumber(addr, unit)
  if (t == nil) then
    return nil
  else
    return t
  end
end

-- Return module table
return M
User avatar
By dumenicu
#10776 Hello Pram,
I get exactly the same problem....
I use NodeMCU 0.9.5 build 20150213 powered by Lua 5.1.4 I'm thinking about changing back to 0.9. whatever .
I tell you more when done.
User avatar
By dpwhittaker
#11258 Does it help if you put connection:close inside the connection:on("sent", callback)?

Safari may also be expecting some headers. Try at least connection:send("Content-Type: text/html\r\n\r\n") before you send the result.