tuanpmt / espduino

ESP8266 network client (mqtt, restful) for Arduino
http://tuanpm.net/post/espduino
MIT License
383 stars 122 forks source link

"crash" when connecting (semi-cold) #9

Closed VincentGijsen closed 9 years ago

VincentGijsen commented 9 years ago

Hi

Thanks for the nice port and coding! Unfortunately i've got a crash, the code is waiting indefinitly for a callback...

Perhaps you guys can pinpoint me in the right direction?

the debug output:

MD: 1, cb: 0, ret: 0, argc: 0
Read CRC: 8555, calculated crc:ú(cÖ¼6a…Œ‰aÑ):X $   $քeH)iÿJŒôþ!ÿmode : sta(18:fe:34:9d:f1:e5)
add if0
CMD: 2, cb: 0, ret: 1, argc: 0
Read CRC: 0690, calculated crc: 0690
CMD: Check ready
CMD: Response return value: 1, cmd: 2
ARDUINO: setup mqtt client
CMD: 4, cb: 0, ret: 1, argc: 9
Arg[0], len: 8:45-53-50-38-32-36-36-00-

Arg[1], len: 8:61-64-6D-69-6E-00-00-00-

Arg[2], len: 4:31-32-33-34-

Arg[3], len: 4:0A-00-00-00-

Arg[4], len: 4:01-00-00-00-

Arg[5], len: 4:6A-05-00-00-

Arg[6], len: 4:70-05-00-00-

Arg[7], len: 4:76-05-00-00-

Arg[8], len: 4:7C-05-00-00-

Read CRC: 9976, calculated crc: 9976
MQTT: clientid = ESP8266, user = admin, pass = 1234, keepalive = 10, session = 1
MQTT_InitClient
CMD: Response return value: 1073687384, cmd: 4
ARDUINO: setup mqtt lwt
CMD: 9, cb: 0, ret: 1, argc: 5
Arg[0], len: 4:58-2B-FF-3F-

Arg[1], len: 4:2F-6C-77-74-

Arg[2], len: 8:6F-66-66-6C-69-6E-65-00-

Arg[3], len: 4:00-00-00-00-

Arg[4], len: 4:00-00-00-00-

Read CRC: AF54, calculated crc: AF54
MQTT: lwt client addr = 1073687384
MQTT: lwt topic = /lwt, message = offline, qos = 0, retain = 0
CMD: Response return value: 1, cmd: 9
ARDUINO: setup wifi
ARDUINO: system started
Dht Sensor Started
CMD: 3, cb: 842, ret: 0, argc: 2
Arg[0], len: 8:67-61-73-74-32-5F-34-00-

Arg[1], len: 16:76-69-6E-63-65-6E-74-64-65-67-65-6B-73-74-65-00-

Read CRC: 23C2, calculated crc: 23C2
WIFI_INIT
WIFI: set ssid = gast2_4, pass= 1234
STATION_IDLE
CMD: 6, cb: 0, ret: 0, argc: 1
Arg[0], len: 4:58-2B-FF-3F-

Read CRC: BF84, calculated crc: BF84
scandone
STATION_IDLE
add 0
aid 1
pm open phy_2,type:2 0 0
cnt 

connected with gast2_4, channel 9
dhcp client start...
STATION_IDLE
STATION_IDLE
ip:192.168.1.101,mask:255.255.255.0,gw:192.168.1.1
WIFI CONNECTED
CMD: 5, cb: 0, ret: 0, argc: 4
Arg[0], len: 4:58-2B-FF-3F-

1:1883
MTT: Sending type: 1, i: 0000
TCP: Sent
TCP: data received 4 bytes
MQTT: Connected to 192.168.1.131:1883
MQTT: Connected
Callback data: 1386, 1392, 1398, 1404

after this point, the arduino doesn't print anything to the debug output, nor is my blinking action showing.

The code is used:

#include <SoftwareSerial.h>

#include <espduino.h>
#include <mqtt.h>
#include <avr/wdt.h>

#include <RemoteTransmitter.h>
//#include <VirtualWire.h>

#include "DHT.h"
#define DHTTYPE DHT11   // DHT 11 

#define PIN_TX A1
#define PIN_LDR A0
#define PIN_DHT 4
#define LED 13

//
// Putting on light 1: 31,A,01 -> 1F 41 01 
//

//Transmitter stuff
ElroTransmitter elroTransmitter(PIN_TX);
ActionTransmitter actionTransmitter(PIN_TX);
KaKuTransmitter kaKuTransmitter(PIN_TX);
BlokkerTransmitter blokkerTransmitter(PIN_TX);

//DHT / TEMP stuff
DHT dht(PIN_DHT, DHTTYPE);

int temperature;
int humidity;
int light;

long lastMillis = millis();
long lastBlink = millis();
int lastUpdated = 0;

SoftwareSerial debugPort(2, 3); // RX, TX
ESP esp(&Serial, &debugPort, 4);
MQTT mqtt(&esp);
boolean wifiConnected = false;
boolean _mqttConnected = false;

void wifiCb(void* response)
{
  uint32_t status;
  RESPONSE res(response);

  if(res.getArgc() == 1) {
    res.popArgs((uint8_t*)&status, 4);
    if(status == STATION_GOT_IP) {
      debugPort.println("WIFI CONNECTED");
      mqtt.connect("192.168.1.131", 1883, false);
      wifiConnected = true;
      //or mqtt.connect("host", 1883); /*without security ssl*/
    } 
    else {
      wifiConnected = false;
      mqtt.disconnect();
    }

  }
}

