Created
December 15, 2018 18:44
-
-
Save dbarrerap/cec2f5deaaa3b321426103c168efb6d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
// For I2C LCD: https://bitbucket.org/fmalpartida/new-liquidcrystal/overview | |
#include <Wire.h> | |
#include <LiquidCrystal_I2C.h> | |
#include <Bridge.h> | |
#include <BridgeClient.h> | |
#include <HttpClient.h> | |
#define LCD_I2C_ADDR 0x3F | |
const char *arduino_id = "YUN0"; // Variar al momento de carga en cada Arduino | |
const char *server = "192.168.1.116"; // Modificar al momento de configuracion de red | |
char buffer[64]; | |
BridgeClient bClient; | |
HttpClient hClient; | |
LiquidCrystal_I2C lcd(LCD_I2C_ADDR, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); | |
byte buttonNum[5] = {3, 4, 5, 6, 7}; | |
byte q = 1, a = 0; | |
void setup() { | |
Serial.begin(9600); | |
lcd.begin(20, 4); | |
lcd.backlight(); | |
lcd.home(); | |
lcd.print("Loading..."); | |
Bridge.begin(); | |
lcd.clear(); | |
lcd.home(); | |
lcd.print("Bridge ready..."); | |
delay(750); | |
for (int x = 0; x < 5; x++) | |
{ | |
pinMode(buttonNum[x], INPUT_PULLUP); | |
} | |
lcd.clear(); | |
lcd.home(); | |
lcd.print("Buttons ready..."); | |
delay(750); | |
} | |
void loop() { | |
// Show question | |
show_q(q); | |
// Wait for an answer | |
while (a == 0) | |
{ | |
a = getAnswer(); | |
} | |
Serial.print("Respuesta: "); | |
Serial.println(a); | |
// Send answer to server | |
send_answer(q, a); | |
a = 0; | |
} | |
// Get and show question in LCD | |
void show_q(int i) | |
{ | |
String url = String(server) + "/proyecto_iot/question.php?id=" + String(id, DEC); | |
hClient.get(url); | |
String question = ""; | |
while (hClient.available()) | |
{ | |
char c = hClient.read(); | |
question = question + c; | |
} | |
lcd.clear(); | |
lcd.home(); | |
lcd.print(question); | |
} | |
void change_q() | |
{ | |
q++; | |
} | |
int getAnswer() | |
{ | |
if (digitalRead(buttonNum[0]) == LOW) | |
{ | |
while (digitalRead(buttonNum[0]) == LOW) | |
{ | |
delay(10); | |
} | |
return 1; | |
} | |
else if (digitalRead(buttonNum[1]) == LOW) | |
{ | |
while (digitalRead(buttonNum[1]) == LOW) | |
{ | |
delay(10); | |
} | |
return 2; | |
} | |
else if (digitalRead(buttonNum[2]) == LOW) | |
{ | |
while (digitalRead(buttonNum[2]) == LOW) | |
{ | |
delay(10); | |
} | |
return 3; | |
} | |
else if (digitalRead(buttonNum[3]) == LOW) | |
{ | |
while (digitalRead(buttonNum[3]) == LOW) | |
{ | |
delay(10); | |
} | |
return 4; | |
} | |
else if (digitalRead(buttonNum[4]) == LOW) | |
{ | |
while (digitalRead(buttonNum[4]) == LOW) | |
{ | |
delay(10); | |
} | |
return buttonNum[4]; | |
} | |
else if (digitalRead(buttonNum[5]) == LOW) | |
{ | |
while (digitalRead(buttonNum[5]) == LOW) | |
{ | |
delay(10); | |
} | |
return 5; | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
// Sends answer to server | |
void send_answer(int q, int a) | |
{ | |
if (bClient.connect(server, 80)) | |
{ | |
sprintf(buffer, "POST /proyecto_iot/receiver.php HTTP/1.1"); | |
bClient.println(buffer); | |
sprintf(buffer, "Host: %s", server); | |
bClient.println(buffer); | |
bClient.println("Content-Type: application/json"); | |
sprintf(buffer, "{ \"ID\" : \"%s\", \"pregunta\" : \" %i \", \"respuesta\" : \" %i \" }", arduino_id, q, n); | |
bClient.print("Content-Length: "); | |
bClient.println(strlen(buffer)); | |
bClient.println(); | |
bClient.println(buffer); | |
} else { | |
lcd.clear(); | |
lcd.home(); | |
lcd.print("Connection failed"); | |
delay(2500); | |
} | |
a++; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment