More than one value in an http post

8 years 7 months ago #2227 by gordonendersby
Hi chaps,

Just getting to grips with EasyIoT.
Im running it on a Raspi v2 and intend to use ESP8266 boards as the basis of the devices over http. Im using the Arduino IDE for now to program the ESP boards directly.

Ive got one running with a DHT22 Temp and humidity sensor posting the temperature to EasyIoT without any issues.
After connecting to the server the Http post and response looks like this:
POST data to URL: POST /Api/EasyIoT/Control/Module/Virtual/N5S0/ControlLevel/23.80 HTTP/1.1
Host: 192.168.0.52
Connection: close
Authorization: Basic xxxxxxxxxxxxxxx 
Content-Length: 0


HTTP/1.1 200 OK
Server: HttpGateway
Content-Type: application/json; charset=utf-8
Date: Tue, 15 Sep 2015 16:35:58 GMT
Content-Length: 27
Connection: close

[{ "ResponseValue" : "1" }]
Connection closed

All well and good and the values are getting logged and appearing in the graph.

But how do I post multiple sensor values in the same http post?
For example Id like to also post up the humidity at the same time.
Or do I have to make multiple connections and http posts for each separate value?
Sorry couldnt find an answer in the forum or website.

Thanks

Gordon

Please Log in or Create an account to join the conversation.

8 years 7 months ago #2228 by gordonendersby
Also it seems a virtual module can have more than one parameter but the http post string doesnt specify which parameter the value is for.

If the virtual module im using has 2 parameters.
Humidity and temperature how would i specify which parameter the value goes in to?

At some point ill need to add the battery level to each module as well so i have a need to post multiple parameters for each module. Am i using the wrong module for this in the virtual module?

Thanks

Gordon

Please Log in or Create an account to join the conversation.

8 years 6 months ago #2239 by gordonendersby
Bit quiet on here init?
Anybody able to point me in the right direction?

This is bit of a show stopper for me not being able to find any documentation on the http post stuff for the virtual driver.
EasyIoT seemed a good choice as it seemed to have a good balance between simplicity and too many features.

Gordon

Please Log in or Create an account to join the conversation.

8 years 6 months ago - 8 years 6 months ago #2240 by NightOne
Yes it is possible to send as many variables as you want form your sketch, I have found that I have learned most of what I need to know about EasyIOT by looking at the diffrent projects that others are doing as well as reading about other peoples problems.

Here is my code for an ESP connected to a DHT11.....same as DHT22 just not as accurate. You will notice that I only update to the EasyIOT server when something changes....

Code:

#include <ESP8266WiFi.h>
#include <SPI.h>
#include <DHT.h>
#include <Base64.h>

const char* ssid = "Super Secret SSID goes here";
const char* password = "even more secret password goes here";

#define EIOT_USERNAME "admin"
#define EIOT_PASSWORD "test"
#define EIOT_IP_ADDRESS "192.168.1.15"
#define EIOT_PORT 80
#define EIOT_NODE_1 "N8S0"
#define EIOT_NODE_2 "N3S0"

#define REPORT_INTERVAL 60

#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
float oldTemp;
float oldHumid;

// DHT Sensor Setup
#define DHTPIN 2 // We have connected the DHT to Digital Pin 2
#define DHTTYPE DHT11 // This is the type of DHT Sensor (Change it to DHT11 if you're using that model)
DHT dht(DHTPIN, DHTTYPE, 15); // Initialize DHT object
void startWiFi(){

Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

}

void setup() {

// Open serial communications and wait for port to open:
Serial.begin(115200);
delay(10);
startWiFi();

char uname[USER_PWD_LEN];
String str = String(EIOT_USERNAME)+":"+String(EIOT_PASSWORD);
str.toCharArray(uname, USER_PWD_LEN);
memset(unameenc,0,sizeof(unameenc));
base64_encode(unameenc, uname, strlen(uname));
oldTemp = -1;
oldHumid = -1;
}

float h, t;

void loop() {



do {
t = dht.readTemperature();
h = dht.readHumidity();
Serial.print("Temperature: ");
Serial.println(t);
Serial.print("Humidity: ");
Serial.println(h);
} while (t == 85.0 || t == (-127.0));

if (t != oldTemp)
{
sendTemp(t);
oldTemp = t;
}

if (h != oldHumid)
{
sendHumidity(h);
oldHumid = h;
}


int cnt = REPORT_INTERVAL;

while(cnt--)
delay(1000);
}
void sendTemp(float t) {
WiFiClient client;

while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");

}

String url = "";
url += "/Api/EasyIoT/Control/Module/Virtual/"+ String(EIOT_NODE_1) + "/ControlLevel/"+String(t); // generate EasIoT server node URL

Serial.print("POST Temp data to URL: ");
Serial.println(url);

client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + String(EIOT_IP_ADDRESS) + "\r\n" +
"Connection: close\r\n" +
"Authorization: Basic " + unameenc + " \r\n" +
"Content-Length: 0\r\n" +
"\r\n");

delay(100);
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

Serial.println();
Serial.println("Connection closed");
}
void sendHumidity(float h) {
WiFiClient client;

while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");

}

String url = "";
url += "/Api/EasyIoT/Control/Module/Virtual/"+ String(EIOT_NODE_2) + "/ControlLevel/"+String(h); // generate EasIoT server node URL

Serial.print("POST Humidity data to URL: ");
Serial.println(url);

client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + String(EIOT_IP_ADDRESS) + "\r\n" +
"Connection: close\r\n" +
"Authorization: Basic " + unameenc + " \r\n" +
"Content-Length: 0\r\n" +
"\r\n");

delay(100);
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

Serial.println();
Serial.println("Connection closed");
}

Hope this helps
The following user(s) said Thank You: osalval, godfish

Please Log in or Create an account to join the conversation.

8 years 6 months ago - 8 years 6 months ago #2260 by gordonendersby
Thanks for that.
Its pretty much what Ive already got.
Im trying to find out if I can do it in one http transaction.

You have 2 separate http posts.
You open a connection to the server.
Post one value to the first node and close the connection.
Then open a connection post the next value to the second node and close the connection again.

If thats the only way to do it Ill have to.

But also you have 2 separate nodes rather than a single node able to hold 2 values.

Id like a single node to hold multiple related values.
For example Temp, humidity and voltage.
To hold each of these in a separate node for each value would make a dozen sensors very difficult to manage.

Ive not found any examples that does this.


Gordon

Please Log in or Create an account to join the conversation.

8 years 6 months ago #2282 by gordonendersby
@EasyIoT, admin,

Is there any documentation ive missed that might help me with this?
Theres a query on esp8266.com as well asking how its possible to do this.
So the info is needed by more than just me. www.esp8266.com/viewtopic.php?f=29&t=5629

Can you help us out.
Once i have the answers about multiple fields per node and multiple values in an http post i can write it up for others to use.

Gordon

Please Log in or Create an account to join the conversation.

Time to create page: 0.279 seconds

Forum latest

  • No posts to display.