Hai, can anybody please help me check out this program code, there are no error but the GET data cannot be retrieve. It din't show out in the Serial Monitor although I already set it to be print out. Thank you.
#include <ArduinoJson.h>
#include <SPI.h>
#include <Bridge.h>
#include <YunClient.h>
#include <ArduinoJson.h>
#include <HttpClient.h>
#include <Process.h>
//include the library code
#include <LiquidCrystal.h>
//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd (12,11,5,4,3,2);
//Yun Client client;
YunClient client;
//char buffer[64];
int LIGHT_GREEN_1= 13;
int LIGHT_RED_1= 7;
int i;
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
boolean connecting = false;
char httpResponse[150];
int charIndex = 0;
bool bodyStarted = false;
void setup() {
Serial.begin(9600);
Serial.println("Starting Bridge");
Bridge.begin();
pinMode (motion_1,INPUT);
pinMode (LIGHT_RED_1, OUTPUT);
pinMode (LIGHT_GREEN_1, OUTPUT);
lcd.clear();
lcd.setCursor(0,0);
delay(1500);
lcd.print("Initializing.....");
delay(1500);
lcd.begin(16,2);
}
void loop()
{
digitalWrite (LIGHT_GREEN_1,HIGH);
digitalWrite (LIGHT_RED_1,LOW);
lcd.setCursor(0,0);
lcd.print("No Instructions");
delay(1500);
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
Serial.println("client available");
char c = client.read();
Serial.print(c); //Add code
// The JSON parser isn't happy with the surrounding [ and ] brackets, so we'll grab the inner contents of these from the HTTP reponse body
if (c == ']' && bodyStarted)
{
bodyStarted = false;
}
if (bodyStarted)
{
httpResponse[charIndex] = c;
charIndex++;
}
if (c == '[')
{
bodyStarted = true;
}
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
Serial.println("http response");
Serial.println(httpResponse);
// create a JSON buffer, we know it won't exceed 150 bytes
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(httpResponse);
if (root.success())
{
Serial.println("root success!");
const char* eventdata = root["Text"];
String strEventData = eventdata;
int rColour = strEventData.toInt();
Serial.println(eventdata);
if(rColour=='2')
{
digitalWrite (LIGHT_RED_1,HIGH);
delay(500);
digitalWrite (LIGHT_RED_1,HIGH);
delay(500);
}
else
{
}
}
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
Serial.println("making http request");
httpRequest();
}
// store the state of the connection for next time through the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void httpRequest() {
connecting=true;
// if there's a successful connection:
if (client.connect("xxxx.azure-mobile.net", 80)) {
Serial.println("connecting...");
// send the HTTP GET request:
client.println("GET http://xxxx.azure-mobile.net/tables/Item?$top=1&$orderby=__createdAt%20desc HTTP/1.1");
client.println("Host: http://xxxx.azure-mobile.net/");
client.println("Connection: close");
Serial.println("connection close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
}
}