Go Down

Topic: [Solved]Arduino Yun communication with Azure Mobile Services back-end (Read 2035 times) previous topic - next topic

Sheve

I am currently using the Arduino Yun to send a string data into my Microsoft Azure Mobile Services back-end tables. The coding work fine and the Serial Monitor can shown the data is sending out but there was no any update in my Microsoft Azure Mobile Service tables. Below is the coding for my program. Can anyone please help me to check out the problem? Appreciate your help.



void send_request()
{
    Serial.println("\nconnecting...");
    if (client.connect(server, 80))
    {
    Serial.print("sending ");
    }
}


/*
** Wait for response
*/

void wait_response()
{
  while (!client.available()) {
    if (!client.connected()) {
      return;
    }
  }
}


/*
** Read the response and dump to serial
*/

void read_response()
{
  bool print = true;

  while (client.available()) {
    char c = client.read();
    // Print only until the first carriage return
    if (c == '\n')
      print = false;
    if (print)
      Serial.print(c);
  }
}

/*
** Close the connection
*/

void end_request()
{
  client.stop();
}



jessemonroy650

@Sheve,
next time, please use markup when posting your code. (see image below.)

TIA
Jesse

jessemonroy650

#2
Apr 06, 2015, 01:47 am Last Edit: Apr 06, 2015, 02:08 am by jessemonroy650
Conflicting logic.

Code: [Select]

void wait_response()
{
  while (!client.available()) {
    if (!client.connected()) {
      return;
    }
  }
}


Change to :

Code: [Select]

void wait_response()
{
  while (!client.available()) {
  }
}


ALSO you probably don't want to break the code up that way.
Typically, you want to keep a transaction in a single code block. If you want to break it into subroutines, then make sure those subroutines are functional (and general purpose); otherwise your code will become unmaintainable.

I also noticed something you want to avoid. In the code block below you are using the word print. You want to avoid using keywords or functions names or names of methods, since it is easy to get confused when reading this. You could change print to shouldIprint or prnt, anything that does not look too close to something in the library.
Code: [Select]

void read_response()
{
  bool print = true;

  while (client.available()) {
    char c = client.read();
    // Print only until the first carriage return
    if (c == '\n')
      print = false;
    if (print)
      Serial.print(c);
  }
}


Best of Luck
Jesse

sonnyyu

Wrote decouple code, which allow you debug and run without couple CPU(AR9331) and MCU (ATmega32u4 ).

Code of AR9331:

Code: [Select]
nano /mnt/sda1/sensor.py

Code: [Select]
#!/usr/bin/python
import sys,json
import requests, pprint
url = 'https://sonnyyu.azure-mobile.net/tables/sensor'
data={"text":sys.argv[1],"complete":False}
#print data
data_json = json.dumps(data)
headers = {"Content-type":"application/json", "Accept":"application/json", "X-ZUMO-APPLICATION":"X-ZUMO-APPLICATION-PASSWORD"}
response = requests.post(url, data=data_json, headers=headers)
#pprint.pprint(response.json())


Code: [Select]
chmod 755 /mnt/sda1/sensor.py

Code: [Select]
/mnt/sda1/sensor.py 34

confirm data insert DB is OK.




sonnyyu

Code of ATmega32u4:

Code: [Select]
#include <Process.h>
int temperature;
void setup() {
 Bridge.begin();  // Initialize Bridge
}
void loop() {
 int temperature = random(0, 100);
 Process p;             
 p.begin("/mnt/sda1/sensor.py");     
 p.addParameter(String(temperature));
 p.run();
 delay(10000);
}






Go Up