ArduinoEventHub
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
#include <Process.h> | |
const int sensorPin = A0; | |
const int greenLedPin = 4; | |
const int redLedPin = 2; | |
// the HTTP status that we expect back from the Event Hub | |
const char okHttpStatus[] = "201"; | |
void setup() { | |
pinMode(greenLedPin, OUTPUT); | |
pinMode(redLedPin, OUTPUT); | |
// turn the LED on (HIGH is the voltage level) | |
digitalWrite(greenLedPin, HIGH); | |
// turn the LED on (HIGH is the voltage level) | |
digitalWrite(redLedPin, HIGH); | |
Bridge.begin(); // Initialize the Bridge | |
Serial.begin(9600); // Initialize the Serial | |
} | |
void loop() { | |
// turn the LED off (LOW is the voltage level) | |
digitalWrite(greenLedPin, LOW); | |
// turn the LED off (LOW is the voltage level) | |
digitalWrite(redLedPin, LOW); | |
// read the value on AnalogIn pin 0 and store it in a variable | |
int sensorVal = analogRead(sensorPin); | |
// convert the ADC reading to voltage, | |
// somehow the Yun gives me only 4.5V instead of 5V | |
float voltage = (sensorVal / 1024.0) * 4.5; | |
// convert the voltage to temperature in degrees C, | |
// the sensor changes 10 mV per degree | |
float temperature = (voltage - .5) * 100; | |
Process p; | |
// Run the python script with the temperature and device ID | |
// as parameters | |
String cmd = "python /root/sendtoeventhub.py temperature" \ | |
+ String(temperature) + " yun"; | |
p.runShellCommand(cmd); | |
// do nothing until the process finishes, so you get the whole output: | |
while (p.running()); | |
char httpStatus[4]; | |
int i = 0; | |
// Read command output. If all went well the | |
// Python script returns the HTTP status 201 | |
while (p.available()) { | |
int serialResult = p.read(); // look for an integer | |
if (p.available()) | |
{ | |
if (i < 3) | |
{ | |
httpStatus[i] = (char)serialResult; | |
i++; | |
} | |
} | |
} | |
// Compare the return status with the expected HTTP status | |
if (strcmp(httpStatus, okHttpStatus) == 0) | |
{ | |
digitalWrite(greenLedPin, HIGH); | |
digitalWrite(redLedPin, LOW); | |
} | |
else | |
{ | |
digitalWrite(greenLedPin, LOW); | |
digitalWrite(redLedPin, HIGH); | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment