lauchpad cc3200 điều khiển thiết bị qua wifi - control devices with Lauchpad cc3200 over wifi, LAN


Ví dụ dưới đây cho phép chúng ta điều khiển thiết bị điện thông qua mạng wifi/LAN một cách đơn giản.
#ifndef __CC3200R1M1RGC__
// Do not include SPI for CC3200 LaunchPad
#include <SPI.h>
#endif
#include <WiFi.h>
// your network name also called SSID
char ssid[] = "tên wifi";
// your network password
char password[] = "password wifi";
// your network key Index number (needed only for WEP)
int keyIndex = 0;
int aa = 0;
WiFiServer server(80);
const int buttonPin = PUSH2;
const int buttonPin1 = PUSH1;
int buttonState = 0;
int buttonState1 = 0;
void setup() {
  Serial.begin(115200);      // initialize serial communication
  pinMode(RED_LED, OUTPUT);      // set the LED pin mode
  pinMode(10, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin1, INPUT_PULLUP);
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid);
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    delay(300);
  }
 
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
 
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    delay(300);
  }
  Serial.println("\nIP Address obtained");
 
  // you're connected now, so print out the status 
  printWifiStatus();
  Serial.println("Starting webserver on port 80");
  server.begin();                           // start the web server on port 80
  Serial.println("Webserver started!");
}
void loop() {
  int i = 0;
  WiFiClient client = server.available();   // listen for incoming clients
  buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH && aa == 0) {    
    // turn LED on:
    aa = 1; 
   //digitalWrite(10, HIGH);
  }
if (buttonState == HIGH & aa==1) {
    // turn LED off:
   aa = 0;
   //digitalWrite(10, LOW);
  }
if (aa == 0){
digitalWrite(10, LOW);}
if (aa == 1){
digitalWrite(10, HIGH);}

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    char buffer[150] = {0};                 // make a buffer to hold incoming data
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send '"response:
          if (strlen(buffer) == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
            // the content of the HTTP response follows the header:
            client.println("<html><head><title>CC3200 WiFi Web Server</title></head><body bgcolor=blue align=center>");
            client.println("<h1 align=center><font color=\"red\">Welcome to Van Hien University</font></h1>");
            client.println("<hr size=5px width=50% color=red align=center>");
            client.print("<font face=arial size=4 color=yellow>LED 1</font><button onclick=\"location.href='/H'\">HIGH</button>");
            client.println(" <button onclick=\"location.href='/L'\">LOW</button><br>");
            client.println("<br>");
            client.print("<font face=arial size=4 color=yellow>LED 2</font><button onclick=\"location.href='/A'\">HIGH</button>");
            client.println(" <button onclick=\"location.href='/B'\">LOW</button><br>");
            if (aa == 1){
              client.println("LED xanh sang");
            }
            if (aa == 0){
              client.println("LED xanh tat");
            }
            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear the buffer:
            memset(buffer, 0, 150);
            i = 0;
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          buffer[i++] = c;      // add it to the end of the currentLine
        }
        // Check to see if the client request was "GET /H" or "GET /L":
        if (endsWith(buffer, "GET /H")) {
          digitalWrite(RED_LED, HIGH);               // GET /H turns the LED on
      }
        if (endsWith(buffer, "GET /L")) {
          digitalWrite(RED_LED, LOW);                // GET /L turns the LED off
        }
         if (endsWith(buffer, "GET /A")) {
          digitalWrite(10, HIGH);               // GET /H turns the LED on
          aa =1;
        }
        if (endsWith(buffer, "GET /B")) {
          digitalWrite(10, LOW);                // GET /L turns the LED off
          aa = 0;
        }
    }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}
//
//a way to check if one array ends with another array
//
boolean endsWith(char* inString, char* compString) {
  int compLength = strlen(compString);
  int strLength = strlen(inString);
 
  //compare the last "compLength" values of the inString
  int i;
  for (i = 0; i < compLength; i++) {
    char a = inString[(strLength - 1) - i];
    char b = compString[(compLength - 1) - i];
    if (a != b) {
      return false;
    }
  }
  return true;
}
void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}
++++++++++++>>>>> link download code <<<<<+++++++++
Trong đoạn code trên có chức năng :
- thao tác bật tắt thiệt bị bằng button trên giao diện web
- Bật tắt thiết bị bằng 2 button vật lý có trên board.

Nhận xét

Bài đăng phổ biến từ blog này

Dòng điện, điện áp 1 chiều và các định luật cơ bản

Dòng điện 1 chiều (DC) là gì ?

Các cách mắc điện trở

Dòng điện xoay chiều

Biến áp, Triết áp, Phân loại điện trở