CTO20220705.py 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import paho.mqtt.client as mqtt
  4. import time
  5. import sys
  6. import urllib
  7. import json
  8. import threading
  9. import os
  10. import shutil
  11. import uuid
  12. import hashlib
  13. import array
  14. import numpy
  15. import base64
  16. import datetime
  17. import numpy as np
  18. import pymysql.cursors
  19. import subprocess
  20. """ Device Information - the information about this device
  21. These device information is used for the MQTT topic. This program will subscribe and publish to
  22. the MQTT topic.
  23. MQTT topic to subscribe to: AISKY/<project_name>/<model_name>/<device_id>
  24. MQTT topic to publish to : AISKY/<project_name>/<model_name>/<device_id>/Log
  25. """
  26. # @var project_name The project name comes from the u-boot environment variable 'project'.
  27. # @var model_name The model name comes from the u-boot environment variable 'model'.
  28. # @var device_id The device id comes from the mac address of eth0.
  29. project_name = "Coffee"
  30. model_name = "MK-G"
  31. device_id = "b8:27:eb:b4:59:3e"
  32. """ NOTE: Remember to setup the u-boot environment variables before executing this program. The
  33. commands to setup the u-boot environment variables are as follows.
  34. Setup the 'project' variable: The following command sets the 'project' variable to AppleFarm.
  35. root@mylinkit:~# fw_setenv project AppleFarm
  36. Setup the 'model' variable: The following command sets the 'model' variable to MK-G.
  37. root@mylinkit:~# fw_setenv model MK-G
  38. Then, the following command can be used to display the u-boot environment variables.
  39. root@mylinkit:~# fw_printenv
  40. """
  41. """ MQTT Server
  42. If you don't have your own MQTT server, you can use the public MQTT server 'iot.eclipse.org'. But
  43. with the public MQTT server, you can only publish and subscribe without a user name and password.
  44. Sometimes the public MQTT server is unstable.
  45. """
  46. # @var mqtt_server The URL or IP address of the MQTT server to connect to.
  47. # @var mqtt_port The port of the MQTT server to connect to.
  48. # @var mqtt_alive Maximum period in seconds allowed between communications with the broker. If
  49. # no other messages are being exchanged, this controls the rate at which the
  50. # client will send ping messages to the broker.
  51. mqtt_server = "60.250.156.234"
  52. mqtt_port = 1883
  53. mqtt_alive = 60
  54. # @var mqtt_sub_topic The MQTT topic to subscribe to.
  55. # @var mqtt_pub_topic The MQTT topic to publish to.
  56. mqtt_sub_topic = "AISKY/" + project_name + "/" + model_name + "/" + device_id
  57. mqtt_pub_topic = mqtt_sub_topic + "/Log"
  58. ## Calculate the SHA256 checksum of the file.
  59. # @param file [in] The file path for which you want to calculate the checksum.
  60. def get_sha256sum(file):
  61. with open(file, "rb") as f:
  62. bytes = f.read()
  63. return hashlib.sha256(bytes).hexdigest()
  64. ## Send logs to the server.
  65. # @param command [in] The command received from the server.
  66. # @param response [in] The response message to the command.
  67. #串口設定
  68. #MQTT回覆
  69. def server_log(command,number,response):
  70. localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  71. # message to be sent in JSON format
  72. payload = {
  73. # let server know which device the message came from
  74. 'device_id': device_id,
  75. # let server know when the message was sent from the device
  76. 'localtime': localtime,
  77. 'tank_number':number,
  78. 'command': command,
  79. 'response': response
  80. }
  81. jsonobj = json.dumps(payload, sort_keys=True, indent=4)
  82. mqtt_client.publish(mqtt_pub_topic, jsonobj, qos=2)
  83. print('Sent:')
  84. print(jsonobj)
  85. ## Reset this device.
  86. def system_reboot():
  87. server_log('reboot', 'Restarting system')
  88. time.sleep(5)
  89. os.system('reboot -f')
  90. time.sleep(10)
  91. ## Reset this device to factory default.
  92. def factory_reset():
  93. server_log('factory_reset', 'Resetting to factory default')
  94. time.sleep(5)
  95. os.system('firstboot -y')
  96. os.system('reboot -f')
  97. time.sleep(10)
  98. ## Report the system information about this device to the server.
  99. def system_info():
  100. # get firmware version from the file
  101. fw_version = open('/etc/fw_version').readline().strip()
  102. localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  103. # message to be sent in JSON format
  104. payload = {
  105. 'device_id': device_id,
  106. 'localtime': localtime,
  107. 'command': 'system_info',
  108. 'project': project_name,
  109. 'model': model_name,
  110. 'fw_version': fw_version,
  111. 'mqtt_sub_topic': mqtt_sub_topic,
  112. 'mqtt_pub_topic': mqtt_pub_topic
  113. }
  114. jsonobj = json.dumps(payload, sort_keys=True, indent=4)
  115. mqtt_client.publish(mqtt_pub_topic, jsonobj, qos=2)
  116. print('Sent:')
  117. print(jsonobj)
  118. ## Update system firmware.
  119. # @param msg [in] The message from the server in JSON format.
  120. def system_update(msg):
  121. if (msg['filetype'] == 'img'):
  122. # create a directory to store the firmware image
  123. dirname = '/tmp/' + str(uuid.uuid1()) + '/'
  124. filepath = dirname + 'update.img'
  125. os.makedirs(dirname)
  126. # start to download the firmware image
  127. server_log('system_update', 'Downloading system firmware')
  128. urllib.urlretrieve(msg['url'], filepath)
  129. # check the SHA256 checksum of the firmware image
  130. if (msg['sha256sum'] == get_sha256sum(filepath)):
  131. server_log('system_update', 'Updating system firmware')
  132. # start to upgrade the system firmware
  133. os.system('sysupgrade -n ' + filepath)
  134. else:
  135. server_log('system_update', 'ERROR: SHA256 checksum is wrong')
  136. shutil.rmtree(dirname, ignore_errors=True)
  137. def creat_hex(msg):
  138. f=open("/home/gitc/桌面/CTO20220622/Src/main.c", mode='w')
  139. f.write("/* USER CODE BEGIN Header */\n"
  140. +"/**\n"
  141. +" ******************************************************************************\n"
  142. +" * @file : main.c\n"
  143. +" * @brief : Main program body\n"
  144. +" * @attention\n"
  145. +" *\n"
  146. +" * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.\n"
  147. +" * All rights reserved.</center></h2>\n"
  148. +" *\n"
  149. +" * This software component is licensed by ST under BSD 3-Clause license,\n"
  150. +" * the License; You may not use this file except in compliance with the\n"
  151. +" * License. You may obtain a copy of the License at:\n"
  152. +" * www.st.com/SLA0044\n"
  153. +" *\n"
  154. +" ******************************************************************************\n"
  155. +" */\n"
  156. +"/* USER CODE END Header */\n"
  157. +"\n"
  158. +"/* Includes ------------------------------------------------------------------*/\n")
  159. #添加標題檔-----------------------------------------------------------------------------------------------------
  160. header=('#include"main.h"\n'
  161. +'#include "adc.h"\n'
  162. +'#include "usart.h"\n'
  163. +'#include "i2c.h"\n'
  164. +'#include "time.h"\n'
  165. +'#include "gpio.h"\n')
  166. f.write(header)
  167. #參數變數宣告說明-----------------------------------------------------------------------------------------------
  168. f.write("\n"
  169. +"/* Private variables ---------------------------------------------------------*/\n"
  170. +"#define VECT_TAB_OFFSET 0x10000\n"
  171. +'int tankstatus = 0;\n')
  172. #連接資料庫
  173. #conn = sqlite3.connect('/home/pi/coffee.db')
  174. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffeemanage', passwd='skyeye', database='CoffeeManage',
  175. charset='utf8')
  176. c = conn.cursor()
  177. print ("connect success")
  178. #搜尋最新腳位配置時間
  179. c.execute("SELECT *from coffee1_0_pin ORDER BY datetime ASC")
  180. results=c.fetchall()
  181. for row in results:
  182. time = row[1]
  183. #腳位配置加入
  184. c.execute("SELECT *from coffee1_0_pin where datetime='%s'"\
  185. %(time))
  186. results=c.fetchall()
  187. for row in results:
  188. for i in range (19):
  189. row[i+26]
  190. for i in range (19):
  191. if(i==3 or i==4 or i==6):
  192. PH("M"+str(i+1),row[i+26])
  193. f.write(pin_variables)
  194. DO("M"+str(i+1),row[i+26])
  195. f.write(pin_variables)
  196. ORP("M"+str(i+1),row[i+26])
  197. f.write(pin_variables)
  198. if(i==13):
  199. SHT11("M"+str(i+1),row[i+26])
  200. f.write(pin_variables)
  201. if(i==14):
  202. BMP280("M"+str(i+1),row[i+26])
  203. f.write(pin_variables)
  204. if(i==5 or i==12):
  205. SEN0189("M"+str(i+1),row[i+26])
  206. f.write(pin_variables)
  207. if(i==0):
  208. SonicESMUS07("M"+str(i+1),row[i+26])
  209. f.write(pin_variables)
  210. if(i==7):
  211. SOIL("M"+str(i+1),row[i+26])
  212. f.write(pin_variables)
  213. if(i==8 or i==11 or i==18 or i==10):
  214. WATERLEVEL("M"+str(i+1),row[i+26])
  215. f.write(pin_variables)
  216. feedback("M"+str(i+1),row[i+26])
  217. f.write(pin_variables)
  218. if(i==15):
  219. EC("M"+str(i+1),row[i+26])
  220. f.write(pin_variables)
  221. #感測器和致動器副函式宣告----------------------------------------------------------------
  222. f.write("/* USER CODE BEGIN PV */\n"
  223. +"typedef void (*pFunction)(void);\n"
  224. +"/* USER CODE END PV */\n"
  225. +"/* Private function prototypes -----------------------------------------------*/\n"
  226. +"void SystemClock_Config(void);\n"
  227. +"void MX_GPIO_Input1(unsigned long pin);\n"
  228. +"void sensor(void);\n")
  229. #感測器和致動器副函式宣告----------------------------------------------------------------
  230. #連接資料庫
  231. #conn = sqlite3.connect('/home/pi/coffee.db')
  232. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffeemanage', passwd='skyeye', database='CoffeeManage',
  233. charset='utf8')
  234. c = conn.cursor()
  235. print ("connect success")
  236. #搜尋最新腳位配置時間
  237. c.execute("SELECT *from coffee1_0_pin ORDER BY datetime ASC")
  238. results=c.fetchall()
  239. for row in results:
  240. time = row[1]
  241. #腳位配置加入
  242. c.execute("SELECT *from coffee1_0_pin where datetime='%s'"\
  243. %(time))
  244. results=c.fetchall()
  245. for row in results:
  246. for i in range (19):
  247. row[i+26]
  248. for i in range (19):
  249. if(i==13):
  250. SHT11("M"+str(i+1),row[i+26])
  251. f.write(sensor_code)
  252. if(i==14):
  253. BMP280("M"+str(i+1),row[i+26])
  254. f.write(sensor_code)
  255. if(i==9):
  256. motor("M"+str(i+1),row[i+26])
  257. f.write(sensor_code)
  258. f.write("/* USER CODE BEGIN PFP */\n"
  259. +"/* USER CODE END PFP */\n"
  260. +"/* Private user code ---------------------------------------------------------*/\n"
  261. +"/* USER CODE BEGIN 0 */\n"
  262. +"/* USER CODE END 0 */\n"
  263. +"/**\n"
  264. +"* @brief The application entry point.\n"
  265. +"* @retval int\n"
  266. +"*/\n")
  267. #主程式撰寫--------------------------------------------------------------------------------
  268. #main宣告
  269. f.write("int main(void)\n"
  270. +"{\n"
  271. +" SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;\n")
  272. #致能週邊---------------------------------------------------------------------------------------
  273. enable=(" HAL_Init();\n"
  274. +" SystemClock_Config();\n"
  275. +" MX_GPIO_Init();\n"
  276. +" MX_USART2_UART_Init();\n"
  277. +" MX_UART4_Init();\n"
  278. +" MX_USART1_UART_Init();\n")
  279. f.write(enable)
  280. #連接資料庫
  281. #conn = sqlite3.connect('/home/pi/coffee.db')
  282. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffeemanage', passwd='skyeye', database='CoffeeManage',
  283. charset='utf8')
  284. c = conn.cursor()
  285. print ("connect success")
  286. #搜尋最新腳位配置時間
  287. c.execute("SELECT *from coffee1_0_pin ORDER BY datetime ASC")
  288. results=c.fetchall()
  289. for row in results:
  290. time = row[1]
  291. #腳位配置加入
  292. c.execute("SELECT *from coffee1_0_pin where datetime='%s'"\
  293. %(time))
  294. results=c.fetchall()
  295. for row in results:
  296. for i in range (19):
  297. row[i+26]
  298. for i in range (19):
  299. if(i==14):
  300. BMP280("M"+str(i+1),row[i+26])
  301. f.write(init_code)
  302. if(i==9):
  303. motor("M"+str(i+1),row[i+26])
  304. f.write(init_code)
  305. #---------------------------------------------------------------------------------------------
  306. code=(" while (1)\n"
  307. +" {\n"
  308. +" sensor();\n")
  309. f.write(code)
  310. #MQTT條件分析寫入--------------------------------------------------------------------------------
  311. for i in range (len(msg['cond'])):
  312. m=msg['cond'][i]['cond_main'].split(" ")
  313. if(m[0]=="else" and m[1]=="if"):
  314. m[0]=m[0]+" "+m[1]
  315. m[1]=m[1]
  316. m[2]=m[2]
  317. m[3]=m[3]
  318. m[4]=m[4]
  319. m=""+m[0]+"("+m[1]+m[2]+m[3]
  320. f.write(" "+m)
  321. if(m[0]==" "):
  322. m=""
  323. else:
  324. print(len(msg['cond'][i]['cond_add']))
  325. if(len(msg['cond'][i]['cond_add'])==0):
  326. add=")\n"
  327. f.write(add)
  328. else:
  329. for j in range (len(msg['cond'][i]['cond_add'])):
  330. if(j==len(msg['cond'][i]['cond_add'])-1):
  331. if(msg['cond'][i]['cond_add'][j]==" "):
  332. add=")\n"
  333. else:
  334. add=" "+str(msg['cond'][i]['cond_add'][j])+")\n"
  335. f.write(add)
  336. else:
  337. if(msg['cond'][i]['cond_add'][j]==" "):
  338. add=""
  339. else:
  340. add=" "+str(msg['cond'][i]['cond_add'][j])
  341. f.write(add)
  342. f.write(" {\n")
  343. for j in range (len(msg['cond'][i]['cond_com'])):
  344. if(j==len(msg['cond'][i]['cond_com'])-1):
  345. if(msg['cond'][i]['cond_com'][j]==" "):
  346. com="\n}\n"
  347. else:
  348. com=msg['cond'][i]['cond_com'][j].split("_")
  349. comm=msg['cond'][i]['cond_com'][j].split(" ")
  350. if(comm[0]=="sleep"):
  351. com[0]=comm[0]
  352. com=" "+GPIO(com[0],comm[1])+"\n }\n"
  353. f.write(com)
  354. else:
  355. if(msg['cond'][i]['cond_com'][j]==" "):
  356. com=""
  357. else:
  358. com=msg['cond'][i]['cond_com'][j].split("_")
  359. comm=msg['cond'][i]['cond_com'][j].split(" ")
  360. if(comm[0]=="sleep"):
  361. com[0]=comm[0]
  362. com=" "+GPIO(com[0],comm[1])+"\n"
  363. f.write(com)
  364. f.write(" }\n")
  365. f.write("}\n")
  366. #系統時鐘宣告--------------------------------------------------------------------------------
  367. time=("void SystemClock_Config(void)\n"
  368. +"{\n"
  369. +" RCC_OscInitTypeDef RCC_OscInitStruct = {0};\n"
  370. +" RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};\n"
  371. +" __HAL_RCC_PWR_CLK_ENABLE();\n"
  372. +" __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);\n"
  373. +" RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;\n"
  374. +" RCC_OscInitStruct.HSIState = RCC_HSI_ON;\n"
  375. +" RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;\n"
  376. +" RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;\n"
  377. +" RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;\n"
  378. +" RCC_OscInitStruct.PLL.PLLM = 8;\n"
  379. +" RCC_OscInitStruct.PLL.PLLN = 72;\n"
  380. +" RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;\n"
  381. +" RCC_OscInitStruct.PLL.PLLQ = 3;\n"
  382. +" RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;\n"
  383. +" if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)\n"
  384. +" {\n"
  385. +" Error_Handler();\n"
  386. +" }\n"
  387. +" RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK\n"
  388. +" |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;\n"
  389. +" RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;\n"
  390. +" RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;\n"
  391. +" RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;\n"
  392. +" RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;\n"
  393. +" if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)\n"
  394. +" {\n"
  395. +" Error_Handler();\n"
  396. +" }\n"
  397. +"}\n")
  398. f.write(time)
  399. #馬達和伺服馬達副函式------------------------------------------------------------------------------
  400. add_code2=("void user_pwm_setvalue(uint16_t value)\n"
  401. +"{\n"
  402. +" TIM_OC_InitTypeDef sConfigOC;\n"
  403. +" sConfigOC.OCMode = TIM_OCMODE_PWM1;\n"
  404. +" sConfigOC.Pulse = value;\n"
  405. +" sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;\n"
  406. +" sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;\n"
  407. +" HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);\n"
  408. +" HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);\n"
  409. +"}\n"
  410. +"void user_pwm_setvalue2(uint16_t value)\n"
  411. +"{\n"
  412. +" TIM_OC_InitTypeDef sConfigOC;\n"
  413. +" sConfigOC.OCMode = TIM_OCMODE_PWM1;\n"
  414. +" sConfigOC.Pulse = value;\n"
  415. +" sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;\n"
  416. +" sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;\n"
  417. +" HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);\n"
  418. +" HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);\n"
  419. +"}\n")
  420. f.write(add_code2)
  421. #感測器程式-----------------------------------------------------------------------------------------------------
  422. sensor=("void sensor(void)\n"
  423. +"{\n")
  424. f.write(sensor)
  425. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffeemanage', passwd='skyeye', database='CoffeeManage',
  426. charset='utf8')
  427. #conn = sqlite3.connect('/home/pi/coffee.db')
  428. c = conn.cursor()
  429. print ("connect success")
  430. #搜尋最新腳位配置時間
  431. c.execute("SELECT *from coffee1_0_pin ORDER BY datetime ASC")
  432. results = c.fetchall()
  433. for row in results:
  434. time = row[1]
  435. #腳位配置加入
  436. c.execute("SELECT *from coffee1_0_pin where datetime='%s'"\
  437. %(time))
  438. results = c.fetchall()
  439. for row in results:
  440. for i in range (19):
  441. row[i+26]
  442. for i in range (19):
  443. if(i==3 or i==4 or i==6):
  444. PH("M"+str(i+1),row[i+26])
  445. f.write(pin_code)
  446. DO("M"+str(i+1),row[i+26])
  447. f.write(pin_code)
  448. ORP("M"+str(i+1),row[i+26])
  449. f.write(pin_code)
  450. if(i==13):
  451. SHT11("M"+str(i+1),row[i+26])
  452. f.write(pin_code)
  453. if(i==14):
  454. BMP280("M"+str(i+1),row[i+26])
  455. f.write(pin_code)
  456. if(i==5 or i==12):
  457. SEN0189("M"+str(i+1),row[i+26])
  458. f.write(pin_code)
  459. if(i==0):
  460. SonicESMUS07("M"+str(i+1),row[i+26])
  461. f.write(pin_code)
  462. if(i==7):
  463. SOIL("M"+str(i+1),row[i+26])
  464. f.write(pin_code)
  465. if(i==8 or i==11 or i==18 or i==10):
  466. WATERLEVEL("M"+str(i+1),row[i+26])
  467. f.write(pin_code)
  468. feedback("M"+str(i+1),row[i+26])
  469. f.write(pin_code)
  470. if(i==15):
  471. EC("M"+str(i+1),row[i+26])
  472. f.write(pin_code)
  473. sensor=(" HAL_Delay(10000);\n"
  474. +"}\n")
  475. f.write(sensor)
  476. #加入感測器副函式程式碼-------------------------------------------------------------------------------
  477. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffeemanage', passwd='skyeye', database='CoffeeManage',
  478. charset='utf8')
  479. #conn = sqlite3.connect('/home/pi/coffee.db')
  480. c = conn.cursor()
  481. print ("connect success")
  482. #搜尋最新腳位配置時間
  483. c.execute("SELECT *from coffee1_0_pin ORDER BY datetime ASC")
  484. results = c.fetchall()
  485. for row in results:
  486. time = row[1]
  487. #腳位配置加入
  488. c.execute("SELECT *from coffee1_0_pin where datetime='%s'"\
  489. %(time))
  490. results = c.fetchall()
  491. for row in results:
  492. for i in range (19):
  493. row[i+26]
  494. for i in range (19):
  495. if(i==13):
  496. SHT11("M"+str(i+1),row[i+26])
  497. f.write(pin_add_code)
  498. if(i==14):
  499. BMP280("M"+str(i+1),row[i+26])
  500. f.write(pin_add_code)
  501. if(i==0):
  502. SonicESMUS07("M"+str(i+1),row[i+26])
  503. f.write(pin_add_code)
  504. if(i==7):
  505. SOIL("M"+str(i+1),row[i+26])
  506. f.write(pin_add_code)
  507. if(i==15):
  508. EC("M"+str(i+1),row[i+26])
  509. f.write(pin_add_code)
  510. #ADC程式宣告----------------------------------------------------------------------------------
  511. gpio=("void MX_ADC1_Init1(char pin)\n"
  512. +"{\n"
  513. +" ADC_ChannelConfTypeDef sConfig = {0};\n"
  514. +" hadc1.Instance = ADC1;\n"
  515. +" hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;\n"
  516. +" hadc1.Init.Resolution = ADC_RESOLUTION_12B;\n"
  517. +" hadc1.Init.ScanConvMode = DISABLE;\n"
  518. +" hadc1.Init.ContinuousConvMode = DISABLE;\n"
  519. +" hadc1.Init.DiscontinuousConvMode = DISABLE;\n"
  520. +" hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;\n"
  521. +" hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;\n"
  522. +" hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;\n"
  523. +" hadc1.Init.NbrOfConversion = 1;\n"
  524. +" hadc1.Init.DMAContinuousRequests = DISABLE;\n"
  525. +" hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;\n"
  526. +" if (HAL_ADC_Init(&hadc1) != HAL_OK)\n"
  527. +" {\n"
  528. +" Error_Handler();\n"
  529. +" }\n"
  530. +" sConfig.Channel = pin;\n"
  531. +" sConfig.Rank = 1;\n"
  532. +" sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;\n"
  533. +" if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)\n"
  534. +" {\n"
  535. +" Error_Handler();\n"
  536. +" }\n"
  537. +"}\n")
  538. f.write(gpio)
  539. #GPIO input程式宣告----------------------------------------------------------------------------------
  540. gpio2=("void MX_GPIO_Input1(unsigned long pin)\n"
  541. +"{\n"
  542. +" GPIO_InitTypeDef GPIO_InitStruct = {0};\n"
  543. +" __HAL_RCC_GPIOE_CLK_ENABLE();\n"
  544. +" GPIO_InitStruct.Pin = pin;\n"
  545. +" GPIO_InitStruct.Mode = GPIO_MODE_INPUT;\n"
  546. +" GPIO_InitStruct.Pull = GPIO_PULLDOWN;\n"
  547. +" HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);\n"
  548. +"}\n")
  549. f.write(gpio2)
  550. #錯誤程式-------------------------------------------------------------------------------------
  551. f.write("void Error_Handler(void)\n{\n}\n")
  552. f.close()
  553. f=open("/home/gitc/桌面/CTO20220622/Src/main.c", mode='r')
  554. words=f.read()
  555. print(words)
  556. f.close()
  557. #程式轉檔成hex檔-----------------------------------------------------------------------------------
  558. n=subprocess.call(["make"],cwd="/home/gitc/桌面/CTO20220622/")
  559. if n>0:
  560. server_log(msg['command'],"D1","error")
  561. else:
  562. server_log(msg['command'],"D1","success")
  563. #致動器功能程式定義
  564. def GPIO(relay,status):
  565. if relay=="R1":
  566. if status=="on":
  567. act=" HAL_GPIO_WritePin(GPIOA,GPIO_PIN_13, GPIO_PIN_RESET);\n"
  568. elif status=="off":
  569. act=" HAL_GPIO_WritePin(GPIOA,GPIO_PIN_13, GPIO_PIN_SET);\n"
  570. else:
  571. act=""
  572. elif relay=="R2":
  573. if status=="on":
  574. act=" HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12, GPIO_PIN_RESET);\n"
  575. elif status=="off":
  576. act=" HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12, GPIO_PIN_SET);\n"
  577. else:
  578. act=""
  579. elif relay=="R3":
  580. if status=="on":
  581. act=" HAL_GPIO_WritePin(GPIOA,GPIO_PIN_11, GPIO_PIN_RESET);\n"
  582. elif status=="off":
  583. act=" HAL_GPIO_WritePin(GPIOA,GPIO_PIN_11, GPIO_PIN_SET);\n"
  584. else:
  585. act=""
  586. elif relay=="R4":
  587. if status=="on":
  588. act=" HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10, GPIO_PIN_RESET);\n"
  589. elif status=="off":
  590. act=" HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10, GPIO_PIN_SET);\n"
  591. else:
  592. act=""
  593. elif relay=="R5":
  594. if status=="on":
  595. act=" HAL_GPIO_WritePin(GPIOA,GPIO_PIN_9, GPIO_PIN_RESET);\n"
  596. elif status=="off":
  597. act=" HAL_GPIO_WritePin(GPIOA,GPIO_PIN_9, GPIO_PIN_SET);\n"
  598. else:
  599. act=""
  600. elif relay=="R6":
  601. if status=="on":
  602. act=" HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_RESET);\n"
  603. elif status=="off":
  604. act=" HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_SET);\n"
  605. else:
  606. act=""
  607. elif relay=="R7":
  608. if status=="on":
  609. act=" HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9, GPIO_PIN_RESET);\n"
  610. elif status=="off":
  611. act=" HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9, GPIO_PIN_SET);\n"
  612. else:
  613. act=""
  614. elif relay=="R8":
  615. if status=="on":
  616. act=" HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_RESET);\n"
  617. elif status=="off":
  618. act=" HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_SET);\n"
  619. else:
  620. act=""
  621. elif relay=="R9":
  622. if status=="on":
  623. act=" HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14, GPIO_PIN_RESET);\n"
  624. elif status=="off":
  625. act=" HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14, GPIO_PIN_SET);\n"
  626. else:
  627. act=""
  628. elif relay=="R10":
  629. if status=="on":
  630. act=" HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);\n"
  631. elif status=="off":
  632. act=" HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_SET);\n"
  633. else:
  634. act=""
  635. elif relay=="R11":
  636. if status=="on":
  637. act=" HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12, GPIO_PIN_RESET);\n"
  638. elif status=="off":
  639. act=" HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12, GPIO_PIN_SET);\n"
  640. else:
  641. act=""
  642. elif relay=="R12":
  643. if status=="on":
  644. act=" HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_RESET);\n"
  645. elif status=="off":
  646. act=" HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_SET);\n"
  647. else:
  648. act=""
  649. elif relay=="R13":
  650. if status=="on":
  651. act=" HAL_GPIO_WritePin(GPIOD,GPIO_PIN_10, GPIO_PIN_RESET);\n"
  652. elif status=="off":
  653. act=" HAL_GPIO_WritePin(GPIOD,GPIO_PIN_10, GPIO_PIN_SET);\n"
  654. else:
  655. act=""
  656. elif relay=="R14":
  657. if status=="on":
  658. act=" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);\n"
  659. elif status=="off":
  660. act=" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_SET);\n"
  661. else:
  662. act=""
  663. elif relay=="R15":
  664. if status=="on":
  665. act=" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_14, GPIO_PIN_RESET);\n"
  666. elif status=="off":
  667. act=" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_14, GPIO_PIN_SET);\n"
  668. else:
  669. act=""
  670. elif relay=="R16":
  671. if status=="on":
  672. act=" HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);\n"
  673. elif status=="off":
  674. act=" HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_SET);\n"
  675. else:
  676. act=""
  677. elif relay=="R17":
  678. if status=="on":
  679. act=" HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14, GPIO_PIN_RESET);\n"
  680. elif status=="off":
  681. act=" HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14, GPIO_PIN_SET);\n"
  682. else:
  683. act=""
  684. elif relay=="R18":
  685. if status=="on":
  686. act=" HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET);\n"
  687. elif status=="off":
  688. act=" HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_SET);\n"
  689. else:
  690. act=""
  691. elif relay=="R19":
  692. if status=="on":
  693. act=" HAL_GPIO_WritePin(GPIOE,GPIO_PIN_12, GPIO_PIN_RESET);\n"
  694. elif status=="off":
  695. act=" HAL_GPIO_WritePin(GPIOE,GPIO_PIN_12, GPIO_PIN_SET);\n"
  696. else:
  697. act=""
  698. elif relay=="R20":
  699. if status=="on":
  700. act=" HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);\n"
  701. elif status=="off":
  702. act=" HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_SET);\n"
  703. else:
  704. act=""
  705. elif relay=="sleep":
  706. act=" HAL_Delay("+status+"000);\n"
  707. elif relay=="M10":
  708. if int(status)>0:
  709. act=(" user_pwm_setvalue2(0);\n"
  710. +" user_pwm_setvalue("+status+");\n")
  711. elif int(status)<0:
  712. status=-1*int(status)
  713. act=(" user_pwm_setvalue(0);\n"
  714. +" user_pwm_setvalue2("+str(status)+");\n")
  715. else:
  716. act=""
  717. elif relay=="M2":
  718. if status=="on":
  719. act=" user_pwm_setvalue3(50);\n"
  720. elif status=="off":
  721. act=" user_pwm_setvalue3(180);\n"
  722. else:
  723. act=""
  724. elif relay=="M3":
  725. if status=="on":
  726. act=" user_pwm_setvalue4(50);\n"
  727. elif status=="off":
  728. act=" user_pwm_setvalue4(180);\n"
  729. else:
  730. act=""
  731. else:
  732. act=""
  733. return act
  734. #感測器功能程式定義
  735. def PH(pin_position,pin_type):
  736. global pin_variables,pin_code
  737. if pin_position=="M4":
  738. pin="ADC_CHANNEL_0"
  739. elif pin_position=="M5":
  740. pin="ADC_CHANNEL_3"
  741. elif pin_position=="M7":
  742. pin="ADC_CHANNEL_4"
  743. if pin_type =="PH":
  744. print("PH ok")
  745. pin_variables=("uint16_t "+pin_position+"_AD_Value = 0;\n"
  746. +"float "+pin_position+"_voltage_V =0;\n"
  747. +"float "+pin_position+"_pH_mid = 1.500;\n"
  748. +"float "+pin_position+"_pH_low = 2.030;\n"
  749. +"float "+pin_position+"_pH_high =0.975;\n"
  750. +"float "+pin_position+"_"+pin_type+"=0;\n")
  751. pin_code=(" MX_ADC1_Init("+pin+");\n"
  752. +" HAL_ADC_Start(&hadc1);\n"
  753. +" HAL_ADC_PollForConversion(&hadc1, 50);\n"
  754. +" if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))\n"
  755. +" {\n"
  756. +" "+pin_position+"_AD_Value = HAL_ADC_GetValue(&hadc1);\n"
  757. +" "+pin_position+"_voltage_V = "+pin_position+"_AD_Value*3.3f/4096;\n"
  758. +" if ("+pin_position+"_voltage_V > "+pin_position+"_pH_mid)\n"
  759. +" {\n"
  760. +" "+pin_position+"_"+pin_type+" = 7.0 - 3.0 / ("+pin_position+"_pH_low - "+pin_position+"_pH_mid) * ("+pin_position+"_voltage_V - "+pin_position+"_pH_mid);\n"
  761. +" }\n"
  762. +" else\n"
  763. +" {\n"
  764. +" "+pin_position+"_"+pin_type+" = 7.0 - 3.0 / ("+pin_position+"_pH_mid - "+pin_position+"_pH_high) * ("+pin_position+"_voltage_V - "+pin_position+"_pH_mid);\n"
  765. +" }\n"
  766. +" }\n"
  767. +" HAL_UART_Transmit(&huart4,(unsigned char*)&"+pin_position+"_"+pin_type+",4,10);\n")
  768. else:
  769. pin_variables=""
  770. pin_code=""
  771. def DO(pin_position,pin_type):
  772. global pin_variables,pin_code
  773. if pin_position=="M4":
  774. pin="ADC_CHANNEL_0"
  775. elif pin_position=="M5":
  776. pin="ADC_CHANNEL_3"
  777. elif pin_position=="M7":
  778. pin="ADC_CHANNEL_4"
  779. if pin_type =="DO":
  780. print("DO ok")
  781. pin_variables=("uint16_t "+pin_position+"_AD_Value = 0;\n"
  782. +"float "+pin_position+"_voltage_V =0;\n"
  783. +"float "+pin_position+"_DO_offset =0.44;\n"
  784. +"float "+pin_position+"_"+pin_type+" =0;\n")
  785. pin_code=(" MX_ADC1_Init("+pin+");\n"
  786. +" HAL_ADC_Start(&hadc1);\n"
  787. +" HAL_ADC_PollForConversion(&hadc1, 50);\n"
  788. +" if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))\n"
  789. +" {\n"
  790. +" "+pin_position+"_AD_Value = HAL_ADC_GetValue(&hadc1);\n"
  791. +" "+pin_position+"_voltage_V = "+pin_position+"_AD_Value*3.3f/4096;\n"
  792. +" "+pin_position+"_"+pin_type+"= ((("+pin_position+"_AD_Value*3.3f/4096)*100)/"+pin_position+"_DO_offset);\n"
  793. +" }\n"
  794. +" HAL_UART_Transmit(&huart4,(unsigned char*)&"+pin_position+"_"+pin_type+",4,10);\n")
  795. else:
  796. pin_variables=""
  797. pin_code=""
  798. def WATERLEVEL(pin_position,pin_type):
  799. global pin_variables,pin_code
  800. if pin_position=="M9":
  801. pin="GPIO_PIN_9"
  802. elif pin_position=="M11":
  803. pin="GPIO_PIN_7"
  804. elif pin_position=="M12":
  805. pin="GPIO_PIN_8"
  806. elif pin_position=="M19":
  807. pin="GPIO_PIN_10"
  808. if pin_type =="WATERLEVEL":
  809. print("WATERLEVEL ok")
  810. pin_variables=("int "+pin_position+"_"+pin_type+" =0;\n")
  811. pin_code=(" MX_GPIO_Input1("+pin+");\n"
  812. +" if (HAL_GPIO_ReadPin(GPIOE, "+pin+") == GPIO_PIN_SET)\n"
  813. +" {\n"
  814. +" "+pin_position+"_"+pin_type+" =1;\n"
  815. +" HAL_UART_Transmit(&huart4,(unsigned char*)&"+pin_position+"_"+pin_type+",1,10);\n"
  816. +" }\n"
  817. +" else\n"
  818. +" {\n"
  819. +" "+pin_position+"_"+pin_type+" =0;\n"
  820. +" HAL_UART_Transmit(&huart4,(unsigned char*)&"+pin_position+"_"+pin_type+",1,10);\n"
  821. +" }\n")
  822. else:
  823. pin_variables=""
  824. pin_code=""
  825. def feedback(pin_position,pin_type):
  826. global pin_variables,pin_code
  827. if pin_position=="M9":
  828. pin="GPIO_PIN_9"
  829. elif pin_position=="M11":
  830. pin="GPIO_PIN_7"
  831. elif pin_position=="M12":
  832. pin="GPIO_PIN_8"
  833. elif pin_position=="M19":
  834. pin="GPIO_PIN_10"
  835. if pin_type =="feedback":
  836. print("feedback ok")
  837. pin_variables=("int "+pin_position+"_"+pin_type+" =0;\n")
  838. pin_code=(" MX_GPIO_Input1("+pin+");\n"
  839. +" if (HAL_GPIO_ReadPin(GPIOE, "+pin+" == GPIO_PIN_SET)\n"
  840. +" {\n"
  841. +" "+pin_position+"_"+pin_type+" =1;\n"
  842. +" HAL_UART_Transmit(&huart4,(unsigned char*)&"+pin_position+"_"+pin_type+",1,10);\n"
  843. +" }\n"
  844. +" else\n"
  845. +" {\n"
  846. +" "+pin_position+"_"+pin_type+" =0;\n"
  847. +" HAL_UART_Transmit(&huart4,(unsigned char*)&"+pin_position+"_"+pin_type+",1,10);\n"
  848. +" }\n")
  849. else:
  850. pin_variables=""
  851. pin_code=""
  852. def butterflyvalvefeedback(pin_position,pin_type):
  853. global pin_variables,pin_code
  854. if pin_position=="M9":
  855. pin="GPIO_PIN_9"
  856. elif pin_position=="M11":
  857. pin="GPIO_PIN_7"
  858. elif pin_position=="M12":
  859. pin="GPIO_PIN_8"
  860. elif pin_position=="M19":
  861. pin="GPIO_PIN_10"
  862. if pin_type =="butterflyvalvefeedback":
  863. print("butterflyvalvefeedback ok")
  864. pin_variables=("int "+pin_position+"_"+pin_type+" =0;\n")
  865. pin_code=(" MX_GPIO_Input1("+pin+");\n"
  866. +" if (HAL_GPIO_ReadPin(GPIOE, "+pin+" == GPIO_PIN_SET)\n"
  867. +" {\n"
  868. +" "+pin_position+"_"+pin_type+" =1;\n"
  869. +" HAL_UART_Transmit(&huart4,(unsigned char*)&"+pin_position+"_"+pin_type+",1,10);\n"
  870. +" }\n"
  871. +" else\n"
  872. +" {\n"
  873. +" "+pin_position+"_"+pin_type+" =0;\n"
  874. +" HAL_UART_Transmit(&huart4,(unsigned char*)&"+pin_position+"_"+pin_type+",1,10);\n"
  875. +" }\n")
  876. else:
  877. pin_variables=""
  878. pin_code=""
  879. def ORP(pin_position,pin_type):
  880. global pin_variables,pin_code
  881. if pin_position=="M4":
  882. pin="ADC_CHANNEL_0"
  883. elif pin_position=="M5":
  884. pin="ADC_CHANNEL_3"
  885. elif pin_position=="M7":
  886. pin="ADC_CHANNEL_4"
  887. if pin_type =="ORP":
  888. print("ORP ok")
  889. pin_variables=("uint16_t "+pin_position+"_AD_Value = 0;\n"
  890. +"float "+pin_position+"_voltage_V =0;\n"
  891. +"float "+pin_position+"_ORP_offset =0;\n"
  892. +"float "+pin_position+"_"+pin_type+" =0;\n")
  893. pin_code=(" MX_ADC1_Init("+pin+");\n"
  894. +" HAL_ADC_Start(&hadc1);\n"
  895. +" HAL_ADC_PollForConversion(&hadc1, 50);\n"
  896. +" if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))\n"
  897. +" {\n"
  898. +" "+pin_position+"_AD_Value = HAL_ADC_GetValue(&hadc1);\n"
  899. +" "+pin_position+"_voltage_V = "+pin_position+"_AD_Value*3.3f/4096;\n"
  900. +" "+pin_position+"_"+pin_type+"= ((("+pin_position+"_AD_Value*3.3f/4096)-(1.5+"+pin_position+"_ORP_offset)));\n"
  901. +" }\n"
  902. +" HAL_UART_Transmit(&huart4,(unsigned char*)&"+pin_position+"_"+pin_type+",4,10);\n")
  903. else:
  904. pin_variables=""
  905. pin_code=""
  906. def SonicESMUS07(pin_position,pin_type):
  907. global pin_variables,pin_code,pin_add_code
  908. if pin_position=="M1":
  909. pin="ADC_CHANNEL_6"
  910. if pin_type =="SonicESMUS07":
  911. print("ESMUS07 ok")
  912. pin_variables=("uint16_t "+pin_position+"_AD_Value = 0;\n"
  913. +"float "+pin_position+"_voltage_V =0;\n"
  914. +"float "+pin_position+"_"+pin_type+" =0;\n")
  915. pin_code=(" MX_ADC1_Init("+pin+");\n"
  916. +" HAL_ADC_Start(&hadc1);\n"
  917. +" HAL_ADC_PollForConversion(&hadc1, 50);\n"
  918. +" if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))\n"
  919. +" {\n"
  920. +" "+pin_position+"_AD_Value = HAL_ADC_GetValue(&hadc1);\n"
  921. +" "+pin_position+"_voltage_V = "+pin_position+"_AD_Value*5.3f/4096;\n"
  922. +" if ("+pin_position+"_voltage_V==0)\n"
  923. +" {\n"
  924. +" "+pin_position+"_"+pin_type+ " = 100;\n"
  925. +" }\n"
  926. +" else\n"
  927. +" {\n"
  928. +" "+pin_position+"_"+pin_type+ " = ("+pin_position+"_voltage_V*180)+100;\n"
  929. +" }\n"
  930. +" }\n"
  931. +" HAL_UART_Transmit(&huart4,(unsigned char*)&"+pin_position+"_"+pin_type+",4,10);\n")
  932. else:
  933. pin_variables=""
  934. pin_code=""
  935. pin_add_code=""
  936. def SEN0189(pin_position,pin_type):
  937. global pin_variables,pin_code,pin_add_code
  938. if pin_position=="M6":
  939. pin="ADC_CHANNEL_13"
  940. elif pin_position=="M13":
  941. pin="ADC_CHANNEL_5"
  942. if pin_type =="SEN0189":
  943. print("SEN0189 ok")
  944. pin_variables=("uint16_t "+pin_position+"_AD_Value = 0;\n"
  945. +"float "+pin_position+"_voltage_V =0;\n"
  946. +"float "+pin_position+"_"+pin_type+" =0;\n")
  947. pin_code=(" MX_ADC1_Init("+pin+");\n"
  948. +" HAL_ADC_Start(&hadc1);\n"
  949. +" HAL_ADC_PollForConversion(&hadc1, 50);\n"
  950. +" if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))\n"
  951. +" {\n"
  952. +" "+pin_position+"_AD_Value = HAL_ADC_GetValue(&hadc1);\n"
  953. +" "+pin_position+"_voltage_V = "+pin_position+"_AD_Value*5.0f/4096;\n"
  954. +" if ("+pin_position+"_voltage_V>4.2)\n"
  955. +" {\n"
  956. +" "+pin_position+"_"+pin_type+ " = 0;\n"
  957. +" }\n"
  958. +" else if ("+pin_position+"_voltage_V<2.5)\n"
  959. +" {\n"
  960. +" "+pin_position+"_"+pin_type+ " = 3000;\n"
  961. +" }\n"
  962. +" else\n"
  963. +" {\n"
  964. +" "+pin_position+"_"+pin_type+"=(-1120.4*"+pin_position+"_voltage_V*"+pin_position+"_voltage_V)+(5742.3*"+pin_position+"_voltage_V)-4352.9;\n"
  965. +" }\n"
  966. +" }\n"
  967. +" HAL_UART_Transmit(&huart4,(unsigned char*)&"+pin_position+"_"+pin_type+",4,10);\n")
  968. print(pin_variables)
  969. print(pin_code)
  970. else:
  971. pin_variables=""
  972. pin_code=""
  973. pin_add_code=""
  974. def SOIL(pin_position,pin_type):
  975. global pin_variables,pin_code,pin_add_code
  976. if pin_type =="Soil":
  977. print("SOIL ok")
  978. pin_variables=("uint8_t soil[8] ={0x01,0x03,0x00,0x00,0x00,0x03,0x05,0xCB};\n"
  979. +"uint8_t data[11] ={0,0,0,0,0,0,0,0,0,0,0};\n"
  980. +"float "+pin_position+"_"+pin_type+" =0;\n"
  981. +"int rh=0;\n"
  982. +"int temp=0;\n"
  983. +"int ec=0;\n")
  984. pin_code=(" HAL_UART_Transmit(&huart2, (uint8_t *)soil,sizeof(soil),50);\n"
  985. +" HAL_UART_Receive_IT(&huart2, (uint8_t *)data,sizeof(data));\n")
  986. pin_add_code=("void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)\n"
  987. +"{\n"
  988. +" if (huart->Instance == USART2)\n"
  989. +" {\n"
  990. +" if (data[0]==0x01 && data[1]==0x03)\n"
  991. +" {\n"
  992. +" rh = (data[3] << 8) + data[4];\n"
  993. +" temp = (data[5] << 8) + data[6];\n"
  994. +" ec = (data[7] << 8) + data[8];\n"
  995. +" }\n"
  996. +" }\n"
  997. +"}\n")
  998. print(pin_code)
  999. print(pin_add_code)
  1000. else:
  1001. pin_variables=""
  1002. pin_code=""
  1003. pin_add_code=""
  1004. def EC(pin_position,pin_type):
  1005. global pin_variables,pin_code,pin_add_code
  1006. if pin_type =="EC":
  1007. print("EC ok")
  1008. pin_variables=("uint8_t data[4] ={0,0,0,0};\n"
  1009. +"float "+pin_position+"_"+pin_type+" =0;\n")
  1010. pin_code=(" HAL_UART_Receive_IT(&huart2, (uint8_t *)data,sizeof(data));\n")
  1011. pin_add_code=("void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)\n"
  1012. +"{\n"
  1013. +" if (huart->Instance == USART2)\n"
  1014. +" {\n"
  1015. +" if (data[0]==0x30 && data[1]==0x2E)\n"
  1016. +" {\n"
  1017. +" HAL_UART_Transmit(&huart4, (uint8_t *)data,sizeof(data),50);\n"
  1018. +" }\n"
  1019. +" }\n"
  1020. +"}\n")
  1021. print(pin_variables)
  1022. print(pin_code)
  1023. print(pin_add_code)
  1024. else:
  1025. pin_variables=""
  1026. pin_code=""
  1027. pin_add_code=""
  1028. def SHT11(pin_position,pin_type):
  1029. global pin_variables,pin_code,pin_add_code
  1030. global sensor_code
  1031. if pin_type =="SHT11":
  1032. print("SHT11 ok")
  1033. pin_variables=("uint8_t cmd[7]={0xFF,0x00,0x00,0x00,0x00,0x00,0x0D};\n"
  1034. +"uint16_t i,val,value_H,value_L,Cvalue,Hvalue;\n"
  1035. +"int error;\n"
  1036. +"float C1=-2.0468;\n"
  1037. +"float C2=0.0367;\n"
  1038. +"float C3=-0.0000015955;\n"
  1039. +"float RH_Lin;\n"
  1040. +"float RH_Ture;\n"
  1041. +"float d1=-39.6;\n"
  1042. +"float d2=0.01;\n"
  1043. +"float T1=0.01;\n"
  1044. +"float T2=0.00008;\n"
  1045. +"float temp_C=0;\n"
  1046. +"int temp;\n"
  1047. +"int RH;\n"
  1048. +"float "+pin_position+"_"+pin_type+" =0;\n")
  1049. sensor_code=("void SHT10_TransStart(void);\n"
  1050. +"void SHT10_WriteByte(void);\n"
  1051. +"void SHT10_WriteByte2(void);\n"
  1052. +"void SHT10_ReadByte(void);\n"
  1053. +"void SHT10_Calculate(void);\n"
  1054. +"void MX_GPIO_Input(void);\n")
  1055. pin_code=(" Cvalue=0;\n"
  1056. +" Hvalue=0;\n"
  1057. +" value_H=0;\n"
  1058. +" value_L=0;\n"
  1059. +" SHT10_TransStart();\n"
  1060. +" SHT10_WriteByte();\n"
  1061. +" MX_GPIO_Input();\n"
  1062. +" HAL_Delay(250);\n"
  1063. +" if (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0)\n"
  1064. +" {\n"
  1065. +" SHT10_ReadByte();\n"
  1066. +" value_H=val;\n"
  1067. +" SHT10_ReadByte();\n"
  1068. +" value_L=val;\n"
  1069. +" Cvalue = (value_H<< 8 | value_L);\n"
  1070. +" }\n"
  1071. +" SHT10_TransStart();\n"
  1072. +" SHT10_WriteByte2();\n"
  1073. +" MX_GPIO_Input();\n"
  1074. +" HAL_Delay(250);\n"
  1075. +" if (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0)\n"
  1076. +" {\n"
  1077. +" SHT10_ReadByte();\n"
  1078. +" value_H=val;\n"
  1079. +" SHT10_ReadByte();\n"
  1080. +" value_L=val;\n"
  1081. +" Hvalue = (value_H<< 8 | value_L);\n"
  1082. +" }\n"
  1083. +" SHT10_Calculate();\n")
  1084. pin_add_code=(" void SHT10_TransStart(void)\n"
  1085. +"{\n"
  1086. +" MX_GPIO_Init();\n"
  1087. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);\n"
  1088. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);\n"
  1089. +" HAL_Delay(10);\n"
  1090. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);\n"
  1091. +" HAL_Delay(10);\n"
  1092. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);\n"
  1093. +" HAL_Delay(10);\n"
  1094. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);\n"
  1095. +" HAL_Delay(10);\n"
  1096. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);\n"
  1097. +" HAL_Delay(10);\n"
  1098. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);\n"
  1099. +" HAL_Delay(10);\n"
  1100. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);\n"
  1101. +" HAL_Delay(10);\n"
  1102. +"}\n"
  1103. +" void SHT10_WriteByte(void)\n"
  1104. +"{\n"
  1105. +" MX_GPIO_Init();\n"
  1106. +" for (i=0x80;i>0;i/=2)\n"
  1107. +" {\n"
  1108. +" if (i & 0x03)\n"
  1109. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);\n"
  1110. +" else\n"
  1111. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);\n"
  1112. +" HAL_Delay(10);\n"
  1113. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);\n"
  1114. +" HAL_Delay(10);\n"
  1115. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);\n"
  1116. +" HAL_Delay(10);\n"
  1117. +" }\n"
  1118. +" MX_GPIO_Input();\n"
  1119. +" HAL_Delay(10);\n"
  1120. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);\n"
  1121. +" HAL_Delay(10);\n"
  1122. +" error=HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);\n"
  1123. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);\n"
  1124. +" HAL_Delay(10);\n"
  1125. +"}\n"
  1126. +" void SHT10_WriteByte2(void)\n"
  1127. +"{\n"
  1128. +" MX_GPIO_Init();\n"
  1129. +" for (i=0x80;i>0;i/=2)\n"
  1130. +" {\n"
  1131. +" if (i & 0x05)\n"
  1132. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);\n"
  1133. +" else\n"
  1134. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);\n"
  1135. +" HAL_Delay(10);\n"
  1136. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);\n"
  1137. +" HAL_Delay(10);\n"
  1138. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);\n"
  1139. +" HAL_Delay(10);\n"
  1140. +" }\n"
  1141. +" MX_GPIO_Input();\n"
  1142. +" HAL_Delay(10);\n"
  1143. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);\n"
  1144. +" HAL_Delay(10);\n"
  1145. +" error=HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);\n"
  1146. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);\n"
  1147. +" HAL_Delay(10);\n"
  1148. +"}\n"
  1149. +" void SHT10_ReadByte(void)\n"
  1150. +"{\n"
  1151. +" val=0;\n"
  1152. +" MX_GPIO_Input();\n"
  1153. +" for (i=0x80;i>0;i/=2)\n"
  1154. +" {\n"
  1155. +" HAL_Delay(10);\n"
  1156. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);\n"
  1157. +" HAL_Delay(10);\n"
  1158. +" if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0))\n"
  1159. +" val=( val | i );\n"
  1160. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);\n"
  1161. +" }\n"
  1162. +" MX_GPIO_Init();\n"
  1163. +" if (1)\n"
  1164. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);\n"
  1165. +" else\n"
  1166. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);\n"
  1167. +" HAL_Delay(10);\n"
  1168. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);\n"
  1169. +" HAL_Delay(10);\n"
  1170. +" HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);\n"
  1171. +" HAL_Delay(10);\n"
  1172. +"}\n"
  1173. +" void SHT10_Calculate(void)\n"
  1174. +"{\n"
  1175. +" temp_C=d1+d2*Cvalue;\n"
  1176. +" RH_Lin = C1+C2*Hvalue+C3*Hvalue*Hvalue;\n"
  1177. +" RH_Ture= (temp_C-25)*(T1+T2*Hvalue)+RH_Lin;\n"
  1178. +" if (RH_Ture>100)\n"
  1179. +" RH_Ture= 100;\n"
  1180. +" if (RH_Ture<0.1)\n"
  1181. +" RH_Ture=0.1;\n"
  1182. +" if (temp_C<0)\n"
  1183. +" {\n"
  1184. +" cmd[1]=1;\n"
  1185. +" cmd[2]=-1*temp_C;\n"
  1186. +" temp=(-10*temp_C);\n"
  1187. +" cmd[3]=temp%10;\n"
  1188. +" cmd[4]=RH_Ture;\n"
  1189. +" RH=RH_Ture*10;\n"
  1190. +" cmd[5]=RH%10;\n"
  1191. +" }\n"
  1192. +" else\n"
  1193. +" {\n"
  1194. +" cmd[1]=0;\n"
  1195. +" cmd[2]=temp_C;\n"
  1196. +" temp=(10*temp_C);\n"
  1197. +" cmd[3]=temp%10;\n"
  1198. +" cmd[4]=RH_Ture;\n"
  1199. +" RH=RH_Ture*10;\n"
  1200. +" cmd[5]=RH%10;\n"
  1201. +" }\n"
  1202. +"}\n")
  1203. else:
  1204. pin_variables=""
  1205. pin_code=""
  1206. pin_add_code=""
  1207. sensor_code=""
  1208. def BMP280(pin_position,pin_type):
  1209. global pin_variables,pin_code,pin_add_code,init_code
  1210. global sensor_code
  1211. if pin_type =="BMP280":
  1212. print("BMP280 ok")
  1213. pin_variables=("uint8_t cmd[7]={0xFF,0x00,0x00,0x00,0x00,0x00,0x0D};\n"
  1214. +"HAL_StatusTypeDef Status;\n"
  1215. +"#define ADDR_AT24C04_WRITE_FIRST_16_PAGES 0xEC\n"
  1216. +"#define ADDR_AT24C04_WRITE_FIRST_16_PAGES 0xEC\n"
  1217. +"#define ADDR_AT24C04_READ 0xED\n"
  1218. +"#define AT24C04_TIMEOUT 0xED\n"
  1219. +"#define AT24C04_PAGE_SIZE 16\n"
  1220. +"#define BUFFER_SIZE 1\n"
  1221. +"uint16_t dig_T1;\n"
  1222. +"uint16_t dig_T2;\n"
  1223. +"uint16_t dig_T3;\n"
  1224. +"uint16_t dig_P1;\n"
  1225. +"uint16_t dig_P2;\n"
  1226. +"uint16_t dig_P3;\n"
  1227. +"uint16_t dig_P4;\n"
  1228. +"uint16_t dig_P5;\n"
  1229. +"uint16_t dig_P6;\n"
  1230. +"uint16_t dig_P7;\n"
  1231. +"uint16_t dig_P8;\n"
  1232. +"uint16_t dig_P9;\n"
  1233. +"uint32_t adc_P=0;\n"
  1234. +"uint32_t adc_T=0;\n"
  1235. +"uint8_t WriteBuffer[BUFFER_SIZE]={0xb6};\n"
  1236. +"uint8_t WriteBuffer1[BUFFER_SIZE]={0xff};\n"
  1237. +"uint8_t WriteBuffer2[BUFFER_SIZE]={0x00};\n"
  1238. +"uint8_t ReadBuffer[BUFFER_SIZE];\n"
  1239. +"int RH;\n")
  1240. sensor_code=("HAL_StatusTypeDef AT24C04_Write(I2C_HandleTypeDef *hi2c,uint16_t MemAddress,uint8_t *pData);\n"
  1241. +"HAL_StatusTypeDef AT24C04_Read(I2C_HandleTypeDef *hi2c,uint16_t MemAddress,uint8_t *pData);\n"
  1242. +"long bmp280_T_MultipleReadThree();\n"
  1243. +"long bmp280_P_MultipleReadThree();\n"
  1244. +"short bmp280_MultipleReadTwo(uint16_t addr);\n")
  1245. init_code=(" AT24C04_Write(&hi2c1,0xe0,WriteBuffer);\n"
  1246. +" AT24C04_Write(&hi2c1,0xf4,WriteBuffer1);\n"
  1247. +" AT24C04_Write(&hi2c1,0xf5,WriteBuffer2);\n"
  1248. +" dig_T1 = bmp280_MultipleReadTwo(0x88);\n"
  1249. +" dig_T2 = bmp280_MultipleReadTwo(0x8A);\n"
  1250. +" dig_T3 = bmp280_MultipleReadTwo(0x8C);\n"
  1251. +" dig_P1 = bmp280_MultipleReadTwo(0x8E);\n"
  1252. +" dig_P2 = bmp280_MultipleReadTwo(0x90);\n"
  1253. +" dig_P3 = bmp280_MultipleReadTwo(0x92);\n"
  1254. +" dig_P4 = bmp280_MultipleReadTwo(0x94);\n"
  1255. +" dig_P5 = bmp280_MultipleReadTwo(0x96);\n"
  1256. +" dig_P6 = bmp280_MultipleReadTwo(0x98);\n"
  1257. +" dig_P7 = bmp280_MultipleReadTwo(0x9A);\n"
  1258. +" dig_P8 = bmp280_MultipleReadTwo(0x9C);\n"
  1259. +" dig_P9 = bmp280_MultipleReadTwo(0x9E);\n")
  1260. pin_code=(" adc_T=bmp280_T_MultipleReadThree();\n"
  1261. +" adc_P=bmp280_P_MultipleReadThree();\n"
  1262. +" double var1, var2,temperature,t_fine;\n"
  1263. +" var1 = (((double) adc_T) / 16384.0 - ((double) dig_T1) / 1024.0)* ((double) dig_T2);\n"
  1264. +" var2 = ((((double) adc_T) / 131072.0 - ((double) dig_T1) / 8192.0)* (((double) adc_T) / 131072.0 - ((double) dig_T1) / 8192.0))* ((double) dig_T3);\n"
  1265. +" t_fine = (int32_t) (var1 + var2);\n"
  1266. +" temperature = (var1 + var2) / 5120.0;\n"
  1267. +" double pressure;\n"
  1268. +" var1 = (t_fine / 2.0) - 64000.0;\n"
  1269. +" var2 = var1 * var1 * ((double) dig_P6) / 32768.0;\n"
  1270. +" var2 = var2 + var1 * ((double) dig_P5) * 2.0;\n"
  1271. +" var2 = (var2 / 4.0) + (((double) dig_P4) * 65536.0);\n"
  1272. +" var1 = (((double) dig_P3) * var1 * var1 / 524288.0+ ((double) dig_P2) * var1) / 524288.0;\n"
  1273. +" var1 = (1.0 + var1 / 32768.0) * ((double) dig_P1);\n"
  1274. +" if (var1 == 0.0)\n"
  1275. +" {\n"
  1276. +" var1 = 0;\n"
  1277. +" }\n"
  1278. +" pressure = 1048576.0 - (double) adc_P;\n"
  1279. +" pressure = (pressure - (var2 / 4096.0)) * 6250.0 / var1;\n"
  1280. +" var1 = ((double) dig_P9) * pressure * pressure / 2147483648.0;"
  1281. +" var2 = pressure * ((double) dig_P8) / 32768.0;\n"
  1282. +" pressure = pressure + (var1 + var2 + ((double) dig_P7)) / 16.0;\n"
  1283. +" cmd[1]=pressure/10000; \n"
  1284. +" int pa =pressure;\n"
  1285. +" cmd[2]=(pa%10000)/100;\n"
  1286. +" cmd[3]=(pa%10000)%100;\n"
  1287. +" HAL_UART_Transmit(&huart1, (uint8_t *)cmd, sizeof(cmd),1);")
  1288. pin_add_code=("HAL_StatusTypeDef AT24C04_Write(I2C_HandleTypeDef *hi2c,uint16_t MemAddress,uint8_t *pData)\n"
  1289. +"{\n"
  1290. +" Status = HAL_I2C_Mem_Write(&hi2c1, ADDR_AT24C04_WRITE_FIRST_16_PAGES, MemAddress, I2C_MEMADD_SIZE_8BIT, pData, AT24C04_PAGE_SIZE, AT24C04_TIMEOUT);\n"
  1291. +" return Status;\n"
  1292. +"}\n"
  1293. +"HAL_StatusTypeDef AT24C04_Read(I2C_HandleTypeDef *hi2c,uint16_t MemAddress,uint8_t *pData)\n"
  1294. +"{\n"
  1295. +" Status = HAL_I2C_Mem_Read(&hi2c1, ADDR_AT24C04_READ, MemAddress, I2C_MEMADD_SIZE_8BIT, pData, BUFFER_SIZE, AT24C04_TIMEOUT);\n"
  1296. +" return Status;\n"
  1297. +"}\n"
  1298. +" long bmp280_T_MultipleReadThree()\n"
  1299. +"{\n"
  1300. +" uint8_t msb, lsb, xlsb;\n"
  1301. +" int32_t temp = 0;\n"
  1302. +" AT24C04_Read(&hi2c1,0xfa,ReadBuffer);\n"
  1303. +" msb=ReadBuffer[0];\n"
  1304. +" AT24C04_Read(&hi2c1,0xfb,ReadBuffer);\n"
  1305. +" lsb=ReadBuffer[0];\n"
  1306. +" AT24C04_Read(&hi2c1,0xfc,ReadBuffer);\n"
  1307. +" xlsb=ReadBuffer[0];\n"
  1308. +" temp = ((msb << 12)|(lsb << 4)|(xlsb >> 4));\n"
  1309. +" return temp;\n"
  1310. +"}\n"
  1311. +"long bmp280_P_MultipleReadThree()\n"
  1312. +"{\n"
  1313. +" uint8_t msb, lsb, xlsb;\n"
  1314. +" int32_t pressure = 0;\n"
  1315. +" AT24C04_Read(&hi2c1,0xf7,ReadBuffer);\n"
  1316. +" msb=ReadBuffer[0];\n"
  1317. +" AT24C04_Read(&hi2c1,0xf8,ReadBuffer);\n"
  1318. +" lsb=ReadBuffer[0];\n"
  1319. +" AT24C04_Read(&hi2c1,0xf9,ReadBuffer);\n"
  1320. +" xlsb=ReadBuffer[0];\n"
  1321. +" pressure = ((msb << 12)|(lsb << 4)|(xlsb >> 4));\n"
  1322. +" return pressure;\n"
  1323. +"}\n"
  1324. +" short bmp280_MultipleReadTwo(uint16_t addr )\n"
  1325. +"{\n"
  1326. +" uint8_t msb, lsb;\n"
  1327. +" uint16_t temp = 0;\n"
  1328. +" AT24C04_Read(&hi2c1,addr,ReadBuffer);\n"
  1329. +" lsb = ReadBuffer[0];\n"
  1330. +" AT24C04_Read(&hi2c1,addr+1,ReadBuffer);\n"
  1331. +" msb = ReadBuffer[0];\n"
  1332. +" temp = msb << 8|lsb;\n"
  1333. +" return temp;\n"
  1334. +"}\n")
  1335. else:
  1336. pin_variables=""
  1337. pin_code=""
  1338. pin_add_code=""
  1339. init_code=""
  1340. def motor(pin_position,pin_type):
  1341. global act_code,init_code
  1342. global sensor_code
  1343. if pin_type =="Motor":
  1344. print("motor ok")
  1345. sensor_code=("void user_pwm_setvalue(uint16_t value);\n"
  1346. +"void user_pwm_setvalue2(uint16_t value);\n")
  1347. act_code=("void user_pwm_setvalue(uint16_t value)\n"
  1348. +"{\n"
  1349. +" TIM_OC_InitTypeDef sConfigOC;\n"
  1350. +" sConfigOC.OCMode = TIM_OCMODE_PWM1;\n"
  1351. +" sConfigOC.Pulse = value;\n"
  1352. +" sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;\n"
  1353. +" sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;\n"
  1354. +" HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);\n"
  1355. +" HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);\n"
  1356. +"}\n")
  1357. init_code=(" HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);\n"
  1358. +" HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);\n")
  1359. ## The callback function for connecting.
  1360. # @param client [in] The client instance for this callback.
  1361. # @param userdata [in] The private user data as set in Client() or user_data_set().
  1362. # @param flags [in] Response flags sent by the broker.
  1363. # @param rc [in] The connection result.
  1364. def on_connect(client, userdata, flags, rc):
  1365. # subscribe MQTT topic on connection
  1366. client.subscribe(mqtt_sub_topic, qos=2)
  1367. server_log('reboot | factory_reset','','Boot completed')
  1368. ## The callback function for processing messages from the server.
  1369. # @param client [in] The client instance for this callback.
  1370. # @param userdata [in] The private user data as set in Client() or user_data_set().
  1371. # @param msg [in] An instance of MQTT message.
  1372. def on_message(client, userdata, msg):
  1373. msg.payload = msg.payload.decode('utf-8')
  1374. jsonmsg = json.loads(msg.payload)
  1375. print('Received:')
  1376. print(json.dumps(jsonmsg, sort_keys=True, indent=4, separators=(',', ':')))
  1377. # processing the command from the server
  1378. if (jsonmsg['command'] == 'reboot'):
  1379. system_reboot()
  1380. elif (jsonmsg['command'] == 'factory_reset'):
  1381. factory_reset()
  1382. elif (jsonmsg['command'] == 'system_info'):
  1383. system_info()
  1384. elif (jsonmsg['command'] == 'Dry_OTA'):
  1385. creat_hex(jsonmsg)
  1386. ## A thread used to subscribe to and wait for messages from the server.
  1387. def thread_job():
  1388. # create a MQTT client with a user name and password to subscribe to the messages
  1389. mqtt_thread_client = mqtt.Client()
  1390. mqtt_thread_client.on_connect = on_connect
  1391. mqtt_thread_client.on_message = on_message
  1392. mqtt_thread_client.username_pw_set(username='aisky-client', password='aiskyc')
  1393. mqtt_thread_client.connect(mqtt_server, mqtt_port, mqtt_alive)
  1394. mqtt_thread_client.loop_forever()
  1395. # create a MQTT client with a user name and password to publish messages
  1396. mqtt_client = mqtt.Client()
  1397. mqtt_client.username_pw_set(username='aisky-client', password='aiskyc')
  1398. mqtt_client.connect(mqtt_server, mqtt_port, mqtt_alive)
  1399. # create a thread to subscribe to and wait for messages from the server
  1400. mqtt_subscribe_thread = threading.Thread(target=thread_job)
  1401. mqtt_subscribe_thread.start()
  1402. mqtt_client.loop_forever()