Skip to content

Instantly share code, notes, and snippets.

@dyadica

dyadica/About Secret

Created March 8, 2017 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyadica/29530a52adfb2dfded051c9c61d191bb to your computer and use it in GitHub Desktop.
Save dyadica/29530a52adfb2dfded051c9c61d191bb to your computer and use it in GitHub Desktop.
Photon and Azure Integration via IoT Hub
Photon and Azure Integration via IoT Hub
Formatting of JSON within Example Photon IoT Hub Integration:
{
"accX": "{{x}}",
"accY": "{{y}}",
"accZ": "{{z}}",
"device_id": "{{PARTICLE_DEVICE_ID}}",
"published_at": "{{PARTICLE_PUBLISHED_AT}}"
}
Example Trigger for Example Photon IoT Hub Integration:
void loop()
{
// Get some data
String data = String(10);
// Trigger the integration
Particle.publish("<< event assigned to integration >>", data, PRIVATE);
// Wait 60 seconds
delay(60000);
}
// Holders for the accelerometer data
float accX = 0;
float accY = 0;
float accZ = 0;
// The scale of photon read
int minVal = 0;
int maxVal = 4095;
// The status LED
int led = D7;
// Initialise the program
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);
}
// Loop the program
void loop()
{
// Get the current data from the accelerometer
// Update each of the variables
// by performing a read of pin.
int ax = analogRead(A0);
int ay = analogRead(A1);
int az = analogRead(A2);
//convert read values to degrees -90 to 90
// Needed for atan2
int xAng = map(ax, minVal, maxVal, -90, 90);
int yAng = map(ay, minVal, maxVal, -90, 90);
int zAng = map(az, 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);
// Match the JSON naming
int x = accX;
int y = accY;
int z = accZ;
// Create a buffer for the data to be sent
char buffer[128];
// The following is an example of data population
sprintf(buffer,"{\"x\": %u, \"y\": %u, \"z\": %u}", x, y, z);
// set the led to on to show data is being sent
digitalWrite(D7, HIGH);
Serial.println("**************************************************************");
Serial.println("Publishing data from buffer to Azure IoT Hub");
Serial.println("**************************************************************");
// Call the creation of next table line
// Publish an event to trigger the IoT integration. eplace "my-event"
// with the event name you used when configuring the integration
// Replace "test-data" with the real data you'd like to send to Azure
// IoT Hub
Particle.publish("iost-data-event", buffer, PRIVATE);
// Set the led to off
digitalWrite(D7, LOW);
// Perform a pause to limit the volume of data
// sent to the service.
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment