-->
Page 1 of 2

MQtt, timers, gpio interrupts

PostPosted: Thu Apr 23, 2015 4:32 am
by cherowley
Hi all,

I'm trying to use mqtt and I presume there are restrictions on where objects can be used?

After the pin13Int is called, the led lights ok.

The lightOut void is called and the serial message is displayed but the led does not go off and no mqtt message is published.
I then get another call straight away to pin13Int even though there's no more activity on pin 13..

Any ideas anyone? Thanks :) BTW - I don't know c, not used arduino dev env before and just trying to get basic things to work hence dodgy code ...

Code: Select all#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <Ticker.h>

const char* ssid = "wifi";
const char* password = "passy";
char* topic = "kitchen/mainlight";
char* server = "192.168.2.103";
String clientName = "Arduino-esp";
Ticker lightOut;

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
 // handle message arrived
}

void setup() {
 Serial.begin(115200);
 delay(10);
 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);

 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected");
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());

 Serial.print("Connecting to ");
 Serial.print(server);
 Serial.print(" as ");
 Serial.println(clientName);

pinMode(13, INPUT);
pinMode(12, OUTPUT);
attachInterrupt(13, pin13Int, RISING);

 if (client.connect((char*) clientName.c_str())) {
 Serial.println("Connected to MQTT broker");
 Serial.print("Topic is: ");
 Serial.println(topic);

 }
 else {
 Serial.println("MQTT connect failed");
 Serial.println("Will reset and try again...");
 abort();
 }
}

void turnLightOff() {
Serial.println("Light Off");
digitalWrite(12,0);
  String payload;
  payload="Light Off";
  client.publish(topic, (char*) payload.c_str());
 
  lightOut.detach();
}
 
void pin13Int() {
 
  Serial.print("Movement!");
  digitalWrite(12,1);
  String payload;
  payload="Movement!";
 client.publish(topic, (char*) payload.c_str());

  lightOut.attach(5, turnLightOff);
 
 }


void loop() {


}


serial monitor:

WiFi connected
IP address:
192.168.2.26
Connecting to 192.168.2.103 as Arduino-esp
Connected to MQTT broker
Topic is: kitchen/mainlight
Movement!Light Off
Movement!Movement!Light Off
Movement!Movement!Light Off
Movement!

Re: MQtt, timers, gpio interrupts

PostPosted: Thu Apr 23, 2015 12:03 pm
by cherowley
Also, some times upon powering up if just repeats this for a dozen or 2 times!


ets Jan 8 2013,rst cause:4, boot mode:(3,0)

wdt reset
load 0x40100000, len 30000, room 16
tail 0
chksum 0xea
load 0x3ffe8000, len 2556, room 8
tail 4
chksum 0x92
load 0x3ffe8a00, len 2388, room 4
tail 0
chksum 0x79
csum 0x79
rĂ”

Re: MQtt, timers, gpio interrupts

PostPosted: Thu Apr 23, 2015 11:42 pm
by swilson
Try keeping your interrupt as simple as possible by setting a state and then doing the stuff in the other functions.

For example try the following. Code may not be perfect but maybe it will help you along.

Code: Select allvoid turnLightOn() {
  if (movement == 1) {
  Serial.print("Movement!");
  digitalWrite(12,1);
  String payload;
  payload="Movement!";
 client.publish(topic, (char*) payload.c_str());

  lightOut.attach(5, turnLightOff);
  movement = 0;
}
}

void pin13int() {
    movement = 1;
    turnLightOn();
}

Re: MQtt, timers, gpio interrupts

PostPosted: Fri Apr 24, 2015 10:43 am
by cherowley
Cheers!

Have amended my code to:

Code: Select all#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <Ticker.h>

const char* ssid = "";
const char* password = "";
char* topic = "kitchen/mainlight";
char* server = "192.168.2.103";
String clientName = "Arduino-esp";
Ticker Timer1;
volatile int MovementFlag;
volatile int TimeOutFlag;
int LEDStatus;
int LocalMode;
WiFiClient wifiClient;


void HandleTimeOut()
{
  TimeOutFlag=1;
}

void pin13Int()
{
  MovementFlag=1;
}

void setup()
{
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  MovementFlag=0;
  LEDStatus=0;
  TimeOutFlag=0;
  LocalMode=1;
 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
   delay(500);
   Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print("Connecting to ");
  Serial.print(server);
  Serial.print(" as ");
  Serial.println(clientName);
 
  pinMode(13, INPUT);
  pinMode(12, OUTPUT);
  attachInterrupt(13, pin13Int, RISING);
  Serial.println("Version 4");
}


void loop()
{
 
  if (MovementFlag==1)
  {
    detachInterrupt(13);
    MovementFlag=0;
    Serial.println("Movement!");
    digitalWrite(12,1);
    LEDStatus=1;
    Timer1.attach(8, HandleTimeOut);
    attachInterrupt(13, pin13Int, RISING);
  }
 
  if (TimeOutFlag==1)
  {
    detachInterrupt(13);
    TimeOutFlag=0;
    Serial.println("Timeout");
    digitalWrite(12,0);
    LEDStatus=0;
    Timer1.detach();
    attachInterrupt(13, pin13Int, RISING);
  }
}


which seems to work, although I'm sure I can see the lights flickering slightly?! I think someone else has reported the same with lines held high flickering...

Now to try and add MQTT code into it....

PS - Anyone know why my second attempt at code works but the first didn't? Is it down to keeping the ISR short as possible as suggested?