Hello Dudes and Dudettes,
I have flshed my NodeMCU with some Code and I will not connect to my internet proberly I think.
I want to use Amazon Alexa and IFTTT to comunicate with the Node and a hooked up relais to create a wifi switch.
this is the code i used for it:
/*
*Netmedias
*
* Created on: 24.05.2015
*
*/
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h>
#include <Hash.h>
// @@@@@@@@@@@@@@@ You only need to midify modify wi-fi and domain info @@@@@@@@@@@@@@@@@@@@
const char* ssid = "**Censored :D **"; //enter your ssid/ wi-fi(case sensitive) router name - 2.4 Ghz only
const char* password = "**Censored :D **"; // enter ssid password (case sensitive)
char host[] = "https://blackholecorealexa.herokuapp.com/"; //192.168.0.100 -enter your Heroku domain name like "alexagoogleifttt.herokuapp.com"
// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
int port = 80;
char path[] = "/ws";
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
const int relayPin = 5;
DynamicJsonBuffer jsonBuffer;
String currState;
int pingCount = 0;
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { //uint8_t *
switch(type) {
case WStype_DISCONNECTED:
Serial.println("Disconnected! ");
Serial.println("Connecting...");
webSocket.begin(host, port, path);
webSocket.onEvent(webSocketEvent);
break;
case WStype_CONNECTED:
{
Serial.println("Connected! ");
// send message to server when Connected
webSocket.sendTXT("Connected");
}
break;
case WStype_TEXT:
Serial.println("Got data");
//data = (char*)payload;
processWebScoketRequest((char*)payload);
break;
case WStype_BIN:
hexdump(payload, length);
Serial.print("Got bin");
// send data to server
webSocket.sendBIN(payload, length);
break;
}
}
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
pinMode(relayPin, OUTPUT);
for(uint8_t t = 4; t > 0; t--) {
delay(1000);
}
Serial.println();
Serial.println();
Serial.print("Connecting to ");
//Serial.println(ssid);
WiFiMulti.addAP(ssid, password);
//WiFi.disconnect();
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("Connected to wi-fi");
webSocket.begin(host, port, path);
webSocket.onEvent(webSocketEvent);
}
void loop() {
webSocket.loop();
//If you make change to delay make sure adjust the ping
delay(2000);
// make sure after every 40 seconds send a ping to Heroku
//so it does not terminate the websocket connection
//This is to keep the conncetion alive between ESP and Heroku
if (pingCount > 20) {
pingCount = 0;
webSocket.sendTXT("\"heartbeat\":\"keepalive\"");
}
else {
pingCount += 1;
}
}
void processWebScoketRequest(String data){
JsonObject& root = jsonBuffer.parseObject(data);
String device = (const char*)root["device"];
String location = (const char*)root["location"];
String state = (const char*)root["state"];
String query = (const char*)root["query"];
String message="";
Serial.println(data);
Serial.println(state);
if(query == "cmd"){ //if query check state
Serial.println("Recieved command!");
if(state=="on"){
digitalWrite(relayPin, HIGH);
message = "{\"state\":\"ON\"}";
currState = "ON";
}else{
digitalWrite(relayPin, LOW);
message = "{\"state\":\"OFF\"}";
currState = "OFF";
}
}else if(query == "?"){ //if command then execute
Serial.println("Recieved query!");
int state = digitalRead(relayPin);
if(currState=="ON"){
message = "{\"state\":\"ON\"}";
}else{
message = "{\"state\":\"OFF\"}";
}
}else{//can not recognized the command
Serial.println("Command is not recognized!");
}
Serial.print("Sending response back");
Serial.println(message);
// send message to server
webSocket.sendTXT(message);
if(query == "cmd" || query == "?"){webSocket.sendTXT(message);}
}
and thats the failure code that comes out
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 3
cnt
connected with Hellmich, channel 1
dhcp client start...
ip:192.168.178.56,mask:255.255.255.0,gw:192.168.178.1
Connecting to Connected to wi-fi
pm open,type:2 0
Disconnected!
Connecting...
Disconnected!
Connecting...
Disconnected!
Connecting...
Disconnected!
I really hope that you can help me out of this ...
Kind regards
blackholecore