UNO + ESP01s can't connect to API (api.asksensors.com)

Hi everyone,

I've been trying for 3 days straight now and I'm so stuck and frustrated that I can't get this to work :cry:

I have this simple arduino setup:

with these codes:

/*
Connect Arduino WiFi to AskSensors
* Description: This sketch connects the Arduino to AskSensors IoT Platform (https://asksensors.com) using an ESP8266 WiFi.
* Author: https://asksensors.com, 2018-2020
* github: https://github.com/asksensors/AskSensors-Arduino-WiFi
*/
#include <SoftwareSerial.h>



// serial config
#define RX 10
#define TX 11
SoftwareSerial AT(RX,TX);
// TODO: change user config
String ssid = "Lee_Family_2.4Ghz"; //Wifi SSID
String password = "randompassword"; //Wifi Password
String apiKeyIn = "dx5JGwaidzyQjPnmANRiuGfqWnMNvXDw"; // API Key
String host = "api.asksensors.com"; // API host name
String port = "80"; // port
int AT_cmd_time;
boolean AT_cmd_result = false;


void setup() {
Serial.begin(9600);
// open serial
Serial.println("*****************************************************");
Serial.println("********** Program Start : Connect Arduino WiFi to AskSensors");
AT.begin(115200);
Serial.println("Initiate AT commands with ESP8266 ");
sendATcmd("AT",5,"OK");
sendATcmd("AT+CWMODE=3",5,"OK");
Serial.print("Connecting to WiFi:");
Serial.println(ssid);
sendATcmd("AT+CWJAP=\""+ ssid +"\",\""+ password +"\"",20,"OK");
}
void loop() {
// Create the URL for the request
String url = "GET /write/";
url += apiKeyIn;
url += "?module1=";
url += random(10, 100);
Serial.println("*****************************************************");
Serial.println("********** Open TCP connection ");
sendATcmd("AT+CIPMUX=1", 10, "OK");
sendATcmd("AT+CIPSTART=0, \"TCP\",\"" + host +"\"," + port, 20, "OK");
sendATcmd("AT+CIPSEND=0," + String(url.length() + 4), 10, ">");
Serial.print("********** requesting URL: ");
Serial.println(url);
AT.println(url);
delay(2000);
sendATcmd("AT+CIPCLOSE=0", 10, "OK");
Serial.println("********** Close TCP Connection ");
Serial.println("*****************************************************");
delay(20000); // delay
}
// sendATcmd
void sendATcmd(String AT_cmd, int AT_cmd_maxTime, char readReplay[]) {
Serial.print("AT command:");
Serial.println(AT_cmd);
while(AT_cmd_time < (AT_cmd_maxTime)) {
AT.println(AT_cmd);
if(AT.find(readReplay)) {
AT_cmd_result = true;
break;
}
AT_cmd_time++;
}
Serial.print("...Result:");
if(AT_cmd_result == true) {
Serial.println("DONE");
AT_cmd_time = 0;
}
if(AT_cmd_result == false) {
Serial.println("FAILED");
AT_cmd_time = 0;
}
AT_cmd_result = false;
}

The esp01s works fine up to connect with my wifi but fails everytime when trying to connect to the api.

Output from serial monitor as below:

What is wrong? Help please :cry:

doesn't the site require SSL?

Pardon my limited knowledge on this matter.

What is SSL and why is it needed?

I'm just trying to follow this instructables,

It should be pretty straightforward but for the life of me it just does not work.. :frowning:

SSL Secure Socket Layer is a name for a techgnology already not used but the name become generic for TCP secure layer. (TLS is used now.)
the site uses https. it is http over secure layer. default https port is 443.

in CIPSTART use "SSL" instead of "TCP" and port 443

Juraj:
SSL Secure Socket Layer is a name for a techgnology already not used but the name become generic for TCP secure layer. (TLS is used now.)
the site uses https. it is http over secure layer. default https port is 443.

in CIPSTART use "SSL" instead of "TCP" and port 443

Hi Juraj,

Thanks for the reply. I've tried it out but it still won't connect unfortunately. But there is a slight difference whereby it is successful for CIP+SEND.

ssl.PNG

ssl.PNG

the second parameter is a server name or IP address. don't put https:// there

Juraj:
the second parameter is a server name or IP address. don't put https:// there

