hello,
I'm fairly new to coding so i really don't know if this is a problem with my IDE or the code. What must happen when i upload the code is it should print some stuff on the serial. eg: complete msgs, WiFi connected, OK and failed messages. but nothing prints. sometimes when i re-upload the code it shows half of the text but not the complete one.
below is my full code and its in parts because it exceeds 9000 characters
PS i tried changing bud rates on both the code and the serial monitor and at 2400 and 4800 i get just a bit of what needs to be printed but higher than that, NOTHING
[code]#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h" // including the library of DHT11 temperature and humidity sensor
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "TP-LINK_B8B51A"
#define WLAN_PASS "0312221554"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "black_ace"
#define AIO_KEY "8c8e70a6202f4bba984e3603e314f4a8"
/************************* Tempreture *********************************/
#define DHTTYPE DHT11 // DHT 11
#define dht_dpin 1
DHT dht(dht_dpin, DHTTYPE);
/************ Global State (you don't need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish Temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Temperature");
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe relay1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Living");
Adafruit_MQTT_Subscribe relay2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Kitchen");
Adafruit_MQTT_Subscribe relay3 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Room01");
Adafruit_MQTT_Subscribe relay4 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Room02");
/*************************** Sketch Code ************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
/************************PIR***************************************************/
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
//int pirPin = 2; //the digital pin connected to the PIR sensor's output
//int relay = 12; //the pin connected to the fan
// Now we can publish stuff!
int t = dht.readTemperature();
if (t >= 50) {
digitalWrite(14, LOW);
}
else {
digitalWrite(14, HIGH);
}
if (! Temperature.publish(t)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK"));
delay(5000);
}
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
// if(! mqtt.ping()) {
// mqtt.disconnect();
// }
/**************************************PIR***************************************************************/
if (digitalRead(2) == HIGH) {
digitalWrite(12, LOW); //the led visualizes the sensors output pin state
if (lockLow) {
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis() / 1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if (digitalRead(2) == LOW) {
digitalWrite(12, HIGH); //the led visualizes the sensors output pin state
if (takeLowTime) {
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if (!lockLow && millis() - lowIn > pause) {
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause) / 1000);
Serial.println(" sec");
delay(50);
}
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
You have not provided enough information. Why are you using such a low baud rate?
Why is Serial.begin() not the FIRST thing in setup()? Why is Serial.print() not the SECOND thing in setup()?
Why are you setting the mode of pins 0 and 1? What Arduino are you using? On the UNO, those are the hardware serial pins, and you should NOT be f**king with them.