{"comment":{"url":"https://api.github.com/repos/kuominglu/pi/comments/29854268","html_url":"https://github.com/kuominglu/pi/commit/2c9d923e63c5d89003bb32dbdbee7fcb23ac070f#commitcomment-29854268","id":29854268,"node_id":"MDEzOkNvbW1pdENvbW1lbnQyOTg1NDI2OA==","user":{"login":"kuominglu","id":41773324,"node_id":"MDQ6VXNlcjQxNzczMzI0","avatar_url":"https://avatars2.githubusercontent.com/u/41773324?v=4","gravatar_id":"","url":"https://api.github.com/users/kuominglu","html_url":"https://github.com/kuominglu","followers_url":"https://api.github.com/users/kuominglu/followers","following_url":"https://api.github.com/users/kuominglu/following{/other_user}","gists_url":"https://api.github.com/users/kuominglu/gists{/gist_id}","starred_url":"https://api.github.com/users/kuominglu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kuominglu/subscriptions","organizations_url":"https://api.github.com/users/kuominglu/orgs","repos_url":"https://api.github.com/users/kuominglu/repos","events_url":"https://api.github.com/users/kuominglu/events{/privacy}","received_events_url":"https://api.github.com/users/kuominglu/received_events","type":"User","site_admin":false},"position":null,"line":null,"path":null,"commit_id":"2c9d923e63c5d89003bb32dbdbee7fcb23ac070f","created_at":"2018-07-27T06:15:24Z","updated_at":"2018-07-27T06:15:24Z","author_association":"OWNER","body":"// 2017 CAVEDU\r\n＃包括 < LTask.h >\r\n＃包括 < LWiFi.h >\r\n＃包括 < LWiFiClient.h >\r\n＃包括 < Wire.h >\r\n＃包括 < math.h中>\r\n＃包括 < LDateTime.h >\r\n＃包括 < HttpClient.h >\r\n\r\n＃定義 BMP085_ADDRESS  0x77    // BMP085的I2C地址\r\nconst  unsigned  char OSS = 0 ;  //過採樣設置\r\n//校準值\r\nint16_t ac1，ac2，ac3;\r\nuint16_t ac4，ac5，ac6;\r\nint16_t b1，b2，mb，mc，md;\r\n\r\nint state = 0 ;\r\nint i = 0 ;\r\n\r\n// b5是在bmp085GetTemperature（...）中計算的，這個變量也用在bmp085GetPressure（...）中\r\n// so ...溫度（...）必須在......壓力（...）之前調用。\r\n長 b5;\r\nunsigned  int rtc;\r\nunsigned  int lrtc;\r\nunsigned  int lrtc2;\r\nunsigned  int rtc2;\r\nunsigned  int rtc1;\r\nunsigned  int lrtc1;\r\n＃定義 sht21address B1000000\r\n＃定義 sht21temp   B11110011\r\n＃定義 sht21humid B11110101\r\n＃定義 WIFI_AP  “ MiFi4620LE Jetpack的1C04安全”\r\n＃定義 WIFI_PASSWORD  “ a248b26c ”\r\n＃定義 WIFI_AUTH LWIFI_WPA   //從LWIFI_OPEN，LWIFI_WPA，或LWIFI_WEP選擇。\r\n＃定義 SITE_URL  “ api.mediatek.com ”\r\n＃限定 每 5\r\n＃定義 PER1  50\r\n＃定義 DEVICEID  “ DtVa0VgS ”\r\n＃定義 DEVICEKEY  “ 0xVqfFbnoDeIuv6d ”\r\nLWiFiClient c;\r\nLWiFiClient c2;\r\nchar port [ 5 ] = “     ” ;\r\nchar connection_info [ 21 ] = “                     ” ;\r\nchar ip [ 15 ] = “               ” ;\r\nint portnum;\r\nint val = 0 ;\r\nString tcpdata = String（DEVICEID）+ “，” + String（DEVICEKEY）+ “，0 ” ;\r\nString tcpcmd_led_on = String（DEVICEID）+ “，” + String（DEVICEKEY）+ “，0，fan_control，on ” ;\r\nString tcpcmd_led_off = String（DEVICEID）+ “，” + String（DEVICEKEY）+ “，0，fan_control，off ” ;\r\nString upload_led;\r\nHttpClient http（c2）;\r\n\r\nvoid  setup（）{\r\n  線。begin（）;\r\n  bmp085Calibration（）;\r\n  LTask。begin（）;\r\n  LWiFi。begin（）;\r\n  串行。開始（115200）;\r\n  while（！Serial）延遲（1000）; //準備好演示時標記這個\r\n  串行。println（“連接到AP ”）;\r\n  而（0 == LWiFi。連接（WIFI_AP，LWiFiLoginInfo（WIFI_AUTH，WIFI_PASSWORD）））\r\n  {\r\n    延遲（1000）;\r\n  }\r\n \r\n  延遲（100）;\r\n  pinMode（12，OUTPUT）;\r\n  digitalWrite（12，HIGH）;\r\n  串行。打印（“ LED設置為LOW ”）;\r\n  // connection（）;\r\n  LDateTime。getRtc（＆lrtc）;\r\n  LDateTime。getRtc（＆lrtc2）;\r\n  而（！C2。連接（SITE_URL，80））\r\n  {\r\n     串行。println（“重新連接到WebSite ”）;\r\n     延遲（1000）;\r\n  }\r\n  延遲（100）;\r\n  getconnectInfo（）;\r\n  connectTCP（）;\r\n}\r\n\r\nvoid  loop（）{\r\n  如果（LWiFi。狀態（）== LWIFI_STATUS_DISCONNECTED）{\r\n    串行。println（“與AP斷開連接”）;\r\n    而（0 == LWiFi。連接（WIFI_AP，LWiFiLoginInfo（WIFI_AUTH，WIFI_PASSWORD）））\r\n    {\r\n      串行。println（“連接到AP ”）;\r\n      延遲（1000）;\r\n    }\r\n  }\r\n\r\n  LDateTime。getRtc（＆rtc）;\r\n  if（（rtc  -  lrtc）> = per）{\r\n    串行。println（“ Senddata ”）;\r\n     senddata（）;\r\n    lrtc = rtc;\r\n  }\r\n  / *   if（（rtc  -  lrtc2）> = 0.3）{\r\n    READDATA（）;\r\n    lrtc2 = rtc;\r\n  } * /\r\n  \r\n  //從MCS服務器檢查TCP套接字命令\r\n  String tcpcmd = “ ” ;\r\n  而（c。可用（））\r\n   {\r\n      int v = c。read（）;\r\n      if（v！=  - 1）\r\n      {\r\n        串行。print（（char）v）;\r\n        tcpcmd + =（char）v;\r\n        如果（tcpcmd。等號（tcpcmd_led_on））{\r\n          digitalWrite（12，LOW）;\r\n          串行。print（“ Switch FAN ON ”）;\r\n          tcpcmd = “ ” ;\r\n        } 否則 如果（tcpcmd。的equals（tcpcmd_led_off））{  \r\n          digitalWrite（12，HIGH）;\r\n          串行。打印（“ Switch FAN OFF ”）;\r\n          tcpcmd = “ ” ;\r\n        }\r\n      }\r\n   }\r\n  //檢查聽力間隔\r\n  LDateTime。getRtc（＆rtc2）;\r\n  if（（rtc2  -  lrtc2）> = per1）{\r\n    heartBeat（）;\r\n    lrtc2 = rtc2;\r\n  }\r\n}\r\n\r\nvoid  getconnectInfo（）{\r\n  //調用RESTful API來獲取TCP套接字連接\r\n  C2。print（“ GET / mcs / v2 / devices / ”）;\r\n  C2。打印（DEVICEID）;\r\n  C2。println（“ / connections.csv HTTP / 1.1 ”）;\r\n  C2。print（“主持人：”）;\r\n  C2。println（SITE_URL）;\r\n  C2。print（“ deviceKey：”）;\r\n  C2。println（DEVICEKEY）;\r\n  C2。println（“連接：關閉”）;\r\n  C2。println（）;\r\n  \r\n  延遲（500）;\r\n\r\n  int errorcount = 0 ;\r\n  而（！C2。可用（））\r\n  {\r\n    串行。println（“等待HTTP響應：”）;\r\n    串行。println（errorcount）;\r\n    errorcount + = 1 ;\r\n    if（errorcount> 10）{\r\n      C2。停止（）;\r\n      回歸 ;\r\n    }\r\n    延遲（100）;\r\n  }\r\n  int err = http。skipResponseHeaders（）;\r\n\r\n  int bodyLen = http。contentLength（）;\r\n  串行。print（“內容長度為：”）;\r\n  串行。println（bodyLen）;\r\n  串行。println（）;\r\n  char c;\r\n  int ipcount = 0 ;\r\n  int count = 0 ;\r\n  int separater = 0 ;\r\n  而（c2）\r\n  {\r\n    int v = c2。read（）;\r\n    if（v！=  - 1）\r\n    {\r\n      c = v;\r\n      串行。print（c）;\r\n      connection_info [ipcount] = C;\r\n      if（c == '，'）\r\n      分體= ipcount;\r\n      ipcount ++;    \r\n    }\r\n    其他\r\n    {\r\n      串行。println（“不再內容，斷開連接”）;\r\n      C2。停止（）;\r\n    }\r\n  }\r\n  \r\n  串行。print（“連接信息：”）;\r\n  串行。println（connection_info）;\r\n  int i;\r\n  for（i = 0 ; i <separater; i ++）\r\n  {  \r\n     IP [I] = connection_info [I];\r\n  }\r\n  int j = 0 ;\r\n  分體++;\r\n  for（i = separater; i < 21 && j < 4 ; i ++）\r\n  {  \r\n     端口[J] = connection_info [I];\r\n     J ++;\r\n  }\r\n  串行。println（“ TCP套接字連接說明：”）;\r\n  串行。print（“ IP：”）;\r\n  串行。println（ip）;\r\n  串行。print（“ Port：”）;\r\n  串行。println（port）;\r\n  portnum = atoi（port）;\r\n} // getconnectInfo\r\n\r\nvoid  connectTCP（）{\r\n  //使用指定IP和端口與TCP服務器建立TCP連接\r\n  C。停止（）;\r\n  串行。println（“連接到TCP ”）;\r\n  而（0 ==℃。連接（“ 54.255.209.252 ”，443））\r\n  {\r\n     串行。println（“重新連接到TCP ”）;    \r\n     延遲（1000）;\r\n  }  \r\n  串行。println（“發送TCP連接”）;\r\n  C。println（tcpdata）;\r\n  C。println（）;\r\n  串行。println（“等待TCP響應：”）;\r\n} // connectTCP\r\n\r\nvoid  heartBeat（）{\r\n  串行。println（“發送TCP heartBeat ”）;\r\n  C。println（tcpdata）;\r\n  C。println（）;\r\n} // heartBeat\r\n\r\nvoid  senddata（）{\r\n  串行。println（“ senddata的開頭”）;\r\n  float temp = readsht21temp（）;\r\n  float hum = readsht21humidity（）;\r\n  float temperature = bmp085GetTemperature（bmp085ReadUT（））; //必須先打個電話\r\n\r\n  浮壓= 0 ;\r\n  while（壓力< 90000）{\r\n    bmp085GetTemperature（bmp085ReadUT（））;\r\n    pressure = bmp085GetPressure（bmp085ReadUP（））;\r\n  }\r\n  串行。println（“ 1 ”）;\r\n  char buffert [ 5 ];\r\n  char bufferh [ 5 ];\r\n  char bufferp [ 10 ];\r\n  sprintf（buffert，“％。 2f ”，temp）;\r\n  sprintf（bufferh，“％。 2f ”，hum）;\r\n  sprintf（bufferp，“％6.1f ”，壓力）;\r\n  串行。println（“ 2 ”）;\r\n\r\n  String data = “ temperature ,, ” + String（buffert）+ “ \\ n humidity，” + String（bufferh）+ “ \\ n pressure ,, ” + String（bufferp）;\r\n  串行。println（數據）;\r\n\r\n  //    int light = analogRead（A0）;\r\n  //    String data =“1000000013 ,,”+ String（light）;\r\n  int thisLength =數據。長度（）;\r\n  // Serial.println（data）;\r\n  LWiFiClient c2;\r\n  unsigned  long t1 = millis（）;\r\n  而（0 == C2。連接（SITE_URL，80））\r\n  {\r\n    串行。println（“重新連接到WebSite ”）;\r\n    延遲（1000）;\r\n  }\r\n\r\n  延遲（500）;\r\n  串行。println（“發送POST請求”）;\r\n\r\n  C2。print（“ POST / mcs / v2 / devices / ”）;\r\n  C2。打印（DEVICEID）;\r\n  C2。println（“ / datapoints.csv HTTP / 1.1 ”）;\r\n  C2。print（“主持人：”）;\r\n  C2。println（SITE_URL）;\r\n  C2。print（“ deviceKey：”）;\r\n  C2。println（DEVICEKEY）;\r\n  C2。print（“ Content-Length：”）;\r\n  C2。println（thisLength）;\r\n  C2。println（“ Content-Type：text / csv ”）;\r\n  C2。println（“連接：關閉”）;\r\n  C2。println（）;\r\n  C2。println（數據）;\r\n\r\n  String dataget = “ ” ;\r\n  串行。println（“等待HTTP響應：”）;\r\n\r\n  int errorcount = 0 ;\r\n  而（！C2。可用（））\r\n  {\r\n    串行。println（“等待HTTP響應：”）;\r\n    errorcount + = 1 ;\r\n    if（errorcount> 10）{\r\n      C2。停止（）;\r\n      回歸 ;\r\n    }\r\n    延遲（100）;\r\n  }\r\n  而（c2）\r\n  {\r\n    int v = c2。read（）;\r\n    if（v！=  - 1）\r\n    {\r\n      串行。print（char（v））;\r\n      延遲（1）;\r\n    }\r\n    其他\r\n    {\r\n      串行。println（“不再內容，斷開連接”）;\r\n      C2。停止（）;\r\n    }\r\n  }\r\n  延遲（300）;\r\n  unsigned  long t2 = millis（）;\r\n} // senddata\r\n\r\nfloat  readsht21temp（）{\r\n  線。flush（）;\r\n  線。beginTransmission（sht21address）;\r\n  線。寫（sht21temp）;\r\n  線。endTransmission（）;\r\n  延遲（100）;\r\n  線。requestFrom（sht21address，3）;\r\n\r\n  byte msb = Wire。read（）;\r\n  byte lsb = Wire。read（）;\r\n  int t =（msb << 8）+ lsb;\r\n  int crc = Wire。read（）;\r\n\r\n  浮動溫度=  - 46.85 + 175.72 * t / 65536.0 ;\r\n  返回溫度;\r\n}\r\n\r\nfloat  readsht21humidity（）{\r\n  線。flush（）;\r\n  線。beginTransmission（sht21address）;\r\n  線。寫（sht21humid）;\r\n  線。endTransmission（）;\r\n  延遲（100）;\r\n  線。requestFrom（sht21address，3）;\r\n\r\n  byte msb = Wire。read（）;\r\n  byte lsb = Wire。read（）;\r\n  int h =（msb << 8）+ lsb;\r\n  int crc = Wire。read（）;\r\n\r\n  浮動濕度=  - 6 + 125 * h / 65536.0 ;\r\n  返回濕度;\r\n}\r\n\r\nvoid  bmp085Calibration（）\r\n{\r\n  ac1 = bmp085ReadInt（0xAA）;\r\n  ac2 = bmp085ReadInt（0xAC）;\r\n  ac3 = bmp085ReadInt（0xAE）;\r\n  ac4 = bmp085ReadInt（0xB0）;\r\n  ac5 = bmp085ReadInt（0xB2）;\r\n  ac6 = bmp085ReadInt（0xB4）;\r\n  b1 = bmp085ReadInt（0xB6）;\r\n  b2 = bmp085ReadInt（0xB8）;\r\n  mb = bmp085ReadInt（0xBA）;\r\n  mc = bmp085ReadInt（0xBC）;\r\n  md = bmp085ReadInt（0xBE）;\r\n}\r\n\r\n//以攝氏度計算溫度\r\nfloat  bmp085GetTemperature（unsigned  int ut）{\r\n  長 x1，x2;\r\n\r\n  x1 =（（（long）ut  - （long）ac6）*（long）ac5）>> 15 ;\r\n  long a =（long）mc << 11 ;\r\n  long b = x1 + md;\r\n  x2 = a / b;\r\n  b5 = x1 + x2;\r\n  float temp =（（b5 + 8）>> 4）;\r\n  temp = temp / 10 ;\r\n\r\n  回溫;\r\n}\r\n\r\n//計算放棄的壓力\r\n//必須知道校準值\r\n// b5也是必需的，因此必須首先調用bmp085GetTemperature（...）。\r\n//返回的值將以Pa為單位的壓力。\r\nlong  bmp085GetPressure（unsigned  long up）{\r\n  long x1，x2，x3，b3，b6，p;\r\n  無符號 長 b4，b7;\r\n\r\n  b6 = b5  - 4000 ;\r\n  //計算B3\r\n  x1 =（b2 *（b6 * b6）>> 12）>> 11 ;\r\n  x2 =（ac2 * b6）>> 11 ;\r\n  x3 = x1 + x2;\r\n  b3 =（（（（（long）ac1）* 4 + x3）<< OSS）+ 2）>> 2 ;\r\n\r\n  //計算B4\r\n  x1 =（ac3 * b6）>> 13 ;\r\n  x2 =（b1 *（（b6 * b6）>> 12））>> 16 ;\r\n  x3 =（（x1 + x2）+ 2）>> 2 ;\r\n  b4 =（ac4 *（無符號 長）（x3 + 32768））>> 15 ;\r\n\r\n  b7 =（（無符號 長）（向上 -  b3）*（50000 >> OSS））;\r\n  if（b7 < 0x80000000）\r\n    p =（b7 << 1）/ b4;\r\n  其他\r\n    p =（b7 / b4）<< 1 ;\r\n\r\n  x1 =（p >> 8）*（p >> 8）;\r\n  x1 =（x1 * 3038）>> 16 ;\r\n  x2 =（ - 7357 * p）>> 16 ;\r\n  p + =（x1 + x2 + 3791）>> 4 ;\r\n\r\n  long temp = p;\r\n  回溫;\r\n}\r\n\r\n//從“地址”的BMP085讀取1個字節\r\nchar  bmp085Read（unsigned  char address）\r\n{\r\n  unsigned  char數據;\r\n\r\n  線。beginTransmission（BMP085_ADDRESS）;\r\n  線。寫（地址）;\r\n  線。endTransmission（）;\r\n\r\n  線。requestFrom（BMP085_ADDRESS，1）;\r\n  而（！線可用（））;\r\n\r\n  返回電線。read（）;\r\n}\r\n\r\n//從BMP085讀取2個字節\r\n//第一個字節來自'地址'\r\n//第二個字節來自'地址'+ 1\r\nint  bmp085ReadInt（unsigned  char address）\r\n{\r\n  unsigned  char msb，lsb;\r\n\r\n  線。beginTransmission（BMP085_ADDRESS）;\r\n  線。寫（地址）;\r\n  線。endTransmission（）;\r\n\r\n  線。requestFrom（BMP085_ADDRESS，2）;\r\n  而（電線。可用的（）< 2）\r\n    ;\r\n  msb =電線。read（）;\r\n  lsb =電線。read（）;\r\n\r\n  return（int）msb << 8 | LSB;\r\n}\r\n\r\n//讀取未補償的溫度值\r\nunsigned  int  bmp085ReadUT（）{\r\n  unsigned  int ut;\r\n\r\n  //將0x2E寫入寄存器0xF4\r\n  //這要求溫度讀數\r\n  線。beginTransmission（BMP085_ADDRESS）;\r\n  線。寫（0xF4）;\r\n  線。寫（0x2E）;\r\n  線。endTransmission（）;\r\n\r\n  //至少等待4.5毫秒\r\n  延遲（5）;\r\n\r\n  //從寄存器0xF6和0xF7讀取兩個字節\r\n  ut = bmp085ReadInt（0xF6）;\r\n\r\n  返回 ;\r\n}\r\n\r\n//讀取未補償的壓力值\r\nunsigned  long  bmp085ReadUP（）{\r\n\r\n  unsigned  char msb，lsb，xlsb;\r\n  unsigned  long up = 0 ;\r\n\r\n  //將0x34 +（OSS << 6）寫入寄存器0xF4\r\n  //請求壓力讀數和過採樣設置\r\n  線。beginTransmission（BMP085_ADDRESS）;\r\n  線。寫（0xF4）;\r\n  線。寫（0x34 +（OSS << 6））;\r\n  線。endTransmission（）;\r\n\r\n  //等待轉換，延遲時間取決於OSS\r\n  延遲（2 +（3 << OSS））;\r\n\r\n  //讀取寄存器0xF6（MSB），0xF7（LSB）和0xF8（XLSB）\r\n  msb = bmp085Read（0xF6）;\r\n  lsb = bmp085Read（0xF7）;\r\n  xlsb = bmp085Read（0xF8）;\r\n\r\n  up =（（（unsigned  long）msb << 16）|（（unsigned  long）lsb << 8）|（unsigned  long）xlsb）>>（8 -OSS）;\r\n\r\n  返回了;\r\n}\r\n\r\nvoid  writeRegister（int deviceAddress，byte address，byte val）{\r\n  線。beginTransmission（deviceAddress）; //開始傳輸到設備\r\n  線。寫（地址）;       //發送寄存器地址\r\n  線。寫（val）;         //發送要寫入的值\r\n  線。endTransmission（）;     //結束傳輸\r\n}\r\n\r\nint  readRegister（int deviceAddress，byte address）{\r\n\r\n  int v;\r\n  線。beginTransmission（deviceAddress）;\r\n  線。寫（地址）; //註冊閱讀\r\n  線。endTransmission（）;\r\n\r\n  線。requestFrom（deviceAddress，1）; //讀一個字節\r\n\r\n  而（！線。可用（））{\r\n    //等等\r\n  }\r\n\r\n  v =電線。read（）;\r\n  返回 v;\r\n}\r\n\r\nfloat  calcAltitude（float pressure）{\r\n\r\n  浮動 A =壓力/ 101325 ;\r\n  浮動 B = 1 / 5.25588 ;\r\n  float C = pow（A，B）;\r\n  C = 1 -  C;\r\n  C = C / 0.0000225577 ;\r\n\r\n  返回 C;\r\n}"}}
{"actor":{"display_login":"kuominglu"}}