1

My Question is How to easily invoke Simple REST Service trigerred using Arduino UNO + ESP8266 WIFI Shield programmed with AT command? I am new to this Subject and gone through many tutorial over internet but so far I could not making this working as per my requirement

My Requirement: Invoke a simple Get request originated from Arduino Uno and via ESP8266 WIFI Shield to submit to server and process the response.

What all I have done so far:

  1. Used below Blynk example to Submit Up time to the Server and View in MobileThis is Working which means hardware setup at my side should be ok, but I dont want to Submit request to Blynk Server and I want to Submit request to my REST server and first of all I want to do a simple GET request may be to any already working API for ex: http://jsonplaceholder.typicode.com/posts/1

    #define BLYNK_PRINT Serial
     #include <ESP8266_Lib.h>
     #include <BlynkSimpleShieldEsp8266.h>
    
     // You should get Auth Token in the Blynk App.
     // Go to the Project Settings (nut icon).
     char auth[] = "xyz";
    
     // Your WiFi credentials.
     // Set password to "" for open networks.
     char ssid[] = "xyz";
     char pass[] = "xyz";
    
     // Hardware Serial on Mega, Leonardo, Micro...
     //#define EspSerial Serial1
    
     // or Software Serial on Uno, Nano...
     #include <SoftwareSerial.h>
     SoftwareSerial EspSerial(2, 3); // RX, TX
    
     // Your ESP8266 baud rate:
     #define ESP8266_BAUD 115200
    
     ESP8266 wifi(&EspSerial);
    
     BlynkTimer timer;
    
     void myTimerEvent()
     {
       // You can send any value at any time.
       // Please don't send more that 10 values per second.
       Blynk.virtualWrite(V5, millis() / 1000);
     }
    
     void setup()
     {
       // Debug console
       Serial.begin(115200);
    
       // Set ESP8266 baud rate
       EspSerial.begin(ESP8266_BAUD);
       delay(10);
    
       Blynk.begin(auth, wifi, ssid, pass);
       // You can also specify server:
       //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
       //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
    
       // Setup a function to be called every second
       timer.setInterval(1000L, myTimerEvent);
     }
    
     void loop()
     {
       Blynk.run();
       timer.run(); // Initiates BlynkTimer
     }
    

Output :

  ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Uno

[517] Connecting to xyz
[13550] AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaea 1
[23591] Failed to connect WiFi
[31752] Ready (ping: 11ms).
[70474] Ready (ping: 12ms).
[86528] Ready (ping: 13ms).

Though I get Failed to connect to wifi but probably it retries and works there after as I can see timer in Mobile APP which is progressing.

  1. Invoke HTTP request using AT Command, but I am getting output as ">" which I am not able to get as why the output is like this:

    #include <SoftwareSerial.h>
    
    SoftwareSerial ESP8266(2, 3);
    
    String Network = "xyz";
    String Password = "xyz";
    
    
    void setup()
    {
    
      Serial.begin(9600);
    
      ESP8266.begin(115200);
      ESP8266.println("AT+CIOBAUD=9600");
      readESP8266(4000);
    
      ESP8266.begin(9600); 
      InitESP8266();
      ConnectToWebsite();
    }
    
    void loop()
    {
       while(ESP8266.available())
       {   
         Serial.println(ESP8266.readString());
       } 
    
    }
    
    /* Function to initialise ESP8266 */
    void InitESP8266()
    { 
    
      ESP8266.println("AT");
      readESP8266(2000);
    
      ESP8266.println("AT+CWMODE=3"); //Wifi mode
      readESP8266(5000);
    
      ESP8266.println("AT+CWJAP=\""+ Network + "\",\"" + Password +"\""); //connect wifi
      readESP8266(10000);
    
      ESP8266.println("AT+CIFSR"); // IP adress
      readESP8266(1000);
    
      ESP8266.println("AT+CIPMUX=1");  // multiple connections
      readESP8266(1000);
    
    }
    
    /* Function to connect to the server */
    void ConnectToWebsite()
    {
    
      ESP8266.println("AT+CIPSTART=1,\"TCP\",\"jsonplaceholder.typicode.com\",80"); //connect to website
      readESP8266(10000);
    
      ESP8266.println("AT+CIPSEND=1,114");
      readESP8266(2000);
    
      String httpreq = "GET /posts/1 HTTP/1.1";
      // Make a HTTP request:
      ESP8266.println(httpreq);
      readESP8266(10000);
    
      readESP8266(5000);
    
      //Serial.println("\r\n");
    }
    
    
    //read and display message
    void readESP8266(const int timeout)
    {
      String reponse = "";
      long int time = millis();
      while( (time+timeout) > millis())
      {
        while(ESP8266.available())
        {
          char c = ESP8266.read();
          reponse+=c;
        }
      }
      Serial.print(reponse);   
    }````
    
    
    
    

