Skip to content

Commit

Permalink
first attempt at setup mode
Browse files Browse the repository at this point in the history
doesn't build 
20:1: error: 'REST' does not name a type
  • Loading branch information
walduino committed Dec 12, 2015
1 parent e82607a commit 721d142
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions Cheapspark.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <espduino.h>
#include <mqtt.h>
#include <dht.h>
#include <rest.h>

#define MYSSID "tim"
#define MYPASS "PASSWORD"
Expand All @@ -16,6 +17,8 @@

ESP esp(&Serial, 4);
MQTT mqtt(&esp);
REST rest(&esp);

boolean wifiConnected = false;
int reportInterval = 15000;
int switchInterval = 100;
Expand All @@ -25,6 +28,7 @@ unsigned long nextPub = reportInterval;
unsigned long nextSwitch = switchInterval;
unsigned long nextPulse = pulseInterval;
int ledpin = 13;
boolean setupmode = false;
boolean switchstate = false;
boolean rel1_pulse = false;
boolean rel2_pulse = false;
Expand All @@ -39,12 +43,12 @@ void wifiCb(void* response)
if(res.getArgc() == 1) {
res.popArgs((uint8_t*)&status, 4);
if(status == STATION_GOT_IP) { //WIFI CONNECTED
mqtt.connect(BROKERIP, 1883, false);
if (setupmode == false ) mqtt.connect(BROKERIP, 1883, false);
wifiConnected = true;
} else {
wifiConnected = false;
mqtt.disconnect();
}
}
}
}

Expand Down Expand Up @@ -94,26 +98,35 @@ void setup() {
pinMode(REL2_PIN,OUTPUT);
pinMode(REL3_PIN,OUTPUT);
pinMode(REL4_PIN,OUTPUT);

//setup ESP
if (analogRead(0)>500) setupmode = true;

//setup ESP
delay(5000);
Serial.begin(19200);
esp.enable();
delay(500);
esp.reset();
delay(500);
while(!esp.ready());
//setup mqtt client");
if(!mqtt.begin(MQTTCLIENT, "", "", 30, 1)) {
while(1);

if (setupmode == false){
//setup mqtt client");
if(!mqtt.begin(MQTTCLIENT, "", "", 30, 1)) {
while(1);
}
//setup mqtt lwt
mqtt.lwt("/lwt", MQTTCLIENT " offline", 0, 0);
//setup mqtt events
mqtt.connectedCb.attach(&mqttConnected);
mqtt.disconnectedCb.attach(&mqttDisconnected);
mqtt.publishedCb.attach(&mqttPublished);
mqtt.dataCb.attach(&mqttData);
}
if (setupmode == true){
if(!rest.begin("192.168.1.2")) {
while(1);
}
}
//setup mqtt lwt
mqtt.lwt("/lwt", MQTTCLIENT " offline", 0, 0);
//setup mqtt events
mqtt.connectedCb.attach(&mqttConnected);
mqtt.disconnectedCb.attach(&mqttDisconnected);
mqtt.publishedCb.attach(&mqttPublished);
mqtt.dataCb.attach(&mqttData);
//setup wifi
esp.wifiCb.attach(&wifiCb);
esp.wifiConnect(MYSSID,MYPASS);
Expand All @@ -123,13 +136,13 @@ void setup() {
void loop() {
esp.process();

if(wifiConnected) {
if((wifiConnected) && (setupmode = false)) {
now = millis();
int switchval = analogRead(0);

if (now >= nextPub) {
nextPub = reportInterval + now;

int chk = DHT.read22(DHT_PIN);
float humid = DHT.humidity;
float tempe = DHT.temperature;
Expand All @@ -153,11 +166,11 @@ void loop() {
switchstate = !switchstate;
}
}

if (now >= nextPulse) {
nextPulse = pulseInterval + now;
digitalWrite(ledpin, !digitalRead(ledpin));

if ((rel1_pulse == true) && (digitalRead(REL1_PIN) == LOW)) digitalWrite(REL1_PIN,HIGH);
else if ((rel1_pulse == true) && (digitalRead(REL1_PIN) == HIGH)) {
rel1_pulse = false;
Expand Down

0 comments on commit 721d142

Please sign in to comment.