티스토리 뷰

Today, I would like to describe how to control LED based on Web server. As you might know Arduino is lower specification than RaspberryPI which means it does not have storage area in order to keep their data. Arduino is more suitable to control prompt data like gathering sensor data. In my solution(Pet Feeder), I have been using EV3 brick for Pet Feeder and Arduino is for getting signal from Pet manager and send command to EV3 brick via I2C. Therefore Arduino should receive signal from the Pet Manager.

# REST

REST(Representational State Transfer) is kind a network architecture in order to communicate. Briefly, we can define protocol based on HTTP or SOAP. For this project, I do not want to make big burden and make it easy communication way using wifi. There is 4 method to use REST which are 'GET, PUT, POST, DELETE'. I can use GET method with parameter. If Pet manager wants to feed he can send 'on' signal otherwise he can send 'off' signal. I think Pet feeder just knows 'on' which means feeding once. Like I said, I have defined REST API in order to communicate between Pet feeder and Pet manager.

GET /FEED=ON

Not sure how much you understand my explanation for REST, if you need to know, you can search it and find out more information.

# ESP-01 WIFI module

I bought wifi module for Arduino which is ESP-01 and you can find it specification here.(https://www.sparkfun.com/products/13678)

Under 5 dollars, we can get it and connect to Arduino for WIFI connection. Before I used to wifi usb dongle to enable wifi in case of not wifi model but Arduino doesn't have usb host and selected this module which can be connected via serial(tx, rx).

Here is circuit how it is. But I was struggling to use this wifi module because of baud-rate. I have to check the log by serial message but if wifi module uses tx/rx channel, I am not able to see serial message. Searching web sites to find out solution and finally get the answer how to use it. Once I flash wifi module firmware which is using 9600 baud-rate and then use SofwareSerial library(softwareserial is supported for 9600 baud-rate) to connect wifi module from Arduino. Like I used Arduino before, I can see serial log via serial window using 9600 baud-rate either. In order to set up download mode, you need to put GND for GPIO0 too. Also you have to prepare download tool tool and binary which is programmed with 9600 baud-rate. Somebody tells it used to flash using FTDI but I do not use it for flashing, I flashed bin using TX/RX serial but It took so much time because of connection problem!!.

here is binary to flash in ESP-01 and make up circuit to flash properly.

https://github.com/sleemanj/ESP8266_Simple/tree/master/firmware

(ai-thinker-0.9.5.2-9600.bin)

and then please visit to the below site in order to flash it.

http://www.instructables.com/id/Intro-Esp-8266-firmware-update/step4/Uploading-Firmware/

If you done to flash into esp01(esp8266) module, you can test it for wifi connectioni itself using serial command.

Once you have been connecting esp-01 via TX/RX serial channel, once unplug GPIO0(before you connected GND for the pin) and reset. you can see some log from wifi module if it is flashed well.

Set up the ending like for serial terminal from Arduino IDE, select BOTH NL & CR and 9600 for baud-rate.

AT+GMR is for checking firmware version of ESP-01.

Here is list of command for ESP-01.

http://room-15.github.io/blog/2015/03/26/esp8266-at-command-reference/

# Make Connection and Waiting signal as Web server

https://github.com/esp8266/source-code-examples

Here is sample codes which are provided from esp-01 module company. I refer the code and make web server after connection and then control LED according to REST API.

#include softwareserial.h
#define DEBUG true
 
SoftwareSerial esp8266(7, 6); // make TX Arduino line is pin 7, make RX Arduino line is pin 6.
                                        // This means that you need to connect the TX line from the esp to the Arduino's pin 7
                                        // and the RX line from the esp to the Arduino's pin 6
#define LED 5

void setup() {
  Serial.begin(9600);
  esp8266.begin(9600); // your esp's baud rate might be different
  
 pinMode(LED,OUTPUT);
 digitalWrite(LED, HIGH);
 Serial.println("Start");
  sendData("AT+RST\r\n",2000,DEBUG); // reset module
  sendData("AT+CIOBAUD?\r\n",2000,DEBUG); // check baudrate (redundant)
  sendData("AT+CWMODE=3\r\n",1000,DEBUG); // configure as access point (working mode: AP+STA)
  sendData("AT+CWLAP\r\n",3000,DEBUG); // list available access points
  sendData("AT+CWJAP=\"ynshero-main\",\"********\"\r\n",5000,DEBUG); // join the access point
  sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
  sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
  sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
}
 
void loop() {
  if(esp8266.available()) { // check if the esp is sending a message
    if(esp8266.find("+IPD,")) {
      delay(1000); // wait for the serial buffer to fill up (read all the serial data)
      // get the connection id so that we can then disconnect
      int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns 
                                           // the ASCII decimal value and 0 (the first decimal number) starts at 48
      esp8266.find("pin="); // advance cursor to "pin="
      int pinNumber = 0;
      pinNumber += (esp8266.read()-48); // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
      if(pinNumber == 5) {
        digitalWrite(LED, HIGH);       
      }
      else {
        digitalWrite(LED, LOW);
      }
      
      // make close command
      String closeCommand = "AT+CIPCLOSE="; 
      closeCommand+=connectionId; // append connection id
      closeCommand+="\r\n";
      sendData(closeCommand,1000,DEBUG); // close connection
    }
  }
}
 
String sendData(String command, const int timeout, boolean debug) {
    String response = "";
    esp8266.print(command); // send the read character to the esp8266
    long int time = millis();
    
    while( (time+timeout) > millis()) {
      while(esp8266.available()) {
        // The esp has data so display its output to the serial window 
        char c = esp8266.read(); // read the next character.
        response+=c;
      }
    }
    
    if(debug) {
      Serial.print(response);
    }
    return response;
}

This code is working like this scenario. Wifi is connected to 'ynshero-main' SSID and make web server using 80 port. If request is coming, it can check parameter in order to control LED whether it turns or not.

Here is the result how it works. 

This module is going to connect with EV3 if it gets signal from Pet manager and send it command via I2C in order to feed properly. I am sure the next time, it will be integrated. 

 

'Technology > Arduino' 카테고리의 다른 글

Pressure Module Control  (0) 2018.01.29
[Project] Pet Feeder with Arduino + EV3  (0) 2017.02.12
EV3 + Arduino connection via I2C  (0) 2017.01.22
Actuator control - SG90  (0) 2017.01.15
ISR or Falling??  (0) 2017.01.11
댓글