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

Committer:
38domo
Date:
Wed Dec 30 15:09:07 2020 +0000
Branch:
jpa
Revision:
14:c5ca2a8e63a5
Parent:
13:fdec444a7b30
Child:
15:ddd006a66cca
update 30/12

Who changed what in which revision?

UserRevisionLine numberNew contents of line
38domo 0:48863a0964f5 1 /* --------------------------------------------------------------------------
38domo 14:c5ca2a8e63a5 2
38domo 14:c5ca2a8e63a5 3 // update 30/12
38domo 0:48863a0964f5 4 // test compile ok 31/07/2020
38domo 0:48863a0964f5 5
38domo 10:41aff2b7acc6 6 // 01/09/2020 : update JPA serial et CAN : tests OK sur CAN et 1 srial 2
38domo 10:41aff2b7acc6 7 : attention 1 seul serial operationnel serial 2 - serial 7 ne marche pas en reception
38domo 10:41aff2b7acc6 8 // : test ok send/receive tasmota sur serial 2
38domo 0:48863a0964f5 9 // 30/08/2020 : emission CAN OK en Wifi sur tasmota ou udp bridge
38domo 0:48863a0964f5 10 // mais pb reception caracteres sur port serie ( idem port series pc ouu serial 7
38domo 0:48863a0964f5 11
38domo 0:48863a0964f5 12 // 02/08/2020
38domo 0:48863a0964f5 13 // add sd parameters: read ok
38domo 12:15ae35cfb03f 14 192.168.1.30
38domo 12:15ae35cfb03f 15 0;Ethernet
38domo 12:15ae35cfb03f 16 1;WiFi
38domo 12:15ae35cfb03f 17 1;can
38domo 12:15ae35cfb03f 18 1;serial
38domo 12:15ae35cfb03f 19 F;Fonction
38domo 12:15ae35cfb03f 20 0;Touche1 status : off
38domo 12:15ae35cfb03f 21 0xFF00FF00;Touche1 BackColor
38domo 12:15ae35cfb03f 22 0xFF000000;Touche1 TextColor
38domo 12:15ae35cfb03f 23 Pompe ; Touche1 text
38domo 12:15ae35cfb03f 24 Piscine ; Touche1 text1
38domo 12:15ae35cfb03f 25 1;Touche2 Status : off
38domo 12:15ae35cfb03f 26 0xFFFF0000; Touche2 BackColor
38domo 12:15ae35cfb03f 27 0xFFFFFFFF; Touche2 TextColor
38domo 12:15ae35cfb03f 28 Eclair ;Touche2 text
38domo 12:15ae35cfb03f 29 Piscine ;Touche2 text1
38domo 12:15ae35cfb03f 30 0; Touche3 Status : off
38domo 12:15ae35cfb03f 31 0xFF00FF00; Touche3 BackColor
38domo 12:15ae35cfb03f 32 0xFFFFFF00; Touche3 TextColor
38domo 12:15ae35cfb03f 33 Leds Bl ;Touche3 text
38domo 12:15ae35cfb03f 34 Piscine ; Touche3 text1
38domo 12:15ae35cfb03f 35 1; Touche4 Status : off
38domo 12:15ae35cfb03f 36 0xFF800000; Touche4 BackColor
38domo 12:15ae35cfb03f 37 0xFFFFFF00; Touche4 TextColor
38domo 12:15ae35cfb03f 38 Porche ; Touche4 text
38domo 12:15ae35cfb03f 39 entree ; Touche4 text1
38domo 12:15ae35cfb03f 40 Temp Piscine ; Temp1 text
38domo 12:15ae35cfb03f 41 0xFF404040; backColorTemp1
38domo 12:15ae35cfb03f 42 0xFFFFFF00; txtColorTemp1
38domo 12:15ae35cfb03f 43 Temp Exterieur ; Temp2 text
38domo 12:15ae35cfb03f 44 0xFF404040; backColorTemp2
38domo 12:15ae35cfb03f 45 0xFFFFFF00; txtColorTemp2
38domo 0:48863a0964f5 46
38domo 0:48863a0964f5 47 // works ok 31/07/2020
38domo 0:48863a0964f5 48 // updated package from jpa
38domo 0:48863a0964f5 49 // added fonts in bsp_disco_f746ng/utilities/fonts
38domo 0:48863a0964f5 50 // fonts.h updated to manage added fonts
38domo 0:48863a0964f5 51 // grovestream.cpp & grovesttream.h updated
38domo 0:48863a0964f5 52 // remove stm32746ng_dicovery_sd in /bsp_disco_f746ng/drivers/stm32746ng_discovery
38domo 0:48863a0964f5 53 // mainSDAcardJPA issue: add sdcardjpa.cpp in lib /sdfilesystem_warning_fixed
38domo 0:48863a0964f5 54 // isue with big fonts: replace /bsp_disco_f746ng/drivers/stm32746ng-discovery/stm32746ng-discovery_lcd.c &.h
38domo 0:48863a0964f5 55
38domo 0:48863a0964f5 56 // need to add CAN and WiFi
38domo 0:48863a0964f5 57
38domo 0:48863a0964f5 58 STM32F746 GroveStreams Stream Feed via Ethernet
38domo 0:48863a0964f5 59
38domo 0:48863a0964f5 60 This GroveStreams example is designed for the STM32F746.
38domo 0:48863a0964f5 61 https://www.grovestreams.com/developers/getting_started_stm32F746.html
38domo 0:48863a0964f5 62
38domo 0:48863a0964f5 63 The STM32 uses DHCP and DNS for a simpler network setup.
38domo 0:48863a0964f5 64
38domo 0:48863a0964f5 65 */
38domo 0:48863a0964f5 66
38domo 0:48863a0964f5 67 #if !FEATURE_LWIP
38domo 0:48863a0964f5 68 #error [NOT_SUPPORTED] LWIP not supported for this target
38domo 0:48863a0964f5 69 #endif
38domo 0:48863a0964f5 70
38domo 0:48863a0964f5 71 #include "mbed.h"
38domo 0:48863a0964f5 72 #include "LcdDiscoF746NgTracer.h"
38domo 0:48863a0964f5 73 #include "GroveStreams.h"
38domo 0:48863a0964f5 74 #include "MbedJSONValue.h"
38domo 0:48863a0964f5 75 #include "stm32746g_discovery_lcd.h"
38domo 0:48863a0964f5 76 #include "stm32746g_discovery_ts.h"
38domo 0:48863a0964f5 77 #include "CANMsg.h"
jeanpierreaulas 3:2de8e4771674 78 //#include "ManageBufferJPA.h"
38domo 0:48863a0964f5 79
38domo 0:48863a0964f5 80 #include "main.h"
38domo 0:48863a0964f5 81
38domo 10:41aff2b7acc6 82 extern char textButton[5][8];
38domo 10:41aff2b7acc6 83 extern char text1Button[5][8];
38domo 10:41aff2b7acc6 84 extern char textTempTitle[3][30];
38domo 12:15ae35cfb03f 85 extern uint32_t backColorButton[5], txtColorButton[5];
38domo 12:15ae35cfb03f 86 extern char statusButton[5][30];
jeanpierreaulas 3:2de8e4771674 87
38domo 0:48863a0964f5 88 //#include "touchScreen.cpp"
38domo 13:fdec444a7b30 89 int touchInitJPA1(); // from touchScreenJPA.cpp
38domo 13:fdec444a7b30 90 int touchJPA1(); // from touchScreenJPA.cpp
38domo 13:fdec444a7b30 91 int touchInitGuiJPA1(); // from touchScreenJPA.cpp
38domo 13:fdec444a7b30 92 int sendTouchJPA(int xx); // from touchScreenJPA.cpp
38domo 0:48863a0964f5 93 time_t lastToucheTime = 0;
38domo 13:fdec444a7b30 94 int touchButtonJPA1(int numButton); // from touchScreenJPa.cpp
38domo 0:48863a0964f5 95
38domo 0:48863a0964f5 96 //#include "GuiJPA.cpp"
38domo 13:fdec444a7b30 97 int initDisplayTemp();
38domo 0:48863a0964f5 98 int initDatasGuiJPA();
38domo 0:48863a0964f5 99 int setTemp1(std::string temp1);
38domo 0:48863a0964f5 100 int setTemp2(std::string temp2);
38domo 0:48863a0964f5 101 int sendInitDatasJPA(int xx);
38domo 0:48863a0964f5 102
38domo 0:48863a0964f5 103 //#SDCARD
38domo 0:48863a0964f5 104 int mainSDCardJPA(); //in sdcardjpa.cpp in lib /sdfilesystem_warning_fixedd
38domo 0:48863a0964f5 105
38domo 0:48863a0964f5 106 //GroveStreams Stream IDs. Stream IDs tell GroveStreams which component streams the values will be assigned to.
38domo 0:48863a0964f5 107 //Don't change these unless you edit your GroveStreams component definition and change the stream IDs to match these.
jeanpierreaulas 3:2de8e4771674 108 const char gsStreamId1[] = "voltage";
jeanpierreaulas 3:2de8e4771674 109 const char gsStreamId2[] = "temperature";
jeanpierreaulas 3:2de8e4771674 110 char gsStreamIPdatas[] = "192.168.1.49";
38domo 10:41aff2b7acc6 111 int ethernet_actif = '0';
38domo 10:41aff2b7acc6 112 int wifi_actif = '1';
38domo 10:41aff2b7acc6 113 int can_actif = '1';
38domo 10:41aff2b7acc6 114 int serial_actif = '1';
jeanpierreaulas 3:2de8e4771674 115 char read_sd[] = "";
38domo 10:41aff2b7acc6 116 char function_mod[]= "O";
38domo 10:41aff2b7acc6 117
38domo 10:41aff2b7acc6 118 char touch1_status[]= "0";
38domo 10:41aff2b7acc6 119 char touch1_back_color[]= "0xFF404040";
38domo 10:41aff2b7acc6 120 char touch1_text_color[]= "0xFFFFFF00";
38domo 10:41aff2b7acc6 121 char touch1_text[]= "Touch1";
38domo 10:41aff2b7acc6 122 char touch1_text1[]= "Text1";
38domo 10:41aff2b7acc6 123
38domo 10:41aff2b7acc6 124 char touch2_status[]= "0";
38domo 10:41aff2b7acc6 125 char touch2_back_color[]= "0xFF404040";
38domo 10:41aff2b7acc6 126 char touch2_text_color[]= "0xFFFFFF00";
38domo 10:41aff2b7acc6 127 char touch2_text[]= "Touch2";
38domo 10:41aff2b7acc6 128 char touch2_text1[]= "Text2";
38domo 10:41aff2b7acc6 129
38domo 10:41aff2b7acc6 130 char touch3_status[]= "0";
38domo 10:41aff2b7acc6 131 char touch3_back_color[]= "0xFF404040";
38domo 10:41aff2b7acc6 132 char touch3_text_color[]= "0xFFFFFF00";
38domo 10:41aff2b7acc6 133 char touch3_text[]= "Touch3";
38domo 10:41aff2b7acc6 134 char touch3_text1[]= "Text3";
38domo 10:41aff2b7acc6 135
38domo 10:41aff2b7acc6 136 char touch4_status[]= "0";
38domo 10:41aff2b7acc6 137 char touch4_back_color[]= "0xFF404040";
38domo 10:41aff2b7acc6 138 char touch4_text_color[]= "0xFFFFFF00";
38domo 10:41aff2b7acc6 139 char touch4_text[]= "Touch4";
38domo 10:41aff2b7acc6 140 char touch4_text1[]= "Text4";
38domo 10:41aff2b7acc6 141
38domo 10:41aff2b7acc6 142 char temp1_text[]= "Temp1";
38domo 10:41aff2b7acc6 143 char temp1_back_color[]= "0xFF404040";
38domo 10:41aff2b7acc6 144 char temp1_text_color[]= "0xFFFFFF00";
38domo 10:41aff2b7acc6 145
38domo 10:41aff2b7acc6 146 char temp2_text[]= "Temp2";
38domo 10:41aff2b7acc6 147 char temp2_back_color[]= "0xFF404040";
38domo 10:41aff2b7acc6 148 char temp2_text_color[]= "0xFFFFFF00";
38domo 10:41aff2b7acc6 149
38domo 10:41aff2b7acc6 150
38domo 10:41aff2b7acc6 151
38domo 10:41aff2b7acc6 152 int len_line_sd = 0;
38domo 10:41aff2b7acc6 153
jeanpierreaulas 3:2de8e4771674 154 uint8_t text2[30];
jeanpierreaulas 3:2de8e4771674 155 uint8_t counter = 0;
jeanpierreaulas 3:2de8e4771674 156 float voltage;
jeanpierreaulas 3:2de8e4771674 157
jeanpierreaulas 3:2de8e4771674 158 Timer timer;
38domo 0:48863a0964f5 159 AnalogIn analogIn(A0);
38domo 0:48863a0964f5 160
38domo 0:48863a0964f5 161 // CAN ------------------------------------------------------------
jeanpierreaulas 3:2de8e4771674 162 CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name
jeanpierreaulas 3:2de8e4771674 163 CANMsg rxMsg;
jeanpierreaulas 3:2de8e4771674 164 CANMsg txMsg;
38domo 0:48863a0964f5 165
38domo 0:48863a0964f5 166 // Other Settings
jeanpierreaulas 3:2de8e4771674 167 int updateFrequency = 20; // Update frequency in seconds. Change this to change your sample frequency.
jeanpierreaulas 3:2de8e4771674 168
jeanpierreaulas 3:2de8e4771674 169 AnalogIn adc_temp(ADC_TEMP);
jeanpierreaulas 3:2de8e4771674 170 AnalogIn adc_vref(ADC_VREF);
jeanpierreaulas 3:2de8e4771674 171 DigitalOut myled(LED1);
38domo 0:48863a0964f5 172
jeanpierreaulas 3:2de8e4771674 173 void initSerial();
jeanpierreaulas 3:2de8e4771674 174 void sendSerialUSB(char * str);
jeanpierreaulas 3:2de8e4771674 175 void receiveSerialUSB();
jeanpierreaulas 4:fbbf5d1785e7 176 void sendSerial2(char * str);
jeanpierreaulas 4:fbbf5d1785e7 177 void receiveSerial2();
jeanpierreaulas 4:fbbf5d1785e7 178 void sendSerial7(char * str);
jeanpierreaulas 4:fbbf5d1785e7 179 void receiveSerial7();
38domo 0:48863a0964f5 180
jeanpierreaulas 4:fbbf5d1785e7 181 //extern volatile char bufferSerialUSB[];
jeanpierreaulas 3:2de8e4771674 182 extern volatile int msgSerialUSB;
jeanpierreaulas 8:275ae4382788 183 extern char stringSerialUSB[]; // trame from serialUSB
jeanpierreaulas 8:275ae4382788 184 extern int stringSerialUSBok; // si ==1 alors trame dispo (fin de trame == LF ou 10
jeanpierreaulas 8:275ae4382788 185
jeanpierreaulas 4:fbbf5d1785e7 186 extern volatile int msgSerial2;
jeanpierreaulas 8:275ae4382788 187 extern char stringSerial2[]; // trame from serialUSB
jeanpierreaulas 8:275ae4382788 188 extern int stringSerial2ok; // si ==1 alors trame dispo (fin de trame == LF ou 10
jeanpierreaulas 8:275ae4382788 189
jeanpierreaulas 4:fbbf5d1785e7 190 extern volatile int msgSerial7;
jeanpierreaulas 8:275ae4382788 191 extern char stringSerial7[]; // trame from serialUSB
jeanpierreaulas 8:275ae4382788 192 extern int stringSerial7ok; // si ==1 alors trame dispo (fin de trame == LF ou 10
jeanpierreaulas 3:2de8e4771674 193
jeanpierreaulas 3:2de8e4771674 194 //extern Serial serialUSB(USBTX, USBRX); // serial PC
jeanpierreaulas 3:2de8e4771674 195 //extern Serial serial2(PC_6, PC_7); // TX, RX for udp bridge
jeanpierreaulas 3:2de8e4771674 196 //extern Serial serial7(PF_7, PF_6); // TX, RX for extension
38domo 0:48863a0964f5 197
38domo 0:48863a0964f5 198 #define BOARD1 1 // comment out this line when compiling for board #2
38domo 0:48863a0964f5 199 const unsigned int RX_ID = 0x101;
38domo 0:48863a0964f5 200 const unsigned int TX_ID = 0x100;
jeanpierreaulas 3:2de8e4771674 201
38domo 0:48863a0964f5 202
38domo 0:48863a0964f5 203 // ---------------------------------------------------------------------------------------
38domo 0:48863a0964f5 204 //* @brief Prints CAN message to PC's serial terminal
38domo 0:48863a0964f5 205 void printMsg(CANMessage& msg, int dir)
38domo 0:48863a0964f5 206 { if (dir ==0)
38domo 0:48863a0964f5 207 { //BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"Message sent ", LEFT_MODE);
jeanpierreaulas 7:6ec7a4b94d0b 208 printf(" Can message sent : ");
jeanpierreaulas 2:462b5c0d3472 209 printf("ID = 0x%.3X Data=", msg.id);
jeanpierreaulas 2:462b5c0d3472 210 for(int i = 0; i < msg.len; i++) printf(" 0x%.2X", msg.data[i]);
jeanpierreaulas 7:6ec7a4b94d0b 211 printf("\n");
38domo 0:48863a0964f5 212 }
38domo 0:48863a0964f5 213 else
38domo 10:41aff2b7acc6 214 { printf(" Can message receive : ");
jeanpierreaulas 4:fbbf5d1785e7 215 /*
jeanpierreaulas 2:462b5c0d3472 216 printf(" ID = 0x%.3X\r\n", msg.id);
jeanpierreaulas 2:462b5c0d3472 217 printf(" Type = %d\r\n", msg.type);
jeanpierreaulas 2:462b5c0d3472 218 printf(" Format = %d\r\n", msg.format);
jeanpierreaulas 2:462b5c0d3472 219 printf(" Length = %d\r\n", msg.len);
jeanpierreaulas 7:6ec7a4b94d0b 220 */
jeanpierreaulas 4:fbbf5d1785e7 221 printf("ID = 0x%.3X Data=", msg.id);
jeanpierreaulas 4:fbbf5d1785e7 222 for(int i = 0; i < msg.len; i++) printf(" 0x%.2X", msg.data[i]);
jeanpierreaulas 7:6ec7a4b94d0b 223 printf("\n");
38domo 0:48863a0964f5 224 }
38domo 0:48863a0964f5 225 }
38domo 0:48863a0964f5 226
38domo 0:48863a0964f5 227 /** -----------------------------------------------------------------------------------------
38domo 0:48863a0964f5 228 * @brief Handles received CAN messages
38domo 0:48863a0964f5 229 * @note Called on 'CAN message received' interrupt.
38domo 0:48863a0964f5 230 */
jeanpierreaulas 2:462b5c0d3472 231 int CanRcv = 0;
38domo 0:48863a0964f5 232 void onCanReceived(void)
38domo 10:41aff2b7acc6 233 { can.read(rxMsg);
jeanpierreaulas 2:462b5c0d3472 234 CanRcv = 1;
38domo 0:48863a0964f5 235 }
jeanpierreaulas 3:2de8e4771674 236
38domo 0:48863a0964f5 237 // ------------------------------------------------------------------------------------------
38domo 0:48863a0964f5 238 int main()
38domo 10:41aff2b7acc6 239 { initSerial();
jeanpierreaulas 2:462b5c0d3472 240 // attach ISR to handle received messages
38domo 0:48863a0964f5 241 timer.start(); // start timer
jeanpierreaulas 3:2de8e4771674 242 sendSerialUSB("CAN_Hello board serialUSB #1\r\n");
38domo 10:41aff2b7acc6 243
jeanpierreaulas 5:ceefd0cfa233 244 sendSerial2("CAN_Hello board serial2 #1\r\n");
jeanpierreaulas 5:ceefd0cfa233 245 sendSerial7("CAN_Hello board serial7 #1\r\n");
jeanpierreaulas 3:2de8e4771674 246
38domo 0:48863a0964f5 247 printf("\nInit SDCard ...\n");
38domo 0:48863a0964f5 248 mainSDCardJPA() ; // init SD - read prog parameters from SD (from sdcardjpa.cpp in sdfilesystem_warning_fixed lib
38domo 0:48863a0964f5 249 // reaf following datas
38domo 0:48863a0964f5 250 // IP address, Ethernet actif, Wifi actif, Can actif, serial actif,fonction, buttons parameters
38domo 0:48863a0964f5 251 // store local variables from sd
38domo 10:41aff2b7acc6 252 // gsStreamIPdatas[];
38domo 10:41aff2b7acc6 253 // ethernet_actif[] ; wifi_actif[];can_actif[]; serial_actif[]; function_mod[]= "O";
38domo 10:41aff2b7acc6 254 // touch1_status[]= "0"; touch1_back_color[]= "0xFF404040"; touch1_text_color[]= "0xFFFFFF00"; touch1_text[]= "Touch1"; touch1_text1[]= "Text1";
38domo 10:41aff2b7acc6 255 // touch2_status[]= "0"; touch2_back_color[]= "0xFF404040"; touch2_text_color[]= "0xFFFFFF00"; touch2_text[]= "Touch2"; touch2_text1[]= "Text2";
38domo 10:41aff2b7acc6 256 // touch3_status[]= "0"; touch3_back_color[]= "0xFF404040"; touch3_text_color[]= "0xFFFFFF00"; touch3_text[]= "Touch3"; touch3_text1[]= "Text3";
38domo 10:41aff2b7acc6 257 // touch4_status[]= "0"; touch4_back_color[]= "0xFF404040"; touch4_text_color[]= "0xFFFFFF00"; touch4_text[]= "Touch4"; touch4_text1[]= "Text4";
38domo 10:41aff2b7acc6 258 // temp1_text[]= "Temp1"; temp1_back_color[]= "0xFF404040"; temp1_text_color[]= "0xFFFFFF00";
38domo 10:41aff2b7acc6 259 // temp2_text[]= "Temp2"; temp2_back_color[]= "0xFF404040"; temp2_text_color[]= "0xFFFFFF00";
38domo 10:41aff2b7acc6 260
38domo 0:48863a0964f5 261 printf("\nFin init SDCard ...\n");
38domo 0:48863a0964f5 262 BSP_LCD_Clear(LCD_COLOR_DARKBLUE); // clear LCD
38domo 0:48863a0964f5 263 uint8_t text[300];
38domo 0:48863a0964f5 264 printf("Start Prog\n");
38domo 0:48863a0964f5 265 std::string temp1;
38domo 0:48863a0964f5 266 std::string temp2;
38domo 0:48863a0964f5 267
38domo 0:48863a0964f5 268 //lastSuccessfulUploadTime is used for upload frequency.
38domo 0:48863a0964f5 269 time_t lastSuccessfulUploadTime = 0;
38domo 0:48863a0964f5 270 printf("\n Init touchJPA1 ...\n");
38domo 0:48863a0964f5 271 int yy;
38domo 0:48863a0964f5 272 printf("\n Init initDisplayTemp ...\n");
38domo 0:48863a0964f5 273 yy = initDisplayTemp(); // from guijpa.cpp - Init display
38domo 0:48863a0964f5 274 printf("\ninit touchInitJPA1 ...\n");
38domo 0:48863a0964f5 275 yy = touchInitJPA1(); // from touchscreen.cpp - init touchscreen
38domo 10:41aff2b7acc6 276 yy = touchInitGuiJPA1(); // from touchscreen.cpp - display buttons
38domo 0:48863a0964f5 277 printf("\ninit initDatasGuiJPA ...\n");
38domo 10:41aff2b7acc6 278 yy = initDatasGuiJPA(); // from guijpa.cpp - config buttons - ethernet connection
38domo 0:48863a0964f5 279 ////lcd.printf("Starting...");
38domo 0:48863a0964f5 280 printf("Starting...\n");
38domo 0:48863a0964f5 281 lastToucheTime = time(NULL);
38domo 0:48863a0964f5 282 char data[5];
jeanpierreaulas 2:462b5c0d3472 283
jeanpierreaulas 2:462b5c0d3472 284 // config can --------------------------------------
jeanpierreaulas 2:462b5c0d3472 285 can.frequency(125000); // set CAN bit rate to 125 kbps
jeanpierreaulas 2:462b5c0d3472 286 //can.filter(RX_ID, 0xFFF, CANStandard, 0); // set filter #0 to accept only standard messages with ID == RX_ID
jeanpierreaulas 3:2de8e4771674 287 can.attach(onCanReceived, CAN::RxIrq);
jeanpierreaulas 2:462b5c0d3472 288
jeanpierreaulas 5:ceefd0cfa233 289 BSP_LCD_DisplayOff();
jeanpierreaulas 5:ceefd0cfa233 290 BSP_LCD_DisplayOn();
38domo 10:41aff2b7acc6 291 //BSP_LCD_SetBackColor(LCD_COLOR_RED);
38domo 10:41aff2b7acc6 292 BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
38domo 10:41aff2b7acc6 293 BSP_LCD_SetBackColor(LCD_COLOR_RED);
38domo 12:15ae35cfb03f 294 BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
38domo 12:15ae35cfb03f 295
jeanpierreaulas 3:2de8e4771674 296 // main loop ---------------------------------------------------
38domo 10:41aff2b7acc6 297 while (true) {
jeanpierreaulas 2:462b5c0d3472 298 if (CanRcv > 0)
38domo 10:41aff2b7acc6 299 { CanRcv = 0;
jeanpierreaulas 2:462b5c0d3472 300 printMsg(rxMsg,1);
jeanpierreaulas 2:462b5c0d3472 301 BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"Can msg rec", CENTER_MODE);
38domo 10:41aff2b7acc6 302 if (rxMsg.id == RX_ID)
38domo 10:41aff2b7acc6 303 { rxMsg >> counter;
jeanpierreaulas 2:462b5c0d3472 304 rxMsg >> voltage;
jeanpierreaulas 3:2de8e4771674 305 //serialUSB.printf(" counter = %d\r\n", counter);
jeanpierreaulas 3:2de8e4771674 306 //serialUSB.printf(" voltage = %e V\r\n", voltage);
jeanpierreaulas 2:462b5c0d3472 307 }
38domo 10:41aff2b7acc6 308 }
jeanpierreaulas 3:2de8e4771674 309 if (msgSerialUSB > 0) {receiveSerialUSB();}
jeanpierreaulas 9:332bd237b98f 310 if (stringSerialUSBok == 1) {printf("Trame SerialUSB : %s",stringSerialUSB);stringSerialUSBok=0;}
jeanpierreaulas 8:275ae4382788 311
jeanpierreaulas 4:fbbf5d1785e7 312 if (msgSerial2 > 0) {receiveSerial2();}
38domo 10:41aff2b7acc6 313 if (stringSerial2ok == 1)
38domo 10:41aff2b7acc6 314 { printf("Trame Serial2 : %s",stringSerial2);
38domo 10:41aff2b7acc6 315 stringSerial2ok=0;
38domo 10:41aff2b7acc6 316 // decodage json datas
38domo 10:41aff2b7acc6 317 // trame reçue : {"Temp1":"25","Temp2":"30"}
38domo 10:41aff2b7acc6 318 char resultBuffer[100]= {0};
38domo 10:41aff2b7acc6 319 //resultBuffer = stringSerial2;
38domo 10:41aff2b7acc6 320 //if (strlen(resultBuffer) > 0 && resultBuffer[0] == '{')
38domo 10:41aff2b7acc6 321 if (strlen(stringSerial2) > 0 && stringSerial2[0] == '{')
38domo 12:15ae35cfb03f 322 { MbedJSONValue mbedJson; //parse(mbedJson, resultBuffer);
38domo 10:41aff2b7acc6 323 parse(mbedJson, stringSerial2);
38domo 12:15ae35cfb03f 324 sprintf((char*)text, "HTTP string : %s\n", stringSerial2);
38domo 12:15ae35cfb03f 325 if (mbedJson.hasMember("Temp1"))
38domo 12:15ae35cfb03f 326 { printf("Serial2 temp1: %s",stringSerial2);
38domo 12:15ae35cfb03f 327 int pp = setTemp1(mbedJson["Temp1"].get<std::string>()); // display temp1 on lcd
38domo 12:15ae35cfb03f 328 }
38domo 12:15ae35cfb03f 329 if (mbedJson.hasMember("Temp2"))
38domo 12:15ae35cfb03f 330 { int tt=setTemp2(mbedJson["Temp2"].get<std::string>());
38domo 12:15ae35cfb03f 331 }
38domo 12:15ae35cfb03f 332 // add buttons parameters
38domo 12:15ae35cfb03f 333 // decode button trame
38domo 12:15ae35cfb03f 334 if (mbedJson.hasMember("Touche4"))
38domo 12:15ae35cfb03f 335 { printf("Touche 4 update status received");
38domo 12:15ae35cfb03f 336 string s;
38domo 12:15ae35cfb03f 337 s = mbedJson["Touche4"]["Status"].get<std::string>();
38domo 12:15ae35cfb03f 338 sprintf(statusButton[4],s.c_str());
38domo 12:15ae35cfb03f 339 printf(s.c_str());
38domo 12:15ae35cfb03f 340 printf("\n");
38domo 12:15ae35cfb03f 341 if (s=="on")
38domo 12:15ae35cfb03f 342 { txtColorButton[4] = (uint32_t)strtoul("0xFF00FF00", NULL,16);
38domo 12:15ae35cfb03f 343 backColorButton[4] = (uint32_t)strtoul("0xFF000000", NULL,16);
38domo 12:15ae35cfb03f 344 }
38domo 12:15ae35cfb03f 345 else
38domo 12:15ae35cfb03f 346 { txtColorButton[4] = (uint32_t)strtoul("0xFF000000", NULL,16);
38domo 12:15ae35cfb03f 347 backColorButton[4] = (uint32_t)strtoul("0xFF00FF00", NULL,16);
38domo 12:15ae35cfb03f 348 }
38domo 12:15ae35cfb03f 349 touchButtonJPA1(4);
38domo 10:41aff2b7acc6 350 }
38domo 12:15ae35cfb03f 351 if (mbedJson.hasMember("Touche1"))
38domo 12:15ae35cfb03f 352 { printf("Touche 1 update status received");
38domo 12:15ae35cfb03f 353 string s;
38domo 12:15ae35cfb03f 354 s = mbedJson["Touche1"]["Status"].get<std::string>();
38domo 12:15ae35cfb03f 355 sprintf(statusButton[1],s.c_str());
38domo 12:15ae35cfb03f 356 printf(s.c_str());
38domo 12:15ae35cfb03f 357 printf("\n");
38domo 12:15ae35cfb03f 358 if (s=="on")
38domo 12:15ae35cfb03f 359 { txtColorButton[1] = (uint32_t)strtoul("0xFF00FF00", NULL,16);
38domo 12:15ae35cfb03f 360 backColorButton[1] = (uint32_t)strtoul("0xFF000000", NULL,16);
38domo 12:15ae35cfb03f 361 }
38domo 12:15ae35cfb03f 362 else
38domo 12:15ae35cfb03f 363 { txtColorButton[1] = (uint32_t)strtoul("0xFF000000", NULL,16);
38domo 12:15ae35cfb03f 364 backColorButton[1] = (uint32_t)strtoul("0xFF00FF00", NULL,16);
38domo 12:15ae35cfb03f 365 }
38domo 12:15ae35cfb03f 366 touchButtonJPA1(1);
38domo 10:41aff2b7acc6 367 }
38domo 12:15ae35cfb03f 368 if (mbedJson.hasMember("Touche2"))
38domo 12:15ae35cfb03f 369 { printf("Touche 2 update status received");
38domo 12:15ae35cfb03f 370 string s;
38domo 12:15ae35cfb03f 371 s = mbedJson["Touche2"]["Status"].get<std::string>();
38domo 12:15ae35cfb03f 372 sprintf(statusButton[2],s.c_str());
38domo 12:15ae35cfb03f 373 printf(s.c_str());
38domo 12:15ae35cfb03f 374 printf("\n");
38domo 12:15ae35cfb03f 375 if (s=="on")
38domo 12:15ae35cfb03f 376 { txtColorButton[2] = (uint32_t)strtoul("0xFF00FF00", NULL,16);
38domo 12:15ae35cfb03f 377 backColorButton[2] = (uint32_t)strtoul("0xFF000000", NULL,16);
38domo 12:15ae35cfb03f 378 }
38domo 12:15ae35cfb03f 379 else
38domo 12:15ae35cfb03f 380 { txtColorButton[2] = (uint32_t)strtoul("0xFF000000", NULL,16);
38domo 12:15ae35cfb03f 381 backColorButton[2] = (uint32_t)strtoul("0xFF00FF00", NULL,16);
38domo 12:15ae35cfb03f 382 }
38domo 12:15ae35cfb03f 383 touchButtonJPA1(2);
38domo 12:15ae35cfb03f 384 }
38domo 12:15ae35cfb03f 385 if (mbedJson.hasMember("Touche3"))
38domo 12:15ae35cfb03f 386 { printf("Touche 3 update status received");
38domo 12:15ae35cfb03f 387 string s;
38domo 12:15ae35cfb03f 388 s = mbedJson["Touche3"]["Status"].get<std::string>();
38domo 12:15ae35cfb03f 389 sprintf(statusButton[3],s.c_str());
38domo 12:15ae35cfb03f 390 printf(s.c_str());
38domo 12:15ae35cfb03f 391 printf("\n");
38domo 12:15ae35cfb03f 392 if (s=="on")
38domo 12:15ae35cfb03f 393 { txtColorButton[3] = (uint32_t)strtoul("0xFF00FF00", NULL,16);
38domo 12:15ae35cfb03f 394 backColorButton[3] = (uint32_t)strtoul("0xFF000000", NULL,16);
38domo 12:15ae35cfb03f 395 }
38domo 12:15ae35cfb03f 396 else
38domo 12:15ae35cfb03f 397 { txtColorButton[3] = (uint32_t)strtoul("0xFF000000", NULL,16);
38domo 12:15ae35cfb03f 398 backColorButton[3] = (uint32_t)strtoul("0xFF00FF00", NULL,16);
38domo 12:15ae35cfb03f 399 }
38domo 12:15ae35cfb03f 400 touchButtonJPA1(3);
38domo 12:15ae35cfb03f 401 }
38domo 12:15ae35cfb03f 402 }
38domo 10:41aff2b7acc6 403 }
jeanpierreaulas 4:fbbf5d1785e7 404 if (msgSerial7 > 0) {receiveSerial7();}
jeanpierreaulas 9:332bd237b98f 405 if (stringSerial7ok == 1) {printf("Trame Serial7 : %s",stringSerial7);stringSerial7ok=0;}
jeanpierreaulas 3:2de8e4771674 406
38domo 0:48863a0964f5 407 // timer send can
jeanpierreaulas 3:2de8e4771674 408 if(timer.read_ms() >= 10000) // check for timeout
38domo 10:41aff2b7acc6 409 { timer.stop(); // stop the timer
38domo 0:48863a0964f5 410 timer.reset(); // reset the timer
jeanpierreaulas 2:462b5c0d3472 411 counter = (counter+1) & 1; // increment the counter
jeanpierreaulas 2:462b5c0d3472 412 voltage = 0;////(analogIn * 3.3f)/4096.0f;// read the small drifting voltage from analog input
38domo 0:48863a0964f5 413 txMsg.clear(); // clear the Tx message storage
jeanpierreaulas 2:462b5c0d3472 414 txMsg.id = 0x100130CA;//TX_ID; // set the message ID
38domo 0:48863a0964f5 415 txMsg.format = CANExtended ; //extended
jeanpierreaulas 2:462b5c0d3472 416 txMsg.len=2;
38domo 10:41aff2b7acc6 417 txMsg.data[0]=8;txMsg.data[1]=counter; // append data (total data length must be <= 8 bytes!)
38domo 0:48863a0964f5 418 if(can.write(txMsg)) // transmit message
38domo 0:48863a0964f5 419 { //if(can->write(txMsg)) { // transmit the CAN message
38domo 12:15ae35cfb03f 420 printf("TX CAN OK \n");
38domo 0:48863a0964f5 421 printMsg(txMsg,0);
38domo 12:15ae35cfb03f 422 //BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)"TX CAN OK", CENTER_MODE);
38domo 12:15ae35cfb03f 423 //BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"--------", CENTER_MODE);
38domo 0:48863a0964f5 424 }
38domo 0:48863a0964f5 425 else
jeanpierreaulas 3:2de8e4771674 426 { //serialUSB.printf("Transmission error\r\n");
38domo 12:15ae35cfb03f 427 printf("TX CAN Fail \n");
38domo 12:15ae35cfb03f 428 //BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"TX CAN Fail", CENTER_MODE);
38domo 12:15ae35cfb03f 429 //BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)"-----------", CENTER_MODE);
38domo 0:48863a0964f5 430 }
jeanpierreaulas 2:462b5c0d3472 431 timer.start(); // insert transmission lag
38domo 0:48863a0964f5 432 } // end if timer
38domo 0:48863a0964f5 433 //touch screen
38domo 0:48863a0964f5 434 int xx = touchJPA1(); //
38domo 0:48863a0964f5 435 if (xx > 0)
38domo 10:41aff2b7acc6 436 { if (xx == 4)
38domo 0:48863a0964f5 437 { // send trame to can
jeanpierreaulas 3:2de8e4771674 438 txMsg.id = 0x100130CB;//TX_ID; // set the message ID
jeanpierreaulas 3:2de8e4771674 439 txMsg.format = CANExtended ; //extended
jeanpierreaulas 3:2de8e4771674 440 txMsg.len=2;
jeanpierreaulas 3:2de8e4771674 441 txMsg.data[0]=8;txMsg.data[1]=2; // append data (total data length must be <= 8 bytes!)
jeanpierreaulas 3:2de8e4771674 442
38domo 0:48863a0964f5 443 if(can.write(txMsg)) // transmit message
38domo 10:41aff2b7acc6 444 { printMsg(txMsg,0);
38domo 12:15ae35cfb03f 445 printf("TX CAN OK \n");
38domo 12:15ae35cfb03f 446 //BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)"TX CAN OK", CENTER_MODE);
38domo 12:15ae35cfb03f 447 //BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"--------", CENTER_MODE);
jeanpierreaulas 3:2de8e4771674 448 }
38domo 0:48863a0964f5 449 else
38domo 12:15ae35cfb03f 450 { printf("TX CAN Fail \n");
38domo 12:15ae35cfb03f 451 BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"TX CAN Fail", CENTER_MODE);
38domo 0:48863a0964f5 452 BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)"-----------", CENTER_MODE);
38domo 0:48863a0964f5 453 }
38domo 0:48863a0964f5 454 }
38domo 10:41aff2b7acc6 455 xx = sendTouchJPA(xx);
38domo 10:41aff2b7acc6 456 lastToucheTime = time(NULL);
38domo 0:48863a0964f5 457 }
38domo 0:48863a0964f5 458 else
38domo 0:48863a0964f5 459 { lastToucheTime = 0;
38domo 0:48863a0964f5 460 } // send mqtt
38domo 0:48863a0964f5 461 // Update sensor data to GroveStreams
jeanpierreaulas 3:2de8e4771674 462 time_t seconds = time(NULL);
38domo 0:48863a0964f5 463 if(seconds - lastSuccessfulUploadTime > updateFrequency) {
38domo 0:48863a0964f5 464 ////lcd.clear();
38domo 0:48863a0964f5 465 ////lcd.printf("Getting Samples...");
38domo 0:48863a0964f5 466 printf("Getting Samples...\n");
38domo 0:48863a0964f5 467 //Assemble the samples into URL parameters which are seperated with the "&" character
38domo 0:48863a0964f5 468 // Example: &s1=6.2&s2=78.231
jeanpierreaulas 2:462b5c0d3472 469 int temperature = 0; //adc_temp.read() * 100.0f;
jeanpierreaulas 2:462b5c0d3472 470 int voltage = 0; //adc_vref.read() * 100.0f;
38domo 0:48863a0964f5 471 char samples[64] = {0};
38domo 0:48863a0964f5 472 sprintf(samples, "&%s=%d&%s=%d", gsStreamId1, voltage, gsStreamId2, temperature);
38domo 0:48863a0964f5 473 //Append on command requests (request stream values)
38domo 0:48863a0964f5 474 //This will indicate to GroveStreams to return the last value
38domo 0:48863a0964f5 475 // of each request stream during the sample upload
38domo 0:48863a0964f5 476 strcat(samples, "&rsid=freq&rsid=led");
38domo 0:48863a0964f5 477 char resultBuffer[700]= {0};
38domo 0:48863a0964f5 478 //Sending Samples (and returning current command stream values)
38domo 0:48863a0964f5 479 time_t connectAttemptTime = time(NULL);
38domo 0:48863a0964f5 480 int sendResult = groveStreams.send(myMac, samples, gsCompName, gsCompTmplId, resultBuffer, sizeof resultBuffer); //receive mqtt
38domo 0:48863a0964f5 481
38domo 0:48863a0964f5 482 if (sendResult == 0) {
38domo 0:48863a0964f5 483 ////lcd.printf("Send Successful");
38domo 0:48863a0964f5 484 printf("Send Successful\n");
38domo 0:48863a0964f5 485 lastSuccessfulUploadTime = connectAttemptTime;
38domo 0:48863a0964f5 486 //Handle command streams
38domo 0:48863a0964f5 487 if (strlen(resultBuffer) > 0 && resultBuffer[0] == '{') {
38domo 0:48863a0964f5 488 MbedJSONValue mbedJson;
38domo 0:48863a0964f5 489 parse(mbedJson, resultBuffer);
38domo 0:48863a0964f5 490 sprintf((char*)text, "HTTP string : %s\n", resultBuffer);
38domo 0:48863a0964f5 491 //BSP_LCD_DisplayStringAt(0, LINE(1), (uint8_t *)&text, LEFT_MODE);
38domo 0:48863a0964f5 492 if (mbedJson.hasMember("Temp1")) {
38domo 0:48863a0964f5 493 int pp=setTemp1(mbedJson["Temp1"].get<std::string>());
38domo 0:48863a0964f5 494 }
38domo 0:48863a0964f5 495 if (mbedJson.hasMember("Temp2")) {
38domo 0:48863a0964f5 496 int tt=setTemp2(mbedJson["Temp2"].get<std::string>());
38domo 0:48863a0964f5 497 }
38domo 0:48863a0964f5 498 if (mbedJson.hasMember("led")) {
38domo 0:48863a0964f5 499 //Change LED
38domo 0:48863a0964f5 500 myled = mbedJson["led"].get<bool>() ? 1 : 0;
38domo 0:48863a0964f5 501 ////lcd.printf("LED: %s", mbedJson["led"].get<bool>() ? "On" : "Off");
38domo 0:48863a0964f5 502 printf("LED: %s\n", mbedJson["led"].get<bool>() ? "On" : "Off");
38domo 0:48863a0964f5 503 }
38domo 0:48863a0964f5 504 }
38domo 0:48863a0964f5 505 }
38domo 0:48863a0964f5 506 }
38domo 0:48863a0964f5 507
38domo 0:48863a0964f5 508 }
38domo 0:48863a0964f5 509 }
38domo 0:48863a0964f5 510
38domo 0:48863a0964f5 511