The use of the ESP8266 in the world of IoT

User avatar
By joefly888
#20157 Hello, I am trying to post data to ubidots from the ESP on LUA. I do not know how to pass the data, apparently it has to be in a form-data format. I am using postman to test and was able to get it to successfully work the format is: the value I am passing here is "3"

Code: Select allPOST /api/v1.6/variables/5579297d7624326b3560cd2e/values HTTP/1.1
Host: things.ubidots.com
X-Auth-Token: qGd3CE08Gny9YtIzsdwfdu6kyTepwHHDW6DjUde6EeUyjJdonsVepTQ3EvOB
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="value"

3
----WebKitFormBoundaryE19zNvXGzXaLvS5C


the FAQ is here http://ubidots.com/docs/api/v1_6/data/post_variables_id_values.html

can anybody give some inputs?
User avatar
By tytower
#20198 Here is my POST detail . If you set yours up the same way it should work
Code: Select all Serial.println(InsideTemp.Kelvin());
  float value = (((readValue / 1023) * 3.3) * 100 -273); // convert
     
   ubiSave_value(String(value));
   
    Serial.println("Ubidots data");
    Serial.println(value);
    Serial.println(" Going to Sleep for a while !" );
 
 // deepSleep time is defined in microseconds. Multiply
 // seconds by 1e6   
  //ESP.deepSleep(sleepTimeS * 1000000);//one or other
  delay(30000);
  }
  void ubiSave_value(String value)
   {
       // if you get a connection, report back via serial:
       int num=0;
       String var = "{\"value\": " + String(value)+"}";
       num = var.length();
       if (client.connect("things.ubidots.com", 80)) {
         Serial.println("connected ubidots");
       // New lines according to ubidots support:
 delay(100);
client.println("POST /api/v1.6/variables/"+idvariable+"/values HTTP/1.1");
Serial.println("POST /api/v1.6/variables/"+idvariable+"/values HTTP/1.1");
client.println("Content-Type: application/json");
Serial.println("Content-Type: application/json");
client.println("Content-Length: "+String(num));
Serial.println("Content-Length: "+String(num));
client.println("X-Auth-Token: "+token);
Serial.println("X-Auth-Token: "+token);
client.println("Host: things.ubidots.com\n");
Serial.println("Host: things.ubidots.com\n");
client.print(var);
Serial.print(var+"\n");
         
       }
       else {
         // if you didn't get a connection to the server:
         Serial.println("ubidots connection failed");
       }

       if (!client.connected()) {
       Serial.println("NotConnected");
       Serial.println("disconnecting ubidots.");
       client.stop();

       // do nothing forevermore:
       for(;;);
     }
     if (client.available()) {
       char c = client.read();
       Serial.print(c);
     }
   }

User avatar
By joefly888
#20232 thanks TyTower:

I believe you are using the arduino+esp8266 by looking at your code. I have not tried this yet, as I have gotten used to working with NodeMCU directly and am trying to stay this way to keep it simple. So wondering if there is a way to do it in LUA.. I have taken your format and converted it to NODEMCU but still get 400 bad request.
Code: Select allidvariable = "5579297d76256b3560cd2e"
token = "qGd3CE08Gny9YtIzsdW41u6kepwHHDW6DjUde6EeUyjJdonsVepTQ3EvOB"
 
function postThingSpeak(level)
    connout = nil
    connout = net.createConnection(net.TCP, 0)
 
    connout:on("receive", function(connout, payloadout)
        print (payloadout);
        if (string.find(payloadout, "400 Bad Request") ~= nil) then
            print("BAD POST");
        end
    end)
 
    connout:on("connection", function(connout, payloadout)
 
        print ("Posting...");
 
        local value = node.readvdd33() / 1000;   
        local var = '{"value": '..value..'}';
        num = string.len(var);   
        print(num);     
        print(var);
        print(value);
        local postval = "POST /api/v1.6/variables/"..idvariable.."/values HTTP/1.1"
 --         .."X-Auth-Token: "..token 
 --         .."Cache-Control: no-cache"       
          .." Content-Type: application/json"
          .." Content-Length: "..num
          .." X-Auth-Token: "..token 
          .." Host: things.ubidots.com\n"
          ..var.."\n";
        print (postval);
 
        connout:send(postval)
    end)
 
    connout:on("disconnection", function(connout, payloadout)
        connout:close();
        collectgarbage();
    end)
 
    connout:connect(80,'api.thingspeak.com')
end
 
tmr.alarm(1, 15000, 1, function() postThingSpeak(0) end)


for clarity, the post line format is

Code: Select allPOST /api/v1.6/variables/5579297d76256b3560cd2e/values
 HTTP/1.1 Content-Type: application/json
 Content-Length: 14
 X-Auth-Token: qGd3CE08Gny9YtIzsdW41u6kepwHHDW6DjUde6EeUyjJdonsVepTQ3EvOB
 Host: things.ubidots.com
{"value": 3.3}


fyi, the variableID and token has been altered for security
Last edited by joefly888 on Fri Jun 12, 2015 5:07 am, edited 2 times in total.
User avatar
By tytower
#20236 Your line

POST /api/v1.6/variables/5579297d76256b3560cd2e/values HTTP/1.1 Content-Type: application/json Content-Length: 14 X-Auth-Token: qGd3CE08Gny9YtIzsdW41u6kepwHHDW6DjUde6EeUyjJdonsVepTQ3EvOBHost: things.ubidots.com


Suggested by Ubidots


client.println("POST /api/v1.6/variables/"+idvariable+"/values HTTP/1.1");
client.println("Content-Type: application/json");
client.println("Content-Length: "+String(num));
client.println("X-Auth-Token: "+token);
client.println("Host: things.ubidots.com\n");
client.print(var);


Can you set it up like that in your coding with new lines between?
Do you have "var" in there - thats the value being posted.
The whole lot has got to be a string ""
If you are getting a 404 though its not finding your variable page perhaps. You have to generate that variables key on its setup page Are you sure you have that right . Perhaps something ive said here might help but I've never used below
I think its the lines though. Its expecting a line break between strings.
connout:on("receive", function(connout, payloadout)
print (payloadout);
if (string.find(payloadout, "400 Bad Request") ~= nil) then
print("BAD POST");