void mqttConnected(void* response)
{
  debugPort.println("Connected");
  mqtt.subscribe("/remote/elro");
  mqtt.subscribe("/topic/0"); //or mqtt.subscribe("topic"); /*with qos = 0*/
  mqtt.subscribe("/topic/1");
  mqtt.subscribe("/topic/2");
  mqtt.publish("/topic/0", "data0");
  publishInt("/hub/init", 1234);

  _mqttConnected = true;
  debugPort.println(":) :) :) connected");
}
void mqttDisconnected(void* response)
{
  _mqttConnected = false;
  debugPort.println("!!!ERROR, disconnected");

}
void mqttData(void* response)
{
  RESPONSE res(response);

  debugPort.print("Received: topic=");
  String topic = res.popString();
  debugPort.println(topic);

  debugPort.print("data=");
  String data = res.popString();
  debugPort.println(data);

  if(topic== "/remote/elro"){
    handleElro(data); 
  }

}
void mqttPublished(void* response)
{
}

void handleElro(String cmd){
  int code = getValue(cmd, ',', 0).toInt();
  char device = getValue(cmd, ',', 1)[0];
  int state = getValue(cmd, ',', 2).toInt();    
  //elro needs systemcode (numerical), device (upper char), and boolean
  debugPort.println("sending");
  debugPort.println(code,DEC);
  debugPort.println(device);
  debugPort.println(state);
  elroTransmitter.sendSignal(code, device, state);   
}

void setup() {
  Serial.begin(19200);
  debugPort.begin(19200);
  debugPort.println("Setup..");
  ;

 // vw_set_tx_pin(PIN_TX);
  // vw_set_ptt_inverted(true); // Required for DR3100
 // vw_setup(2000);  // Bits per sec
  debugPort.println("VirtualWire stared");

  esp.enable();
  delay(500);
  esp.reset();
  delay(500);
  while(!esp.ready());

  debugPort.println("ARDUINO: setup mqtt client");
  if(!mqtt.begin("ESP8266", "admin", "1234", 10, 1)) {
    debugPort.println("ARDUINO: fail to setup mqtt");
    while(1);
  }

  debugPort.println("ARDUINO: setup mqtt lwt");
  mqtt.lwt("/lwt", "offline", 0, 0); //or mqtt.lwt("/lwt", "offline");

  /*setup mqtt events */
  mqtt.connectedCb.attach(&mqttConnected);
  mqtt.disconnectedCb.attach(&mqttDisconnected);
  mqtt.publishedCb.attach(&mqttPublished);
  mqtt.dataCb.attach(&mqttData);

  /*setup wifi*/
  debugPort.println("ARDUINO: setup wifi");
  esp.wifiCb.attach(&wifiCb);

  esp.wifiConnect("gast2_4","vincentdegekste");

  debugPort.println("ARDUINO: system started");

  //start DHT sensor
  dht.begin();
  debugPort.println("Dht Sensor Started");

}

void publishInt(const char* key, int data){
  char t[5];
  String t1;
  t1 = String(data);
  t1.toCharArray(t, 5);
  mqtt.publish(key, t);
}
void loop() {
  blinker();
  esp.process();
  if(wifiConnected) {
    light = analogRead(PIN_LDR);

    humidity = dht.readHumidity();
    temperature = dht.readTemperature();

    if(millis() - lastMillis >= 10000){
      mqtt.publish("/hub/ping", "a");
      if(_mqttConnected){
     //   debugPort.print("=====");
       // debugPort.print(millis(),DEC);
     //   debugPort.println("=====");

        switch(lastUpdated){
        case 0:
         publishInt("/hub/light", light);  
         // debugPort.print("light:" );
         // debugPort.println(light, DEC);
          break;

        case 1:
          publishInt("/hub/temperature", temperature);
         // debugPort.print("temperature:" );
         // debugPort.println(temperature, DEC);
          break;

        case 2:
          publishInt("/hub/humidity", humidity);
        //  debugPort.print("humidity:" );
        //  debugPort.println(humidity, DEC);
          break; 

        case 3:
         // publishInt("/hub/ping", millis());
          break;

        }
        lastUpdated++;
        if (lastUpdated > 3)
          lastUpdated = 0;
      }
      lastMillis = millis();
    }
  }
}

void blinker(){
  if(!wifiConnected){
    if(millis() - lastBlink >= 100){
      digitalWrite(LED, !digitalRead(LED));
      lastBlink = millis();
    }
  }
  else if(!_mqttConnected){
    if(millis() - lastBlink >= 250){
      digitalWrite(LED, !digitalRead(LED));
      lastBlink = millis();
    }
  }
  else{
    //running 'stable'
    if(millis() - lastBlink >= 750){
      digitalWrite(LED, !digitalRead(LED));
      lastBlink = millis();
    }
  }

}

//
// Helper stuff
//
String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {
    0, -1              };
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
      found++;
      strIndex[0] = strIndex[1]+1;
      strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

Cheers!

tuanpmt commented 9 years ago

Try new esp8266 firmware and new arduino example