Forum: Mikrocontroller und Digitale Elektronik ESP8266 - nodemcu - http.get() tut nicht wie erwartet


von Falk W. (dl3daz) Benutzerseite


Lesenswert?

In folgendem Programm landet "user_http_get_cb" immer im Teil mit 
"print("HTTP request failed.")"

Die Daten werden trotzdem hochgeladen: 
https://thingspeak.com/channels/109206

Was mache ich falsch?

(NodeMCU 1.5.1 build unspecified powered by Lua 5.1.4 on SDK 
1.5.1(e67da894))

1
function user_wifi_init( ssid, password )
2
  wifi.setmode(wifi.STATION)
3
  wifi.sta.config(ssid,password)
4
  wifi.sta.autoconnect(0)
5
  wifi.sta.eventMonReg(wifi.STA_GOTIP, user_cb_wifi_got_ip, "state")
6
  wifi.sta.eventMonStart()
7
  wifi.sta.connect()
8
end
9
10
function user_cb_wifi_got_ip( prev_state )
11
  ip = wifi.sta.getip()
12
  if ( ip )
13
  then
14
    print ("Got Ip: " .. ip )
15
    status, temp, humi, temp_dec, humi_dec = dht.read(7)
16
    vdd = adc.readvdd33(0)
17
    print ("DTH Temp: " .. temp .. " Humidity: " .. humi .. " VDD: " .. vdd)
18
    user_http_get(temp, humi, vdd)
19
  else
20
    print ("No IP!!!")
21
  end
22
end
23
24
-- Sende Daten an thingspeak
25
26
function user_http_get(temp, humi, vdd)
27
  api_key="XXXXXXXXXXXXXXXX"
28
  host="http://api.thingspeak.com"
29
30
  url=host .. "/update?api_key=XXXXXXXXXXXXXXXXX&field1=" .. temp .. "&field2=" .. humi .. "&field3=" .. vdd 
31
  hdr = "Content-Type: text/plain\r\n"
32
  print ("Send: " .. url)
33
  http.get( url, hdr, user_http_get_cb( code, data ))
34
  print ("Done!")
35
end
36
37
-- Callback for http_get()
38
39
function user_http_get_cb ( code, data )
40
  print ("user_http_get_cb!")
41
  if (code == nil) then
42
    print("HTTP request failed.")
43
  else
44
    print("user_http_get_cb Code:" .. code .. " Data: " .. data)
45
    wifi.sta.disconnect()
46
    node.dsleep(10 * 1000000)
47
  end
48
  gpio.write(6,0)
49
end
50
51
print ("Start!!!")
52
gpio.mode(6,gpio.OUTPUT)
53
gpio.write(6,1)
54
55
user_wifi_init("XXXX.1","XXXXXXXXXXXXXX")
56
57
print ("Ende!!!")

Gruß,
Falk

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.