Chat freely about the open source Javascript projects for ESP8266

User avatar
By jankop
#32053 Client for esp8266 embeded espruino and thinkspeak.com, with barometric sensor BMP85/180.

Code: Select allvar ssid = "YourSSID";
var pass = "YourPassword";
var ApiKey = 'YourApiKey';
var altitude = 188; // current altitude in m
var mode = 2; //resolution of sensor conversion (0-3)
var afdt = 1; //present numbers after dot
var press = 0; //pressure
var temp = 0; //temperature
var peri = 15; //period of reading sensor in sec.
var cl = 0; // SCL to GPIO0
var da = 2; // SDA to GPIO2

console.log('connecting to wifi');
ESP8266WiFi.init();
ESP8266WiFi.connect(ssid, pass, function() {
    console.log('connected to wifi');
    var ipInfo = ESP8266WiFi.getIPInfo();
    print("Current IP address is: " +
        ESP8266WiFi.getAddressAsString(ipInfo.ip) + ':' +
        ESP8266WiFi.getAddressAsString(ipInfo.netmask));
});


function aftDot(number, ad) //Formatting numbers.
{
    return (number.toString()).substr(0, (number.toString()).indexOf('.') + 1 + ad);
}

function readBMP() //read BMP85 or BMP180 sensor
{
    var bmp = require("BMP085").connect(I2C1, mode);
    bmp.getPressure(function(d) {
        press = aftDot(bmp.getSeaLevel(d.pressure, altitude) / 100, afdt);
        temp = aftDot(d.temperature, afdt);
        console.log("\nPressure at sea level: " + press + " hPa");
        console.log("Temperature: " + temp + " C");
    });
    return;
}

function sendTs(press, temp) {
    var http = require("http");
    http.get({
            host: "184.106.153.149",
            port: 80,
            path: '/update?key=' + ApiKey + '&field3=' + press + '&field4=' + temp
        },
        function(response) {
            print("get callback!");
            response.on('data', function(data) {
                print(data);
            });
            response.on('close', function() {
                print("The response connection closed");
            });
        }
    );
}
I2C1.setup({
    scl: cl,
    sda: da
});

function repeat() {
    readBMP(), sendTs(press, temp), setTimeout(repeat, peri * 1000);
}
repeat();