wasdpkj 发表于 2013-11-23 19:22:06

用Microduino CC3000搭建的WLAN天气系统

本帖最后由 wasdpkj 于 2013-11-23 20:09 编辑

以Microduino Core+为核心,CC3000作为无线网关,获取Weather.com 上的天气数据,并且加以OLED显示

效果视频:
http://www.tudou.com/v/zlFjwquAbu8/&resourceId=56044369_04_11_99/v.swf

本次系统涉及成员一览:


搭建:




通电:








由于程序写得太傻,MEGA328的Core没法跑,所以用了MEGA644的Core+

库为了适配MEGA644做了一定更改,放在文末


#include "U8glib.h"

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);      // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins areSCK = 13 and MOSI = 11)
//-------字体设置,大、中、小
#define setFont_L u8g.setFont(u8g_font_7x13)
#define setFont_M u8g.setFont(u8g_font_fixed_v0r)
#define setFont_S u8g.setFont(u8g_font_chikitar)
/*
font:
u8g.setFont(u8g_font_7x13)
u8g.setFont(u8g_font_fixed_v0r);
u8g.setFont(u8g_font_chikitar);
u8g.setFont(u8g_font_osb21);
*/

//================================

#include <iterator>
#include <string>
#include <pnew.cpp>

#include <Adafruit_CC3000.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   2// MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT9
#define ADAFRUIT_CC3000_CS    10
// Use hardware SPI for the remaining pins
// 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_DIV2); // you can change this clock speed

#define WLAN_SSID       "your ssid"         // cannot be longer than 32 characters!
#define WLAN_PASS       "your password"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

// What page to grab!
#define WEBSITE      "wxdata.weather.com"
#define WEBPAGE      "/wxdata/weather/local/chxx0008?cc=*&unit=m"

uint32_t ip;

Adafruit_CC3000_Client www;

/**************************************************************************/
/*!
@briefSets up the HW and the CC3000 module (called automatically
on startup)
*/
/**************************************************************************/

#define startbuf 228
#define NUM 41

String s_data;

int i_data_num=0;

//===============================================
int updata;

int tem,hum;

int wind_level,wind_angle;

//=============================
#define INTERVAL_LCD             100
#define INTERVAL_LCD_SWITCH      2000
#define INTERVAL_UPDATA          1000*60*60*2

unsigned long updatatime=millis();
unsigned long lcdtime=millis();
unsigned long lcd_switch_time=millis();

int lcd_switch_num=0;

//=============================

void voUPDATA()
{
volcdsetup("-----UPDATA-----");
//----------------------------------------------------------
cc3000.reboot();
//----------------------------------------------------------
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
//----------------------------------------------------------
voCC3000_get_dhcp();
//----------------------------------------------------------
voCC3000_get_server();
//----------------------------------------------------------
if(voCC3000_pos())
{
    voDATA_begin();
    voCC3000_rec();
    volcdsetup("---UPDATA OK---");
}
else
    volcdsetup("--UPDATA REEOR--");

delay(1000);

cc3000.stop();
//----------------------------------------------------------
voDATA_get();
}


void voDATA_begin()
{
i_data_num=0;

for(int a=0;a<NUM;a++)
{
    s_data="";
}

s_data+="<lsup>";
}

void voCC3000_get_dhcp()
{
//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(500);
}

void voCC3000_get_server()
{
ip = 0;
// Try looking up the website's IP address
Serial.print(WEBSITE);
Serial.print(F(" -> "));
while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
}
cc3000.printIPdotsRev(ip);
}

boolean voCC3000_pos()
{
// Optional: Do a ping test on the website
/*
Serial.print(F("\n\rPinging ")); cc3000.printIPdotsRev(ip); Serial.print("...");
   replies = cc3000.ping(ip, 5);
   Serial.print(replies); Serial.println(F(" replies"));
   */

/* Try connecting to the website */
www = cc3000.connectTCP(ip, 80);
if (www.connected()) {
    www.print(F("GET "));
    www.print(WEBPAGE);
    www.print(F(" HTTP/1.0\r\n"));
    www.print(F("Host: "));
    www.println(WEBSITE);
    www.println(F("Connection: close"));
    www.println();
    return true;
}
else {
    Serial.println(F("Connection failed"));   
    return false;
}

}


