Skip to content

Instantly share code, notes, and snippets.

@macegr
Created November 9, 2016 01:17
Show Gist options
  • Save macegr/e795181e04f81e0eefa406d5e6b5f519 to your computer and use it in GitHub Desktop.
Save macegr/e795181e04f81e0eefa406d5e6b5f519 to your computer and use it in GitHub Desktop.
ESP8266 vote meter example with JSON
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
// WiFi network parameters
const char* ssid = "<your SSID>";
const char* password = "<your passphrase>";
void setup() {
pinMode(1, OUTPUT);
//Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
}
void parsePayload(String payload) {
DynamicJsonBuffer jsonbuffer;
JsonObject& root = jsonbuffer.parseObject(payload);
if (root.success()) {
String dElectoral = root["dElectoral"];
String rElectoral = root["rElectoral"];
float D = dElectoral.toInt();
float R = rElectoral.toInt();
float T = D + R;
float ratio = 0.5;
if (T != 0) ratio = R/T;
int meterValue = 1023 * ratio;
analogWrite(1, meterValue);
}
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("http://35.162.231.64/election2016.json");
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
parsePayload(payload);
}
}
http.end();
}
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment