Skip to content

Commit

Permalink
Implementação 24-10
Browse files Browse the repository at this point in the history
Implementações de ajuste em tela e comunicação com serviço REST.
  • Loading branch information
plollaton committed Oct 25, 2017
1 parent f570680 commit a6305ef
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 38 deletions.
6 changes: 6 additions & 0 deletions .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"board": "arduino:avr:nano",
"configuration": "cpu=atmega328",
"port": "COM8",
"sketch": "MedidorControlador\\MedidorControlador.ino"
}
19 changes: 19 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino"
],
"browse": {
"limitSymbolsToIncludedHeaders": false,
"path": [
"C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino",
"${workspaceRoot}"
]
},
"intelliSenseMode": "msvc-x64"
}
],
"version": 3
}
67 changes: 44 additions & 23 deletions MedidorComunicador/MedidorComunicador.ino
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#include <ESP8266HTTPClient.h>

#include <ESP8266WiFi.h>

String inputString;
const char* ssid = "FAST3184-567C";
const char* password = "LCNTJVJV1WHQ";
const char* ssid = "GVT9865";
const char* password = "12345678";
const int LED_BLOQUEADO = 0;
const int LED_LIBERADO = 2;
HTTPClient http;
int areaSelecionada = -1;
int votoSelecionado = -1;
bool bBloqueado;

void bloquearMedidor(boolean pbBloquear){
digitalWrite(LED_BLOQUEADO, pbBloquear);
digitalWrite(LED_LIBERADO, !pbBloquear);
String b;
b+='B';
b+=pbBloquear;
b+='\n';
Serial.write(b.c_str());
bBloqueado = pbBloquear;
}

void limparVariaveis(){
Expand All @@ -25,32 +31,36 @@ void setup() {
pinMode(LED_BLOQUEADO, OUTPUT);
pinMode(LED_LIBERADO, OUTPUT);
Serial.begin(115200);
bloquearMedidor(true);
WiFi.begin(ssid, password);
bloquearMedidor(true);
while(WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Conectando...");
}
bloquearMedidor(false);
bloquearMedidor(false);
}

void loop() {
if (Serial.available()){
digitalWrite(LED_BLOQUEADO, HIGH);
char inChar=(char)Serial.read();
inputString+=inChar;
if (inChar == '&'){
if(inputString.lastIndexOf("I") >= 0){
carregarDadosAreas();
delay(1000);
limparVariaveis();
} else if (inputString.lastIndexOf("A") >= 0) {
areaSelecionada = inputString.substring(1).toInt();
limparVariaveis();
Serial.println(inputString);
if (inChar == '$'){
if (inputString.lastIndexOf("A") >= 0) {
areaSelecionada = inputString.substring(1,2).toInt();
} else if (inputString.lastIndexOf("V") >= 0) {
votoSelecionado = inputString.substring(1).toInt();
limparVariaveis();
votoSelecionado = inputString.substring(1,2).toInt();
} else if (inputString.lastIndexOf("S")>=0){
retornarStatusConexaoWiFi();
}
else {
digitalWrite(LED_BLOQUEADO, HIGH);
}
limparVariaveis();
}
}
else{
limparVariaveis();
}
if((areaSelecionada >=0) && (votoSelecionado > 0)){
gravarDadosVoto();
Expand All @@ -60,17 +70,28 @@ void loop() {
delay(100);
}

void carregarDadosAreas(){
bloquearMedidor(true);
http.begin("http://jsonplaceholder.typicode.com/users/1");
int httpCode = http.GET();
String payload = http.getString();
Serial.println(payload);
bloquearMedidor(false);
void retornarStatusConexaoWiFi(){
Serial.write(WiFi.status());
Serial.println(WiFi.status());
}

void gravarDadosVoto(){
bloquearMedidor(true);



http.begin("http://demo8844204.mockable.io/voto"); //Specify request destination
http.addHeader("Content-Type", "app/json"); //Specify content-type header

int httpCode = http.POST("{"area":1,"voto":2}"); //Send the request
String payload = http.getString(); //Get the response payload

Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload

http.end();


Serial.println("Gravando dados...");
bloquearMedidor(false);
}
144 changes: 129 additions & 15 deletions MedidorControlador/MedidorControlador.ino
Original file line number Diff line number Diff line change
@@ -1,20 +1,134 @@
bool bProcessando = false;
void setup()
{
Serial.begin(115200);
#include <LCD5110_Basic.h>

/*Cria objeto da classe LCD5110
CLK – Pino 9
DIN – Pino 8
DC – Pino 7
RST – Pino 5
CE – Pino 6
*/
LCD5110 tela(9,8,7,5,6);

//Obtendo as fontes
extern uint8_t SmallFont[];
extern uint8_t MediumNumbers[];
extern uint8_t BigNumbers[];
int area = 0;
int numAreas=8;
int AREACIMA = 2;
int AREABAIXO = 3;
int BTN_VOTE_1 = A1;
int BTN_VOTE_2 = A2;
int BTN_VOTE_3 = A3;
int BTN_VOTE_4 = A4;
int BTN_VOTE_5 = A5;
bool should_write = true;



char *areas[] = {"Anymarket", "Consignet", "Holding", "IT Services", "TechGov", "Edig", "Gestao Emp.", "Gileade"};

void setup() {
tela.InitLCD();
tela.setFont(SmallFont);
pinMode(AREACIMA, INPUT_PULLUP);
pinMode(AREABAIXO, INPUT_PULLUP);
pinMode(BTN_VOTE_1, INPUT_PULLUP);
pinMode(BTN_VOTE_2, INPUT_PULLUP);
pinMode(BTN_VOTE_3, INPUT_PULLUP);
pinMode(BTN_VOTE_4, INPUT_PULLUP);
pinMode(BTN_VOTE_5, INPUT_PULLUP);
Serial.begin(9600);
}

void loop() {
montarMenu();
handleUp();
handleDown();
/* handleVoteButton(BTN_VOTE_1, 1);
handleVoteButton(BTN_VOTE_2, 2);
handleVoteButton(BTN_VOTE_3, 3);
handleVoteButton(BTN_VOTE_4, 4);
handleVoteButton(BTN_VOTE_5, 5);*/
}

void handleVoteButton(int btn, int value) {
if (digitalRead(btn) == LOW) {
Serial.print("Area = ");
Serial.println(areas[area]);
Serial.print("Voto = ");
Serial.println(value);
Serial.println("***DEBUG***");
Serial.print("idArea: ");
Serial.println(area);

int nLinha = area + 1;
if(nLinha >= 6){
nLinha = 5;
}
tela.clrRow(nLinha);
Serial.print("nLinha: ");
Serial.println(nLinha);
Serial.println("***END DEBUG***");
Serial.println("");
area = 0;

delay(5000);
}
}

void loop()
{
if(!bProcessando){
digitalWrite(13, !digitalRead(13));
delay(2000);
void handleUp() {
if(!digitalRead(AREACIMA)){
area++;
while(digitalRead(AREACIMA) == LOW){
}
tela.clrRow(area);
if(area > 6){
tela.clrRow(5);
area = 0;
}
should_write = true;
}
}

void serialEvent(){
char inChar=(char)Serial.read();
Serial.println(inChar);
bProcessando = inChar=='1';
Serial.println(bProcessando);
}
void handleDown() {
if(!digitalRead(AREABAIXO)){
area--;
while(digitalRead(AREABAIXO) == LOW){
}
if(area < 0){
tela.clrRow(area - 1);
area = 6;
} else {
tela.clrRow(area+ 2);
}
should_write = true;
}
}

void montarMenu(){
if (!should_write) {
return;
}

tela.clrScr();
tela.print("Selecione:", 0, 0);

int pos = 9;
for(int i=0; i<numAreas; i++){

if((area-i) > 4){
continue;
}

if(i==area){
tela.print("<", 0, pos);
tela.print(areas[i], 7,pos);
tela.print(">", 7 + (6 * strlen(areas[i])), pos);
} else {
tela.print(areas[i], 0, pos);
}
pos = pos + 9;
}
should_write = false;
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# MedidorFelicidade
Projeto arduino do medidor.

#Protocolo de comunicação
\n - fim de string.
A<N> - Registrar a area que será computado o voto.
V<N> - Registrar o voto para a área previamente registrada.
S - Status da conexão WIFI.

0 comments on commit a6305ef

Please sign in to comment.