Thursday, August 5, 2021

How to connect Arduino UNO to a WIFI Network: ESP8266 - AT Commands



ESP8266 is an integrated circuit with a microcontroller allowing connection in WiFi.
In this video, we will study this WIFI module and learn how to use it with the Arduino UNO board.
You would then be able to:
-Connect the ESP-01 to Arduino.
-Send AT commands using Arduino UNO
-Use the Arduino to program the ESP-01 (the SoftwareSerial library)
Finally, You will have your first web server.
Connecting arduino to the internet allows remote control of objects. We're talking about Internet of Things (IOT)



#include <SoftwareSerial.h>                        
SoftwareSerial esp8266(10,11);                   
#define serialCommunicationSpeed 9600               
#define DEBUG true                                 

void setup()

{
  Serial.begin(serialCommunicationSpeed);           
  esp8266.begin(serialCommunicationSpeed);     
  InitWifiModule();                              
}

void loop()                                                         
{

  if(esp8266.available())                                           
 {    
    if(esp8266.find("+IPD,"))
    {
     delay(1000);
 
     int connectionId = esp8266.read()-48;                                                
     String webpage = "<h1>Hello World!</h1>";
     String cipSend = "AT+CIPSEND=";
     cipSend += connectionId;
     cipSend += ",";
     cipSend +=webpage.length();
     cipSend +="\r\n";
     
     sendData(cipSend,1000,DEBUG);
     sendData(webpage,1000,DEBUG);
 
     String closeCommand = "AT+CIPCLOSE="; 
     closeCommand+=connectionId; // append connection id
     closeCommand+="\r\n";    
     sendData(closeCommand,3000,DEBUG);
    }
  }
}

String sendData(String command, const int timeout, boolean debug)
{
    String response = "";                                             
    esp8266.print(command);                                          
    long int time = millis();                                      
    while( (time+timeout) > millis())                                 
    {      
      while(esp8266.available())                                      
      {
        char c = esp8266.read();                                     
        response+=c;                                                  
      }  
    }    
    if(debug)                                                        
    {
      Serial.print(response);
    }    
    return response;                                                  
}

void InitWifiModule()
{
  sendData("AT+RST\r\n", 2000, DEBUG);                                                  
  sendData("AT+CWJAP=\"WIFI_name\",\"pwd\"\r\n", 2000, DEBUG);        
  delay (3000);
  sendData("AT+CWMODE=1\r\n", 1500, DEBUG);                                             
  delay (1500);
  sendData("AT+CIFSR\r\n", 1500, DEBUG);                                             
  delay (1500);
  sendData("AT+CIPMUX=1\r\n", 1500, DEBUG);                                             
  delay (1500);
  sendData("AT+CIPSERVER=1,80\r\n", 1500, DEBUG);                                     

}


Monday, August 2, 2021

Arduino and PIR Sensor: Home security system


Code Arduino:

int sensor=2;
int led=13;
int buzzer=9;
int state=LOW;
int val=0;

void setup() {
 pinMode(sensor,INPUT);
 pinMode(led,OUTPUT);
 pinMode(buzzer,OUTPUT);
 Serial.begin(9600); 

}

void loop() {
val=digitalRead(sensor);
if(val==HIGH){
  digitalWrite(led,HIGH);
  digitalWrite(buzzer,HIGH);
  delay(400);
}
else{
  digitalWrite(led,LOW);
  digitalWrite(buzzer,LOW);  
}
}


Sunday, September 20, 2020

Microbit - App Inventor: Access the car park with my phone

With the Mit App inventor couple, BBC Micro: bit we create a parking barrier that opens / closes automatically using voice. The voice is detected by the smartphone's microphone, and transmitted to the Microbit card via a Bluetooth connection.