Arduino Yun GET data from Microsoft Azure Database Table

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();
  }
}

Hi,

I can't help you with this specific problem, but I have solved similar problems.

Maybe the architecture you chose is not very efficient.

IMHO accessing the web from the MCU part of the Yun through the bridge is a bit like painting a room through the key hole. It might work, but is it efficient?

I would write python scripts that run on Atheros processor and handle all the web stuff, parse the json, etc., and then with the same python program put the result into the bridge in the form of a simple command or value.

The sketch on the Atmel processor could then just poll for just that specific value in the bridge.

jafrei

nano /mnt/sda1/sensor.py
#!/usr/bin/python
import requests, pprint
url = 'https://sonnyyu.azure-mobile.net/tables/sensor'
headers = { "Accept":"application/json", "X-ZUMO-APPLICATION":"X-ZUMO-APPLICATION-PASSWORD", "Host":"sonnyyu.azure-mobile.net" }
response = requests.get(url, headers=headers)
#pprint.pprint(response.json())
print "50"
chmod 755 /mnt/sda1/sensor.py

add your own json parse code to print database result (0~100)

http://forum.arduino.cc/index.php?topic=315585.msg2201807#msg2201807

ATmega32u4 code:

#include <Process.h>
void setup() {
  Bridge.begin();	// Initialize the Bridge
  Serial.begin(9600);	// Initialize the Serial
  // Wait until a Serial Monitor is connected.
  while (!Serial);
}
void loop() {
  Process p;
  p.runShellCommand("/mnt/sda1/sensor.py");
  while (p.running());
  while (p.available()) {
    int result = p.parseInt();			// look for an integer
    int signal = map(result, 0, 100, 0, 255);	// map result from 0-100 range to 0-255
    analogWrite(9, signal);			// set the brightness of LED on pin 9
    Serial.println(result);			// print the number as well
  }
  delay(5000);  // wait 5 seconds before you do it again
}

@sonnyyu

I tried the code you provided, but no difference or effect.

Sheve:
...
I tried the code you provided, but no difference or effect.

Give us more detail error message!

What did you get console print? (should be 50)

/mnt/sda1/sensor.py
50

What did you get Serial print? (should be 50)

The LED on pin 9 should have half brightness as well.