1

This is my final project, I have to communicate with two wemos d1 mini by LAN. The problem is that when I read the state of the PIR sensor it is "HIGH" all the time, but when I connect the led light, it blinks as I move my hand - so the sensor is not broken. I have tried with D7,D2,D3 and its the same... code is in the loop body PIR output is directly connected to the D0.

#include <SPI.h>
#include <ESP8266WiFi.h>

int pirPin = 0;
int val=0;
/*int led1 = 1;
int  led2 = 3;
int led3 = 4;
int senzor2 = 5;
int led4 = 6;
int led5 = 7;
//int led6 = ;
*/
int ledPin = 2;
String ID = "001";

boolean uspostavljenostVezeSaServerom=false;
char ssid[] = "arduinoserver";           // SSID od mreže na koju se spaja
char pass[] = "arduinoserver";            // lozinka mreže na koju se spaja

unsigned long askTimer = 0;

IPAddress server(192,168,1,80);       // fiksna adresa servera na koji se spaja arduino(klijent)
IPAddress ip(192, 168, 1, 79);
IPAddress gateway(192,168,1,1);           // gateway mreže (nije obavezno)
IPAddress subnet(255,255,255,0);          // subnet mreže (nije obavezno)

WiFiClient client;

void setup() {
  pinMode(pirPin,INPUT_PULLUP);

  Serial.begin(115200);               // podešavanje bauda za komunikaciju
  spajanjeNaMrezu();                  // spajanje na mrežu 
}




void loop () {

  if(uspostavljenostVezeSaServerom==false)spajanjeSaServerom();  // arduino uspostavlja komunikaciju sa serverom
  if(WiFi.status() != WL_CONNECTED) spajanjeNaMrezu();

val = digitalRead(pirPin);
  Serial.println(val); 
delay(300);

  //Serial.println(digitalRead(val));

 /* client.connect(server, 80);   // Spajanje na server
  client.flush();
  client.println(ID); 
  String answer = client.readStringUntil('\r');   // čekanje povratne poruke dok ne dođe do znaka "/r" 
  */
}

void spajanjeSaServerom(){

  String answer;
  while(answer != "OK")
    {
      Serial.println("Uspotavljanje veze s serverom...");
      client.connect(server, 80);   // Spajanje na server
      client.flush();
      client.println(ID); // slanje koda klijenta serveru 
      Serial.println("Šaljem ID: " + ID);
      answer = client.readStringUntil('\r');
      Serial.println("Odgovor servera: " + answer);
    }

  Serial.print("Veza uspostavljena!");  
  uspostavljenostVezeSaServerom=true;
} 



void spajanjeNaMrezu(){
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, pass);             // spajanje s ruterom
  while (WiFi.status() != WL_CONNECTED) {
      Serial.println("Spajanje na mrežu...");
      delay(500);
  }
  Serial.println("Uspjesno spajanje na WiFi");
  Serial.print("Status: "); Serial.println(WiFi.status());    
  Serial.print("IP: ");     Serial.println(WiFi.localIP());
  Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
  Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
  Serial.print("SSID: "); Serial.println(WiFi.SSID());
  Serial.print("Signal: "); Serial.println(WiFi.RSSI());

}
  • use Dx constants. pirPin = D2; electronics.stackexchange.com/questions/402066/… – Juraj May 29 at 17:01
  • Yeah, int pirPin = 0; does not look right. Instead use int pirPin = D0; – Duncan C May 29 at 18:27
  • I tried before, but not working :( "error: 'D0' was not declared in this scope" – Manuel May 29 at 21:53
  • did you select "Wemos D1 R2 or mini" in Tools menu? – Juraj May 30 at 4:56
  • I selected ESP8266, that was the problem :). Thanks alot ! – Manuel May 30 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.