Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
prashantha_hj
Employee
Employee

In Part-2, we found a way to overcome the limitations of ESP8266. In this part we will program ESP8266. There are several ways of programming ESP8266, in this blog we will use Arduino IDE to program the module.

Downloading and Configuring Arduino IDE to program ESP8266

Download and Install Arduino IDE for ESP8266 from this github page. Follow the steps mentioned in the github page to install the Arduino IDE for ESP8266. See the pictures below to for a quick check on your installation.

          1. Download the Arduino Software from the link provided in the github page. I downloaded "Windows zip file for non admin install".

         2. Open the Arduino IDE by clicking on arduino.exe

        3. Select the Boards Manager..

        4. Install esp8266 board by clicking on the "Install" button. Installation takes couple of minutes.

        5. Now select "Generic ESP8266 Module" as your board.

     6. We use DHT11 sensor hence we have to add the library to Arduino IDE so that we can make use of this library while programming ESP8266. We have to download .zip file from github for DHT sensor. Add this zip file as library.

       7. Select the zip file downloaded previously to add it to IDE.

Setup the circuit and connect to USB


Part-1 has circuit connection details. There are several resources online to help you with this.

  1. esp8266/Arduino · GitHub
  2. Instructible Tutorial
  3. If you have Arduino UNO, you can use it instead of CP2102. There is a video which explains how to do it.

Program ESP8266

You can write program for ESP8266 as if it is Arduino board. The syntax remains same. If you are new to both ESP8266 or Arduino you might have to spend some time on reading through some basics.

Programming ESP8266 is pretty simple

  1. We connect to an access point by providing ssid and password.
  2. In a loop, we read humidity and temperature.
  3. Connect to the web server and fire GET request on HCP_IOT_HTTPS_Relay.php with parameters as Token, DeviceID and Message.
  4. Repeat this for 20 seconds.


#include <DHT.h>
#include <ESP8266WiFi.h>
const char* ssid = "your-ssid";
const char* password = "your-password";

const char* server = "192.168.1.3"; //Change it to the IP Address where your Palapa Webserver. Please see Part-2 of this blog
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE,11);
WiFiClient client;
  

void setup() {               
  Serial.begin(9600);
  delay(10);
  dht.begin();
 
  WiFi.begin(ssid, password);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
}


void loop() {
  
  int h = dht.readHumidity();
  int t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  if (client.connect(server,9090)) { 
    String postStr = "";
           postStr +="token=<<your oauth tocken>>";
           postStr +="&device=90c8ca3d-b1a7-4496-889f-f9305b2892e2"; //your device id
           postStr +="&message={%22mode%22:%22sync%22,%22messageType%22:%221%22,%22messages%22:[{%22temperature%22:"+String(t)+",%22humidity%22:"+String(h)+"}]}";
     client.println("GET /HCP_IOT_HTTPS_Relay.php?"+postStr+" HTTP/1.0");
     client.println();
     Serial.print("Temperature: ");
     Serial.print(t);
     Serial.print(" degrees Celcius Humidity: ");
     Serial.print(h);
     Serial.println("% send to HCP");
  }
  client.stop();
  Serial.println("Waiting...");   
  delay(20000); 
}






Compiling and Uploading the program to ESP8266

Once you connect CP2102 to one of computer's USB, a COM port is assigned to it. We can find the COM port in Device Manager. In the Arduino IDE, we have to select that COM port.

Upload by clicking on the icon shown in the picture.

Sometimes upload might not work for several reasons. Please watch this video if you are stuck :smile:

Test the data uploaded to HCP

Login to IOT services application to check whether the data is being sent to HCP

Conclusion

In this blog series I just tried to provide one of the ways to send sensor data to Hana Cloud Platform using ESP8266 micro-controller. ESP8266 is cheaper and more practical way to do projects which involves sensing data from multiple sensors located at different locations. Size of the device is very small which is an added advantage.

If you are starting with ESP8266, it is better if do some research and buy a development board which makes prototyping quick and easy.

10 Comments