Output:

OK

OK
WIFI DISCONNECT
WIFI CONNECTED
WIFI GOT IP

OK
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"abcd"
+CIFSR:STAIP,"192.168.0.169"
+CIFSR:STAMAC,"abcd"

OK

OK
1,CONNECT

OK

OK
> 1,CLOSED
  1. If I use WIFI ESP then I am getting following error

    
    // Emulate Serial1 on pins 6/7 if not present
    #ifndef HAVE_HWSERIAL1
    #include "SoftwareSerial.h"
    SoftwareSerial Serial1(2, 3); // RX, TX
    #endif
    
    char ssid[] = "xyz";            // your network SSID (name)
    char pass[] = "xyz";        // your network password
    int status = WL_IDLE_STATUS;     // the Wifi radio's status
    
    char server[] = "arduino.cc";
    
    // Initialize the Ethernet client object
    WiFiEspClient client;
    
    void setup()
    {
      // initialize serial for debugging
      Serial.begin(115200);
      // initialize serial for ESP module
      Serial1.begin(9600);
      // initialize ESP module
      WiFi.init(&Serial1);
    
      // check for the presence of the shield
      if (WiFi.status() == WL_NO_SHIELD) {
        Serial.println("WiFi shield not present");
        // don't continue
        //while (true);
      }
    
      // attempt to connect to WiFi network
      while ( status != WL_CONNECTED) {
        Serial.print("Attempting to connect to WPA SSID: ");
        Serial.println(ssid);
        // Connect to WPA/WPA2 network
        status = WiFi.begin(ssid, pass);
      }
    
      // you're connected now, so print out the data
      Serial.println("You're connected to the network");
    
      printWifiStatus();
    
      Serial.println();
      Serial.println("Starting connection to server...");
      // if you get a connection, report back via serial
      if (client.connect(server, 80)) {
        Serial.println("Connected to server");
        // Make a HTTP request
        client.println("GET /asciilogo.txt HTTP/1.1");
        client.println("Host: arduino.cc");
        client.println("Connection: close");
        client.println();
      }
    }
    
    void loop()
    {
      // if there are incoming bytes available
      // from the server, read them and print them
      while (client.available()) {
        char c = client.read();
        Serial.write(c);
      }
    
      // if the server's disconnected, stop the client
      if (!client.connected()) {
        Serial.println();
        Serial.println("Disconnecting from server...");
        client.stop();
    
        // do nothing forevermore
        while (true);
      }
    }
    
    
    void printWifiStatus()
    {
      // print the SSID of the network you're attached to
      Serial.print("SSID: ");
      Serial.println(WiFi.SSID());
    
      // print your WiFi shield's IP address
      IPAddress ip = WiFi.localIP();
      Serial.print("IP Address: ");
      Serial.println(ip);
    
      // print the received signal strength
      long rssi = WiFi.RSSI();
      Serial.print("Signal strength (RSSI):");
      Serial.print(rssi);
      Serial.println(" dBm");
    }````
    
    

Output:

````[WiFiEsp] I >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Initializing ESP module
[WiFiEsp] >>> TIMEOUT >>> 
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Cannot initialize ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] No tag found
WiFi shield not present
Attempting to connect to WPA SSID: xyz
[WiFiEsp] >>> TIMEOUT >>>
Please let me know in case you figure out the issue.
  • you don't set the AT baud rate in the WiFiEsp sketch. use AT+UART=9600,8,1,0,0 to set 9600 baud as default – Juraj Aug 6 at 14:26
  • @jsotola tried formatting with ctrl+k but it's not taking effect, looks like a bug in stack overflow :D – Rajesh Aug 6 at 14:52
  • @Juraj meaning set these in WiFi esp sketch code?? – Rajesh Aug 6 at 14:53
  • It’s not a bug in Stack Overflow. – romkey Aug 6 at 15:13
  • doesn't matter how you execute it. it will be remembered as default – Juraj Aug 6 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.