ESP8266: Stop nach 99 Durchläufen

OP #4687607
Lesenswert?

Hallo,

ich habe einen ESP8266-01 mit ein paar DS1820. Der ESP liest die DS1820 
aus und senden dann die Daten via GET-Request an meinen Webspace. Leider 
funktioniert das nur 99 mal, d.h. nach 99 mal Senden (Variable heisst 
zaehler) hängt sich das Teil irgendwie auf.

Es wird ein outstring aufbereitet mit den Meßwerten der Sensoren, danach 
noch eine Variable angehängt, die die Anzahl der bisher erfolgten 
GET-Requests anzeigt.

Vielleicht sieht jemand einen Fehler?
1
zaehler=0   
2
  
3

4

5
tt=0
6
sk=net.createConnection(net.TCP, 0)
7
    sk:on("receive", function(sck, c) 
8
        print(c) 
9
        tt=tt+1
10
    end )
11
  
12
  
13
  
14

15
    ip="xx.xx.xx.xx"
16
    sk:connect(80,ip)
17
  
18
  
19
tmr.alarm(2,60000, 1, function() // jede Minute ausführen
20
  t=require("DS18B20")
21
    t.setup(4)
22
    addrs=t.addrs()
23
    -- Total DS18B20 numbers, assume it is 2
24
    print("Sensors found: "..table.getn(addrs))
25
  output=""..table.getn(addrs)
26
  for i=1, table.getn(addrs) do
27
    id=""
28
    for j = 1, string.len(addrs[i]) do      
29
      id = id..string.format("%02x",addrs[i]:byte(j))
30
    end
31
    output=output.."&"..string.format("%i=",i)..id.."&"..id.."="..t.read(addrs[i],t.C)
32
    print("Id["..i.."]="..id.." --> "..t.read(addrs[i],t.C))
33
    
34
  end
35
    
36

37
  sendstring = "GET /avrweb/esp8266.php?Module=ESP02".."&Anz="..output.."&Counter="..zaehler.." HTTP/1.1\r\n"  
38
  print("SendString="..sendstring.."\n")
39
    sk:send(sendstring.."Host: www.xyz.de\r\n".."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
40
  sk:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
41
  
42
  
43
  -- Don't forget to release it after use
44
    t = nil
45
    DS18B20 = nil
46
  
47
  if tt<2 then 
48
    wifi.sta.autoconnect(1)
49
    print("AUTOCONNECT")
50
    tt=0
51
  end
52

53
  zaehler=zaehler+1
54
  
55
  end)  // tmr.alarm
Gast #4689693
Lesenswert?

Die beiden sk:send() hintereinander werden auf Dauer nicht 
funktionieren. Entweder sie werden zu einem send zusammengefaßt, oder 
das 2. send muß in den callback vom ersten send. Ich zitier mal aus dem 
Link weiter unten:

> You must therefore use an Event-driven programming style in writing your
> ESP8266 Lua programs.

http://www.esp8266.com/wiki/doku.php?id=nodemcu-unofficial-faq

MfG Klaus
OP #4701462
Lesenswert?

So, habe noch einmal ein Redesign gemacht. Das Problem war wohl, dass 
die connection nicht geschlossen wurde. Ich vermute, dass hier intern 
bei der ESP-8266 Firmware eine Obergrenze definiert ist für offene 
Verbindungen.

Hier der Code:
1
t=require("DS18B20")
2

3
zaehler=0    
4

5
function gettemp()    
6
    t.setup(4) -- 4=pin of 1wire
7
    addrs=t.addrs()
8
    
9
    print("Sensors found: "..table.getn(addrs))
10
    output=""..table.getn(addrs)
11

12
    for i=1, table.getn(addrs) do
13

14
        id=""
15
        for j = 1, string.len(addrs[i]) do          
16
            id = id..string.format("%02x",addrs[i]:byte(j))
17
        end
18

19
        temperature=""..t.read(addrs[i],t.C)
20
        if (temperature==nil) then
21
          print("vor output temp=nil\n")
22
        end
23

24
        output=output.."&"..string.format("%i=",i)..id.."&"..id.."="..temperature
25
         
26
    end
27
end
28

29
function senddata()
30
  gettemp()
31

32
  print("Create sendstring \n")
33
        
34
  sendstring = "GET avrweb/esp8266.php?Module=ESP02".."&Anz="..output.."&Counter="..zaehler
35
                     .." HTTP/1.1\r\n"  
36
                     .."Host: localhost\r\n"                     
37
                     .."Accept: */*\r\n"
38
                     .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
39
                     .."\r\n"
40
  print("SendString="..sendstring.."\n")
41
  
42

43
  conn=net.createConnection(net.TCP, 0)
44
    
45
  conn:connect(80,"127.0.0.1")
46
  
47
  conn:on("connection", function (conn, payload)
48
        print("Connected ")
49
        zaehler=zaehler+1
50
        conn:send(sendstring)
51
        print(".. string was sent, counter = "..zaehler.."\n")      
52
        
53
  end)
54
        
55
  conn:on("sent", function(sck)
56
         print("Closing connection")
57
         conn:close()
58
  end)
59
  
60
  conn:on("receive", function(sck, payload) print(payload)  end)      
61
  
62
  conn:on("disconnection", function(sck)
63
     print("Got disconnection...")
64
  end)  
65
         
66
end
67
    
68
tmr.alarm(2,60000, 1, function() senddata() end)
Gast #4877749
Lesenswert?

Hallo Peter,
auf der Basis Deiner Arbeit versuche ich den ESP8266 mit meinem 
localhost zu verbinden. Im Appache access.log sehe ich die 
Verbindungsaufbau-Versuche. Meine localhost Umgebung 
C:\xampp\htdocs\esp8266\esp8266.php wird aber nicht erreicht. Hier mein 
sendstring:
sendstring = "GET 
esp8266/esp8266.php?Module=ESP02".."&Anz-DS18B20="..output.."&Messungen= 
"..zaehler
                     .." HTTP/1.1\r\n"
                     .."Host: xxx.xxx.xxx.20\r\n"
                     .."Accept: */*\r\n"
                     .."User-Agent: Mozilla/4.0 (compatible; esp8266 
Lua; Windows NT 5.1)\r\n"
                     .."\r\n"

Wenn ich vom Browser http://xxx.xxx.xxx.20:81//esp8266/esp8266.php 
localhost aufrufe erreiche ich mein esp8266.php problemlos.
Welche Funktion hat "Module=ESP02"?
Es wäre toll wenn mir jemand helfen könnte.

Beste Grüße Richard

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren