본문 바로가기

프로그래밍/아두이노03- 연습

아두이노 개발 연습 - wido arduino web server + radiation kit




wido arduino web server + radiation kit을 부착한 코드이다.


웹서버와 방사선측정 킷과의 연동을 하고 있으며


방사선 킷 구입처는 아래와 같다.


http://www.rhelectronics.net/store/radiation-detector-geiger-counter-diy-kit-second-edition.html


ems express 비행기 배송으로 약 18만원정도 들었다.


radiation kit으로서 아두이노와 연동도 가능한 유일한 kit제품이다.


아래 작성한 코드에는 특별히 interrupt함수가 들어가 있어서 방사선 측정 량을 계산해준다.


웹서버는 서버로서 text/plain으로 값을 전달해준다.


/*************************************************** 

  This is an example for the Adafruit CC3000 Wifi Breakout & Shield


  Designed specifically to work with the Adafruit WiFi products:

  ----> https://www.adafruit.com/products/1469


  Adafruit invests time and resources providing this open source code, 

  please support Adafruit and open-source hardware by purchasing 

  products from Adafruit!


  Written by Kevin Townsend & Limor Fried for Adafruit Industries.  

  BSD license, all text above must be included in any redistribution

 ****************************************************/


/*


This example does a full test of core connectivity:

* Initialization

* SSID Scan

* AP connection

* DHCP printout

* DNS lookup

* Ping

* Disconnect

It's a good idea to run this sketch when first setting up the

module.


*/


#include <Adafruit_CC3000.h>

#include <Adafruit_CC3000_Server.h>

#include <ccspi.h>

#include <SPI.h>

#include <string.h>

#include "utility/debug.h"


// These are the interrupt and control pins

#define ADAFRUIT_CC3000_IRQ   7  // MUST be an interrupt pin!

// These can be any two pins

#define ADAFRUIT_CC3000_VBAT  5

#define ADAFRUIT_CC3000_CS    10

// Use hardware SPI for the remaining pins


#define WLAN_SSID       "coala"        // cannot be longer than 32 characters!

#define WLAN_PASS       "1234567890"

// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2

#define WLAN_SECURITY   WLAN_SEC_WPA2


#define LOG_PERIOD 15000     //Logging period in milliseconds, recommended value 15000-60000.

#define MAX_PERIOD 60000    //Maximum logging period


unsigned long counts;             //variable for GM Tube events

unsigned long cpm;                 //variable for CPM

unsigned int multiplier;             //variable for calculation CPM in this sketch

unsigned long previousMillis;      //variable for time measurement

void tube_impulse(){               //procedure for capturing events from Geiger Kit

  counts++;

    //Serial.println(F("counts++")); 

}


// On an UNO, SCK = 13, MISO = 12, and MOSI = 11

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,

                                         SPI_CLOCK_DIVIDER); // you can change this clock speed but DI

                                         

Adafruit_CC3000_Server wifiServer(80);


/**************************************************************************/

/*!

    @brief  Sets up the HW and the CC3000 module (called automatically

            on startup)

*/

/**************************************************************************/

void setup(void)

{

  Serial.begin(115200);

  Serial.println(F("Hello, CC3000!\n")); 

  


  


  displayDriverMode();

  Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);

  

  /* Initialise the module */

  Serial.println(F("\nInitialising the CC3000 ..."));

  if (!cc3000.begin())

  {

    Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));

    while(1);

  }


  /* Optional: Update the Mac Address to a known value */

/*

  uint8_t macAddress[6] = { 0x08, 0x00, 0x28, 0x01, 0x79, 0xB7 };

   if (!cc3000.setMacAddress(macAddress))

   {

     Serial.println(F("Failed trying to update the MAC address"));

     while(1);

   }

*/

  

  /*

  uint16_t firmware = checkFirmwareVersion();

  if ((firmware != 0x113) && (firmware != 0x118)) {

    Serial.println(F("Wrong firmware version!"));

    for(;;);

  }*/

  

  displayMACAddress();

  

  /* Optional: Get the SSID list (not available in 'tiny' mode) */

#ifndef CC3000_TINY_DRIVER

  listSSIDResults();

#endif

  

  /* Delete any old connection data on the module */

  Serial.println(F("\nDeleting old connection profiles"));

  if (!cc3000.deleteProfiles()) {

    Serial.println(F("Failed!"));

    while(1);

  }


  /* Attempt to connect to an access point */

  char *ssid = WLAN_SSID;             /* Max 32 chars */

  Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid);

  

  /* NOTE: Secure connections are not available in 'Tiny' mode! */

  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {

    Serial.println(F("Failed!"));

    while(1);

  }

   

  Serial.println(F("Connected!"));

  

  /* Wait for DHCP to complete */

  Serial.println(F("Request DHCP"));

  while (!cc3000.checkDHCP())

  {

    delay(100); // ToDo: Insert a DHCP timeout!

  }  


  /* Display the IP address DNS, Gateway, etc. */  

  while (! displayConnectionDetails()) {

    delay(1000);

  }

  

  

  Serial.println(F("begin~!"));

  wifiServer.begin();

  

  

  counts = 0;

  cpm = 0;

  multiplier = MAX_PERIOD / LOG_PERIOD;      //calculating multiplier, depend on your log perio

  pinMode(2, INPUT);                                   // set pin INT0 input for capturing GM Tube events

  digitalWrite(2, HIGH);                                 // turn on internal pullup resistors, solder C-INT on the PCB

  attachInterrupt(1, tube_impulse, FALLING);  //define external interrupts

}


