ESP8266 + Ubidots

greetings.

not sure if I'm posting at the correct section but here goes;

I'm trying to get photosensor reading on Ubidots.

I've updated the firmware of the Esp module and the following;

#define SSID        "FreeWifi"
#define PASS        "b1tchpl3as3"
#define DST_IP      "things.ubidots.com"
#define idvariable  "557000297625423d2e059974"   // replace with your Ubidots Variable ID
#define token       "JTV7TqHW7GmDZuQOmb1KASZiYjel62a1PFz7w1rdW4sMjD8xwPY39h06zpmU"  // replace with your Ubidots token
int len;
int sensorValue = 0; 
int sensorPin = A5;

void setup()
{
  // Open serial communications and wait for port to open:
  char cmd[254];
  Serial.begin(115200);
  Serial.setTimeout(5000);
  //test if the module is ready
  Serial.println("AT+RST");
  delay(1000);
  if (Serial.find("ready"))
  {
    //dbgSerial.println("Module is ready");
  }
  else
  {
    //dbgSerial.println("Module have no response.");
    while (1);
  }
  delay (1000);
  //connect to the wifi
  boolean connected = false;
  for (int i = 0; i < 5; i++)
  {
    if (connectWiFi())
    {
      connected = true;
      break;
    }
  }
  if (!connected) {
    while (1);
  }
  delay(5000);
  Serial.println("AT+CIPMUX=0");
}

void loop()
{
  int num = 0;
  sensorValue = analogRead(sensorPin);
  String var = "{\"sensorValue\":" + String(sensorValue) + "}";
  num = var.length();
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += DST_IP;
  cmd += "\",80";
  Serial.println(cmd);
  if (Serial.find("Error")) return;

  len = strlen ("POST /api/v1.6/datasources/");
  len = len + strlen (idvariable);
  len = len + strlen ("/values HTTP/1.1\nContent-Type: application/json\nContent-Length: ");
  char numlenght[4]; // this will hold the length of num which is the length of the JSON element
  sprintf(numlenght, "%d", num); // saw this clever code off the net; works yay
  len = len + strlen (numlenght);
  len = len + num; //fixed length of the string that will print as Content-Length: in the POST
  len = len + strlen ("\nX-Auth-Token: ");
  len = len + strlen (token);
  len = len + strlen ("\nHost: things.ubidots.com\n\n");
  len = len + strlen ("\n\n");
  Serial.print("AT+CIPSEND=");
  Serial.println (len); //lenght of the entire data POST for the CIPSEND command of ESP2866
  //Serial.println(cmd.length());
  if (Serial.find(">"))
  {
    //Serial.print(">");
  } else
  {
    Serial.println("AT+CIPCLOSE");
    delay(1000);
    return;
  }
  Serial.print ("POST /api/v1.6/variables/");
  Serial.print (idvariable);
  Serial.print ("/values HTTP/1.1\nContent-Type: application/json\nContent-Length: ");
  Serial.print (num);
  Serial.print ("\nX-Auth-Token: ");
  Serial.print (token);
  Serial.print ("\nHost: things.ubidots.com\n\n");
  Serial.print (var);
  Serial.println ("\n\n");
  delay(9000);
  //Serial.find("+IPD"); clear the input buffer after the web site responds to the POST
  while (Serial.available())
  {
    char c = Serial.read();
  }
  delay(1000);
}
boolean connectWiFi()
{
  Serial.println("AT+CWMODE=1");
  String cmd = "AT+CWJAP=\"";
  cmd += SSID;
  cmd += "\",\"";
  cmd += PASS;
  cmd += "\"";
  Serial.println(cmd);
  delay(2000);
  if (Serial.find("OK"))
  {
    return true;
  } else
  {
    return false;
  }
}

I got this on my serial monitor and it just stuck like so;

Any idea why is it so?

thanks in advance.

holymarder:
Any idea why is it so?

Once upon a time Marilyn Monroe sang the song "when love goes wrong nothing goes right".

And today you write the code "when serial goes wrong nothing goes right".

Your code looks as if you have connected THREE serial devices together on a single Arduino "Serial" port:

  • Serial ESP8266
  • Serial Arduino
  • Serial PC

But a serial connection provides one point-to-point connection between exactly TWO serial devices.

You'd need an Arduino with two serial ports to provide two different serial connections:
Arduino 1st serial ("Serial"): connect Arduino to PCs serial monitor
Arduino 2nd serial ("Serial1"): connect Arduino to ESP8266

BTW: Most Arduino boards operate on 5V, but the ESP8266 can handle 3.3V only.
So you could possibly use an Arduino DUE: It operates on 3.3V and it has more than one Serial.

You can do the necessary level shifting with a few resistors...

But you can't use serial for debug, and an esp8266 at the same time with a microcontroller with only one serial port

You could:
Disconnect esp when programming, and then don't use serial monitor
Use software serial(with all the usual caveats) for the serial debug, (still have to disconnect the esp8266 while programming)
Or
Use a microcontroller with more than one serial port, like the mega (4 serials, tons of pins, pricey), 1284p based board (2 serial, 32io pins, 128k flash).There are even Attinys that have two serial ports (1634 and x41)...

jurs:
Once upon a time Marilyn Monroe sang the song "when love goes wrong nothing goes right".

And today you write the code "when serial goes wrong nothing goes right".

Your code looks as if you have connected THREE serial devices together on a single Arduino "Serial" port:

  • Serial ESP8266
  • Serial Arduino
  • Serial PC

But a serial connection provides one point-to-point connection between exactly TWO serial devices.

You'd need an Arduino with two serial ports to provide two different serial connections:
Arduino 1st serial ("Serial"): connect Arduino to PCs serial monitor
Arduino 2nd serial ("Serial1"): connect Arduino to ESP8266

BTW: Most Arduino boards operate on 5V, but the ESP8266 can handle 3.3V only.
So you could possibly use an Arduino DUE: It operates on 3.3V and it has more than one Serial.

i run my esp8266 on different on stand alone power supply. i only connect the esp8266 tx,rx to arduino uno rx,tx.

DrAzzy:
You can do the necessary level shifting with a few resistors...

But you can't use serial for debug, and an esp8266 at the same time with a microcontroller with only one serial port

You could:
Disconnect esp when programming, and then don't use serial monitor
Use software serial(with all the usual caveats) for the serial debug, (still have to disconnect the esp8266 while programming)
Or
Use a microcontroller with more than one serial port, like the mega (4 serials, tons of pins, pricey), 1284p based board (2 serial, 32io pins, 128k flash).There are even Attinys that have two serial ports (1634 and x41)...

alright i'll try with mega

But a serial connection provides one point-to-point connection between exactly TWO serial devices.

That is not totally correct. Electrcally a single arduino tx can be connected to several other TTL rx devices. Several other device tx lines can be connected to an arduino rx if small isolation diodes are used.

holymarder:
i only connect the esp8266 tx,rx to arduino uno rx,tx.

You must connect ground too, otherwise you'll get garbage.

DrAzzy:
You must connect ground too, otherwise you'll get garbage.

yes i connected the ground too.