joi, 19 februarie 2015

Placa de retea cu inscriptor SD si.. Arduino (partea a III-a: comanda periferice prin retea) - continuare

   Am zis sa continui testele pentru comanda unui led conectat la placa de retea pe care le-am prezentat in articolul precedent, de data asta sa am control din afara retelei.
   Furnizorul de internet, in cazul meu RCS&RDS, are un servicu de alocare a unui nume contului de acasa. Eu am ales numele de tehniq.go.ro:
   Alt pas care l-am facut este acela de a deschde un port, ales de mine ca 8081:
   Am schimbat si in sketch-ul prezentat in articolul precedent portul:
   La tastarea pentru prima data a adresei tehniq.go.ro:8081 pe ecranul calculatorului, a tabletei sau telefonului mobil vom avea urmatoarea imagine:
   Daca se apasa butonul virtual Turn on LED pe ecran va aparea situatia:
si led-ul verde se va aprinde.
   Daca se apasa butonul virtual Turn off LED pe ecran va aparea situatia:
iar led-ul verde se va stinge.

21.02.2015
   Am mai modificat sketch-ul in functie de observatiile celor care au testat pagina si am afisata temperatura microcontrolului ATmega328 pe pagina, am afisat numarul de aprinderi ale led-ului, am eliminat problemea cresterii nereale prin comanda de "refresh" sau apasare repeatata pe "on", se stocheaza in memoria EEPROM a microcontrolerului numarul de aprinderi si starea anteriora, pentru a nu se sterge datele la un restart sau pana de curent...
- led verde aprins:
- led verde stins:
   Am facut, in sfarsit, si un filmulet numit LED controlat prin internet in care se vede modul de control si ce cum reactioneaza montajul prin comanda de pe un telefon conectat la internet:

   Sketch-ul folosit de mine, ajuns la versiunea 3f3, este:
/*
 Created by Rui Santos
 Visit: http://randomnerdtutorials.com for more arduino projects
 Modified by Nicu Florica aka niq_ro
 visit http://nicuflorica.blogspot.ro/
 or http://arduinotehniq.blogspot.com
 or http://www.tehnic.go.ro

 Arduino with Ethernet Shield
 */

#include <SPI.h>
#include <Ethernet.h>
int led = 9;
int pos = 0; 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 2, 178 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 2, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(8081);                             //server port     
String readString;

int clic; // number of mouse clicks
int clic1; // number of recovered mouse clicks
byte stare; // 0 is OFF, 1 is ON
byte stare0; // before state
int a = 0;  // decinal value
int b = 0;  // unitar value
int a1 = 0;  // recovered decimal value
int b1 = 0;  // recovered unitar value

// http://tronixstuff.com/2011/03/16/tutorial-your-arduinos-inbuilt-eeprom/
#include <EEPROM.h>


void setup() {
  
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
   
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

a1 = EEPROM.read(100);
b1 = EEPROM.read(101);
// recover number of mouse click
clic = a1*256 + b1;

stare0 = EEPROM.read(102);  
  
}


void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
     
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {          
           Serial.println(readString); //print to serial monitor for debuging
     
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           // add a meta refresh tag, so the browser pulls again every 10 seconds:
           client.println("<meta http-equiv=\"refresh\" content=\"10\">");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Arduino with Ethernet Shield</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H2>tehniq.go.ro by niq_ro</H1>");
           client.println("<hr />");
         //  client.println("<br />");  
           client.println("<H1>Arduino with Ethernet Shield</H2>");
           client.println("<br />");  
           client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off LED</a><br />");   
           client.println("<br />");     
         //  client.println("<br />"); 
           client.println("<H2>");
 
 if (readString.indexOf("?button1on") >0){
           stare = 1;        
          // client.print("<p>LED is ON</p>");
          // digitalWrite(led, HIGH);
          //     clic = clic + 1;
           }
 if (readString.indexOf("?button1off") >0){
            stare = 0;
           //client.print("<p>LED is OFF</p>");
           //digitalWrite(led, LOW);
           //     clic = clic + 1;
           }
// read old state:
stare0 == EEPROM.read(102);

if (stare == 1) {
           client.print("<p>LED is ON</p>");
           digitalWrite(led, HIGH);
           if (stare0 == 0) {clic = clic + 1; }
}

if (stare == 0) {
           client.print("<p>LED is OFF</p>");
           digitalWrite(led, LOW);
//           if (stare0 == 1) {clic = clic + 1; }
}


//if (stare0 =! stare) {clic = clic + 1; }
stare0 = stare;

// write & read data in/from internal EEPROM
// calculate the unitar and zecinal units
a = clic/256;
b = clic - a*256;
// write in internal eeprom
EEPROM.write(100,a);
EEPROM.write(101,b);
// read from internal eeprom
a1 = EEPROM.read(100);
b1 = EEPROM.read(101);
// recover number of mouse click
clic1 = a1*256 + b1;
// write state in internal eeprom 
EEPROM.write(102,stare0); 

           client.print("<p>LED was on ");
           client.print(clic1);
           client.print(" times from 13:33 (21.febr.2015)<p>");

           client.print("<p>Internal temperature of ATmega328P from Arduino board is ");
           client.print(readTemp()/10000);
           client.print("<sup>o</sup>");
           client.print("C<p>");
           
//           client.println("<br />");     
//           client.println("<br />"); 
           client.println("<H4>");
           client.println("<p>Created by Rui Santos. Visit <a href=http://randomnerdtutorials.com target=blank>http://randomnerdtutorials.com</a> for more projects!</p>");  
//           client.println("<br />"); 
           client.println("<p>Tested by Nicu Florica aka niq_ro. Visit <a href=http://www.tehnic.go.ro target=blank>http://www.tehnic.go.ro</a> for more projects!</p>");  
           client.println("<br />"); 
           client.println("<H5>");
           client.println("version. 3e2 / 21.02.2015<p>");
           client.println("</BODY>");
           client.println("</HTML>");
     
     //  readString="";  
           delay(1);
           //stopping client
           client.stop();
             //clearing string for next read
           readString="";  
     
         }
       }
    }
}
}

// https://code.google.com/p/tinkerit/wiki/SecretThermometer
long readTemp() {
  long result;
  // Read temperature sensor against 1.1V reference
  ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX3);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = (result - 125) * 1075;
  return result;
}
   
22.02.2015
   Am realizat un filmulet in limba engleza, numit control a LED from Anywhere in the World in care se vede ca am pus si un senzor de umiditate si temperatura de tip DHT11:
25.02.2015
   Cu ajutorul lui Cosmin, un coleg de servici, am pus si partea de parola, dupa cum se vede in filmuletul LED controlat prin internet (2)

Niciun comentariu:

Trimiteți un comentariu