The use of the ESP8266 in the world of IoT

User avatar
By Sankeerth Reddy
#89165 I'm working on an IoT project which sends data to thingspeak using wifi and IR sensors. but when I initialize pinMode for Sensors and LEDs the board has difficulties connecting to wifi
Code: Select all//Include the Libraries Needed
#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
#include <ESP8266HTTPClient.h>
//Initialize wifi server
WiFiServer server(80);
WiFiClient client;
HTTPClient http;
//Initialize some Variables
//HIGH means No Obstacle -> No Vehicle
// Data Variables for IR Sensors
int lane1[3] = {HIGH,HIGH,HIGH};
int lane2[3] = {HIGH,HIGH,HIGH};
//Sound Sensor
int data_sound = HIGH;
// Count Variables for No of Vehicles in a Lane
int count1 = 0;
int count2 = 0;
int j=0;
String  i;
void setup(){
  pinMode(0,INPUT);// 6 IR sensors
  pinMode(2,INPUT);
  pinMode(14,INPUT);
  pinMode(12,INPUT);
  pinMode(13,INPUT);
  pinMode(15,INPUT);
  pinMode(16,INPUT); // Sound Sensor
  pinMode(5,OUTPUT); // Lane1 RED LED
  pinMode(4,OUTPUT); // Lane2 RED LED
  pinMode(10,OUTPUT); // Lane1 GREEN LED
  pinMode(9,OUTPUT);  // Lane2 GREEN LED 
  i = "";
  //Setup WiFi and Serial Communiation
  Serial.begin(9600);
  WiFi.disconnect();
  delay(3000);
  WiFi.begin("Prodduturi","sahithya123");
  Serial.println(".");
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);
  ThingSpeak.begin(client);  // Initialize ThingSpeak
  }
  Serial.println("connected");
  Serial.println((WiFi.localIP().toString()));
  server.begin();
}
void loop(){
  count1 = 0;
  count2 = 0;

//Setup IR sensors and Sound module to read data into data fields
  lane1[0] = digitalRead(0);
  lane1[1] = digitalRead(2);
  lane1[2] = digitalRead(14);
  lane2[0] = digitalRead(12);   
  lane2[1] = digitalRead(13);
  lane2[2] = digitalRead(15);
/*lane1[0] = HIGH;
  lane1[1] = LOW;
  lane1[2] = LOW;
  lane2[0] = LOW;
  lane2[1] = LOW;
  lane2[2] = LOW;*/
  data_sound = digitalRead(16);
//Count the Vehicles
  for(j = 0; j < 3; j++){
    if(lane1[j] == LOW){
      count1+=1;
    }
  }
  Serial.println("Lane2 Vehicle Count = "  + count1);
  for(j = 0; j < 3; j++){
    if(lane2[j] == LOW){
      count2+=1;
    }
  }
  Serial.println("Lane2 Vehicle Count = "  + count2); 
// Setup for LED's
//If a Lane has Three Vehicles(Max Count) then the lane shows Red otherwise Green
//For Lane1 which has a sound sensor to override when Sound
//is detected the Lane1 gets Green and others get Red
 if(data_sound == LOW){ // LOW Means SOUND is Present
      digitalWrite(10, HIGH); // Lane1 made Green
      digitalWrite(4, LOW); // Lane2 made Red
 }
 else{
      if(count1 == 3){
        digitalWrite(5, HIGH); // 3 Vehicles so RED -> HIGH
        digitalWrite(9, LOW); // GREEN -> LOW
      }
      if(count2 == 3){
        digitalWrite(4, HIGH); // 3 Vehicles so RED -> HIGH
        digitalWrite(10, LOW); // GREEN -> LOW
      }
      else{
        digitalWrite(9,HIGH); // Less than 3 so GREEN HIGH
        digitalWrite(10,HIGH); // Less than 3 so GREEN HIGH
        digitalWrite(4,LOW); // RED -> LOW
        digitalWrite(5,LOW); // RED -> LOW
      }
 }
// Write Data to ThingSpeak
//Field 1
  int x1 = ThingSpeak.writeField(1189296, 1,count1, "R9UKP4J869N94RSW");
  if(x1 == 200){
    Serial.println("Field 1 update successful.");
  }
  else{
    Serial.println("Problem updating Field 1. HTTP error code " + String(x1));
  }
    delay(20000); // Wait 20 seconds to update the channel again
//Field 2
  int x2 = ThingSpeak.writeField(1189296, 2,count2, "R9UKP4J869N94RSW");
  if(x2 == 200){
    Serial.println("Field 2 update successful.");
  }
  else{
    Serial.println("Problem updating Field 2. HTTP error code " + String(x2));
  }
    delay(20000); // Wait 20 seconds to update the channel again   
}

Here is an image of the Serial Monitor
Image
But when I remove the pinMode the board gets connected to WiFi.
Image
I'm a beginner with NodeMCU and wifi. Apologies if it is a silly error.
I can't find the error.