Questo è uno sketch di prova:
#include <CayenneEthernet.h>
#include <SoftwareSerial.h>
#define SSID "MY SSID" //name of wireless access point to connect to
#define PASS "MY PASS" //wifi password
#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#define LED 13
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "MY TOKEN";
SoftwareSerial ss(10, 11);
void setup() //initialise device & connect to access point in setup
{
pinMode(LED, OUTPUT);
reset();
//Serial1.begin(115200); // hardware serial connects to esp8266 module
ss.begin(9600);
Serial.begin(115200); // usb serial connects to to pc
delay(4000); //wait for usb serial enumeration on 'Serial' & device startup
boolean wifi_connected = false; //not connected yet...
for (int i = 0; i < 5; i++) //attempt 5 times to connect to wifi - this is a good idea
{
if (connectWiFi()) //are we connected?
{
wifi_connected = true; //yes
break; //get outta here!
Serial.println(" SEND Cayenne.begin(token) COMMAND ");
Cayenne.begin(token);
}
Serial.print("i= ");
Serial.println(i);
}
if (!wifi_connected) hang("wifi not connected"); //these seem ok - never had a problem
delay(250);
if (!cipmux0()) hang("cipmux0 failed");
delay(250);
if (!cipmode0()) hang("cipmode0 failed");
delay(250);
}
void loop()
{
Serial.println("SEND Cayenne.run() COMMAND");
Cayenne.run();
}
//------------------------------------------------------------------------------------
boolean connectWiFi()
{
Serial.println("Try to connect...");
String cmd = "AT+CWJAP=\""; //form eg: AT+CWJAP="dynamode","55555555555555555555555555"
cmd += SSID;
cmd += "\",\"";
cmd += PASS;
cmd += "\"";
Serial.println(cmd);
ss.println(cmd);
delay(5000); //give it time - my access point can be very slow sometimes
Serial.println(char(ss.read()));
if (ss.find("OK"))
{
Serial.println("Connected to WiFi...");
return true;
//Cayenne.begin(token);
}
else
{
Serial.println("Not connected to WiFi.");
return false;
}
}
//--------------------------------------------------------------------------------
//ditch this in favour of hardware reset. Done
boolean softwarereset()
{
Serial.println("AT+RST");
ss.println("AT+RST");
//if (Serial1.find("ready"))
if (ss.find("ready"))
{
return true;
}
else
{
return false;
}
}
//--------------------------------------------------------------------------------
void reset()
{
Serial.println("Reset with AT+RST");
ss.println("AT+RST");
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
}
//------------------------------------------------------------------------------
boolean cwmode3()
// Odd one. CWMODE=3 means configure the device as access point & station. This function can't fail?
{
Serial.println("AT+CWMODE=3");
ss.println("AT+CWMODE=3");
//if (Serial1.find("no change")) //only works if CWMODE was 3 previously
if (ss.find("no change"))
{
return true;
}
else
{
return false;
}
}
//----------------------------------------------------------------------------------
boolean cipmux0()
{
Serial.println("AT+CIPMUX=0");
ss.println("AT+CIPMUX=0");
//if (Serial1.find("OK"))
if (ss.find("OK"))
{
return true;
}
else
{
return false;
}
}
//-----------------------------------------------------------------------
boolean cipmode0()
{
Serial.println("AT+CIPMODE=0");
ss.println("AT+CIPMODE=0");
//if (Serial1.find("OK"))
if (ss.find("OK"))
{
return true;
}
else
{
return false;
}
}
//------------------------------------------------------------------------
void hang(String error_String) //for debugging
{
Serial.print("Halted... ");
Serial.println(error_String);
while (1)
{
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);
}
}
//----------------------------------------------------------------------------
void hangreset (String error_String) //for debugging
{
Serial.print(error_String);
Serial.println(" - resetting");
reset();
}
E questo è l'output seriale
Try to connect...
AT+CWJAP="MY SSID","MY PASS"
A
Connected to WiFi...
AT+CIPMUX=0
AT+CIPMODE=0
SEND Cayenne.run() COMMAND
Interrogando l'home page del router vedo l'ip della ESP ma il sito cayenne resta sempre in attesa.