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 17:22:27 2020 +0000
Branch:
jpa
Revision:
16:f0d946e6da36
Parent:
15:ddd006a66cca
version with updated libs

Who changed what in which revision?

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