Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By cherowley
#15726 Ok, added some mqtt stuff and amazingly it's been powered up for 40+ hours and still working!
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;
String LEDStatus;
long LEDTime;
String LocalMode;
String msgReceived;
String msgToSend;
String topicToSend;

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

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

void HandleTimeOut()
{
  TimeOutFlag=1;
}

void pin13Int()
{
  MovementFlag=1;
}

// Callback function
void callback(char* topic, byte* payload, unsigned int lengthy)
{
  // create character buffer with ending null terminator (string)
  char message_buff[100];
  int i = 0;
  for(i=0; i<lengthy; i++)
  {
    message_buff[i] = payload[i];
  }
  message_buff[i] = '\0';
  msgReceived = String(message_buff);
}

void SendMsg(String loTopic, String loMsg)
{
  msgToSend=loMsg;
  topicToSend=loTopic;
  if (client.publish((char*) topicToSend.c_str(), (char*) msgToSend.c_str()) )
  {
    Serial.println("Publish Succeeded!");
  }
    else
  {
    Serial.println("Publish Failed!");
  }
}

void LightOn()
{
 detachInterrupt(13);
 digitalWrite(12,1);
 LEDStatus="On";
 attachInterrupt(13, pin13Int, RISING);
}

void LightOff()
{
  detachInterrupt(13);
  digitalWrite(12,0);
  LEDStatus="Off";
  attachInterrupt(13, pin13Int, RISING);
}

void setup()
{
  //variables
  MovementFlag=0;
  LEDStatus="Off";
  TimeOutFlag=0;
  LocalMode="Local";
  LEDTime=8;
 
  //wifi
  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());
 
 
  //mqtt
  Serial.print("Connecting to ");
  Serial.print(server);
  Serial.print(" as ");
  Serial.println(clientName);
  if (client.connect((char*) clientName.c_str()))
  { 
    Serial.println("Connected to MQTT broker");
    Serial.print("Topic is: ");
    Serial.println(topic);
    client.subscribe(topic);
     //   client.subscribe("inTopic");
    SendMsg(topic,clientName+" Joined Server.");   
  }
  else
  {
    Serial.println("MQTT connect failed");
    Serial.println("Will reset and try again...");
    abort();
  }
 
 //gpio
  pinMode(13, INPUT);
  pinMode(12, OUTPUT);
  attachInterrupt(13, pin13Int, RISING);
  Serial.println("Version 4");
}


void loop()
{
 
  if (MovementFlag==1)
  {
    Serial.println("Movement!");
    MovementFlag=0;
    if (LocalMode=="Local")
    {
      LightOn();
    }
    Timer1.attach(LEDTime, HandleTimeOut);
    SendMsg(topic,"Movement!");
  }
 
  if (TimeOutFlag==1)
  {
    Serial.println("Timeout");
    TimeOutFlag=0;
    if (LocalMode=="Local")
    {
      LightOff();
    }
    Timer1.detach();
    SendMsg(topic,"Auto Light Off!");
  }
 
  if (msgReceived != "")
  {

// should use case select when I get around to looking how it's done in C..

    if (msgReceived == "STATUS")
    {
      SendMsg(topic,"LED="+LEDStatus+" Mode="+LocalMode);
    }
   
    if (msgReceived == "ON")
    {
      SendMsg(topic,"Turning LED ON");
      LightOn();
    }
   
    if (msgReceived == "OFF")
    {
      SendMsg(topic,"Turning LED OFF");
      LightOff();
    }
   
    if (msgReceived == "LOCAL")
    {
      SendMsg(topic,"Set To Local Mode");
      LocalMode="Local";
    }
   
    if (msgReceived == "REMOTE")
    {
      SendMsg(topic,"Set To REMOTE Mode");
      LocalMode="Remote";
    }
   
    if (msgReceived.substring(0,7) == "TIMEON=")
    {
       String NewTime=msgReceived.substring(7);
      SendMsg(topic,"Light Remains On For "+NewTime);   
      LEDTime=atoi(NewTime.c_str());
    }
   
    msgReceived="";
  }
 
 
   // MQTT client loop processing
  client.loop();
}


Need to add some reconnect and remote config, hopefully make use of the excellent stuff by Chris Cook :)