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!!!")
|