极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 27546|回复: 9

用Microduino CC3000搭建的WLAN天气系统

[复制链接]
发表于 2013-11-23 19:22:06 | 显示全部楼层 |阅读模式
本帖最后由 wasdpkj 于 2013-11-23 20:09 编辑

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

效果视频:


本次系统涉及成员一览:


搭建:




通电:








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

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


  1. #include "U8glib.h"

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

  14. //================================

  15. #include <iterator>
  16. #include <string>
  17. #include <pnew.cpp>

  18. #include <Adafruit_CC3000.h>
  19. #include <ccspi.h>
  20. #include <SPI.h>
  21. #include <string.h>
  22. #include "utility/debug.h"

  23. // These are the interrupt and control pins
  24. #define ADAFRUIT_CC3000_IRQ   2  // MUST be an interrupt pin!
  25. // These can be any two pins
  26. #define ADAFRUIT_CC3000_VBAT  9
  27. #define ADAFRUIT_CC3000_CS    10
  28. // Use hardware SPI for the remaining pins
  29. // On an UNO, SCK = 13, MISO = 12, and MOSI = 11
  30. Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
  31. SPI_CLOCK_DIV2); // you can change this clock speed

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

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

  39. uint32_t ip;

  40. Adafruit_CC3000_Client www;

  41. /**************************************************************************/
  42. /*!
  43. @brief  Sets up the HW and the CC3000 module (called automatically
  44. on startup)
  45. */
  46. /**************************************************************************/

  47. #define startbuf 228
  48. #define NUM 41

  49. String s_data[NUM];

  50. int i_data_num=0;

  51. //===============================================
  52. int updata[5];

  53. int tem,hum;

  54. int wind_level,wind_angle;

  55. //=============================
  56. #define INTERVAL_LCD             100
  57. #define INTERVAL_LCD_SWITCH      2000
  58. #define INTERVAL_UPDATA          1000*60*60*2

  59. unsigned long updatatime=millis();
  60. unsigned long lcdtime=millis();
  61. unsigned long lcd_switch_time=millis();

  62. int lcd_switch_num=0;

  63. //=============================

  64. void voUPDATA()
  65. {
  66.   volcdsetup("-----UPDATA-----");
  67.   //----------------------------------------------------------
  68.   cc3000.reboot();
  69.   //----------------------------------------------------------
  70.   cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  71.   //----------------------------------------------------------
  72.   voCC3000_get_dhcp();
  73.   //----------------------------------------------------------
  74.   voCC3000_get_server();
  75.   //----------------------------------------------------------
  76.   if(voCC3000_pos())
  77.   {
  78.     voDATA_begin();
  79.     voCC3000_rec();
  80.     volcdsetup("---UPDATA OK---");
  81.   }
  82.   else
  83.     volcdsetup("--UPDATA REEOR--");

  84.   delay(1000);

  85.   cc3000.stop();
  86.   //----------------------------------------------------------
  87.   voDATA_get();
  88. }


  89. void voDATA_begin()
  90. {
  91.   i_data_num=0;

  92.   for(int a=0;a<NUM;a++)
  93.   {
  94.     s_data[a]="";
  95.   }
  96.   
  97.   s_data[17]+="<lsup>";
  98. }

  99. void voCC3000_get_dhcp()
  100. {
  101.   //  Serial.println(F("Request DHCP"));
  102.   while (!cc3000.checkDHCP())
  103.     delay(100); // ToDo: Insert a DHCP timeout!

  104.   /* Display the IP address DNS, Gateway, etc. */
  105.   while (! displayConnectionDetails())
  106.     delay(500);
  107. }

  108. void voCC3000_get_server()
  109. {
  110.   ip = 0;
  111.   // Try looking up the website's IP address
  112.   Serial.print(WEBSITE);
  113.   Serial.print(F(" -> "));
  114.   while (ip == 0) {
  115.     if (! cc3000.getHostByName(WEBSITE, &ip)) {
  116.       Serial.println(F("Couldn't resolve!"));
  117.     }
  118.     delay(500);
  119.   }
  120.   cc3000.printIPdotsRev(ip);
  121. }

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

  130.   /* Try connecting to the website */
  131.   www = cc3000.connectTCP(ip, 80);
  132.   if (www.connected()) {
  133.     www.print(F("GET "));
  134.     www.print(WEBPAGE);
  135.     www.print(F(" HTTP/1.0\r\n"));
  136.     www.print(F("Host: "));
  137.     www.println(WEBSITE);
  138.     www.println(F("Connection: close"));
  139.     www.println();
  140.     return true;
  141.   }
  142.   else {
  143.     Serial.println(F("Connection failed"));   
  144.     return false;
  145.   }

  146. }


  147. void voCC3000_rec()
  148. {
  149.   int i_cache_num=0;
  150.   boolean b_cache=false;

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

  152.   while (www.connected())
  153.   {
  154.     while (www.available())
  155.     {
  156.       char c=www.read();

  157.       Serial.print(c);

  158.       if(c!=' ')
  159.       {
  160.         i_data_num++;

  161.         if( i_data_num>startbuf)
  162.         {
  163.           if(c=='<')
  164.           {
  165.             b_cache=false;
  166.           }

  167.           if(c=='>')
  168.           {
  169.             i_cache_num++;
  170.             b_cache=true;
  171.           }

  172.           if(b_cache && c!='>')
  173.             s_data[i_cache_num/2]+=c;

  174.         }
  175.       }

  176.     }
  177.   }
  178.   www.close();

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

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


  186. void voDATA_get()
  187. {
  188.   /*
  189.   for(int a=0;a<NUM;a++)
  190.    {
  191.    Serial.print("s_data[");
  192.    Serial.print(a);
  193.    Serial.print("] is:");
  194.    Serial.println(s_data[a]);
  195.    }
  196.    */

  197.   tem=s_data[19].toInt();
  198.   hum=s_data[31].toInt();

  199.   wind_level=s_data[27].toInt();
  200.   wind_angle=s_data[29].toInt();

  201.   char c_char[5][20];

  202.   s_data[17].toCharArray(c_char[1], i_data_num);

  203.   sscanf((char *)strstr((char *)c_char[1], "<lsup>"), "<lsup>%d/%d/%2d%d:%d", &updata[3],&updata[2],&updata[4],&updata[1],&updata[0]);
  204.   /*
  205.   Serial.print("updata: ");
  206.    Serial.print(updata[4]);
  207.    Serial.print("/");
  208.    Serial.print(updata[3]);
  209.    Serial.print("/");
  210.    Serial.print(updata[2]);
  211.    Serial.print(" ");
  212.    Serial.print(updata[1]);
  213.    Serial.print(":");
  214.    Serial.println(updata[0]);
  215.    */
  216. }


  217. void volcdsetup(String s_1)
  218. {
  219.   u8g.firstPage();  
  220.   do {
  221.     setFont_M;

  222.     u8g.setPrintPos(4, 30);
  223.     u8g.print(s_1);
  224.   }
  225.   while( u8g.nextPage() );
  226. }


  227. void setup(void)
  228. {
  229.   //  u8g.setRot180();

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

  232.   displayFreeRam();

  233.   //----------------------------------------------------------
  234.   Serial.println(F("\nInitializing..."));
  235.   volcdsetup("1/5 begin READY");
  236.   if (!cc3000.begin())
  237.     volcdsetup("1/5 begin ERROR");
  238.   else
  239.     volcdsetup("1/5 begin OK");

  240.   delay(1000);

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

  245.   delay(1000);

  246.   //----------------------------------------------------------
  247.   volcdsetup("3/5 dhcp READY");
  248.   voCC3000_get_dhcp();
  249.   volcdsetup("3/5 dhcp OK");

  250.   delay(1000);

  251.   //----------------------------------------------------------
  252.   volcdsetup("4/5 server READY");
  253.   voCC3000_get_server();
  254.   volcdsetup("4/5 server OK");

  255.   delay(1000);

  256.   //----------------------------------------------------------
  257.   volcdsetup("5/5 pos/rec READY");
  258.   if(voCC3000_pos())
  259.   {
  260.     voDATA_begin();
  261.     voCC3000_rec();
  262.     volcdsetup("5/5 pos/rec OK");
  263.   }
  264.   else
  265.     volcdsetup("5/5 pos/rec ERROR");

  266.   delay(1000);

  267.   cc3000.stop();
  268.   //----------------------------------------------------------
  269.   voDATA_get();

  270.   displayFreeRam();
  271. }


  272. void loop(void)
  273. {
  274.   if (lcdtime > millis()) lcdtime = millis();
  275.   if(millis()-lcdtime>INTERVAL_LCD)
  276.   {
  277.     volcd();
  278.     lcdtime=millis();
  279.   }

  280.   if (lcd_switch_time > millis()) lcd_switch_time = millis();
  281.   if(millis()-lcd_switch_time>INTERVAL_LCD_SWITCH)
  282.   {
  283.     lcd_switch_num++;
  284.     if(lcd_switch_num>3)
  285.       lcd_switch_num=0;

  286.     lcd_switch_time=millis();
  287.   }


  288.   if (updatatime > millis()) updatatime = millis();
  289.   if(millis()-updatatime>INTERVAL_UPDATA)
  290.   {
  291.     voUPDATA();
  292.     updatatime=millis();
  293.   }


  294. }


  295. void volcd()
  296. {
  297.   u8g.firstPage();  
  298.   do {
  299.     setFont_M;
  300.     u8g.setPrintPos(0, 10);
  301.     u8g.print(s_data[9]);

  302.     u8g.setPrintPos(0, 10+12*1);
  303.     u8g.print(s_data[21]);

  304.     u8g.setPrintPos(0, 10+12*2);
  305.     u8g.print(tem);
  306.     u8g.print("`");
  307.     u8g.print(s_data[3]);
  308.     u8g.print(" ");
  309.     u8g.print(hum);
  310.     u8g.print("%");

  311.     u8g.setPrintPos(0, 14+12*3);
  312.     switch(lcd_switch_num)
  313.     {
  314.     case 0:   
  315.       u8g.print("sunr: ");
  316.       u8g.print(s_data[13]);
  317.       break;

  318.     case 1:   
  319.       u8g.print("suns: ");
  320.       u8g.print(s_data[14]);
  321.       break;

  322.     case 2:   
  323.       u8g.print("uv: ");
  324.       u8g.print(s_data[35]);
  325.       break;

  326.     case 3:   
  327.       u8g.print("vis: ");
  328.       u8g.print(s_data[32]);
  329.       u8g.print(s_data[4]);
  330.       break;

  331.     }


  332.     //-----------------------------------------
  333.     float hudu=3.14*(wind_angle/180.0);
  334.     int x,y;
  335.     x=12*sin(hudu);
  336.     y=12*cos(hudu);

  337. #define xqishi 104
  338. #define yqishi 42

  339.     u8g.drawCircle(xqishi,yqishi,14);

  340.     u8g.drawLine(xqishi, yqishi,   xqishi-x, yqishi+y);
  341.     u8g.drawLine(xqishi, yqishi-1,   xqishi-x, yqishi+y);
  342.     u8g.drawLine(xqishi, yqishi+1,   xqishi-x, yqishi+y);
  343.     u8g.drawLine(xqishi-1, yqishi,   xqishi-x, yqishi+y);
  344.     u8g.drawLine(xqishi+1, yqishi,   xqishi-x, yqishi+y);

  345.     //-----------------------------------------

  346.     setFont_S;
  347.     if(wind_angle>140 && wind_angle<220)
  348.     {
  349.       u8g.setPrintPos(102, 54);
  350.       u8g.print("S");   
  351.     }
  352.     else
  353.     {
  354.       u8g.setPrintPos(102, 36);
  355.       u8g.print("N");   
  356.     }


  357.     u8g.setPrintPos(90, 63);
  358.     u8g.print(wind_level);
  359.     u8g.print(s_data[5]);


  360.     //    u8g.drawFrame(0, 54,128,10);
  361.     u8g.setPrintPos(0, 63);
  362.     u8g.print("updata: ");
  363.     u8g.print(updata[3]);
  364.     u8g.print("/");
  365.     u8g.print(updata[2]);
  366.     u8g.print("  ");
  367.     u8g.print(updata[1]);
  368.     u8g.print(":");
  369.     u8g.println(updata[0]);
  370.   }
  371.   while( u8g.nextPage() );
  372. }


  373. /**************************************************************************/
  374. /*!
  375. @brief  Begins an SSID scan and prints out all the visible networks
  376. */
  377. /**************************************************************************/

  378. void listSSIDResults(void)
  379. {
  380.   uint8_t valid, rssi, sec, index;
  381.   char ssidname[33];

  382.   index = cc3000.startSSIDscan();

  383.   Serial.print(F("Networks found: "));
  384.   Serial.println(index);
  385.   Serial.println(F("================================================"));

  386.   while (index) {
  387.     index--;

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

  389.     Serial.print(F("SSID Name    : "));
  390.     Serial.print(ssidname);
  391.     Serial.println();
  392.     Serial.print(F("RSSI         : "));
  393.     Serial.println(rssi);
  394.     Serial.print(F("Security Mode: "));
  395.     Serial.println(sec);
  396.     Serial.println();
  397.   }
  398.   Serial.println(F("================================================"));

  399.   cc3000.stopSSIDscan();
  400. }

  401. /**************************************************************************/
  402. /*!
  403. @brief  Tries to read the IP address and other connection details
  404. */
  405. /**************************************************************************/
  406. bool displayConnectionDetails(void)
  407. {
  408.   uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;

  409.   if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  410.   {
  411.     Serial.println(F("Unable to retrieve the IP Address!\r\n"));
  412.     return false;
  413.   }
  414.   else
  415.   {
  416.     Serial.print(F("\nIP Addr: "));
  417.     cc3000.printIPdotsRev(ipAddress);
  418.     Serial.print(F("\nNetmask: "));
  419.     cc3000.printIPdotsRev(netmask);
  420.     Serial.print(F("\nGateway: "));
  421.     cc3000.printIPdotsRev(gateway);
  422.     Serial.print(F("\nDHCPsrv: "));
  423.     cc3000.printIPdotsRev(dhcpserv);
  424.     Serial.print(F("\nDNSserv: "));
  425.     cc3000.printIPdotsRev(dnsserv);
  426.     Serial.println();
  427.     return true;
  428.   }
  429. }
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2013-11-23 22:12:06 | 显示全部楼层
现在好像没发现有microduino卖的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-24 00:32:20 | 显示全部楼层
milk6060 发表于 2013-11-23 22:12
现在好像没发现有microduino卖的

在量产了,12月开卖
回复 支持 反对

使用道具 举报

发表于 2013-11-24 09:08:56 | 显示全部楼层
很酷的东西
回复 支持 反对

使用道具 举报

发表于 2013-11-24 09:58:48 | 显示全部楼层
老潘越做越大了哈,44这个做个字符串优化吧,我4过,比F()靠谱

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-25 01:56:37 | 显示全部楼层
幻生幻灭 发表于 2013-11-24 09:58
老潘越做越大了哈,44这个做个字符串优化吧,我4过,比F()靠谱

哎呀 好东西,果断先收下了 多谢
回复 支持 反对

使用道具 举报

发表于 2013-11-25 14:47:05 | 显示全部楼层
请问这种封装结构可以开源使用吧

我想移植到我的NXP板上
回复 支持 反对

使用道具 举报

发表于 2014-6-4 14:46:30 | 显示全部楼层
很厉害 赞一个~!
回复 支持 反对

使用道具 举报

发表于 2014-6-10 10:46:19 | 显示全部楼层
恩 学习了  
回复 支持 反对

使用道具 举报

发表于 2014-6-11 16:21:11 | 显示全部楼层
我看例子是做的短连接,可以做成长连接吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 07:04 , Processed in 0.047000 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表