MakerPoll is a internet connected voting system which is based on nodeMCU (esp8266). User casts his vote on a breadboard (The Maker Way!), NodeMCU then records the data and send it to data.sparkfun.com. Then we visualize the raw data received by sparkfun.com on a webpage using AngularJS.
How To Make?You'll need the following components
Making Connections
We'll be using pins D1 & D2 for Hillary and Trump respectively. Both pins are pulled to GND using 2x 10K resistors. Then, we'll be using the 3.3v pin to to turn these pins HIGH as per the voter's choice.
Program the NodeMCU with the following code:
/* This Project is developed by Shivam Gautam (https://github.com/Shivamgautam1/Makerpoll/)
* Please note that we do not support politics nor any of these politicians
* For any queries contact me at shivamgautam1@gmail.com
*/
#include <ESP8266WiFi.h>
const char* ssid = "Your-SSID";// Your WiFi
const char* password = "SSID-Password";// The Password
const char* host = "data.sparkfun.com";// Host to send data
const char* streamId = "roaE2LAoD2TlA6ElNpEg";// This is my stream data.sparkfun.com/makerpolll, Please do not misuse.
const char* privateKey = "jkRlvb2kovCWrx1WDg1e";// Private Key for the stream
bool voteCasted = false;// Set the voteCaste initially be undone
int hillPin = D1;// Pin to connect for Hillary
int trumPin = D2;// Pin to connect for Trump
int hillary = 0;// Initial vote count
int trump = 0;// Initial vote count
// Creating a function to post data to sparkfun
void postPoll(int hillary, int trump){
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
}
// We now create a URI for the request
String url = "/input/";
url += streamId;
url += "?private_key=";
url += privateKey;
url += "&hillary=";
url += hillary;
url += "&trump=";
url += trump;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
pinMode(hillPin, INPUT);
pinMode(trumPin, INPUT);
}
void loop() {
delay(10000);
int hill = digitalRead(hillPin);// Read Hillary Pin
int trum = digitalRead(trumPin);// Read Trump Pin
if(!voteCasted){
//Check if vote is casted to Hillary
if(hill == HIGH && trum == LOW){
postPoll(1, 0);
voteCasted = true;// Vote has been casted set to True
}
//Check if vote is casted to Trump
else if(trum == HIGH && hill == LOW){
postPoll(0, 1);
voteCasted = true;// Vote has been casted set to True
}
else{
postPoll(0, 0);
}
}
}
Please Note that I have provided my Stream access details in the code, So that you don't have to create one. Please do not abuse my stream.
Enter your WiFi credentials & Upload the code to your nodeMCU.
Testing data being sent to data.sparkfun.com
HTML (Index.html)
<!DOCTYPE html>
<html lang="en" ng-app="makerPoll">
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="8">
<meta http-equiv="X-UA-Compatible">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Maker Poll</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384- BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<div class="page-header">
<h1 class="text-center">Welcome to Maker Poll</h1>
<h6 class="text-center">Please note that we neither support Politics nor any of these politicians</h6>
</div>
<div class="row" ng-controller="Result">
<div class="col-md-6">
<div class="thumbnail">
<img src="hillary.jpg">
<div class="caption">
<h3 ng-repeat="data in poll" class="text-center">{{ data.hillary }}</h3>
</div>
</div>
</div>
<div class="col-md-6">
<div class="thumbnail">
<img src="trump.jpg">
<div class="caption">
<h3 ng-repeat="data in poll" class="text-center">{{ data.trump }}</h3>
</div>
</div>
</div> </div>
</body>
</html>
Please note that images are in the root folder. You need to copy them from here https://shivamgautam1.github.io/Makerpoll/
Javascript (js/app.js)
angular.module('makerPoll', [])
.controller('Result', function($scope, $http) {
$http.get('http://data.sparkfun.com/output/roaE2LAoD2TlA6ElNpEg/latest.json?page=1').
then(function(response) {
$scope.poll = response.data;
});
});
NOTE: You need to host the pages on a server (Local/web hosted) in order to angularJS to work.
Testing the webpage:
And you're ready to cast your vote!
Feel free to contact me, if you face any problem with the code or wiring. Happy Voting :-)You can find the complete code for Arduino IDE and Webpage on my Github
Comments