I've tried that earlier but it didn't work. But then I randomly tried to set AT+CIPSTART=1 instead of 0 and it worked! (I believe and I'm not sure what this does exactly)

But thank you as the SSL + 443 port actually did solve part of the problem I'm facing.

But now I can't get AT+CIPSEND to work :confused:

woohoo.PNG

woohoo.PNG

SSL connection can take longer then a second then the find will wait so with your haphazard code it sometimes connects sometimes not. and same applies for CIPSEND.

Juraj:
SSL connection can take longer then a second then the find will wait so with your haphazard code it sometimes connects sometimes not. and same applies for CIPSEND.

Haphazard code? If there a solution for this because I have no idea

Serial.println(AT_cmd);
while(AT_cmd_time < (AT_cmd_maxTime)) {
AT.println(AT_cmd);
if(AT.find(readReplay)) {
AT_cmd_result = true;
break;
}

what this does:

send the command
check the count. not reached
send the command again ! AT firmware responses "busy p..." but you don't see it
find waits for timeout (1 sec) or for some characters. for SSL this can timeout

check the count. not reached
send the command, AT firmware responses "busy p..." but you don't see it
find waits for timeout (1 sec) or for some characters. some characters arrive but if it is not the expect response then find times out again.
if the connection failed AT firmware sends ERROR. you don't see it.

if the response it is not the expected OK, it repeats this (10 times) again, but you don't see it

I would recommend my WiFiEspAT library, but for SSL it requires AT firmware 2.1

Juraj:
SSL connection can take longer then a second then the find will wait so with your haphazard code it sometimes connects sometimes not. and same applies for CIPSEND.

I also don't understand what you mean by "haphazard".
haphazard is defined as marked by lack of plan, order, or direction

What does he need to do to fix his code to make it work?
That is the answer that would be the most useful.

.

ieee488:
I also don't understand what you mean by "haphazard".
haphazard is defined as marked by lack of plan, order, or direction

What does he need to do to fix his code to make it work?
That is the answer that would be the most useful.

.

yes haphazard is the right word. did you read my previous comment about the sendATcmd? how it send the command at least twice and on other then expected answer 20 times?

the esp8266 AT commands syntax and responses to them are inconsistent. so it takes a lot of code to handle the responses properly with one function. that is why I made a library. you can see the EspAtDrv class in my WiFiEspAT library.

Juraj:
yes haphazard is the right word. did you read my previous comment about the sendATcmd? how it send the command at least twice and on other then expected answer 20 times?

the esp8266 AT commands syntax and responses to them are inconsistent. so it takes a lot of code to handle the responses properly with one function. that is way a made a library. you can see the EspAtDrv class in my WiFiEspAT library.

Alirght I'll try this. So I should update my Esp8266 firmware to 2.1 first?

Juraj:
yes haphazard is the right word. did you read my previous comment about the sendATcmd? how it send the command at least twice and on other then expected answer 20 times?

the esp8266 AT commands syntax and responses to them are inconsistent. so it takes a lot of code to handle the responses properly with one function. that is way a made a library. you can see the EspAtDrv class in my WiFiEspAT library.

No haphazard is not the right word.
I know English is not your first language.
I am still not understanding what you are writing.
I find your explanation very difficult to understand.

You write : "that is way a made a library. you can see the EspAtDrv class in my WiFiEspAT library. "
huh?

Instead of just saying that something is wrong, you should provide a solution.
I don't see a fix for his code.

.

ieee488:
No haphazard is not the right word.
I know English is not your first language.
I am still not understanding what you are writing.
I find your explanation very difficult to understand.

You write : "that is way a made a library. you can see the EspAtDrv class in my WiFiEspAT library. "
huh?

Instead of just saying that something is wrong, you should provide a solution.
I don't see a fix for his code.

.

ieee488:
No haphazard is not the right word.
I know English is not your first language.
I am still not understanding what you are writing.
I find your explanation very difficult to understand.

You write : "that is way a made a library. you can see the EspAtDrv class in my WiFiEspAT library. "
huh?

Instead of just saying that something is wrong, you should provide a solution.
I don't see a fix for his code.

.

typos fixed.

is driving blind haphazard?

I've updated to the latest SDK V3.02,

But I'm unable to update the AT to 2.1. How do I do that? I tried flashing the factory_WROOM-02. bin file into address 0x0 but it gives download failed.

Please assist me thank you :cry:

this could help GitHub - JAndrassy/WiFiEspAT: Arduino networking library. Standard Arduino WiFi networking API over ESP8266 or ESP32 AT commands.

Juraj:
this could help GitHub - JAndrassy/WiFiEspAT: Arduino networking library. Standard Arduino WiFi networking API over ESP8266 or ESP32 AT commands.

I tried to flash and it was successful flash but now my AT commands don't work. Is this compatible with ESP-01s?

Flash is successful with flashing AT2_esp8266_factory_param_tx1rx3.bin @ 0x00000

did you see: "It is possible to build the AT 2 for esp8266 with 1 MB flash, but Espressif didn't publish a binary for this option. You can download it here."

Juraj:
did you see: "It is possible to build the AT 2 for esp8266 with 1 MB flash, but Espressif didn't publish a binary for this option. You can download it here."

Owhhhh which one should I flash?

flash.PNG

flash.PNG