Arduino + ESP8266, How can i send Continous Get Request? - tcp

I have a code that was available on this website https://hackaday.io/project/3072/instructions . I made the code work by modifying it a little but the main problem is that it serves the GET request only once. What i want is continuous page fetch and there should be no TCP connection closing.
I have tried different methods but the connection always breaks after 1 GET request.
Moreover, if i do not send any GET request then it serves the domain's index page continuously without breaking TCP connection.
This is the original code http://dunarbin.com/esp8266/retroBrowser.ino.
And this is mine.
#define SSID "vivek"
#define PASS "bustedparamour21"
#define DEST_HOST "www.electronics2work.com"
#define DEST_IP "31.170.161.234"
#define TIMEOUT 10000 // mS
#define CONTINUE false
#define HALT true
#define ECHO_COMMANDS // Un-comment to echo AT+ commands to serial monitor
// Print error message and loop stop.
void errorHalt(String msg)
{
Serial.println(msg);
Serial.println("HALT");
while(true){};
}
// Read characters from WiFi module and echo to serial until keyword occurs or timeout.
boolean echoFind(String keyword)
{
byte current_char = 0;
byte keyword_length = keyword.length();
// Fail if the target string has not been sent by deadline.
long deadline = millis() + TIMEOUT;
while(millis() < deadline)
{
if (Serial1.available())
{
char ch = Serial1.read();
Serial.write(ch);
if (ch == keyword[current_char])
if (++current_char == keyword_length)
{
Serial.println();
return true;
}
}
}
return false; // Timed out
}
// Read and echo all available module output.
// (Used when we're indifferent to "OK" vs. "no change" responses or to get around firmware bugs.)
void echoFlush()
{while(Serial1.available()) Serial.write(Serial1.read());}
// Echo module output until 3 newlines encountered.
// (Used when we're indifferent to "OK" vs. "no change" responses.)
void echoSkip()
{
echoFind("\n"); // Search for nl at end of command echo
echoFind("\n"); // Search for 2nd nl at end of response.
echoFind("\n"); // Search for 3rd nl at end of blank line.
}
// Send a command to the module and wait for acknowledgement string
// (or flush module output if no ack specified).
// Echoes all data received to the serial monitor.
boolean echoCommand(String cmd, String ack, boolean halt_on_fail)
{
Serial1.println(cmd);
#ifdef ECHO_COMMANDS
Serial.print("--"); Serial.println(cmd);
#endif
// If no ack response specified, skip all available module output.
if (ack == "")
echoSkip();
else
// Otherwise wait for ack.
if (!echoFind(ack)) // timed out waiting for ack string
if (halt_on_fail)
errorHalt(cmd+" failed");// Critical failure halt.
else
return false; // Let the caller handle it.
return true; // ack blank or ack found
}
// Connect to the specified wireless network.
boolean connectWiFi()
{
String cmd = "AT+CWJAP=\""; cmd += SSID; cmd += "\",\""; cmd += PASS; cmd += "\"";
if (echoCommand(cmd, "OK", CONTINUE)) // Join Access Point
{
Serial.println("Connected to WiFi.");
return true;
}
else
{
Serial.println("Connection to WiFi failed.");
return false;
}
}
// ******** SETUP ********
void setup()
{
Serial.begin(9600); // Communication with PC monitor via USB
Serial1.begin(9600); // Communication with ESP8266 via 5V/3.3V level shifter
Serial1.setTimeout(TIMEOUT);
Serial.println("ESP8266 Demo");
delay(2000);
Serial.println("Module is ready.");
echoCommand("AT+GMR", "OK", CONTINUE); // Retrieves the firmware ID (version number) of the module.
echoCommand("AT+CWMODE?","OK", CONTINUE);// Get module access mode.
// echoCommand("AT+CWLAP", "OK", CONTINUE); // List available access points - DOESN't WORK FOR ME
echoCommand("AT+CWMODE=1", "", HALT); // Station mode
echoCommand("AT+CIPMUX=1", "", HALT); // Allow multiple connections (we'll only use the first).
//connect to the wifi
boolean connection_established = false;
for(int i=0;i<5;i++)
{
if(connectWiFi())
{
connection_established = true;
break;
}
}
if (!connection_established) errorHalt("Connection failed");
delay(5000);
//echoCommand("AT+CWSAP=?", "OK", CONTINUE); // Test connection
echoCommand("AT+CIFSR", "", HALT); // Echo IP address. (Firmware bug - should return "OK".)
//echoCommand("AT+CIPMUX=0", "", HALT); // Set single connection mode
}
// ******** LOOP ********
void loop()
{
// Establish TCP connection
String cmd = "AT+CIPSTART=0,\"TCP\",\""; cmd += DEST_IP; cmd += "\",80";
if (!echoCommand(cmd, "OK", CONTINUE)) return;
delay(2000);
// Get connection status
if (!echoCommand("AT+CIPSTATUS", "OK", CONTINUE))
return;
// Build HTTP request.
cmd = "GET /";
cmd +="iot/graphing.php?a=1&b=ldr&c=41 ";
cmd += "HTTP/1.1\r\nHost: ";
cmd += DEST_HOST;
cmd += ":80\r\n\r\n";
// Ready the module to receive raw data
if (!echoCommand("AT+CIPSEND=0,"+String(cmd.length()), ">", CONTINUE))
{
echoCommand("AT+CIPCLOSE", "", CONTINUE);
Serial.println("Connection timeout.");
return;
}
// Send the raw HTTP request
echoCommand(cmd, "OK",CONTINUE ); // GET
// Loop forever echoing data received from destination server.
while(true)
while (Serial1.available())
Serial.write(Serial1.read());
errorHalt("ONCE ONLY");
}
This code makes get request only once. How can i make it serve GET request Continously without closing TCP connection?
THANKS in Advance!!