void loop(void)

{

  

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > LOG_PERIOD){

    previousMillis = currentMillis;

    cpm = counts * multiplier;

    Serial.print(F(" cpm :"));  

    Serial.println(cpm);                              // send cpm data to Radiation Logger    

    counts = 0;

  }

  

 Adafruit_CC3000_ClientRef client = wifiServer.available();

  if (client) {

    Serial.println("new client");

    // an http request ends with a blank line

    boolean currentLineIsBlank = true;

    while (client.connected()) {

      if (client.available()) {

        char c = client.read();

        Serial.write(c);

        // if you've gotten to the end of the line (received a newline

        // character) and the line is blank, the http request has ended,

        // so you can send a reply

        

        /*

        

        

          client.println("HTTP/1.1 200 OK"); 

          client.println("Content-Type: text/plain"); 

          client.println("Connection: close"); // the connection will be closed after completion of the response 

        

        */

        

        if (c == '\n' && currentLineIsBlank) {

          // send a standard http response header

          client.fastrprintln("HTTP/1.1 200 OK");

          client.fastrprintln("Content-Type: text/plain");

          client.fastrprintln("Connection: close");  // the connection will be closed after completion of the response

          

          client.fastrprintln("");

          

          client.fastrprintln("aaaaaa");

           char buf[50];

           sprintf(buf, "%lu", cpm); 

          client.fastrprintln(buf);

          client.fastrprintln("ccccccc");

          client.fastrprintln("");


          

          Serial.println("End of HTML data send");

          break;

        }

        if (c == '\n') {

          // you're starting a new line

          currentLineIsBlank = true;

        } 

        else if (c != '\r') {

          // you've gotten a character on the current line

          currentLineIsBlank = false;

        }

      }

    }

    // give the web browser time to receive the data

    delay(1);

    // close the connection:

    client.close();

    Serial.println("client disonnected");

  }

  

}


/**************************************************************************/

/*!

    @brief  Displays the driver mode (tiny of normal), and the buffer

            size if tiny mode is not being used


    @note   The buffer size and driver mode are defined in cc3000_common.h

*/

/**************************************************************************/

void displayDriverMode(void)

{

  #ifdef CC3000_TINY_DRIVER

    Serial.println(F("CC3000 is configure in 'Tiny' mode"));

  #else

    Serial.print(F("RX Buffer : "));

    Serial.print(CC3000_RX_BUFFER_SIZE);

    Serial.println(F(" bytes"));

    Serial.print(F("TX Buffer : "));

    Serial.print(CC3000_TX_BUFFER_SIZE);

    Serial.println(F(" bytes"));

  #endif

}


/**************************************************************************/

/*!

    @brief  Tries to read the CC3000's internal firmware patch ID

*/

/**************************************************************************/

uint16_t checkFirmwareVersion(void)

{

  uint8_t major, minor;

  uint16_t version;

  

#ifndef CC3000_TINY_DRIVER  

  if(!cc3000.getFirmwareVersion(&major, &minor))

  {

    Serial.println(F("Unable to retrieve the firmware version!\r\n"));

    version = 0;

  }

  else

  {

    Serial.print(F("Firmware V. : "));

    Serial.print(major); Serial.print(F(".")); Serial.println(minor);

    version = major; version <<= 8; version |= minor;

  }

#endif

  return version;

}


/**************************************************************************/

/*!

    @brief  Tries to read the 6-byte MAC address of the CC3000 module

*/

/**************************************************************************/

void displayMACAddress(void)

{

  uint8_t macAddress[6];

  

  if(!cc3000.getMacAddress(macAddress))

  {

    Serial.println(F("Unable to retrieve MAC Address!\r\n"));

  }

  else

  {

    Serial.print(F("MAC Address : "));

    cc3000.printHex((byte*)&macAddress, 6);

  }

}



/**************************************************************************/

/*!

    @brief  Tries to read the IP address and other connection details

*/

/**************************************************************************/

bool displayConnectionDetails(void)

{

  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;

  

  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))

  {

    Serial.println(F("Unable to retrieve the IP Address!\r\n"));

    return false;

  }

  else

  {

    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);

    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);

    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);

    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);

    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);

    Serial.println();

    return true;

  }

}


/**************************************************************************/

/*!

    @brief  Begins an SSID scan and prints out all the visible networks

*/

/**************************************************************************/


void listSSIDResults(void)

{

  uint8_t valid, rssi, sec, index;

  char ssidname[33]; 


  index = cc3000.startSSIDscan();


  Serial.print(F("Networks found: ")); Serial.println(index);

  Serial.println(F("================================================"));


  while (index) {

    index--;


    valid = cc3000.getNextSSID(&rssi, &sec, ssidname);

    

    Serial.print(F("SSID Name    : ")); Serial.print(ssidname);

    Serial.println();

    Serial.print(F("RSSI         : "));

    Serial.println(rssi);

    Serial.print(F("Security Mode: "));

    Serial.println(sec);

    Serial.println();

  }

  Serial.println(F("================================================"));


  cc3000.stopSSIDscan();

}