![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
updated version with parameters loaded from sdcard
Dependencies: F7_Ethernet mbed MbedJSONValue LCD_DISCO_F746NG mbed-rtos BSP_DISCO_F746NG CANMsg NetworkAPI SDFileSystem_Warning_Fixed GroveStreamsmodified LcdDiscoF746NgTracer JSON
main.cpp@2:462b5c0d3472, 2020-09-01 (annotated)
- Committer:
- jeanpierreaulas
- Date:
- Tue Sep 01 06:37:15 2020 +0000
- Branch:
- jpa
- Revision:
- 2:462b5c0d3472
- Parent:
- 0:48863a0964f5
- Child:
- 3:2de8e4771674
Version Fonctionnelle 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
38domo | 0:48863a0964f5 | 1 | /* -------------------------------------------------------------------------- |
38domo | 0:48863a0964f5 | 2 | // test compile ok 31/07/2020 |
38domo | 0:48863a0964f5 | 3 | |
38domo | 0:48863a0964f5 | 4 | // 30/08/2020 : emission CAN OK en Wifi sur tasmota ou udp bridge |
38domo | 0:48863a0964f5 | 5 | // mais pb reception caracteres sur port serie ( idem port series pc ouu serial 7 |
38domo | 0:48863a0964f5 | 6 | |
38domo | 0:48863a0964f5 | 7 | // 02/08/2020 |
38domo | 0:48863a0964f5 | 8 | // add sd parameters: read ok |
38domo | 0:48863a0964f5 | 9 | // ip address |
38domo | 0:48863a0964f5 | 10 | // ethernet actif |
38domo | 0:48863a0964f5 | 11 | // wifi actif |
38domo | 0:48863a0964f5 | 12 | // can actif |
38domo | 0:48863a0964f5 | 13 | // serial actif |
38domo | 0:48863a0964f5 | 14 | // fonction |
38domo | 0:48863a0964f5 | 15 | // Touche1 status (0/1) |
38domo | 0:48863a0964f5 | 16 | // Touche1 backcolor |
38domo | 0:48863a0964f5 | 17 | // touche1 text color |
38domo | 0:48863a0964f5 | 18 | // Touche1 text |
38domo | 0:48863a0964f5 | 19 | // Touche1 text1 |
38domo | 0:48863a0964f5 | 20 | // Touche2 |
38domo | 0:48863a0964f5 | 21 | // Touche3 |
38domo | 0:48863a0964f5 | 22 | // Touche4 |
38domo | 0:48863a0964f5 | 23 | // Temp1 text |
38domo | 0:48863a0964f5 | 24 | // Temp2 text |
38domo | 0:48863a0964f5 | 25 | |
38domo | 0:48863a0964f5 | 26 | // works ok 31/07/2020 |
38domo | 0:48863a0964f5 | 27 | // updated package from jpa |
38domo | 0:48863a0964f5 | 28 | // added fonts in bsp_disco_f746ng/utilities/fonts |
38domo | 0:48863a0964f5 | 29 | // fonts.h updated to manage added fonts |
38domo | 0:48863a0964f5 | 30 | // grovestream.cpp & grovesttream.h updated |
38domo | 0:48863a0964f5 | 31 | // remove stm32746ng_dicovery_sd in /bsp_disco_f746ng/drivers/stm32746ng_discovery |
38domo | 0:48863a0964f5 | 32 | // mainSDAcardJPA issue: add sdcardjpa.cpp in lib /sdfilesystem_warning_fixed |
38domo | 0:48863a0964f5 | 33 | // isue with big fonts: replace /bsp_disco_f746ng/drivers/stm32746ng-discovery/stm32746ng-discovery_lcd.c &.h |
38domo | 0:48863a0964f5 | 34 | |
38domo | 0:48863a0964f5 | 35 | // need to add CAN and WiFi |
38domo | 0:48863a0964f5 | 36 | |
38domo | 0:48863a0964f5 | 37 | STM32F746 GroveStreams Stream Feed via Ethernet |
38domo | 0:48863a0964f5 | 38 | |
38domo | 0:48863a0964f5 | 39 | This GroveStreams example is designed for the STM32F746. |
38domo | 0:48863a0964f5 | 40 | https://www.grovestreams.com/developers/getting_started_stm32F746.html |
38domo | 0:48863a0964f5 | 41 | |
38domo | 0:48863a0964f5 | 42 | The STM32 uses DHCP and DNS for a simpler network setup. |
38domo | 0:48863a0964f5 | 43 | |
38domo | 0:48863a0964f5 | 44 | */ |
38domo | 0:48863a0964f5 | 45 | |
38domo | 0:48863a0964f5 | 46 | #if !FEATURE_LWIP |
38domo | 0:48863a0964f5 | 47 | #error [NOT_SUPPORTED] LWIP not supported for this target |
38domo | 0:48863a0964f5 | 48 | #endif |
38domo | 0:48863a0964f5 | 49 | |
38domo | 0:48863a0964f5 | 50 | #include "mbed.h" |
38domo | 0:48863a0964f5 | 51 | #include "LcdDiscoF746NgTracer.h" |
38domo | 0:48863a0964f5 | 52 | #include "GroveStreams.h" |
38domo | 0:48863a0964f5 | 53 | #include "MbedJSONValue.h" |
38domo | 0:48863a0964f5 | 54 | #include "stm32746g_discovery_lcd.h" |
38domo | 0:48863a0964f5 | 55 | #include "stm32746g_discovery_ts.h" |
38domo | 0:48863a0964f5 | 56 | #include "CANMsg.h" |
38domo | 0:48863a0964f5 | 57 | |
38domo | 0:48863a0964f5 | 58 | #include "main.h" |
38domo | 0:48863a0964f5 | 59 | |
38domo | 0:48863a0964f5 | 60 | //#include "touchScreen.cpp" |
38domo | 0:48863a0964f5 | 61 | int touchInitJPA1(); |
38domo | 0:48863a0964f5 | 62 | int touchJPA1(); |
38domo | 0:48863a0964f5 | 63 | int touchInitGuiJPA1(); |
38domo | 0:48863a0964f5 | 64 | int sendTouchJPA(int xx); |
38domo | 0:48863a0964f5 | 65 | time_t lastToucheTime = 0; |
38domo | 0:48863a0964f5 | 66 | int touchButtonJPA1(int numButton); |
38domo | 0:48863a0964f5 | 67 | |
38domo | 0:48863a0964f5 | 68 | //#include "GuiJPA.cpp" |
38domo | 0:48863a0964f5 | 69 | int initDisplayTemp(); |
38domo | 0:48863a0964f5 | 70 | int initDatasGuiJPA(); |
38domo | 0:48863a0964f5 | 71 | int setTemp1(std::string temp1); |
38domo | 0:48863a0964f5 | 72 | int setTemp2(std::string temp2); |
38domo | 0:48863a0964f5 | 73 | int sendInitDatasJPA(int xx); |
38domo | 0:48863a0964f5 | 74 | |
38domo | 0:48863a0964f5 | 75 | //#SDCARD |
38domo | 0:48863a0964f5 | 76 | int mainSDCardJPA(); //in sdcardjpa.cpp in lib /sdfilesystem_warning_fixedd |
38domo | 0:48863a0964f5 | 77 | |
38domo | 0:48863a0964f5 | 78 | //GroveStreams Stream IDs. Stream IDs tell GroveStreams which component streams the values will be assigned to. |
38domo | 0:48863a0964f5 | 79 | //Don't change these unless you edit your GroveStreams component definition and change the stream IDs to match these. |
38domo | 0:48863a0964f5 | 80 | const char gsStreamId1[] = "voltage"; |
38domo | 0:48863a0964f5 | 81 | const char gsStreamId2[] = "temperature"; |
38domo | 0:48863a0964f5 | 82 | char gsStreamIPdatas[] = "192.168.1.49"; |
38domo | 0:48863a0964f5 | 83 | char ethernet_actif[] = "O"; |
38domo | 0:48863a0964f5 | 84 | char wifi_actif[] = "O"; |
38domo | 0:48863a0964f5 | 85 | char can_actif[] = "O"; |
38domo | 0:48863a0964f5 | 86 | char serial_actif[] = "O"; |
38domo | 0:48863a0964f5 | 87 | char read_sd[] = ""; |
38domo | 0:48863a0964f5 | 88 | uint8_t text2[30]; |
38domo | 0:48863a0964f5 | 89 | uint8_t counter = 0; |
38domo | 0:48863a0964f5 | 90 | float voltage; |
38domo | 0:48863a0964f5 | 91 | const int kMaxBufferSize = 100; |
38domo | 0:48863a0964f5 | 92 | char buffer[kMaxBufferSize]; |
38domo | 0:48863a0964f5 | 93 | int len = 0; |
38domo | 0:48863a0964f5 | 94 | int i = 0; |
38domo | 0:48863a0964f5 | 95 | int idx_ser =0; |
38domo | 0:48863a0964f5 | 96 | int recv_ser = 0; |
38domo | 0:48863a0964f5 | 97 | Timer timer; |
38domo | 0:48863a0964f5 | 98 | AnalogIn analogIn(A0); |
38domo | 0:48863a0964f5 | 99 | |
38domo | 0:48863a0964f5 | 100 | // CAN ------------------------------------------------------------ |
38domo | 0:48863a0964f5 | 101 | CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name |
38domo | 0:48863a0964f5 | 102 | CANMsg rxMsg; |
38domo | 0:48863a0964f5 | 103 | CANMsg txMsg; |
38domo | 0:48863a0964f5 | 104 | |
38domo | 0:48863a0964f5 | 105 | // Other Settings |
38domo | 0:48863a0964f5 | 106 | int updateFrequency = 20; // Update frequency in seconds. Change this to change your sample frequency. |
38domo | 0:48863a0964f5 | 107 | |
38domo | 0:48863a0964f5 | 108 | AnalogIn adc_temp(ADC_TEMP); |
38domo | 0:48863a0964f5 | 109 | AnalogIn adc_vref(ADC_VREF); |
38domo | 0:48863a0964f5 | 110 | DigitalOut myled(LED1); |
38domo | 0:48863a0964f5 | 111 | |
38domo | 0:48863a0964f5 | 112 | // serial port sur connecteur arduino (D0,D1)-------------------- |
38domo | 0:48863a0964f5 | 113 | Serial pc(USBTX, USBRX); // serial PC |
38domo | 0:48863a0964f5 | 114 | Serial serial2(PC_6, PC_7); // TX, RX for udp bridge |
38domo | 0:48863a0964f5 | 115 | Serial serial7(PF_7, PF_6); // TX, RX for extension |
38domo | 0:48863a0964f5 | 116 | |
38domo | 0:48863a0964f5 | 117 | #define BOARD1 1 // comment out this line when compiling for board #2 |
38domo | 0:48863a0964f5 | 118 | #if defined(BOARD1) |
38domo | 0:48863a0964f5 | 119 | const unsigned int RX_ID = 0x100; |
38domo | 0:48863a0964f5 | 120 | const unsigned int TX_ID = 0x101; |
38domo | 0:48863a0964f5 | 121 | #else |
38domo | 0:48863a0964f5 | 122 | const unsigned int RX_ID = 0x101; |
38domo | 0:48863a0964f5 | 123 | const unsigned int TX_ID = 0x100; |
38domo | 0:48863a0964f5 | 124 | #endif |
38domo | 0:48863a0964f5 | 125 | |
38domo | 0:48863a0964f5 | 126 | // --------------------------------------------------------------------------------------- |
38domo | 0:48863a0964f5 | 127 | //* @brief Prints CAN message to PC's serial terminal |
38domo | 0:48863a0964f5 | 128 | void printMsg(CANMessage& msg, int dir) |
38domo | 0:48863a0964f5 | 129 | { if (dir ==0) |
38domo | 0:48863a0964f5 | 130 | { //BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"Message sent ", LEFT_MODE); |
jeanpierreaulas | 2:462b5c0d3472 | 131 | printf(" Can message sent ok\n"); |
jeanpierreaulas | 2:462b5c0d3472 | 132 | printf("ID = 0x%.3X Data=", msg.id); |
jeanpierreaulas | 2:462b5c0d3472 | 133 | for(int i = 0; i < msg.len; i++) printf(" 0x%.2X", msg.data[i]); |
jeanpierreaulas | 2:462b5c0d3472 | 134 | printf("\r\n"); |
38domo | 0:48863a0964f5 | 135 | } |
38domo | 0:48863a0964f5 | 136 | else |
jeanpierreaulas | 2:462b5c0d3472 | 137 | { |
jeanpierreaulas | 2:462b5c0d3472 | 138 | printf(" Can message receive ok\n"); |
jeanpierreaulas | 2:462b5c0d3472 | 139 | printf(" ID = 0x%.3X\r\n", msg.id); |
jeanpierreaulas | 2:462b5c0d3472 | 140 | printf(" Type = %d\r\n", msg.type); |
jeanpierreaulas | 2:462b5c0d3472 | 141 | printf(" Format = %d\r\n", msg.format); |
jeanpierreaulas | 2:462b5c0d3472 | 142 | printf(" Length = %d\r\n", msg.len); |
jeanpierreaulas | 2:462b5c0d3472 | 143 | printf(" Data ="); |
jeanpierreaulas | 2:462b5c0d3472 | 144 | for(int i = 0; i < msg.len; i++) |
jeanpierreaulas | 2:462b5c0d3472 | 145 | printf(" 0x%.2X", msg.data[i]); |
jeanpierreaulas | 2:462b5c0d3472 | 146 | printf("\r\n"); |
38domo | 0:48863a0964f5 | 147 | } |
38domo | 0:48863a0964f5 | 148 | |
38domo | 0:48863a0964f5 | 149 | } |
38domo | 0:48863a0964f5 | 150 | |
38domo | 0:48863a0964f5 | 151 | /** ----------------------------------------------------------------------------------------- |
38domo | 0:48863a0964f5 | 152 | * @brief Handles received CAN messages |
38domo | 0:48863a0964f5 | 153 | * @note Called on 'CAN message received' interrupt. |
38domo | 0:48863a0964f5 | 154 | */ |
jeanpierreaulas | 2:462b5c0d3472 | 155 | int CanRcv = 0; |
38domo | 0:48863a0964f5 | 156 | void onCanReceived(void) |
jeanpierreaulas | 2:462b5c0d3472 | 157 | { |
jeanpierreaulas | 2:462b5c0d3472 | 158 | can.read(rxMsg); |
jeanpierreaulas | 2:462b5c0d3472 | 159 | CanRcv = 1; |
38domo | 0:48863a0964f5 | 160 | } |
38domo | 0:48863a0964f5 | 161 | // --------------------------------------------------- |
38domo | 0:48863a0964f5 | 162 | void pc_recv() |
jeanpierreaulas | 2:462b5c0d3472 | 163 | { if(serial7.readable()) |
38domo | 0:48863a0964f5 | 164 | { buffer[i]=(serial7.getc()); |
38domo | 0:48863a0964f5 | 165 | if (buffer[i] == '\n') { |
38domo | 0:48863a0964f5 | 166 | serial7.printf("%s",buffer); |
38domo | 0:48863a0964f5 | 167 | len = 0; |
38domo | 0:48863a0964f5 | 168 | idx_ser =0; |
38domo | 0:48863a0964f5 | 169 | recv_ser = 1; |
38domo | 0:48863a0964f5 | 170 | } |
38domo | 0:48863a0964f5 | 171 | idx_ser++; |
38domo | 0:48863a0964f5 | 172 | } |
38domo | 0:48863a0964f5 | 173 | } |
38domo | 0:48863a0964f5 | 174 | |
38domo | 0:48863a0964f5 | 175 | // ------------------------------------------------------------------------------------------ |
38domo | 0:48863a0964f5 | 176 | int main() |
38domo | 0:48863a0964f5 | 177 | { pc.baud(115200); |
38domo | 0:48863a0964f5 | 178 | //serial2.baud(115200); // required for tasmota |
38domo | 0:48863a0964f5 | 179 | serial2.baud(57600); // required for UDP bridge |
38domo | 0:48863a0964f5 | 180 | serial7.baud(115200); // required for tasmota |
38domo | 0:48863a0964f5 | 181 | //serial7.attach(&pc_recv, Serial::RxIrq); |
38domo | 0:48863a0964f5 | 182 | |
jeanpierreaulas | 2:462b5c0d3472 | 183 | // attach ISR to handle received messages |
38domo | 0:48863a0964f5 | 184 | #if defined(BOARD1) |
38domo | 0:48863a0964f5 | 185 | timer.start(); // start timer |
38domo | 0:48863a0964f5 | 186 | pc.printf("CAN_Hello board #1\r\n"); |
38domo | 0:48863a0964f5 | 187 | #else |
38domo | 0:48863a0964f5 | 188 | pc.printf("CAN_Hello board #2\r\n"); |
38domo | 0:48863a0964f5 | 189 | #endif |
38domo | 0:48863a0964f5 | 190 | buffer[0] = '\0'; |
38domo | 0:48863a0964f5 | 191 | printf("\nInit SDCard ...\n"); |
38domo | 0:48863a0964f5 | 192 | mainSDCardJPA() ; // init SD - read prog parameters from SD (from sdcardjpa.cpp in sdfilesystem_warning_fixed lib |
38domo | 0:48863a0964f5 | 193 | // reaf following datas |
38domo | 0:48863a0964f5 | 194 | // IP address, Ethernet actif, Wifi actif, Can actif, serial actif,fonction, buttons parameters |
38domo | 0:48863a0964f5 | 195 | // store local variables from sd |
38domo | 0:48863a0964f5 | 196 | //extern char gsStreamIPdatas[]; |
38domo | 0:48863a0964f5 | 197 | //extern char ethernet_actif[] ; |
38domo | 0:48863a0964f5 | 198 | //extern char wifi_actif[]; |
38domo | 0:48863a0964f5 | 199 | //extern char can_actif[]; |
38domo | 0:48863a0964f5 | 200 | //extern char serial_actif[]; |
38domo | 0:48863a0964f5 | 201 | printf("\nFin init SDCard ...\n"); |
38domo | 0:48863a0964f5 | 202 | BSP_LCD_Clear(LCD_COLOR_DARKBLUE); // clear LCD |
38domo | 0:48863a0964f5 | 203 | uint8_t text[300]; |
38domo | 0:48863a0964f5 | 204 | printf("Start Prog\n"); |
38domo | 0:48863a0964f5 | 205 | std::string temp1; |
38domo | 0:48863a0964f5 | 206 | std::string temp2; |
38domo | 0:48863a0964f5 | 207 | |
38domo | 0:48863a0964f5 | 208 | //lastSuccessfulUploadTime is used for upload frequency. |
38domo | 0:48863a0964f5 | 209 | time_t lastSuccessfulUploadTime = 0; |
38domo | 0:48863a0964f5 | 210 | printf("\n Init touchJPA1 ...\n"); |
38domo | 0:48863a0964f5 | 211 | int yy; |
38domo | 0:48863a0964f5 | 212 | printf("\n Init initDisplayTemp ...\n"); |
38domo | 0:48863a0964f5 | 213 | yy = initDisplayTemp(); // from guijpa.cpp - Init display |
38domo | 0:48863a0964f5 | 214 | printf("\ninit touchInitJPA1 ...\n"); |
38domo | 0:48863a0964f5 | 215 | yy = touchInitJPA1(); // from touchscreen.cpp - init touchscreen |
38domo | 0:48863a0964f5 | 216 | yy = touchInitGuiJPA1(); // from touchscreen.cpp |
38domo | 0:48863a0964f5 | 217 | printf("\ninit initDatasGuiJPA ...\n"); |
38domo | 0:48863a0964f5 | 218 | yy = initDatasGuiJPA(); // from guijpa.cpp - config buttons |
38domo | 0:48863a0964f5 | 219 | ////lcd.printf("Starting..."); |
38domo | 0:48863a0964f5 | 220 | printf("Starting...\n"); |
38domo | 0:48863a0964f5 | 221 | lastToucheTime = time(NULL); |
38domo | 0:48863a0964f5 | 222 | char data[5]; |
jeanpierreaulas | 2:462b5c0d3472 | 223 | |
jeanpierreaulas | 2:462b5c0d3472 | 224 | // config can -------------------------------------- |
jeanpierreaulas | 2:462b5c0d3472 | 225 | can.frequency(125000); // set CAN bit rate to 125 kbps |
jeanpierreaulas | 2:462b5c0d3472 | 226 | //can.filter(RX_ID, 0xFFF, CANStandard, 0); // set filter #0 to accept only standard messages with ID == RX_ID |
jeanpierreaulas | 2:462b5c0d3472 | 227 | can.attach(onCanReceived); |
jeanpierreaulas | 2:462b5c0d3472 | 228 | |
38domo | 0:48863a0964f5 | 229 | // main loop --------------------------------------------------- |
38domo | 0:48863a0964f5 | 230 | while (true) { |
jeanpierreaulas | 2:462b5c0d3472 | 231 | |
jeanpierreaulas | 2:462b5c0d3472 | 232 | if (CanRcv > 0) |
jeanpierreaulas | 2:462b5c0d3472 | 233 | { |
jeanpierreaulas | 2:462b5c0d3472 | 234 | CanRcv = 0; |
jeanpierreaulas | 2:462b5c0d3472 | 235 | printMsg(rxMsg,1); |
jeanpierreaulas | 2:462b5c0d3472 | 236 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"Can msg rec", CENTER_MODE); |
jeanpierreaulas | 2:462b5c0d3472 | 237 | if (rxMsg.id == RX_ID) { |
jeanpierreaulas | 2:462b5c0d3472 | 238 | rxMsg >> counter; |
jeanpierreaulas | 2:462b5c0d3472 | 239 | rxMsg >> voltage; |
jeanpierreaulas | 2:462b5c0d3472 | 240 | pc.printf(" counter = %d\r\n", counter); |
jeanpierreaulas | 2:462b5c0d3472 | 241 | pc.printf(" voltage = %e V\r\n", voltage); |
jeanpierreaulas | 2:462b5c0d3472 | 242 | } |
jeanpierreaulas | 2:462b5c0d3472 | 243 | } |
38domo | 0:48863a0964f5 | 244 | // lecture sur port serie from tasmota |
38domo | 0:48863a0964f5 | 245 | if(serial7.readable()) |
38domo | 0:48863a0964f5 | 246 | { buffer[i]=(serial7.getc()); |
38domo | 0:48863a0964f5 | 247 | serial7.printf("%c",buffer[i]); |
38domo | 0:48863a0964f5 | 248 | if (buffer[i] == '\n') { |
38domo | 0:48863a0964f5 | 249 | serial7.printf("%s",buffer); |
38domo | 0:48863a0964f5 | 250 | len = 0; |
38domo | 0:48863a0964f5 | 251 | idx_ser =0; |
38domo | 0:48863a0964f5 | 252 | recv_ser = 1; |
38domo | 0:48863a0964f5 | 253 | } |
38domo | 0:48863a0964f5 | 254 | idx_ser++; |
38domo | 0:48863a0964f5 | 255 | } |
38domo | 0:48863a0964f5 | 256 | if ( recv_ser == 1) |
38domo | 0:48863a0964f5 | 257 | {serial7.printf("sent: %s",buffer); |
38domo | 0:48863a0964f5 | 258 | pc.printf("sent: %s",buffer); |
38domo | 0:48863a0964f5 | 259 | recv_ser =0; |
38domo | 0:48863a0964f5 | 260 | } |
38domo | 0:48863a0964f5 | 261 | |
38domo | 0:48863a0964f5 | 262 | // timer send can |
jeanpierreaulas | 2:462b5c0d3472 | 263 | if(timer.read_ms() >= 3000) // check for timeout |
38domo | 0:48863a0964f5 | 264 | { timer.stop(); // stop the timer |
38domo | 0:48863a0964f5 | 265 | timer.reset(); // reset the timer |
jeanpierreaulas | 2:462b5c0d3472 | 266 | counter = (counter+1) & 1; // increment the counter |
jeanpierreaulas | 2:462b5c0d3472 | 267 | voltage = 0;////(analogIn * 3.3f)/4096.0f;// read the small drifting voltage from analog input |
38domo | 0:48863a0964f5 | 268 | txMsg.clear(); // clear the Tx message storage |
38domo | 0:48863a0964f5 | 269 | //txMsg.id = TX_ID; // set ID |
jeanpierreaulas | 2:462b5c0d3472 | 270 | //txMsg.type=0; |
jeanpierreaulas | 2:462b5c0d3472 | 271 | //txMsg.format=4; |
jeanpierreaulas | 2:462b5c0d3472 | 272 | txMsg.id = 0x100130CA;//TX_ID; // set the message ID |
38domo | 0:48863a0964f5 | 273 | txMsg.format = CANExtended ; //extended |
38domo | 0:48863a0964f5 | 274 | // We are about to transmit two data items to the CAN bus. |
38domo | 0:48863a0964f5 | 275 | // counter: uint_8 (unsigned eight bits int) value (one byte). |
38domo | 0:48863a0964f5 | 276 | // voltage: floating point value (four bytes). |
38domo | 0:48863a0964f5 | 277 | // So the total length of payload data is five bytes. |
38domo | 0:48863a0964f5 | 278 | // We'll use the "<<" (append) operator to add data to the CAN message. |
38domo | 0:48863a0964f5 | 279 | // The usage is same as of the similar C++ io-stream operators. |
38domo | 0:48863a0964f5 | 280 | // NOTE: The data length of CAN message is automatically updated when using "<<" operators. |
jeanpierreaulas | 2:462b5c0d3472 | 281 | txMsg.len=2; |
jeanpierreaulas | 2:462b5c0d3472 | 282 | txMsg.data[0]=8;txMsg.data[1]=counter; // append data (total data length must be <= 8 bytes!) |
38domo | 0:48863a0964f5 | 283 | |
38domo | 0:48863a0964f5 | 284 | if(can.write(txMsg)) // transmit message |
38domo | 0:48863a0964f5 | 285 | { //if(can->write(txMsg)) { // transmit the CAN message |
38domo | 0:48863a0964f5 | 286 | printMsg(txMsg,0); |
38domo | 0:48863a0964f5 | 287 | BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)"TX CAN OK", CENTER_MODE); |
38domo | 0:48863a0964f5 | 288 | BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"--------", CENTER_MODE); |
38domo | 0:48863a0964f5 | 289 | } |
38domo | 0:48863a0964f5 | 290 | else |
jeanpierreaulas | 2:462b5c0d3472 | 291 | { pc.printf("Transmission error\r\n"); |
38domo | 0:48863a0964f5 | 292 | BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"TX CAN Fail", CENTER_MODE); |
38domo | 0:48863a0964f5 | 293 | BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)"-----------", CENTER_MODE); |
38domo | 0:48863a0964f5 | 294 | } |
jeanpierreaulas | 2:462b5c0d3472 | 295 | timer.start(); // insert transmission lag |
38domo | 0:48863a0964f5 | 296 | } // end if timer |
38domo | 0:48863a0964f5 | 297 | //touch screen |
38domo | 0:48863a0964f5 | 298 | int xx = touchJPA1(); // |
38domo | 0:48863a0964f5 | 299 | if (xx > 0) |
38domo | 0:48863a0964f5 | 300 | { pc.printf("Touche appuyee %d \r\n",xx); |
38domo | 0:48863a0964f5 | 301 | // send data to serial bridge tasmota |
38domo | 0:48863a0964f5 | 302 | serial2.printf(" Touche %d appuyee to Tasmota", xx); |
38domo | 0:48863a0964f5 | 303 | serial7.printf(" Touche %d appuyee to Tasmota", xx); |
38domo | 0:48863a0964f5 | 304 | if (xx ==4 ) |
38domo | 0:48863a0964f5 | 305 | { // send trame to can |
38domo | 0:48863a0964f5 | 306 | if(can.write(txMsg)) // transmit message |
38domo | 0:48863a0964f5 | 307 | { //if(can->write(txMsg)) { // transmit the CAN message |
38domo | 0:48863a0964f5 | 308 | //led = OFF; // turn the LED off |
38domo | 0:48863a0964f5 | 309 | pc.printf("-------------------------------------\r\n"); |
38domo | 0:48863a0964f5 | 310 | pc.printf("CAN message sent\r\n"); |
38domo | 0:48863a0964f5 | 311 | printMsg(txMsg,0); |
38domo | 0:48863a0964f5 | 312 | pc.printf(" counter = %d\r\n", counter); |
38domo | 0:48863a0964f5 | 313 | pc.printf(" voltage = %e V\r\n", voltage); |
38domo | 0:48863a0964f5 | 314 | BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)"TX CAN OK", CENTER_MODE); |
38domo | 0:48863a0964f5 | 315 | BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"--------", CENTER_MODE); |
38domo | 0:48863a0964f5 | 316 | serial7.printf(" send trame can ok"); |
38domo | 0:48863a0964f5 | 317 | } |
38domo | 0:48863a0964f5 | 318 | else |
38domo | 0:48863a0964f5 | 319 | { pc.printf("Transmission error\r\n"); |
38domo | 0:48863a0964f5 | 320 | BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"TX CAN Fail", CENTER_MODE); |
38domo | 0:48863a0964f5 | 321 | BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)"-----------", CENTER_MODE); |
38domo | 0:48863a0964f5 | 322 | } |
38domo | 0:48863a0964f5 | 323 | } |
38domo | 0:48863a0964f5 | 324 | pc.printf("data sent to tasmota serial bridge \n"); |
38domo | 0:48863a0964f5 | 325 | // data sent to ethernet |
38domo | 0:48863a0964f5 | 326 | xx = sendTouchJPA(xx); |
38domo | 0:48863a0964f5 | 327 | |
38domo | 0:48863a0964f5 | 328 | lastToucheTime = time(NULL); |
38domo | 0:48863a0964f5 | 329 | |
38domo | 0:48863a0964f5 | 330 | } |
38domo | 0:48863a0964f5 | 331 | else |
38domo | 0:48863a0964f5 | 332 | { lastToucheTime = 0; |
38domo | 0:48863a0964f5 | 333 | } // send mqtt |
38domo | 0:48863a0964f5 | 334 | // Update sensor data to GroveStreams |
38domo | 0:48863a0964f5 | 335 | seconds = time(NULL); |
38domo | 0:48863a0964f5 | 336 | if(seconds - lastSuccessfulUploadTime > updateFrequency) { |
38domo | 0:48863a0964f5 | 337 | ////lcd.clear(); |
38domo | 0:48863a0964f5 | 338 | ////lcd.printf("Getting Samples..."); |
38domo | 0:48863a0964f5 | 339 | printf("Getting Samples...\n"); |
38domo | 0:48863a0964f5 | 340 | //Assemble the samples into URL parameters which are seperated with the "&" character |
38domo | 0:48863a0964f5 | 341 | // Example: &s1=6.2&s2=78.231 |
jeanpierreaulas | 2:462b5c0d3472 | 342 | int temperature = 0; //adc_temp.read() * 100.0f; |
jeanpierreaulas | 2:462b5c0d3472 | 343 | int voltage = 0; //adc_vref.read() * 100.0f; |
38domo | 0:48863a0964f5 | 344 | char samples[64] = {0}; |
38domo | 0:48863a0964f5 | 345 | sprintf(samples, "&%s=%d&%s=%d", gsStreamId1, voltage, gsStreamId2, temperature); |
38domo | 0:48863a0964f5 | 346 | //Append on command requests (request stream values) |
38domo | 0:48863a0964f5 | 347 | //This will indicate to GroveStreams to return the last value |
38domo | 0:48863a0964f5 | 348 | // of each request stream during the sample upload |
38domo | 0:48863a0964f5 | 349 | strcat(samples, "&rsid=freq&rsid=led"); |
38domo | 0:48863a0964f5 | 350 | char resultBuffer[700]= {0}; |
38domo | 0:48863a0964f5 | 351 | //Sending Samples (and returning current command stream values) |
38domo | 0:48863a0964f5 | 352 | time_t connectAttemptTime = time(NULL); |
38domo | 0:48863a0964f5 | 353 | int sendResult = groveStreams.send(myMac, samples, gsCompName, gsCompTmplId, resultBuffer, sizeof resultBuffer); //receive mqtt |
38domo | 0:48863a0964f5 | 354 | |
38domo | 0:48863a0964f5 | 355 | if (sendResult == 0) { |
38domo | 0:48863a0964f5 | 356 | ////lcd.printf("Send Successful"); |
38domo | 0:48863a0964f5 | 357 | printf("Send Successful\n"); |
38domo | 0:48863a0964f5 | 358 | lastSuccessfulUploadTime = connectAttemptTime; |
38domo | 0:48863a0964f5 | 359 | //Handle command streams |
38domo | 0:48863a0964f5 | 360 | if (strlen(resultBuffer) > 0 && resultBuffer[0] == '{') { |
38domo | 0:48863a0964f5 | 361 | MbedJSONValue mbedJson; |
38domo | 0:48863a0964f5 | 362 | parse(mbedJson, resultBuffer); |
38domo | 0:48863a0964f5 | 363 | sprintf((char*)text, "HTTP string : %s\n", resultBuffer); |
38domo | 0:48863a0964f5 | 364 | //BSP_LCD_DisplayStringAt(0, LINE(1), (uint8_t *)&text, LEFT_MODE); |
38domo | 0:48863a0964f5 | 365 | if (mbedJson.hasMember("Temp1")) { |
38domo | 0:48863a0964f5 | 366 | int pp=setTemp1(mbedJson["Temp1"].get<std::string>()); |
38domo | 0:48863a0964f5 | 367 | } |
38domo | 0:48863a0964f5 | 368 | if (mbedJson.hasMember("Temp2")) { |
38domo | 0:48863a0964f5 | 369 | int tt=setTemp2(mbedJson["Temp2"].get<std::string>()); |
38domo | 0:48863a0964f5 | 370 | } |
38domo | 0:48863a0964f5 | 371 | if (mbedJson.hasMember("led")) { |
38domo | 0:48863a0964f5 | 372 | //Change LED |
38domo | 0:48863a0964f5 | 373 | myled = mbedJson["led"].get<bool>() ? 1 : 0; |
38domo | 0:48863a0964f5 | 374 | ////lcd.printf("LED: %s", mbedJson["led"].get<bool>() ? "On" : "Off"); |
38domo | 0:48863a0964f5 | 375 | printf("LED: %s\n", mbedJson["led"].get<bool>() ? "On" : "Off"); |
38domo | 0:48863a0964f5 | 376 | } |
38domo | 0:48863a0964f5 | 377 | } |
38domo | 0:48863a0964f5 | 378 | } |
38domo | 0:48863a0964f5 | 379 | } |
38domo | 0:48863a0964f5 | 380 | |
38domo | 0:48863a0964f5 | 381 | } |
38domo | 0:48863a0964f5 | 382 | } |
38domo | 0:48863a0964f5 | 383 | |
38domo | 0:48863a0964f5 | 384 |