STM32F103C8T6-Cayenne-WIZnet_SD1306_BMP280
Dependencies: Cayenne-MQTT-mbed mbed Cayenne-WIZnet_Library WIZnet_Library BME280
main.cpp@9:3ba93660c82e, 2018-04-23 (annotated)
- Committer:
- dadangjia
- Date:
- Mon Apr 23 13:43:45 2018 +0000
- Revision:
- 9:3ba93660c82e
- Parent:
- 8:be2ac38e5bb9
- Child:
- 10:63b5e5d19bee
STM32F103C8T6?BME280?MPU6050?OLED,W5500
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jburhenn | 0:8ce4fc106e50 | 1 | /** |
jburhenn | 0:8ce4fc106e50 | 2 | * Example app for using the Cayenne MQTT C++ library to send and receive example data. This example uses |
jburhenn | 0:8ce4fc106e50 | 3 | * the WIZnet_Library library to connect via Ethernet. |
jburhenn | 0:8ce4fc106e50 | 4 | * |
jburhenn | 0:8ce4fc106e50 | 5 | * NOTE: The WIZnet_Library defaults to using code for W5500. If you want to use a W5100 or W5200 code you need to |
jburhenn | 0:8ce4fc106e50 | 6 | * set the appropriate define in wiznet.h. |
jburhenn | 0:8ce4fc106e50 | 7 | */ |
jburhenn | 0:8ce4fc106e50 | 8 | #include "MQTTTimer.h" |
jburhenn | 0:8ce4fc106e50 | 9 | #include "CayenneMQTTClient.h" |
jburhenn | 0:8ce4fc106e50 | 10 | #include "MQTTNetwork.h" |
jburhenn | 0:8ce4fc106e50 | 11 | #include "WIZnetInterface.h" |
jburhenn | 0:8ce4fc106e50 | 12 | // Cayenne authentication info. This should be obtained from the Cayenne Dashboard. |
dadangjia | 9:3ba93660c82e | 13 | char* username = "c5495b90-ff04-11e6-a6b3-05972afbabc2"; |
dadangjia | 9:3ba93660c82e | 14 | char* password = "084cdefbc6a98c0b5b781716c9d102ce06b62cc4"; |
dadangjia | 9:3ba93660c82e | 15 | char* clientID = "267e1460-8aff-11e7-a5d9-9de9b49680ec"; |
dadangjia | 9:3ba93660c82e | 16 | #define USE_DHCP 1 |
dadangjia | 9:3ba93660c82e | 17 | |
dadangjia | 9:3ba93660c82e | 18 | Serial pc(USBTX, USBRX); //TX RX |
jburhenn | 0:8ce4fc106e50 | 19 | |
jburhenn | 0:8ce4fc106e50 | 20 | SPI spi(D11, D12, D13); |
dadangjia | 9:3ba93660c82e | 21 | WIZnetInterface ethernet(&spi, PB_0, PB_1); // SPI, SEL, Reset |
dadangjia | 9:3ba93660c82e | 22 | MQTTNetwork<WIZnetInterface> network(ethernet); |
jburhenn | 2:472a2fd3359a | 23 | CayenneMQTT::MQTTClient<MQTTNetwork<WIZnetInterface>, MQTTTimer> mqttClient(network, username, password, clientID); |
dadangjia | 9:3ba93660c82e | 24 | char buffer[7][20]; |
dadangjia | 9:3ba93660c82e | 25 | float Temperature,Pressure; |
dadangjia | 9:3ba93660c82e | 26 | long ADC_VALUE[3]; |
dadangjia | 9:3ba93660c82e | 27 | AnalogIn ADC_MQ2(A0); //ADC1,MQ2空气质量检测 |
dadangjia | 9:3ba93660c82e | 28 | AnalogIn ADC_MQ3(A1); //ADC2,MQ3空气质量检测 |
dadangjia | 9:3ba93660c82e | 29 | AnalogIn analog_value(A2); // ADC3,可调电位器 |
jburhenn | 0:8ce4fc106e50 | 30 | |
dadangjia | 9:3ba93660c82e | 31 | #include "CriusOLED.h" |
dadangjia | 9:3ba93660c82e | 32 | void OLED_INIT() |
dadangjia | 9:3ba93660c82e | 33 | { |
dadangjia | 9:3ba93660c82e | 34 | i2c.frequency(400000); |
dadangjia | 9:3ba93660c82e | 35 | init_OLED(); |
dadangjia | 9:3ba93660c82e | 36 | displayOn(); |
dadangjia | 9:3ba93660c82e | 37 | reset_display(); |
dadangjia | 9:3ba93660c82e | 38 | } |
dadangjia | 9:3ba93660c82e | 39 | |
dadangjia | 9:3ba93660c82e | 40 | #include "MPU6050.h" |
dadangjia | 9:3ba93660c82e | 41 | //MPU6050采集数据 |
dadangjia | 9:3ba93660c82e | 42 | Ticker ticker; |
dadangjia | 9:3ba93660c82e | 43 | MPU6050 mpu6050; |
dadangjia | 9:3ba93660c82e | 44 | float pitchAngle = 0;float rollAngle = 0; |
dadangjia | 9:3ba93660c82e | 45 | float tickerTime = 0.5; |
dadangjia | 9:3ba93660c82e | 46 | void compFilter() {mpu6050.complementaryFilter(&pitchAngle, &rollAngle);} |
dadangjia | 9:3ba93660c82e | 47 | |
dadangjia | 9:3ba93660c82e | 48 | #include "BME280.h" |
dadangjia | 9:3ba93660c82e | 49 | BME280 sensor(PB_9, PB_8); |
dadangjia | 9:3ba93660c82e | 50 | void bme280_dat() |
dadangjia | 9:3ba93660c82e | 51 | { |
dadangjia | 9:3ba93660c82e | 52 | Temperature=sensor.getTemperature(); |
dadangjia | 9:3ba93660c82e | 53 | Pressure=sensor.getPressure()*100; |
dadangjia | 9:3ba93660c82e | 54 | ADC_VALUE[0] = (long)(ADC_MQ2.read() * 3300); |
dadangjia | 9:3ba93660c82e | 55 | ADC_VALUE[1] = (long)(ADC_MQ3.read() * 3300); |
dadangjia | 9:3ba93660c82e | 56 | ADC_VALUE[2] = (long)(analog_value.read()* 3300); |
dadangjia | 9:3ba93660c82e | 57 | snprintf(buffer[0],sizeof(buffer[0]), "%2.2fC %6.0fPa",Temperature, Pressure); |
dadangjia | 9:3ba93660c82e | 58 | sendStrXY(buffer[0],0,0); |
dadangjia | 9:3ba93660c82e | 59 | } |
dadangjia | 9:3ba93660c82e | 60 | |
dadangjia | 9:3ba93660c82e | 61 | void Stm32f103c8t6_ticker() |
dadangjia | 9:3ba93660c82e | 62 | { |
dadangjia | 9:3ba93660c82e | 63 | compFilter(); |
dadangjia | 9:3ba93660c82e | 64 | bme280_dat(); |
dadangjia | 9:3ba93660c82e | 65 | } |
jburhenn | 0:8ce4fc106e50 | 66 | /** |
jburhenn | 0:8ce4fc106e50 | 67 | * Print the message info. |
jburhenn | 0:8ce4fc106e50 | 68 | * @param[in] message The message received from the Cayenne server. |
jburhenn | 0:8ce4fc106e50 | 69 | */ |
jburhenn | 2:472a2fd3359a | 70 | void outputMessage(CayenneMQTT::MessageData& message) |
jburhenn | 0:8ce4fc106e50 | 71 | { |
dadangjia | 9:3ba93660c82e | 72 | size_t j; |
jburhenn | 0:8ce4fc106e50 | 73 | switch (message.topic) { |
jburhenn | 0:8ce4fc106e50 | 74 | case COMMAND_TOPIC: |
dadangjia | 9:3ba93660c82e | 75 | pc.printf("topic=Command"); |
jburhenn | 0:8ce4fc106e50 | 76 | break; |
jburhenn | 0:8ce4fc106e50 | 77 | case CONFIG_TOPIC: |
dadangjia | 9:3ba93660c82e | 78 | pc.printf("topic=Config"); |
jburhenn | 0:8ce4fc106e50 | 79 | break; |
jburhenn | 0:8ce4fc106e50 | 80 | default: |
dadangjia | 9:3ba93660c82e | 81 | pc.printf("topic=%d", message.topic); |
jburhenn | 0:8ce4fc106e50 | 82 | break; |
jburhenn | 0:8ce4fc106e50 | 83 | } |
dadangjia | 9:3ba93660c82e | 84 | pc.printf(" channel=%d", message.channel); |
jburhenn | 0:8ce4fc106e50 | 85 | if (message.clientID) { |
dadangjia | 9:3ba93660c82e | 86 | pc.printf(" clientID=%s", message.clientID); |
jburhenn | 0:8ce4fc106e50 | 87 | } |
jburhenn | 0:8ce4fc106e50 | 88 | if (message.type) { |
dadangjia | 9:3ba93660c82e | 89 | pc.printf(" type=%s", message.type); |
jburhenn | 0:8ce4fc106e50 | 90 | } |
jburhenn | 0:8ce4fc106e50 | 91 | for (size_t i = 0; i < message.valueCount; ++i) { |
jburhenn | 2:472a2fd3359a | 92 | if (message.getValue(i)) { |
dadangjia | 9:3ba93660c82e | 93 | pc.printf(" value=%s", message.getValue(i)); |
dadangjia | 9:3ba93660c82e | 94 | j = i; |
jburhenn | 2:472a2fd3359a | 95 | } |
jburhenn | 2:472a2fd3359a | 96 | if (message.getUnit(i)) { |
dadangjia | 9:3ba93660c82e | 97 | pc.printf(" unit=%s", message.getUnit(i)); |
jburhenn | 2:472a2fd3359a | 98 | } |
jburhenn | 0:8ce4fc106e50 | 99 | } |
jburhenn | 0:8ce4fc106e50 | 100 | if (message.id) { |
dadangjia | 9:3ba93660c82e | 101 | pc.printf(" id=%s", message.id); |
jburhenn | 0:8ce4fc106e50 | 102 | } |
dadangjia | 9:3ba93660c82e | 103 | pc.printf("\n"); |
dadangjia | 9:3ba93660c82e | 104 | |
dadangjia | 9:3ba93660c82e | 105 | |
jburhenn | 0:8ce4fc106e50 | 106 | } |
jburhenn | 0:8ce4fc106e50 | 107 | |
jburhenn | 0:8ce4fc106e50 | 108 | /** |
jburhenn | 0:8ce4fc106e50 | 109 | * Handle messages received from the Cayenne server. |
jburhenn | 0:8ce4fc106e50 | 110 | * @param[in] message The message received from the Cayenne server. |
jburhenn | 0:8ce4fc106e50 | 111 | */ |
jburhenn | 2:472a2fd3359a | 112 | void messageArrived(CayenneMQTT::MessageData& message) |
jburhenn | 0:8ce4fc106e50 | 113 | { |
jburhenn | 0:8ce4fc106e50 | 114 | int error = 0; |
jburhenn | 0:8ce4fc106e50 | 115 | // Add code to process the message. Here we just ouput the message data. |
jburhenn | 0:8ce4fc106e50 | 116 | outputMessage(message); |
jburhenn | 0:8ce4fc106e50 | 117 | |
jburhenn | 2:472a2fd3359a | 118 | if (message.topic == COMMAND_TOPIC) { |
jburhenn | 2:472a2fd3359a | 119 | // If this is a command message we publish a response to show we recieved it. Here we are just sending a default 'OK' response. |
jburhenn | 2:472a2fd3359a | 120 | // An error response should be sent if there are issues processing the message. |
jburhenn | 5:f3de9c6ca8f5 | 121 | if ((error = mqttClient.publishResponse(message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 122 | pc.printf("Response failure, error: %d\n", error); |
jburhenn | 2:472a2fd3359a | 123 | } |
jburhenn | 2:472a2fd3359a | 124 | |
jburhenn | 2:472a2fd3359a | 125 | // Send the updated state for the channel so it is reflected in the Cayenne dashboard. If a command is successfully processed |
jburhenn | 2:472a2fd3359a | 126 | // the updated state will usually just be the value received in the command message. |
jburhenn | 2:472a2fd3359a | 127 | if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 128 | pc.printf("Publish state failure, error: %d\n", error); |
jburhenn | 2:472a2fd3359a | 129 | } |
jburhenn | 2:472a2fd3359a | 130 | } |
jburhenn | 0:8ce4fc106e50 | 131 | } |
jburhenn | 0:8ce4fc106e50 | 132 | |
jburhenn | 0:8ce4fc106e50 | 133 | /** |
jburhenn | 0:8ce4fc106e50 | 134 | * Connect to the Cayenne server. |
jburhenn | 0:8ce4fc106e50 | 135 | * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise. |
jburhenn | 0:8ce4fc106e50 | 136 | */ |
jburhenn | 0:8ce4fc106e50 | 137 | int connectClient(void) |
jburhenn | 0:8ce4fc106e50 | 138 | { |
jburhenn | 0:8ce4fc106e50 | 139 | int error = 0; |
jburhenn | 0:8ce4fc106e50 | 140 | // Connect to the server. |
dadangjia | 9:3ba93660c82e | 141 | pc.printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT); |
jburhenn | 0:8ce4fc106e50 | 142 | while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) { |
dadangjia | 9:3ba93660c82e | 143 | pc.printf("TCP connect failed, error: %d\n", error); |
jburhenn | 0:8ce4fc106e50 | 144 | wait(2); |
jburhenn | 0:8ce4fc106e50 | 145 | } |
jburhenn | 0:8ce4fc106e50 | 146 | |
jburhenn | 2:472a2fd3359a | 147 | if ((error = mqttClient.connect()) != MQTT::SUCCESS) { |
dadangjia | 9:3ba93660c82e | 148 | pc.printf("MQTT connect failed, error: %d\n", error); |
jburhenn | 0:8ce4fc106e50 | 149 | return error; |
jburhenn | 0:8ce4fc106e50 | 150 | } |
dadangjia | 9:3ba93660c82e | 151 | pc.printf("Connected\n"); |
jburhenn | 0:8ce4fc106e50 | 152 | |
jburhenn | 0:8ce4fc106e50 | 153 | // Subscribe to required topics. |
jburhenn | 0:8ce4fc106e50 | 154 | if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 155 | pc.printf("Subscription to Command topic failed, error: %d\n", error); |
jburhenn | 0:8ce4fc106e50 | 156 | } |
jburhenn | 0:8ce4fc106e50 | 157 | if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 158 | pc.printf("Subscription to Config topic failed, error:%d\n", error); |
jburhenn | 0:8ce4fc106e50 | 159 | } |
jburhenn | 0:8ce4fc106e50 | 160 | |
jburhenn | 0:8ce4fc106e50 | 161 | // Send device info. Here we just send some example values for the system info. These should be changed to use actual system data, or removed if not needed. |
jburhenn | 0:8ce4fc106e50 | 162 | mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION); |
jburhenn | 0:8ce4fc106e50 | 163 | mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice"); |
jburhenn | 2:472a2fd3359a | 164 | //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model"); |
jburhenn | 2:472a2fd3359a | 165 | //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000"); |
jburhenn | 0:8ce4fc106e50 | 166 | |
jburhenn | 0:8ce4fc106e50 | 167 | return CAYENNE_SUCCESS; |
jburhenn | 0:8ce4fc106e50 | 168 | } |
jburhenn | 0:8ce4fc106e50 | 169 | |
jburhenn | 0:8ce4fc106e50 | 170 | /** |
jburhenn | 0:8ce4fc106e50 | 171 | * Main loop where MQTT code is run. |
jburhenn | 0:8ce4fc106e50 | 172 | */ |
jburhenn | 0:8ce4fc106e50 | 173 | void loop(void) |
jburhenn | 0:8ce4fc106e50 | 174 | { |
jburhenn | 8:be2ac38e5bb9 | 175 | // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval. |
jburhenn | 0:8ce4fc106e50 | 176 | MQTTTimer timer(5000); |
jburhenn | 0:8ce4fc106e50 | 177 | |
jburhenn | 0:8ce4fc106e50 | 178 | while (true) { |
jburhenn | 0:8ce4fc106e50 | 179 | // Yield to allow MQTT message processing. |
jburhenn | 0:8ce4fc106e50 | 180 | mqttClient.yield(1000); |
jburhenn | 0:8ce4fc106e50 | 181 | |
jburhenn | 0:8ce4fc106e50 | 182 | // Check that we are still connected, if not, reconnect. |
jburhenn | 0:8ce4fc106e50 | 183 | if (!network.connected() || !mqttClient.connected()) { |
jburhenn | 0:8ce4fc106e50 | 184 | network.disconnect(); |
jburhenn | 0:8ce4fc106e50 | 185 | mqttClient.disconnect(); |
dadangjia | 9:3ba93660c82e | 186 | pc.printf("Reconnecting\n"); |
jburhenn | 0:8ce4fc106e50 | 187 | while (connectClient() != CAYENNE_SUCCESS) { |
jburhenn | 0:8ce4fc106e50 | 188 | wait(2); |
dadangjia | 9:3ba93660c82e | 189 | pc.printf("Reconnect failed, retrying\n"); |
jburhenn | 0:8ce4fc106e50 | 190 | } |
jburhenn | 0:8ce4fc106e50 | 191 | } |
jburhenn | 0:8ce4fc106e50 | 192 | |
jburhenn | 0:8ce4fc106e50 | 193 | // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne. |
jburhenn | 0:8ce4fc106e50 | 194 | if (timer.expired()) { |
dadangjia | 9:3ba93660c82e | 195 | int error=0; |
dadangjia | 9:3ba93660c82e | 196 | if ((error = mqttClient.publishData(DATA_TOPIC, 0, TYPE_TEMPERATURE, UNIT_CELSIUS, Temperature)) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 197 | pc.printf("T:%2.2fC\n",Temperature); |
dadangjia | 9:3ba93660c82e | 198 | } |
dadangjia | 9:3ba93660c82e | 199 | if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_LUMINOSITY, UNIT_LUX, (long)Pressure)) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 200 | pc.printf("P:%6.0fPa\n",Pressure); |
dadangjia | 9:3ba93660c82e | 201 | } |
dadangjia | 9:3ba93660c82e | 202 | if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, ax)) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 203 | pc.printf("x:%03.2f\n", ax); |
jburhenn | 0:8ce4fc106e50 | 204 | } |
dadangjia | 9:3ba93660c82e | 205 | if ((error = mqttClient.publishData(DATA_TOPIC, 3, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, ay)) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 206 | pc.printf("y:%03.2f\n", ay); |
dadangjia | 9:3ba93660c82e | 207 | } |
dadangjia | 9:3ba93660c82e | 208 | if ((error = mqttClient.publishData(DATA_TOPIC, 4, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, az)) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 209 | pc.printf("z:%03.2f\n", az); |
dadangjia | 9:3ba93660c82e | 210 | } |
dadangjia | 9:3ba93660c82e | 211 | if ((error = mqttClient.publishData(DATA_TOPIC, 14, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, gx)) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 212 | pc.printf("X:%03.2f\n", gx); |
jburhenn | 0:8ce4fc106e50 | 213 | } |
dadangjia | 9:3ba93660c82e | 214 | if ((error = mqttClient.publishData(DATA_TOPIC, 15, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, gy)) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 215 | pc.printf("Y:%03.2f\n", gy); |
dadangjia | 9:3ba93660c82e | 216 | } |
dadangjia | 9:3ba93660c82e | 217 | if ((error = mqttClient.publishData(DATA_TOPIC, 16, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, gz)) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 218 | pc.printf("Z:%03.2f\n", gz); |
jburhenn | 0:8ce4fc106e50 | 219 | } |
dadangjia | 9:3ba93660c82e | 220 | |
dadangjia | 9:3ba93660c82e | 221 | if ((error = mqttClient.publishData(DATA_TOPIC, 8, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, ADC_VALUE[0])) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 222 | pc.printf("A0:%4dmV\n", ADC_VALUE[0]); |
dadangjia | 9:3ba93660c82e | 223 | } |
dadangjia | 9:3ba93660c82e | 224 | if ((error = mqttClient.publishData(DATA_TOPIC, 9, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, ADC_VALUE[1])) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 225 | pc.printf("A1:%4dmV\n", ADC_VALUE[1]); |
dadangjia | 9:3ba93660c82e | 226 | } |
dadangjia | 9:3ba93660c82e | 227 | if ((error = mqttClient.publishData(DATA_TOPIC, 10, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, ADC_VALUE[2])) != CAYENNE_SUCCESS) { |
dadangjia | 9:3ba93660c82e | 228 | pc.printf("A2:%4dmV\n", ADC_VALUE[2]); |
dadangjia | 9:3ba93660c82e | 229 | } |
dadangjia | 9:3ba93660c82e | 230 | |
jburhenn | 8:be2ac38e5bb9 | 231 | // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval. |
jburhenn | 0:8ce4fc106e50 | 232 | timer.countdown_ms(5000); |
jburhenn | 0:8ce4fc106e50 | 233 | } |
jburhenn | 0:8ce4fc106e50 | 234 | } |
jburhenn | 0:8ce4fc106e50 | 235 | } |
jburhenn | 0:8ce4fc106e50 | 236 | |
dadangjia | 9:3ba93660c82e | 237 | char * IP_Addr = "192.168.0.194"; |
dadangjia | 9:3ba93660c82e | 238 | char * IP_Subnet = "255.255.255.0"; |
dadangjia | 9:3ba93660c82e | 239 | char * IP_Gateway = "192.168.0.1"; |
dadangjia | 9:3ba93660c82e | 240 | unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56}; |
dadangjia | 9:3ba93660c82e | 241 | void W5500_init() |
dadangjia | 9:3ba93660c82e | 242 | { |
dadangjia | 9:3ba93660c82e | 243 | #if USE_DHCP |
dadangjia | 9:3ba93660c82e | 244 | int ret = ethernet.init(MAC_Addr); |
dadangjia | 9:3ba93660c82e | 245 | #else |
dadangjia | 9:3ba93660c82e | 246 | int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway); |
dadangjia | 9:3ba93660c82e | 247 | #endif |
dadangjia | 9:3ba93660c82e | 248 | if (!ret) { |
dadangjia | 9:3ba93660c82e | 249 | pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); |
dadangjia | 9:3ba93660c82e | 250 | ret = ethernet.connect(); |
dadangjia | 9:3ba93660c82e | 251 | if (!ret) { |
dadangjia | 9:3ba93660c82e | 252 | pc.printf("IP: %s, MASK: %s, GW: %s\r\n", |
dadangjia | 9:3ba93660c82e | 253 | ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); |
dadangjia | 9:3ba93660c82e | 254 | sendStrXY("IP:",1,0); |
dadangjia | 9:3ba93660c82e | 255 | sendStrXY(ethernet.getIPAddress(),1,3); |
dadangjia | 9:3ba93660c82e | 256 | } else { |
dadangjia | 9:3ba93660c82e | 257 | pc.printf("Error ethernet.connect() - ret = %d\r\n", ret); |
dadangjia | 9:3ba93660c82e | 258 | exit(0); |
dadangjia | 9:3ba93660c82e | 259 | } |
dadangjia | 9:3ba93660c82e | 260 | } else { |
dadangjia | 9:3ba93660c82e | 261 | pc.printf("Error ethernet.init() - ret = %d\r\n", ret); |
dadangjia | 9:3ba93660c82e | 262 | exit(0); |
dadangjia | 9:3ba93660c82e | 263 | } |
dadangjia | 9:3ba93660c82e | 264 | } |
jburhenn | 0:8ce4fc106e50 | 265 | /** |
jburhenn | 0:8ce4fc106e50 | 266 | * Main function. |
jburhenn | 0:8ce4fc106e50 | 267 | */ |
jburhenn | 0:8ce4fc106e50 | 268 | int main() |
jburhenn | 0:8ce4fc106e50 | 269 | { |
dadangjia | 9:3ba93660c82e | 270 | pc.baud(921600); |
dadangjia | 9:3ba93660c82e | 271 | pc.printf("Stm32f103k3t6-w5500-Cayeme-ethernet\n"); |
dadangjia | 9:3ba93660c82e | 272 | OLED_INIT(); |
dadangjia | 9:3ba93660c82e | 273 | mpu6050.whoAmI(); // Communication test: WHO_AM_I register reading |
dadangjia | 9:3ba93660c82e | 274 | mpu6050.calibrate(accelBias,gyroBias); // Calibrate MPU6050 and load biases into bias registers |
dadangjia | 9:3ba93660c82e | 275 | mpu6050.init(); |
dadangjia | 9:3ba93660c82e | 276 | ticker.attach(&Stm32f103c8t6_ticker, tickerTime); |
jburhenn | 0:8ce4fc106e50 | 277 | // Set the correct SPI frequency for your shield, if necessary. For example, 42000000 for Arduino Ethernet Shield W5500 or 20000000 for Arduino Ethernet Shield W5100. |
dadangjia | 9:3ba93660c82e | 278 | W5500_init(); |
jburhenn | 0:8ce4fc106e50 | 279 | // Set the default function that receives Cayenne messages. |
jburhenn | 0:8ce4fc106e50 | 280 | mqttClient.setDefaultMessageHandler(messageArrived); |
jburhenn | 0:8ce4fc106e50 | 281 | // Connect to Cayenne. |
jburhenn | 0:8ce4fc106e50 | 282 | if (connectClient() == CAYENNE_SUCCESS) { |
jburhenn | 0:8ce4fc106e50 | 283 | // Run main loop. |
jburhenn | 0:8ce4fc106e50 | 284 | loop(); |
jburhenn | 0:8ce4fc106e50 | 285 | } |
jburhenn | 0:8ce4fc106e50 | 286 | else { |
dadangjia | 9:3ba93660c82e | 287 | pc.printf("Connection failed, exiting\n"); |
jburhenn | 0:8ce4fc106e50 | 288 | } |
jburhenn | 0:8ce4fc106e50 | 289 | |
jburhenn | 0:8ce4fc106e50 | 290 | if (mqttClient.connected()) |
jburhenn | 0:8ce4fc106e50 | 291 | mqttClient.disconnect(); |
jburhenn | 0:8ce4fc106e50 | 292 | if (network.connected()) |
jburhenn | 0:8ce4fc106e50 | 293 | network.disconnect(); |
jburhenn | 0:8ce4fc106e50 | 294 | |
jburhenn | 0:8ce4fc106e50 | 295 | return 0; |
jburhenn | 0:8ce4fc106e50 | 296 | } |
jburhenn | 0:8ce4fc106e50 | 297 |