void voCC3000_rec()
{
int i_cache_num=0;
boolean b_cache=false;

Serial.println(F("connect start--------------------------------"));

while (www.connected())
{
    while (www.available())
    {
      char c=www.read();

      Serial.print(c);

      if(c!=' ')
      {
      i_data_num++;

      if( i_data_num>startbuf)
      {
          if(c=='<')
          {
            b_cache=false;
          }

          if(c=='>')
          {
            i_cache_num++;
            b_cache=true;
          }

          if(b_cache && c!='>')
            s_data+=c;

      }
      }

    }
}
www.close();

/* You need to make sure to clean up after yourself or the CC3000 can freak out */
/* the next time your try to connect ... */
cc3000.disconnect();
Serial.println(F("\n\nDisconnecting"));

//Serial.println(s_data);
Serial.println(F("connect end--------------------------------"));
}


void voDATA_get()
{
/*
for(int a=0;a<NUM;a++)
   {
   Serial.print("s_data[");
   Serial.print(a);
   Serial.print("] is:");
   Serial.println(s_data);
   }
   */

tem=s_data.toInt();
hum=s_data.toInt();

wind_level=s_data.toInt();
wind_angle=s_data.toInt();

char c_char;

s_data.toCharArray(c_char, i_data_num);

sscanf((char *)strstr((char *)c_char, "<lsup>"), "<lsup>%d/%d/%2d%d:%d", &updata,&updata,&updata,&updata,&updata);
/*
Serial.print("updata: ");
   Serial.print(updata);
   Serial.print("/");
   Serial.print(updata);
   Serial.print("/");
   Serial.print(updata);
   Serial.print(" ");
   Serial.print(updata);
   Serial.print(":");
   Serial.println(updata);
   */
}


void volcdsetup(String s_1)
{
u8g.firstPage();
do {
    setFont_M;

    u8g.setPrintPos(4, 30);
    u8g.print(s_1);
}
while( u8g.nextPage() );
}


void setup(void)
{
//u8g.setRot180();

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

displayFreeRam();

//----------------------------------------------------------
Serial.println(F("\nInitializing..."));
volcdsetup("1/5 begin READY");
if (!cc3000.begin())
    volcdsetup("1/5 begin ERROR");
else
    volcdsetup("1/5 begin OK");

delay(1000);

//----------------------------------------------------------
volcdsetup("2/5 scan READY");
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
volcdsetup("2/5 scan OK");

delay(1000);

//----------------------------------------------------------
volcdsetup("3/5 dhcp READY");
voCC3000_get_dhcp();
volcdsetup("3/5 dhcp OK");

delay(1000);

//----------------------------------------------------------
volcdsetup("4/5 server READY");
voCC3000_get_server();
volcdsetup("4/5 server OK");

delay(1000);

//----------------------------------------------------------
volcdsetup("5/5 pos/rec READY");
if(voCC3000_pos())
{
    voDATA_begin();
    voCC3000_rec();
    volcdsetup("5/5 pos/rec OK");
}
else
    volcdsetup("5/5 pos/rec ERROR");

delay(1000);

cc3000.stop();
//----------------------------------------------------------
voDATA_get();

displayFreeRam();
}


void loop(void)
{
if (lcdtime > millis()) lcdtime = millis();
if(millis()-lcdtime>INTERVAL_LCD)
{
    volcd();
    lcdtime=millis();
}

if (lcd_switch_time > millis()) lcd_switch_time = millis();
if(millis()-lcd_switch_time>INTERVAL_LCD_SWITCH)
{
    lcd_switch_num++;
    if(lcd_switch_num>3)
      lcd_switch_num=0;

    lcd_switch_time=millis();
}


if (updatatime > millis()) updatatime = millis();
if(millis()-updatatime>INTERVAL_UPDATA)
{
    voUPDATA();
    updatatime=millis();
}


}


void volcd()
{
u8g.firstPage();
do {
    setFont_M;
    u8g.setPrintPos(0, 10);
    u8g.print(s_data);

    u8g.setPrintPos(0, 10+12*1);
    u8g.print(s_data);

    u8g.setPrintPos(0, 10+12*2);
    u8g.print(tem);
    u8g.print("`");
    u8g.print(s_data);
    u8g.print(" ");
    u8g.print(hum);
    u8g.print("%");

    u8g.setPrintPos(0, 14+12*3);
    switch(lcd_switch_num)
    {
    case 0:   
      u8g.print("sunr: ");
      u8g.print(s_data);
      break;

    case 1:   
      u8g.print("suns: ");
      u8g.print(s_data);
      break;

    case 2:   
      u8g.print("uv: ");
      u8g.print(s_data);
      break;

    case 3:   
      u8g.print("vis: ");
      u8g.print(s_data);
      u8g.print(s_data);
      break;

    }


    //-----------------------------------------
    float hudu=3.14*(wind_angle/180.0);
    int x,y;
    x=12*sin(hudu);
    y=12*cos(hudu);

#define xqishi 104
#define yqishi 42

    u8g.drawCircle(xqishi,yqishi,14);

    u8g.drawLine(xqishi, yqishi,   xqishi-x, yqishi+y);
    u8g.drawLine(xqishi, yqishi-1,   xqishi-x, yqishi+y);
    u8g.drawLine(xqishi, yqishi+1,   xqishi-x, yqishi+y);
    u8g.drawLine(xqishi-1, yqishi,   xqishi-x, yqishi+y);
    u8g.drawLine(xqishi+1, yqishi,   xqishi-x, yqishi+y);

    //-----------------------------------------

    setFont_S;
    if(wind_angle>140 && wind_angle<220)
    {
      u8g.setPrintPos(102, 54);
      u8g.print("S");   
    }
    else
    {
      u8g.setPrintPos(102, 36);
      u8g.print("N");   
    }


    u8g.setPrintPos(90, 63);
    u8g.print(wind_level);
    u8g.print(s_data);


    //    u8g.drawFrame(0, 54,128,10);
    u8g.setPrintPos(0, 63);
    u8g.print("updata: ");
    u8g.print(updata);
    u8g.print("/");
    u8g.print(updata);
    u8g.print("");
    u8g.print(updata);
    u8g.print(":");
    u8g.println(updata);
}
while( u8g.nextPage() );
}


/**************************************************************************/
/*!
@briefBegins an SSID scan and prints out all the visible networks
*/
/**************************************************************************/

void listSSIDResults(void)
{
uint8_t valid, rssi, sec, index;
char ssidname;

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();
}

/**************************************************************************/
/*!
@briefTries 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;
}
}



milk6060 发表于 2013-11-23 22:12:06

现在好像没发现有microduino卖的

wasdpkj 发表于 2013-11-24 00:32:20

milk6060 发表于 2013-11-23 22:12 static/image/common/back.gif
现在好像没发现有microduino卖的

在量产了,12月开卖

迷你强 发表于 2013-11-24 09:08:56

;P很酷的东西

幻生幻灭 发表于 2013-11-24 09:58:48

老潘越做越大了哈,44这个做个字符串优化吧,我4过,比F()靠谱

wasdpkj 发表于 2013-11-25 01:56:37

幻生幻灭 发表于 2013-11-24 09:58 static/image/common/back.gif
老潘越做越大了哈,44这个做个字符串优化吧,我4过,比F()靠谱

哎呀 好东西,果断先收下了:lol 多谢

likunyang 发表于 2013-11-25 14:47:05

请问这种封装结构可以开源使用吧

我想移植到我的NXP板上

↗Anonymous-er. 发表于 2014-6-4 14:46:30

很厉害 赞一个~!

↗Anonymous-er. 发表于 2014-6-10 10:46:19

恩 学习了

yushiqian2012 发表于 2014-6-11 16:21:11

我看例子是做的短连接,可以做成长连接吗?
页: [1]
查看完整版本: 用Microduino CC3000搭建的WLAN天气系统