ESP8266 and HCP Internet Of Things Services (BETA) – Part 3
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.
- esp8266/Arduino · GitHub
- Instructible Tutorial
- 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
- We connect to an access point by providing ssid and password.
- In a loop, we read humidity and temperature.
- Connect to the web server and fire GET request on HCP_IOT_HTTPS_Relay.php with parameters as Token, DeviceID and Message.
- 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 🙂
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.
Hello Prashantha,
Again a high quality blog from you, thank you for your effort.
i read esp8266 forums and realized that nodeMcu is more active then Arduino in forums. Why do you prefer to use Arduino? As a software developer it's difficult to understand hardware side for me so can you guide the community to choose development board.
Regards
Basar Ozgur
Basar, Prasanth,
While I realize these posts are 1 year+ old, I did finally manage to get a blog completed on NodeMCU, Lua and HCP. If intrested, check it out here: NodeMCU and SAP HCP IoT. Enjoy.
Regards,
-Tiest
Tiest,
I checked your blog. it's very informative.
Thank you
Hi Basar Ozgur,
I too not an expert in just that I started little earlier than you 🙂 .
I started with NodeMCU but was not comfortable with Lua language. Sometimes Esplorer used to fail for no reason. It was more comfortable for me to work with Arduino IDE and C like language quiet easy for me to understand. It worked fine for me. I have not checked NodeMCU lately though. If my project absolutely needs anything which is ported only NodeMCU I would not hesitate to use that too.
I use this board. Mainly because it is easily available in my Country (India).
Regulated 3.3 Volts is important and easy indicators in the form of LEDs to at least to know what's going on. I prefer separate USB to TTL converter (like CP2102) as sometimes it is required to use multiple ESP8266 in many practical applications and an integrated USB to TTL on the board is overkill. But If you are starting with ESP8266 it is better to have an easily usable boards as sometimes it is hard to figure out whether problem is with wires or something else.
Just my thoughts on this. Basically I bought a board instead of a chip module as it is little easy to start and availability is also a factor.
Thanks and Regards,
Prashanth
Hi Prashantha,
Thank you for sharing about your experience, this can be another blog topic for newbies, i think you should consider to write another blog seriously 😉
In addition, this is a valuable blog about IoT topic but it gets only 1 like and 1 rating 🙁 i wish community will realize your blog's value later.
Thank you again for your effort
Regards
Basar Ozgur
Hi Basar Ozgur,
Thanks you for your compliments.
I think, community is still in initial stages in IoT implementations and well known devices like raspberry pi and arduino and the help/documents/projects around these devices is lot around. ESP8266 is relatively new chip whose most plus points are price and size.
It has it's own limitations as mentioned in Part-2. May be going forward these limitations might get reduced once SAP supports MQTT protocol in SAP IoT services.
It is good to know that blog helped some people.
Thanks and Regards,
Prashanth
Hi Prasantha,
very nice article,This gives me a detailed knowledge on ESP8266. But I have a small doubt. In your code, you write WiFi.begin(ssid, password); . What if i want to connect my device (esp8266) with SAP corporate network? How can I provide ssid, username and password?
Thanks
Nishant
Hi Nishant,
This is a limitation in ESP8266, One cannot use WPA2 Enterprise security with this chip as of now. I have mentioned this limitation in Part 1 of the this blog series. 🙂
Regards,
Prashanth
Hi Prashantha H J,
I am using Arduino Uno with ESP8266 and connecting to HCP using "AT" Commands. But I am getting below response, any idea how to use AT command for sending data to HCP.
AT+CIPSEND=
OK
>
Recv 291 bytes
SEND OK
+IPD,220:HTTP/1.0 302 Found
Loc
Thanks
Hi Yogendra Ahuja
I am sorry, I have never used AT commands..
Thanks