There is a new Chinese module on the market which will enable enthousiasts to build their own IoT (internet of Things) applications at relative low cost. Time to try them out. I bought 5 of them at AliExpress and received them yesterday. Today I go a simple application working (mostly borrowed code from the WEB). The code I use is:
[cpp]
//
// libraries
//
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "JsonParser.h"
#include <avr/wdt.h>
using namespace ArduinoJson::Parser;
//
// global defines
//
#define SSID "your SSID" // WiFi lan at home
#define PASS "your SSID Password" // password for WiFi lan
#define CONNECT_RETRIES 5 // allow 5 retries to connect to WiFi
#define DST_IP "188.226.224.148" //baidu.com
#define LOCATIONID "2925533" // location id
#define DEBUG_RX 10 // pin 10 is used for receiving input from debug serial
#define DEBUG_TX 11 // pin 11 is used for sending output to debug serial
SoftwareSerial dbgSerial(DEBUG_RX, DEBUG_TX); // used for debugging
JsonParser<32> parser;
//
// function prototypes
//
boolean connectWiFi(); // function to connect to WiFi
void software_Reboot();
//
// global variables
//
char cmd[100]; // buffer used build messages
char buffer[100];
/**
* @name setup()
* initialize the program
*/
void setup() {
//
// Open serial communication with WiFi module
//
Serial.begin(115200);
Serial.setTimeout(5000);
//
// open debug serial
//
dbgSerial.begin(9600); //can’t be faster than 19200 for softserial
dbgSerial.println("ESP8266 Demo"); // show demo is starting
//
//test if the module is ready
//
bool moduleReady = false;
for (uint8_t i = 0; i < CONNECT_RETRIES; i++){
Serial.println("AT+RST"); // reset the module
delay(1000);
if (Serial.find("OK")) {
//
// the module is ready to receive commands
//
dbgSerial.println("Module is ready");
moduleReady = true;
break;
} else {
dbgSerial.println("Module is not responding");
}
}
if (!moduleReady) {
//
// module is not responding
//
dbgSerial.println("Module non functional.");
//
// reboot and try again
//
software_Reboot();
}
// delay(1000);
//
// now connect to the wifi
//
boolean connected = false;
for (int i = 0; i < CONNECT_RETRIES; i++) {
//
// check if we are connected
//
if (connectWiFi()) {
//
// we have a connection
//
connected = true;
//
// done
//
break;
}
}
//
// if we cannot connect no use continuing
//
if (!connected) {
//
// inform user
//
dbgSerial.println("Cannot connect to WiFi lan");
while (1) {};
}
delay(5000);
//
// show the IP address we have received
//
Serial.println("AT+CIFSR");
delay(3000);
//
// wait for incoming message with IP address
//
while (!Serial.available()){};
//
// copy the message to output
//
uint16_t bufferIndex = 0;
while (Serial.available()) {
char x = Serial.read();
//
// ignore any CR’s, LF’s and zero characters
//
if (x != 0x0D && x != 0x0A && x != 0x0) {
//
// add character to buffer
//
buffer[bufferIndex] = x;
bufferIndex++;
}
}
//
// put in a string terminator
//
buffer[bufferIndex] = ‘\0’;
//
// and display the IP address
//
dbgSerial.print("ip address: ");
dbgSerial.println(&buffer[8]);
//
// set the single connection mode
//
Serial.println("AT+CIPMUX=0");
}
void loop() {
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += DST_IP;
cmd += "\",80";
Serial.println(cmd);
dbgSerial.println(cmd);
if(Serial.find("Error")) return;
cmd = "GET /data/2.5/weather?id=";
cmd += LOCATIONID;
cmd += " HTTP/1.0\r\nHost: api.openweathermap.org\r\n\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if(Serial.find(">")){
dbgSerial.print(">");
}else{
Serial.println("AT+CIPCLOSE");
dbgSerial.println("connection timeout");
delay(1000);
return;
}
Serial.print(cmd);
unsigned int i = 0; //timeout counter
int n = 1; // char counter
char json[100]="{";
while (!Serial.find("\"main\":{")){} // find the part we are interested in.
while (i<60000) {
if(Serial.available()) {
char c = Serial.read();
json[n]=c;
if(c==’}’) break;
n++;
i=0;
}
i++;
}
dbgSerial.println(json);
JsonObject root = parser.parse(json);
double temp = root["temp"];
double pressure = root["pressure"];
double humidity = root["humidity"];
temp -= 273.15; // from kelvin to degree celsius
dbgSerial.println(temp);
dbgSerial.println(pressure);
dbgSerial.println(humidity);
dbgSerial.println("====");
delay(30000);
}
boolean connectWiFi() {
Serial.println("AT+CWMODE=1");
strcpy(cmd,"AT+CWJAP=\"");
strcat(cmd,SSID);
strcat(cmd, "\",\"");
strcat(cmd, PASS);
strcat(cmd, "\"");
dbgSerial.println(cmd);
Serial.println(cmd);
delay(2000);
if (Serial.find("OK")) {
dbgSerial.println("OK, Connected to WiFi.");
return true;
} else {
dbgSerial.println("Can not connect to the WiFi.");
return false;
}
}
void software_Reboot()
{
wdt_enable(WDTO_15MS);
while(1)
{
}
}
[/cpp]
But most important…. It works :). I used a simple DC-DC switcher to convert from 5V -> 3.3V. No level shifters… Sofar the chip still works and does not get hot or anything. Pretty straight forward. Next steps:
- Connect the module to a local webserver on my home router
- See if I can switch things on/off through an app connected on my local LAN.
Ik heb nu net ook zo’n developer board erbij besteld. Die heeft alle pinnen naar buiten. Ik weet niet of je al onder Eclipse werkt? Bij mij werkt het als een tierelier onder Ubuntu. Ik heb nu een ontwikkelomgeving voor die ESP’s maar ook voor de Arduino en uiteraard de Teensy’s. Werkt lekker 🙂
Dat is een heel leuk device. Ik gebruik nu 430MHz transceivers. Maar dit is veel mooier.
Ik ga meteen een paar bestellen.
Wim
Hallo,
Wij zijn ook met de ESP8266 Module aan het spelen. Geweldig ding voor weinig geld in vergelijking met andere WiFI Modules (RN-171, MRF24WB0MA etc)
Overigens komt de module ook uit in een iLGA (inspectable Land Grid Array) package waarbij je dan voor een externe antenne kunt kiezen.
Leuke site !!!