curl --request POST --data?
code: [select]
#include <process.h>
#include <dht.h>
#define apikey "xxxxxx" // api key
#define deviceid xxx // deiced id
#define relay xxx // sensor id
#define picture xxx
#define temperature xxx
#define humidity xxx
volatile boolean interruptsoccur;
unsigned long lastconnectiontime = 0;
const unsigned long postinginterval = 30 * 1000;
string path = "/mnt/sda1/";
byte relaypin = 2;
byte dht11pin = 4;
dht dht;
void setup() {
// put setup code here, run once:
bridge.begin();
dht.setup(dht11pin);
pinmode(relaypin, output);
attachinterrupt(0, pinrising, rising);
}
void loop() {
// put main code here, run repeatedly:
delay(dht.getminimumsamplingperiod());
float humidity = dht.gethumidity();
float temperature = dht.gettemperature();
if (millis() - lastconnectiontime > postinginterval) {
relaycontrol();
if (string(dht.getstatusstring()).equals(string("ok"))) {
postvalue(temperature, temperature);
postvalue(humidity, humidity);
}
}
if (interruptsoccur) {
postpicture();
interruptsoccur = false;
}
}
void postvalue(int sensor, float value) {
process p;
p.runshellcommand("curl --request post --data '{\"timestamp\":\"'"
+ string("`date '+%y-%m-%dt%t'`")
+ "'\",\"value\":"
+ value
+ "}' --header \"u-apikey: xxxxxx\""
+ " http://api.yeelink.net/v1.0/device/"
+ deviceid
+ "/sensor/"
+ sensor
+ "/datapoints");
}
void postpicture() {
process p;
string filename = "";
p.runshellcommand("date +%s.png");
while (p.available())
filename += char(p.read());
filename.trim();
p.runshellcommand("fswebcam " + path + filename);
p.runshellcommand("curl --request post --data-binary @"
+ path
+ filename
+ " --header \"u-apikey: xxxxxx\""
+ " http://api.yeelink.net/v1.1/device/"
+ deviceid
+ "/sensor/"
+ picture
+ "/photos");
}
void relaycontrol() {
process p;
string returnvalue = "";
p.runshellcommand("curl --request --header \"u-apikey: "
+ string(apikey)
+ "\" http://api.yeelink.net/v1.1/device/"
+ deviceid
+ "/sensor/"
+ relay
+ "/datapoint/");
while (p.available() > 0) {
returnvalue += char(p.read());
}
if (returnvalue.charat(returnvalue.length() - 2) == '1') {
digitalwrite(relaypin, high);
}
else if (returnvalue.charat(returnvalue.length() - 2) == '0') {
digitalwrite(relaypin, low);
}
lastconnectiontime = millis();
}
void pinrising() {
interruptsoccur = true;
}
dht11- gethumidity, gettemperature data not upload yeelink
how modify code ?
code: [select]
void postvalue(int sensor, float value) {
process p;
p.runshellcommand("curl --request post --data '{\"timestamp\":\"'"
+ string("`date '+%y-%m-%dt%t'`")
+ "'\",\"value\":"
+ value
+ "}' --header \"u-apikey: xxxxxx\""
+ " http://api.yeelink.net/v1.0/device/"
+ deviceid
+ "/sensor/"
+ sensor
+ "/datapoints");
}
i have 2 suggestions:
1) after calling p.runshellcommand() in postvalue, , display error message output process. it's hard debug problem if don't see error messages. example, line this:
2) consider simplifying sketch, , doing command formatting (including getting date) in python. if write python script takes 2 parameters, sensor id , value, can test ssh command line. way, able try various data values, , directly see error messages. then, when know working, can call like:
actually, if writing this, i'd doing as possible in python. example, of commands in postpicture() easier in script on linux side, , call single script run it. consider putting of in single script, first parameter gives operation (post value, picture, or relay control) , additional parameters vary depending on operation: sensor/value post value, , nothing post picture , relay control. way, of remote system details , keys down in 1 python file. when write system this, keep sketch simple possible - make dumb i/o processor. find easier update , debug script on linux side, , linux side faster , more powerful, of work possible on processor.
1) after calling p.runshellcommand() in postvalue, , display error message output process. it's hard debug problem if don't see error messages. example, line this:
code: [select]
while (p.available())
serial.print((char)p.read());
2) consider simplifying sketch, , doing command formatting (including getting date) in python. if write python script takes 2 parameters, sensor id , value, can test ssh command line. way, able try various data values, , directly see error messages. then, when know working, can call like:
code: [select]
p.begin("/usr/bin/python");
p.addparameter(sensor);
p.addparameter(value);
p.run()
actually, if writing this, i'd doing as possible in python. example, of commands in postpicture() easier in script on linux side, , call single script run it. consider putting of in single script, first parameter gives operation (post value, picture, or relay control) , additional parameters vary depending on operation: sensor/value post value, , nothing post picture , relay control. way, of remote system details , keys down in 1 python file. when write system this, keep sketch simple possible - make dumb i/o processor. find easier update , debug script on linux side, , linux side faster , more powerful, of work possible on processor.
Arduino Forum > Products > Arduino Yún (Moderator: fabioc84) > curl --request POST --data?
arduino
Comments
Post a Comment