Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266WiFiMulti.h>
// This code
// 1. Connects to a WiFi network
// 2. Loops:
// a. Makes an http request to a URL
// b. Parses the response, expecting an integer between 0 and 1000
// c. Outputs that on PIN D0 as a voltage between 0 and 3.6V
// d. Waits for 60 seconds
// Wifi settings are wifiMulti.addAP(ssid, password) below
WiFiClientSecure client;
ESP8266WiFiMulti wifiMulti;
// The code calls the path on the host below and
// expects a response with one line containing an integer
// between 0 and maxMetric.
const char* host = "xxxxxxxx.execute-api.eu-west-1.amazonaws.com";
const char* path = "/xxxx";
const int httpsPort = 443;
const int serialBaudRate = 115200;
const int delayWaitingForWifiConnection = 500;
const int delayBetweenUpdates = 60000;
int outputPIN = D0;
int metric = 0;
int maxMetric = 1000;
void setup() {
Serial.begin(serialBaudRate);
Serial.print("Connecting to wifi: ");
// You can add the id and password for several WiFi networks
// it will connect to the first one it sees.
wifiMulti.addAP("SSID 1", "password1");
wifiMulti.addAP("SSID 2", "password2");
while (wifiMulti.run() != WL_CONNECTED) {
delay(delayWaitingForWifiConnection);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected to ");
Serial.print(WiFi.SSID());
Serial.print(" with IP ");
Serial.println(WiFi.localIP());
}
void loop() {
if (client.connect(host, httpsPort)) {
client.print(
String("GET ")+path+" HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: AmmeterMonitor\r\n" +
"Connection: close\r\n\r\n"
);
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
metric = line.toInt();
} else {
Serial.println("Connection to host failed");
metric = 0;
}
Serial.println(metric);
int scaledMetric = (metric * PWMRANGE)/maxMetric;
Serial.println(scaledMetric);
analogWrite(outputPIN, scaledMetric);
delay(delayBetweenUpdates);
}
'use strict';
var AWS = require('aws-sdk');
var cloudwatch = new AWS.CloudWatch({apiVersion: '2010-08-01'});
var maximumValueForMetric = 10000.0;
function getMetric(callBack) {
var now = new Date();
var anHourAgo = new Date()
anHourAgo.setHours(now.getHours()-1);
var params = {
StartTime: anHourAgo,
EndTime: now,
MetricName: 'RequestCount',
Namespace: 'AWS/ELB',
Period: 60,
Statistics: [ 'Sum' ]
};
cloudwatch.getMetricStatistics(params, function(err, data) {
var sorted = data.Datapoints.sort(function(a,b) { return b.Timestamp - a.Timestamp; });
if (err) {
console.log(err, err.stack); // an error occurred
callBack(undefined);
} else {
callBack(sorted[0].Sum);
}
});
}
exports.handler = (event, context, callback) => {
function sendMetric(count) {
var response = {
statusCode: 200,
headers: {},
body: ""+Math.round((count/maximumValueForMetric)*1000)+"\n"
};
console.log("response: " + JSON.stringify(response))
context.succeed(response);
}
getMetric(sendMetric);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment