#include<Servo.h>
#include<Ethernet.h>
#include <SPI.h>
Servo myservo;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "ramp-rampcontrol.rhcloud.com";
EthernetClient client;
IPAddress ip(192,168,1,17);
int button_5 = 5;
int button_6 = 6;
const int ledPin = 3;
const int ledPinG = 2;
int time = 5*1000;
int val=0;
String content = "";
boolean begin1 = false;
void setup() {
Serial.begin(9600);
pinMode(button_5, INPUT);
pinMode(button_6, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPinG, OUTPUT);
myservo.attach(9);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /api/actions?parking_id=39 HTTP/1.1");
client.println("Host: ramp-rampcontrol.rhcloud.com");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
val = digitalRead(button_5);
if (val == HIGH)
{
performServoAction("up");
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
val = digitalRead(button_6);
if (val == HIGH)
{
performServoAction("down");
digitalWrite(ledPinG, HIGH);
delay(1000);
digitalWrite(ledPinG, LOW);
delay(1000);
}
while(client.available()) {
char c = client.read();
if(c == '{'){
begin1 = true;
}
if (begin1) {
content += c;
}
if (c == '}') {
break;
}
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
performAction();
}
}
bool performServoAction(String action_type) {
if (action_type == "up") {
Serial.println("doing up");
myservo.write(95);
delay(time);
// lastRampStatus = action_type ;
// notifyServerAboutAction("true", action_id);
return true;
}
if (action_type == "down") {
Serial.println("doing down");
myservo.write(135);
delay(time);
//lastRampStatus = action_type;
// notifyServerAboutAction("true", action_id);
return true;
}
}
void performAction() {
int nstart = content.indexOf("action_id") + 12;
int nend = content.indexOf(",", nstart);
String actionID = content.substring(nstart, nend);
int nstart1 = content.indexOf("action_type") + 14;
//Get position of "action_type" in a string, increment by 13
(length of 'action_type":"')
int nend1 = content.indexOf("\"", nstart1); // Get next position
of '"' starting from nstart
String actionType = content.substring(nstart1, nend1);
//Return substring between this position
performServoAction(actionType);
Serial.println(nstart1);
Serial.println(nend1);
Serial.println(actionID);
Serial.println(actionType);
Serial.println(content);
}
#include<Servo.h>
#include<Ethernet.h>
#include <SPI.h>
Servo myservo;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "ramp-rampcontrol.rhcloud.com";
EthernetClient client;
IPAddress ip(192,168,1,17);
int button_5 = 5;
int button_6 = 6;
const int ledPin = 3;
const int ledPinG = 2;
int time = 5*1000;
int val=0;
String content = "";
boolean begin1 = false;
void setup() {
Serial.begin(9600);
pinMode(button_5, INPUT);
pinMode(button_6, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPinG, OUTPUT);
myservo.attach(9);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /api/actions?parking_id=39 HTTP/1.1");
client.println("Host: ramp-rampcontrol.rhcloud.com");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
val = digitalRead(button_5);
if (val == HIGH)
{
performServoAction("up");
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
val = digitalRead(button_6);
if (val == HIGH)
{
performServoAction("down");
digitalWrite(ledPinG, HIGH);
delay(1000);
digitalWrite(ledPinG, LOW);
delay(1000);
}
while(client.available()) {
char c = client.read();
if(c == '{'){
begin1 = true;
}
if (begin1) {
content += c;
}
if (c == '}') {
break;
}
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
performAction();
}
}
bool performServoAction(String action_type) {
if (action_type == "up") {
Serial.println("doing up");
myservo.write(95);
delay(time);
// lastRampStatus = action_type ;
// notifyServerAboutAction("true", action_id);
return true;
}
if (action_type == "down") {
Serial.println("doing down");
myservo.write(135);
delay(time);
//lastRampStatus = action_type;
// notifyServerAboutAction("true", action_id);
return true;
}
}
void performAction() {
int nstart = content.indexOf("action_id") + 12;
int nend = content.indexOf(",", nstart);
String actionID = content.substring(nstart, nend);
int nstart1 = content.indexOf("action_type") + 14;
//Get position of "action_type" in a string, increment by 13
(length of 'action_type":"')
int nend1 = content.indexOf("\"", nstart1); // Get next position
of '"' starting from nstart
String actionType = content.substring(nstart1, nend1);
//Return substring between this position
performServoAction(actionType);
Serial.println(nstart1);
Serial.println(nend1);
Serial.println(actionID);
Serial.println(actionType);
Serial.println(content);
}