June 23, 2015

Instapush with NodeMCU (ESP8266)


Instapush is a service that allows you to send push notifications to your smart phone. Below is simple example of how to send a notifications from an ESP8266 running NodeMCU Firmware.

instapush.lua
appid = "APPLICATION_ID"
appsecret = "APPLICATION_SECRET"
key = "Tracker"
event = "Event"
val = "Value"


host = "api.instapush.im"
done = nil

if not wifi.sta.getip() then
print("Connecting to wifi")
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PASSWORD")
ip = wifi.sta.getip()
print(ip)
end

function upload() 
conn = net.createConnection(net.TCP, 0) 
conn:on("receive", 
function(conn, payload) 
success = true
print(payload)
end)
conn:on("disconnection", 
function(conn, payload)
print('\nDisconnected')
end)
conn:on("connection", 
function(conn, payload) 
print('\nConnected')
data = "event=" .. event .. "&" .. key .. "=" .. val
conn:send("POST /php HTTP/1.1\r\n"
.. "Host: " .. host .. "\r\n"
.. "X-INSTAPUSH-APPID: " .. appid .. "\r\n"
.. "X-INSTAPUSH-APPSECRET: " .. appsecret .. "\r\n"
.. "Content-Type: application/x-www-form-urlencoded\r\n"
.. "Content-Length: " .. string.len(data) .. "\r\n"
.. "Connection: Close\r\n\r\n"
.. data)
end)
print("Opening port")
conn:connect(80, host) 
end

function maintask()
        if not wifi.sta.getip() then
            print("Connecting to AP, Waiting...") 
        elseif not done then
            print("Uploading to server...")
            upload()
            done = 1
       end
end

maintask()
tmr.alarm(0, 1000, 1, maintask)


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.