Hello. I am working on a project, trying to communicate between an Arduino 101 and an ESP8266 (ESP-12) running ESP-Link. The code I am using runs but it does not want to synchronize with the ESP. If I run the same code in a Uno with the same connections, it works flawlessly. So I have been wondering what may be wrong??? Could it be a voltage incompatibility? Library incompatibility? Any ideas?
Here is the code I am using:
Certainly not enough info to make a guess right now
Where did you get the libraries ?
Are the libs AVR specific ?
Do you have the full schematic for what you have hooked up to the board and how ?
BTW when posting code please do it using code tags ( </> ).
It is a lot easier for all to see in here and quite often a quick fix can be spotted and made.
The code and libraries are part of the examples provided with ESP-Link.
Here's the code. The only thing that changes is the URL and API stuff. As said, it works in Arduino Uno. I dont know the details of the libraries, but it communicates through serial...
/**
* \file
* ESP8266 RESTful example
* \author
* BeeGee
*/
#include <ELClient.h>
#include <ELClientRest.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
//###########################################################
// For ARDUINO UNO WIFI with I2C to serial chip!
//###########################################################
// Serial port to ESP8266
// #include <SC16IS750.h>
// SC16IS750 i2cuart = SC16IS750(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_AA);
// Initialize a connection to esp-link using the I2Cuart chip of the Arduino Uno WiFi board for
// SLIP messages.
// ELClient esp(&i2cuart);
//###########################################################
// For boards using the hardware serial port!
//###########################################################
// Initialize a connection to esp-link using the normal hardware serial port both for
// SLIP and for debug messages.
//ELClient esp(&Serial, &Serial);
ELClient esp(&mySerial, &mySerial);
// Initialize a REST client on the connection to esp-link
ELClientRest rest(&esp);
boolean wifiConnected = false;
// Callback made from esp-link to notify of wifi status changes
// Here we print something out and set a global flag
void wifiCb(void *response) {
ELClientResponse *res = (ELClientResponse*)response;
if (res->argc() == 1) {
uint8_t status;
res->popArg(&status, 1);
if(status == STATION_GOT_IP) {
Serial.println("WIFI CONNECTED");
wifiConnected = true;
} else {
Serial.print("WIFI NOT READY: ");
Serial.println(status);
wifiConnected = false;
}
}
}
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
//###########################################################
// For ARDUINO UNO WIFI with I2C to serial chip!
//###########################################################
// i2cuart.begin(9600);
Serial.println("");
Serial.println("EL-Client starting!");
// Sync-up with esp-link, this is required at the start of any sketch and initializes the
// callbacks to the wifi status change callback. The callback gets called with the initial
// status right after Sync() below completes.
esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired)
bool ok;
do {
ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds
if (!ok) {
Serial.print("\nEL-Client sync failed! err: ");
Serial.println(ok);
}
} while(!ok);
Serial.println("EL-Client synced!");
// Wait for WiFi to be connected.
Serial.println("esp.GetWifiStatus()");
esp.GetWifiStatus();
ELClientPacket *packet;
Serial.println("Waiting for WiFi ");
if ((packet=esp.WaitReturn()) != NULL) {
Serial.print(".");
Serial.println(packet->value);
}
Serial.println("");
// Set up the REST client to talk to api.thingspeak.com, this doesn't connect to that server,
// it just sets-up stuff on the esp-link side
int err = rest.begin("api.myurl.com");
if (err != 0) {
Serial.print("REST begin failed: ");
Serial.println(err);
while(1) ;
}
Serial.println("EL-REST ready");
}
float solarValue = 99.5;
// Change to your own API key
char *api_key = "myapikey";
// expand buffer size to your needs
#define BUFLEN 266
void loop() {
// process any callbacks coming from esp_link
esp.Process();
// if we're connected make an REST request
if(wifiConnected) {
// Generate a fake value starting from 100 going up to 300
solarValue = solarValue + 0.5;
if (solarValue == 300) {
solarValue = 100;
}
String solarValString = String(solarValue);
const char *solarValChar = solarValString.c_str();
// Reserve a buffer for sending the data
char path_data[BUFLEN];
// Copy the path and API key into the buffer
sprintf(path_data, "%s", "/?token=");
sprintf(path_data + strlen(path_data), "%s", api_key);
// Copy the field number and value into the buffer
// If you have more than one field to update,
// repeat and change field1 to field2, field3, ...
sprintf(path_data + strlen(path_data), "%s", "&time=");
sprintf(path_data + strlen(path_data), "%d", millis());
// Send PUT request to thingspeak.com
rest.post(path_data,"");
//rest.post("/?token=eaaeebace112754929d81074576e9e1b&time=12345","");
// Reserve a buffer for the response from Thingspeak
char response[BUFLEN];
// Clear the buffer
memset(response, 0, BUFLEN);
// Wait for response from Thingspeak
uint16_t code = rest.waitResponse(response, BUFLEN-1);
// Check the response from Thingspeak
if(code == HTTP_STATUS_OK){
Serial.println("Thingspeak: POST successful:");
Serial.print("Response: ");
Serial.println(response);
} else {
Serial.print("Thingspeak: POST failed with error ");
Serial.println(code);
Serial.print("Response: ");
Serial.println(response);
}
// Send next data in 2 seconds
delay(2000);
}
}
I noticed yesterday that the code above was wrong but the forum didnt let me post again... Here is the correct code:
/**
* \file
* ESP8266 RESTful example
* \author
* BeeGee
*/
#include <ELClient.h>
#include <ELClientRest.h>
//###########################################################
// For ARDUINO UNO WIFI with I2C to serial chip!
//###########################################################
// Serial port to ESP8266
// #include <SC16IS750.h>
// SC16IS750 i2cuart = SC16IS750(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_AA);
// Initialize a connection to esp-link using the I2Cuart chip of the Arduino Uno WiFi board for
// SLIP messages.
// ELClient esp(&i2cuart);
//###########################################################
// For boards using the hardware serial port!
//###########################################################
// Initialize a connection to esp-link using the normal hardware serial port both for
// SLIP and for debug messages.
ELClient esp(&Serial, &Serial);
// Initialize a REST client on the connection to esp-link
ELClientRest rest(&esp);
boolean wifiConnected = false;
// Callback made from esp-link to notify of wifi status changes
// Here we print something out and set a global flag
void wifiCb(void *response) {
ELClientResponse *res = (ELClientResponse*)response;
if (res->argc() == 1) {
uint8_t status;
res->popArg(&status, 1);
if(status == STATION_GOT_IP) {
Serial.println("WIFI CONNECTED");
wifiConnected = true;
} else {
Serial.print("WIFI NOT READY: ");
Serial.println(status);
wifiConnected = false;
}
}
}
void setup() {
Serial.begin(9600);
//###########################################################
// For ARDUINO UNO WIFI with I2C to serial chip!
//###########################################################
// i2cuart.begin(9600);
Serial.println("");
Serial.println("EL-Client starting!");
// Sync-up with esp-link, this is required at the start of any sketch and initializes the
// callbacks to the wifi status change callback. The callback gets called with the initial
// status right after Sync() below completes.
esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired)
bool ok;
do {
ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds
if (!ok) {
Serial.print("\nEL-Client sync failed! err: ");
Serial.println(ok);
}
} while(!ok);
Serial.println("EL-Client synced!");
// Wait for WiFi to be connected.
Serial.println("esp.GetWifiStatus()");
esp.GetWifiStatus();
ELClientPacket *packet;
Serial.println("Waiting for WiFi ");
if ((packet=esp.WaitReturn()) != NULL) {
Serial.print(".");
Serial.println(packet->value);
}
Serial.println("");
// Set up the REST client to talk to api.thingspeak.com, this doesn't connect to that server,
// it just sets-up stuff on the esp-link side
int err = rest.begin("api.nubeiot.com");
if (err != 0) {
Serial.print("REST begin failed: ");
Serial.println(err);
while(1) ;
}
Serial.println("EL-REST ready");
}
float solarValue = 99.5;
// Change to your own API key
char *api_key = "eaaeebace112754929d81074576e9e1b";
// expand buffer size to your needs
#define BUFLEN 266
void loop() {
// process any callbacks coming from esp_link
esp.Process();
// if we're connected make an REST request
if(wifiConnected) {
// Generate a fake value starting from 100 going up to 300
solarValue = solarValue + 0.5;
if (solarValue == 300) {
solarValue = 100;
}
String solarValString = String(solarValue);
const char *solarValChar = solarValString.c_str();
// Reserve a buffer for sending the data
char path_data[BUFLEN];
// Copy the path and API key into the buffer
sprintf(path_data, "%s", "/?token=");
sprintf(path_data + strlen(path_data), "%s", api_key);
// Copy the field number and value into the buffer
// If you have more than one field to update,
// repeat and change field1 to field2, field3, ...
sprintf(path_data + strlen(path_data), "%s", "&time=");
sprintf(path_data + strlen(path_data), "%d", millis());
// Send PUT request to thingspeak.com
rest.post(path_data,"");
//rest.post("/?token=eaaeebace112754929d81074576e9e1b&time=12345","");
// Reserve a buffer for the response from Thingspeak
char response[BUFLEN];
// Clear the buffer
memset(response, 0, BUFLEN);
// Wait for response from Thingspeak
uint16_t code = rest.waitResponse(response, BUFLEN-1);
// Check the response from Thingspeak
if(code == HTTP_STATUS_OK){
Serial.println("Thingspeak: POST successful:");
Serial.print("Response: ");
Serial.println(response);
} else {
Serial.print("Thingspeak: POST failed with error ");
Serial.println(code);
Serial.print("Response: ");
Serial.println(response);
}
// Send next data in 2 seconds
delay(2000);
}
}