You need to close your connection using AT+CIPCLOSE and then start a new connection again.
For example, if you need to make two connections everytime(like making connection with 2 websites) , you can make 1 connection, then close this connection. Now make another connection and close it.
I made 2 connections in my loop() function using above logic and it is working fine.

Related

How to have multiple connections to a Arduino webserver?

I have an Arduino running a webserver that is receiving data every 2 seconds. I CAN connect to its IP address by typing into the browser. I also created a web app that pulls data from this IP address every time new data comes in. The issue is that I need to access the IP address with the web app while another program is accessing it. Currently only one program can access at a time. I would like to have a Python script pulling data from the IP address constantly and still allow the web app to connect to view it live. How can I achieve this?
Arduino code with a lot of other stuff removed...
WiFiServer server(80); //server socket
WiFiClient client_1 = server.available();
void setup() {
Serial.begin(9600);
enable_WiFi(); // function to enable wifi
connect_WiFi(); // function to connect to wifi
server.begin();
}
void loop() {
client_1 = server.available();
if (client_1) {
printWEB(client_1); // this posts the data as text to the web IP address
}
delay(2000);
}
void printWEB(WiFiClient client) {
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Access-Control-Allow-Origin: *");
client.println("Access-Control-Allow-Methods: GET");
client.println("Content-type: application/json");
client.println();
moistureReading = analogRead(A1);
tmpString = dataPosted;
tmpString.replace("%moistureData%", String(moistureReading) );
client.flush();
client.print( tmpString );
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
The solution here was to take the delay off of the arduino code by simply removing the sleep function (Turns out this is not required). I then had to create a delay in all of my fetching codes. The React.js needed a sleep function and the python script needed a sleep function. This allows the two platforms to collect data from the same IP Address simultaneously. I needed a minimum of about 5-10 second sleep for both apps in order for this to work. I initially tried 1 second for each and it didn't work.
The webserver as programmed on your Arduino can only serve one request from a client at a time, not two simultaneously.
If two clients are doing requests in a loop, and are doing that fairly quickly, there will be situations where one client is blocking access for the other.
Try making the two clients much slower, i.e. retrieve information at a much lower frequency, just to see if the Arduino can keep up then.
Also, make the webserver on the Arduino as fast as possible, and call it as often as possible.
So, don't put delay()s in the loop, or anywhere else, and try to do the analogread() and the return string preparation outside of the webserver code and in the loop() every x seconds using millis(), so as little time as possible is spent in the webserver code.

Function doesn't return the right value

To begin with, I'm doing a mini project on the verification of RFID card using NodeMCU. I set up a database and a server to handle all the requests from the client. The problem is, whenever I try to verify a valid card, the function that handles it keeps returning the wrong value.
To be clear, here is my main loop:
void loop() {
connectToHost();
key = kpd.getKey();
card = readCard();
delay(200);
//check for card status
if(verifyCard(card)){
Serial.println("Card is valid");
} else {
Serial.println("Invalid Card");
}
//check connection status
if(WiFi.status() == WL_CONNECTION_LOST){
connectToWiFi();
}
}
The main loop calls this function:
boolean verifyCard(String uid){
String url = "/ECafe/terminal_verify.php?uid=";
url += uid;
// This will send the request to the server
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
}
yield();
}
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
//if string contains echo from php file
if(line.indexOf("Valid")>0){
return true;
} else {
return false;
}
yield();
}
Serial.println();
Serial.println("Closing connection");
}
I intentionally put a valid card UID in the database just to test this function. Yet, it keeps returning false. I'm sure that the response from the server is "Valid". I suspect that the function doesn't even wait for the response from the server and returns a false value because the interval between the GET request is sent and the response is returned is too short. Is that because of the if condition in the main loop? See the attachment for the serial monitor output.
The logic of your function is flawed.
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
//if string contains echo from php file
if(line.indexOf("Valid")>0){
return true;
} else {
return false;
When the code reads the first line, and it doesn't contain Valid, it will fail and return false. So your code will ALWAYS return false, as I doubt that the first line returned is Valid...
}
yield();
}
Serial.println();
Serial.println("Closing connection");
The rest of this code is never executed. "Closing connection" is never printed right?

