-
-
Save dyadica/38ad2190d20583195441c98e4f16f1d4 to your computer and use it in GitHub Desktop.
Connecting A Photon to Azure Via A Mobile Service
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
Connecting A Photon to Azure Via A Mobile Service |
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
// This #include statement was automatically added by the Particle IDE. | |
#include <AzureMobileService.h> | |
#define MYSERVICE "azure-mobile-service-name" | |
#define MYKEY "azure-mobile-service-key" | |
AzureMobileService ams; | |
float accX = 0; | |
float accY = 0; | |
float accZ = 0; | |
// The scale of photon read | |
int minVal = 0; | |
int maxVal = 4095; | |
// The name of the table to populate within the mobile service. | |
String table = "IOST_MS"; // "IOST_1" // "IOST_2" | |
void setup() | |
{ | |
// Set the pin mode for x,y,z | |
pinMode(A0, INPUT); | |
pinMode(A1, INPUT); | |
pinMode(A2, INPUT); | |
// Set the pinmode for the led | |
pinMode(D7, OUTPUT); | |
// Initialise the serial port | |
Serial.begin(9600); | |
// Add a delay to allow everything to initialise | |
// and connect. | |
delay(10000); | |
// Initialise the Mobile Service via config call | |
ams.configure(MYSERVICE, MYKEY); | |
} | |
void loop() | |
{ | |
// Get the current data from the accelerometer | |
// Update each of the variables | |
// by performing a read of pin. | |
int x = analogRead(A0); | |
int y = analogRead(A1); | |
int z = analogRead(A2); | |
//convert read values to degrees -90 to 90 | |
// Needed for atan2 | |
int xAng = map(x, minVal, maxVal, -90, 90); | |
int yAng = map(y, minVal, maxVal, -90, 90); | |
int zAng = map(z, minVal, maxVal, -90, 90); | |
// Caculate 360deg values like so: atan2(-yAng, -zAng) | |
// atan2 outputs the value of -π to π (radians) | |
// We are then converting the radians to degrees | |
accX = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI); | |
accY = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI); | |
accZ = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI); | |
// Create a buffer for the data to be sent | |
char buffer[100]; | |
// The following is an example of data population | |
snprintf(buffer, sizeof(buffer), "{\"AccX\":%f,\"AccY\":%f,\"AccZ\":%f }", accX, accY, accZ); | |
// set the led to on to show data is being sent | |
digitalWrite(D7, HIGH); | |
Serial.println("**************************************************************"); | |
Serial.println("Publishing data from buffer to AMS"); | |
Serial.println("**************************************************************"); | |
// Call the creation of next table line | |
ams.create(table, buffer); | |
// Set the led to off | |
digitalWrite(D7, LOW); | |
// Perform a pause to limit the volume of data | |
// sent to the service. | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment