Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By Piman
#29584 Hi am not a coder and like to try things out for myself but I come to a point where I need help, :?
I am trying to send the data from an ESP 8266 to a raspberry PI server using an example found on the net which works fine, but trying to get it to send multiple values i.e.: temperature humidity and switch values, through one TCP connection without having to disconnect and reconnect for each value string. I would appreciate if somebody with a little more experience than myself can help me out in explaining on how this could be achieved and or provide examples of how this could be done Thank You For Your Time Andy.
Code: Select all#include <ESP8266WiFi.h>
#include <Base64.h>

//AP definitions
#define AP_SSID "your AP SSID"
#define AP_PASSWORD "your AP password"

//IoT server definitions
#define IOT_USERNAME    "admin"
#define IOT_PASSWORD    "test"
#define IOT_IP_ADDRESS  ""
#define IOT_PORT        80
#define IOT_NODE        "N8S0"

#define INPUT_PIN         2
#define USER_PWD_LEN      40

char unameenc[USER_PWD_LEN];
bool oldInputState;

void setup() {
  pinMode(INPUT_PIN, INPUT);
  char uname[USER_PWD_LEN];
  String str = String(IOT_USERNAME)+":"+String(IOT_PASSWORD); 
  str.toCharArray(uname, USER_PWD_LEN);
  base64_encode(unameenc, uname, strlen(uname));
  oldInputState = !digitalRead(INPUT_PIN);

void loop() {
  int inputState = digitalRead(INPUT_PIN);; 
  if (inputState != oldInputState)
    oldInputState = inputState;

void wifiConnect()
    Serial.print("Connecting to AP");
    WiFi.begin(AP_SSID, AP_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected"); 

void sendInputState(bool inputState)

   WiFiClient client;
   while(!client.connect(IOT_IP_ADDRESS, IOT_PORT)) {
    Serial.println("connection failed");
  String url = "";
  String command = "";
  if (inputState)
    command = "ControlOn";
    command = "ControlOff";
  url = "/Api/IoT/Control/Module/Virtual/"+ String(IOT_NODE) + "/"+command; // generate IoT server node URL

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

    String line = client.readStringUntil('\r');
  Serial.println("Connection closed");
User avatar
By gordonendersby
#29614 This looks like what im trying to do on EasyIoT myself.
Ive raised it on there forum myself but got no response.
http://iot-playground.com/forum/esp8266 ... -http-post

Its not possible unless the server can interpret the post string.
And they need to tell you how to build that string so the server can interpret it.
So theres not much you can do unless they give more info on how to post values the "virtual" device driver can understand.

The cludge is to make two seperate http calls.
But it also seems a virtual device cant store more than one value either.

Im now looking at other IoT servers with more documentation like Domotiga https://www.domotiga.nl/projects/domotiga/wiki/Home
I liked the simple look of EasyIoT as it didnt over complicate things but it seems to be run by one person.
I dont have the time to spend on the server as im concentrating on the nodes so cant take the time to persevere with it and really dig in to it to work it out myself. Feel a bit guilty as im sure he could do with some help and EasyIoT looks quite promising.

User avatar
By martinayotte
#29621 Maybe I've missed something. Is it with specific server software or a question in general ?

Because it is pretty easy to send a POST with multiple data fields, it is simply a keys/values transmitted.
Also, there still possibility to send a JSON object which itself contains multiple keys/values.
Of course, server side software need to be aware of this protocol. But that's part of the game ...
User avatar
By bbx10node
Piman wrote:...but trying to get it to send multiple values i.e.: temperature humidity and switch values, through one TCP connection without having to disconnect and reconnect for each value string.

Something like this should work assuming the server is designed to handle standard HTTP POST. For example, http://thingspeak.com accepts standard HTTP POST with multiple values.

Code: Select all 
  String value1 = "123";
  String value2 = "456";
  String http_content="temp=" + value1 + "&humi=" + value2;
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(IOT_IP_ADDRESS) + "\r\n" +
               "Connection: close\r\n" +
               "Authorization: Basic " + unameenc + " \r\n" +
               "Content-Type: application/x-www-form-urlencoded\r\n" +
               "Content-Length:" + http_content.length() + "\r\n" +
               "\r\n" +

You may need to URL encode the http_content if special characters are used.