Arduino Uno + ESP8266 reading server's response

I'm sending a GET request from Arduino Uno using ESP8266. The request is sent, but I'm unable to print the received response.
I'm using code from https://elementztechblog.wordpress.com/2015/05/13/esp8266-based-temperature-data-logger-using-arduino/
I have changed the code for connecting to my server and I can see the GET request is received on my server's log.
I tried putting
while (ser.available())
{
Serial.write(ser.read());
}
after the Serial.println("AT+CIPCLOSE"); statement.
BUT I'm not getting anything on Serial monitor after "AT+CIPCLOSE"
EDIT:
Here's my entire code:
// connect 10 to TX of Serial USB
// connect 11 to RX of serial USB
SoftwareSerial ser(10, 11); // TX, RX
// this runs once
void setup()
{
// enable debug serial
Serial.begin(9600);
// enable software serial
ser.begin(9600);
// reset ESP8266
ser.println("AT+RST");
}
// the loop
void loop()
{
// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "192.168.0.25";
cmd += "\",3000";
ser.println(cmd);
if(ser.find("Error"))
{
Serial.println("AT+CIPSTART error");
return;
}
// prepare GET string
String getStr = "GET /api/myservice";
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);
if(ser.find(">")){
ser.print(getStr);
}
else
{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
// CODE I FOUND FOR READING THE REPLY FROM SERVER:
while (ser.available())
{
// char c = ser.read();
Serial.write(ser.read());
// if (c == '\r') Serial.print('\n');
}
}
delay(1000);
}
ESP Details:
ESP-01
AT version: 0.40.0.0
If you are only struggling to read a response then the answer is simple;
You are closing the TCP connection before you try to read:
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
// CODE I FOUND FOR READING THE REPLY FROM SERVER:
while (ser.available())
{
Move the reading while into the above block just beneath ser.print(getStr); but add a delay between the two as well.

Initializing Xbee S1 by an Arduino mini pro

I am trying to configurate my XBee module by an Arduino pro mini that is connected to my computer by de FTDI basic from sparkfun.
I already can write and send data from the Xbee to another Xbee module by the Arduino.
My problem is that I want to configure the Xbee by the arduino. I am sending ‘+++’ with the arduino to my Xbee and want to receive the ‘OK’ from the Xbee with the serial monitor from the arduino editor. The problem is that I can send it but never receive and ‘OK’, and when I am trying to configure the Xbee the configuration never happened. So I cant reach the Xbee command line.
uint8_t pinRx = 0, pinTx = 1; //Initialise pins on the Arduino
char GotChar;
long BaudRate = 4800;
int incomingByte=0;
SoftwareSerial mySerial( pinRx , pinTx ); //Initialise SoftwareSerial
void init_USB()
{
Serial.begin(BaudRate);
Serial.println("Start");
mySerial.begin(BaudRate);
}
void init_XBee()
{
Serial.begin(9600);
int check = 0;
while(T_XBEE_CONTROLLER_CheckOK() == 0)
{
Serial.println("CheckOK");
Serial.write("+++");
delay(2000);
}
Serial.println("ATCH 8\r");
delay(2000);
Serial.write("ATID 1234\r");
delay(2000);
Serial.write("+++");
delay(2000);
Serial.write("ATPL 0\r");
delay(2000);
Serial.write("+++");
delay(2000);
Serial.write("ATAP 2\r");
delay(2000);
}
int T_XBEE_CONTROLLER_CheckOK()
{
char ch[2];
ch[0] = 0x00;
while(! ((ch[0] == 'O' ) && (ch[1] == 'K') ))
{
ch[0] = mySerial.read();
ch[1] = mySerial.read();
if((ch[0] != 'O') && (ch[1] != 'K') && (ch[2] != '\r'))
{
Serial.println("FAILED");
return 0;
}
Serial.println("SUCCES");
return 1;
}
return 0;
}
it is a stupid answer but first of all, you should check that your Xbee is configured as AT device instead of API device. If it is API mode, the module wont understand the messages.
To do that you just have to use X-CTU application and read the configuration of the module, and change it to AT device.
Hope that helps.
Thanks for the response and the help, and also sorry for the late response.
I already solved the problem. The problem was the function write(). If you want to reach the command mode from the XBee you should only send "+++". If there is some kind of character behind the "+++" you can't reach the command line. The function write put a (for me) unknown character behing the "+++". So that's the problem for not reaching the command line.
To resolve this problem just use the function print("+++"). After using this function it is possible to reach the command line.
You have to read from the serial right after you send the +++ command, because this is where the xbee writes 'OK'. Also a better way to respect the guard times is to wait for a reply, and test to see if it is 'OK'.
Here is my code, I don't remember if it was working the last time I checked but I will just paste it here and you can modify it as you like. All it does is broadcast A1, B2, C3, etc.
There's a lot of commenting out where I was experimenting, but the regular comments are informative. Make sure you go through it step by step, it's quite simple when you get your head around it. Don't forget to change the destination address low to 0xFFFF if you want to broadcast.
In the end you'll come to the same realisation I did that AT mode is not suitable for configuring the xbee by writing programs.
For example I had an xbee constantly transmitting the number '2', and when another xbee was entering command mode using this code, it would receive the number 2 from the remote xbee when it should have received the 'OK' message from the local xbee, thus the program didn't acknowledge it being in command mode and breaking. When entering command mode you'd think an xbee would turn it's receiver off, but that's not the case so you can easily get into trouble.
If you want to do it the right way, have a look at API mode. I have series 1 xbee's so I'm implementing the Digimesh protocol, which so far I haven't seen anyone online do, but it's almost identical to the Zigbee so it's easy. If you'd like I can give you my code for that which can serve as a simple example.
/*
unicast_configure
Configure an XBee for unicast transmission and transmit
some characters to test
*/
#include <SoftwareSerial.h>
// Pins on Bees Shield:
SoftwareSerial xbee(2, 3); // TX, RX
boolean configured;
char c = 'A';
boolean configureRadio() {
// Set the data rate for the SoftwareSerial port:
xbee.begin(9600);
// Put the radio in command mode:
Serial.write("Entering command mode\r");
delay(1000);
while(xbee.available()>0) {xbee.read();}
xbee.write("+++");
while(xbee.available()>0) {xbee.read();}
//delay(1000);
//while(xbee.available() > 0) {Serial.write(xbee.read());}
String ok_response = "OK\r"; // The response we expect
// Read the text of the response into the response variable
// This satisfies the guard time by waiting for the OK message
String response = String("");
while (response.length() < ok_response.length()) {
if (xbee.available() > 0) {
response += (char) xbee.read();
}
}
Serial.println("response1: " + response);
// If we got received OK, configure the XBee and return true:
if (response.equals(ok_response)) {
Serial.println("Enter command mode successful");
// Restore to default values:
Serial.println("Restoring default values before making changes");
xbee.write("ATRE\r");
Serial.println("Setting addr high");
xbee.write("ATDH0\r"); // Destination high
//while(xbee.available() > 0) {Serial.write(xbee.read());}
Serial.println("Setting addr low");
xbee.write("ATDL1\r"); // Destination low-REPLACE THIS
//while(xbee.available() > 0) {Serial.write(xbee.read());}
Serial.println("Setting MY address");
xbee.write("ATMYFFFF\r");
// Apply changes:
Serial.println("Applying changes");
xbee.write("ATAC\r");
/*
///////////////////////////////////////////////
// Write to non-volatile memory:
// Use similar technique as above to satisfy guard time
Serial.write("Saving\r");
xbee.write("ATWR\r");
String response2 = String("");
//while (xbee.available() > 0) {Serial.write(xbee.read());}
while (response2.length() < ok_response.length()) {
if (xbee.available() > 0) {
response2 += (char) xbee.read();
}
}
Serial.println("response2: " + response2);
if (response2.equals(ok_response)) {
Serial.println("Save successful");
}
else { Serial.println("Save not successful");
return false;
}
// And reset module:
Serial.println("Resetting");
xbee.write("ATFR\r");
///////////////////////////////////////////////
*/
Serial.write("Exit command mode\r");
xbee.write("ATCN\r"); // Exit command mode
//while(xbee.available() > 0) {Serial.write(xbee.read());}
Serial.write("Finished\r");
return true;
} else {
return false; // This indicates the response was incorrect
}
}
void setup() {
Serial.begin(9600); // Begin serial
configured = configureRadio();
}
void loop() {
// Test transmission:
if (configured) {
xbee.print(c);
Serial.print(c);
c = c + 1;
if (c > 'Z') { c = 'A'; }
}
else {
Serial.println("Not configured (in loop)");
delay(5000);
Serial.println("Retrying configuration");
configured = configureRadio();
}
delay(1500);
}

SIM900 GSM/GPRS not getting a proper AT+CREG? answer

I'm using an Arduino UNO with attached IComsat SIM900 GSM/GPRS shield.
Using the following tutorial: Arduino Live GPS Tracker I'm stuck with the AT+CREG? command, which checks if the SIM-card is registered at the provider.
The following logic is used:
In the GSM_HTTP.INO file within the "void setup()" function, the following line gets executed modem.checkNetwork();
void setup() {
Serial.begin(9600);
Serial.println("GM862 monitor");
modem.switchOn(); // switch the modem on
delay(4000); // wait for the modem to boot
modem.init(); // initialize the GSM part of Module
modem.version(); // request modem version info
while (!modem.isRegistered()) {
delay(1000);
modem.checkNetwork(); // check the network availability
}
}
The function "checkNetwork()" is part of the included library GSM862.cpp and looks like this:
void GM862::checkNetwork() {
char buf[BUF_LENGTH];
char result;
requestModem("AT+CREG?", 1000, true, buf);
result = buf[21];
if (result == '1') {
state |= STATE_REGISTERED;
}
else {
state &= ~STATE_REGISTERED;
}
}
Now this is the important part: The value of "result" that gets received by the function "requestModem" returns cryptic values, but no netword status (number 0-5) which is why there is a endless loop trying to register without error or success message.
As this function gets the "buf" variable out of the function "requestModem" in GSM862.cpp, I've had a look at it as well:
byte GM862::requestModem(const char *command, uint16_t timeout, boolean check, char *buf) {
byte count = 0;
*buf = 0;
modem->flush();
modem->println(command);
count = getsTimeout(buf, timeout);
return count;
}
In order to have a look into the relevant variables for debugging purposes I've changed the last two functions into the following code:
-->checkNetwork
void GSM862::checkNetwork() {
char buf[BUF_LENGTH];
char result;
requestModem("AT+CREG?", 1000, true, buf);
result = buf[21];
Serial.print("Debugging buf2:");
Serial.print(buf[21]);
Serial.print("Debugging buf2:");
Serial.print(buf[1]);
Serial.print("Debugging buf2:");
Serial.print(buf[0]);
Serial.print("Debugging result2:");
Serial.println(result);
if (result == '1') {
state |= STATE_REGISTERED;
Serial.println("Network registered, home network...");
}
else {
state &= ~STATE_REGISTERED;
if(result == '0'){
Serial.println("Network not registered, not searching for a new operator to register to...");
}
if(result == '2'){
Serial.println("Still searching for an operators network to register to...");
}
if(result == '3'){
Serial.println("Network registration denied...");
}
if(result == '4'){
Serial.println("Network registration state unknown, probably still starting up...");
}
if(result == '5'){
Serial.println("Network registered, roaming...");
}
}
}
--> request Modem
byte GSM862::requestModem(const char *command, uint16_t timeout, boolean check, char *buf) {
byte count = 0;
*buf = 0;
modem->flush();
modem->println(command);
count = getsTimeout(buf, timeout);
Serial.print("Debugging command1:");
Serial.println(command);
Serial.print("Debugging count1:");
Serial.println(count);
Serial.print("Debugging buf1:");
Serial.println(buf);
Serial.print("Debugging timeout1:");
Serial.println(timeout);
return count;
}
Like I've mentioned above, it seems that the value out of "result" of the function "checkNetwork" which is actually the value of "buf[21]", displays a cryptic value when displayed on the terminal via Serial.println();
Do you have any idea why or what the exact problem is?
Network registration information (CREG) output depends on modem configuration.
By sending "AT+CREG=0" one can disable network registration code (which is your case)
By sending "AT+CREG=1" one can enable network registration
By sending "AT+CREG=2" one can enable network registration code with
location information (location area code and cell ID)
Options 2. and 3. will also automatically emit +CREG messages upon modem boot/network change.
ps: one should not forget to save current AT configuration by executing AT&W
more details on CREG can be found in "SIM900 AT COMMAND MANUAL"

Resources